Yay for rabbit holes. Revamped the table services to decouple them from places.

While I was in there, I extracted the communication between the client and the
TableManager into a new-style "embedded in the TableLobbyObject" service
instead of wonkily routing everything through the global ParlorService and the
ParlorProvider (which got merged into the ParlorManager as a part of this
rabbit holery). The GG build will break... I will fix it.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@255 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-03-21 21:38:31 +00:00
parent f61250f75b
commit b71007a909
24 changed files with 936 additions and 793 deletions
@@ -28,7 +28,8 @@ import com.samskivert.util.HashIntMap;
import com.samskivert.util.StringUtil;
import com.threerings.util.Name;
import com.threerings.presents.dobj.ChangeListener;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.NamedEvent;
import com.threerings.presents.dobj.ObjectAddedEvent;
import com.threerings.presents.dobj.ObjectDeathListener;
@@ -44,39 +45,43 @@ import com.threerings.crowd.server.CrowdServer;
import com.threerings.crowd.server.PlaceManager;
import com.threerings.parlor.Log;
import com.threerings.parlor.client.TableService;
import com.threerings.parlor.data.ParlorCodes;
import com.threerings.parlor.data.Table;
import com.threerings.parlor.data.TableConfig;
import com.threerings.parlor.data.TableLobbyObject;
import com.threerings.parlor.data.TableMarshaller;
import com.threerings.parlor.game.data.GameConfig;
import com.threerings.parlor.game.data.GameObject;
import com.threerings.parlor.game.server.GameManager;
/**
* A table manager can be used by a place manager to take care of the
* management of a table matchmaking service in the place managed by the
* place manager. The place manager need instantiate a table manager and
* implement the {@link TableManagerProvider} interface to provide access
* to the table manager for the associated invocation services.
* A table manager can be used by a place manager (or other entity) to take care of the management
* of a table matchmaking service in the place managed by the place manager (or on some other
* distributed object). The place manager need instantiate a table manager and implement the {@link
* TableManagerProvider} interface to provide access to the table manager for the associated
* invocation services.
*/
public class TableManager
implements ParlorCodes, OidListListener
implements ParlorCodes, OidListListener, TableProvider
{
/**
* Creates a table manager that will work in tandem with the specified
* place manager to manage a table matchmaking service in this place.
* Creates a table manager that will manage tables in the supplied distributed object (which
* must implement {@link TableLobbyObject}.
*/
public TableManager (PlaceManager plmgr)
public TableManager (DObject tableObject)
{
// get a reference to our place object
_plobj = plmgr.getPlaceObject();
// set up our object references
_tlobj = (TableLobbyObject)tableObject;
_tlobj.setTableService(
(TableMarshaller)CrowdServer.invmgr.registerDispatcher(new TableDispatcher(this)));
_dobj = tableObject;
// add ourselves as an oidlist listener to this lobby so that we
// can tell if a user leaves the lobby without leaving their table
_plobj.addListener(this);
// make sure it implements table lobby object
_tlobj = (TableLobbyObject)_plobj;
// if our table is in a "place" add ourselves as a listener so that we can tell if a user
// leaves the place without leaving their table
if (_dobj instanceof PlaceObject) {
_dobj.addListener(this);
}
}
/**
@@ -87,33 +92,19 @@ public class TableManager
_tableClass = tableClass;
}
/**
* Requests that a new table be created to matchmake the game
* described by the supplied game config instance. The config instance
* provided must implement the {@link TableConfig} interface so that
* the parlor services can determine how to configure the table that
* will be created.
*
* @param creator the body object that will own the new table.
* @param tableConfig the configuration parameters for the table.
* @param config the configuration of the game to be created.
*
* @return the id of the newly created table.
*
* @exception InvocationException thrown if the table creation was
* not able processed for some reason. The explanation will be
* provided in the message data of the exception.
*/
public int createTable (BodyObject creator, TableConfig tableConfig,
GameConfig config)
// from interface ParlorProvider
public void createTable (ClientObject caller, TableConfig tableConfig, GameConfig config,
TableService.ResultListener listener)
throws InvocationException
{
// make sure the creator is an occupant of the lobby in which
// they are requesting to create a table
if (!_plobj.occupants.contains(creator.getOid())) {
Log.warning("Requested to create a table in a lobby not " +
"occupied by the creator [creator=" + creator +
", loboid=" + _plobj.getOid() + "].");
BodyObject creator = (BodyObject)caller;
// if we're managing tables in a place, make sure the creator is an occupant of the place
// in which they are requesting to create a table
if (_dobj instanceof PlaceObject &&
!((PlaceObject)_dobj).occupants.contains(creator.getOid())) {
Log.warning("Requested to create a table in a place not occupied by the creator " +
"[creator=" + creator + ", ploid=" + _dobj.getOid() + "].");
throw new InvocationException(INTERNAL_ERROR);
}
@@ -122,26 +113,25 @@ public class TableManager
try {
table = _tableClass.newInstance();
} catch (Exception e) {
Log.warning("Unable to create a new table instance! " +
"[tableClass=" + _tableClass + ", error=" + e + "].");
Log.warning("Unable to create a new table instance! [tableClass=" + _tableClass +
", error=" + e + "].");
throw new InvocationException(INTERNAL_ERROR);
}
table.init(_plobj.getOid(), tableConfig, config);
table.init(_dobj.getOid(), tableConfig, config);
if (table.bodyOids != null && table.bodyOids.length > 0) {
// stick the creator into the first non-AI position
int cpos = (config.ais == null) ? 0 : config.ais.length;
String error = table.setOccupant(cpos, creator);
if (error != null) {
Log.warning("Unable to add creator to position zero of " +
"table!? [table=" + table + ", creator=" + creator +
", error=" + error + "].");
Log.warning("Unable to add creator to position zero of table!? [table=" + table +
", creator=" + creator + ", error=" + error + "].");
// bail out now and abort the table creation process
throw new InvocationException(error);
}
// make a mapping from the creator to this table
_boidMap.put(creator.getOid(), table);
notePlayerAdded(table, creator.getOid());
}
// stick the table into the table lobby object
@@ -155,32 +145,16 @@ public class TableManager
int oid = createGame(table);
}
// finally let the caller know what the new table id is
return table.tableId;
listener.requestProcessed(table.tableId);
}
/**
* Requests that the specified user be added to the specified table at
* the specified position. If the user successfully joins the table,
* the function will return normally. If they are not able to join for
* some reason (the slot is already full, etc.), a {@link
* InvocationException} will be thrown with a message code that
* describes the reason for failure. If the user does successfully
* join, they will be added to the table entry in the tables set in
* the place object that is hosting the table.
*
* @param joiner the body object of the user that wishes to join the
* table.
* @param tableId the id of the table to be joined.
* @param position the position at which to join the table.
*
* @exception InvocationException thrown if the joining was not able
* processed for some reason. The explanation will be provided in the
* message data of the exception.
*/
public void joinTable (BodyObject joiner, int tableId, int position)
// from interface ParlorProvider
public void joinTable (ClientObject caller, int tableId, int position,
TableService.InvocationListener listener)
throws InvocationException
{
BodyObject joiner = (BodyObject)caller;
// look the table up
Table table = _tables.get(tableId);
if (table == null) {
@@ -199,32 +173,23 @@ public class TableManager
createGame(table);
} else {
// make a mapping from this occupant to this table
_boidMap.put(joiner.getOid(), table);
notePlayerAdded(table, joiner.getOid());
}
// update the table in the lobby
_tlobj.updateTables(table);
// there is normally no success response. the client will see
// themselves show up in the table that they joined
}
/**
* Requests that the specified user be removed from the specified
* table. If the user successfully leaves the table, the function will
* return normally. If they are not able to leave for some reason
* (they aren't sitting at the table, etc.), a {@link
* InvocationException} will be thrown with a message code that
* describes the reason for failure.
*
* @param leaver the body object of the user that wishes to leave the
* table.
* @param tableId the id of the table to be left.
*
* @exception InvocationException thrown if the leaving was not able
* processed for some reason. The explanation will be provided in the
* message data of the exception.
*/
public void leaveTable (BodyObject leaver, int tableId)
// from interface ParlorProvider
public void leaveTable (ClientObject caller, int tableId,
TableService.InvocationListener listener)
throws InvocationException
{
BodyObject leaver = (BodyObject)caller;
// look the table up
Table table = _tables.get(tableId);
if (table == null) {
@@ -237,57 +202,42 @@ public class TableManager
}
// remove the mapping from this user to the table
if (_boidMap.remove(leaver.getOid()) == null) {
Log.warning("No body to table mapping to clear? " +
"[leaver=" + leaver + ", table=" + table + "].");
if (!notePlayerRemoved(table, leaver.getOid())) {
Log.warning("No body to table mapping to clear? [leaver=" + leaver +
", table=" + table + "].");
}
// either update or delete the table depending on whether or not
// we just removed the last occupant
// either update or delete the table depending on whether or not we just removed the last
// occupant
if (table.isEmpty()) {
purgeTable(table);
} else {
_tlobj.updateTables(table);
}
// there is normally no success response. the client will see
// themselves removed from the table they just left
}
/**
* Requests that the specified table be started now, even if there are
* some vacant seats. This may only be done if the minimum number of
* players are already present at the table. By convention, only the
* player at seat 0 may request to have the game start early, as they
* are usually the creator.
*
* @param requester the body object of the user that wishes to start
* the table.
* @param tableId the id of the table to be started.
*
* @exception InvocationException thrown if the starting was not able
* processed for some reason. The explanation will be provided in the
* message data of the exception.
*/
public void startTableNow (BodyObject requester, int tableId)
// from interface ParlorProvider
public void startTableNow (ClientObject caller, int tableId,
TableService.InvocationListener listener)
throws InvocationException
{
// look the table up
BodyObject starter = (BodyObject)caller;
Table table = _tables.get(tableId);
if (table == null) {
throw new InvocationException(NO_SUCH_TABLE);
} else if (requester.getOid() != table.bodyOids[0]) {
} else if (starter.getOid() != table.bodyOids[0]) {
throw new InvocationException(INVALID_TABLE_POSITION);
} else if (!table.mayBeStarted()) {
throw new InvocationException(INTERNAL_ERROR);
}
// I guess we're ready to go!
createGame(table);
}
/**
* Removes the table from all of our internal tables and from its
* lobby's distributed object.
* Removes the table from all of our internal tables and from its lobby's distributed object.
*/
protected void purgeTable (Table table)
{
@@ -297,7 +247,7 @@ public class TableManager
// clear out all matching entries in the boid map
if (table.bodyOids != null) {
for (int ii = 0; ii < table.bodyOids.length; ii++) {
_boidMap.remove(table.bodyOids[ii]);
notePlayerRemoved(table, table.bodyOids[ii]);
}
}
@@ -309,9 +259,30 @@ public class TableManager
}
/**
* Called when we're ready to create a game (either an invitation has
* been accepted or a table is ready to start. If there is a problem
* creating the game manager, it should be reported in the logs.
* Called when a player is added to a table to set up our mappings.
*/
protected void notePlayerAdded (Table table, int playerOid)
{
_boidMap.put(playerOid, table);
// TODO: if we're not in a place, listen to the player's BodyObject for object death so
// that we remove them from their table if they logoff
}
/**
* Called when a player leaves a table to clear our mappings.
*/
protected boolean notePlayerRemoved (Table table, int playerOid)
{
boolean removed = (_boidMap.remove(playerOid) != null);
// TODO: remove our BodyObject death listener
return removed;
}
/**
* Called when we're ready to create a game (either an invitation has been accepted or a table
* is ready to start. If there is a problem creating the game manager, it should be reported in
* the logs.
*
* @return the oid of the newly-created game.
*/
@@ -325,8 +296,7 @@ public class TableManager
return gobj.getOid();
} catch (Throwable t) {
Log.warning("Failed to create manager for game " +
"[config=" + table.config + "]: " + t);
Log.warning("Failed to create manager for game [config=" + table.config + "]: " + t);
throw new InvocationException(INTERNAL_ERROR);
}
}
@@ -344,8 +314,8 @@ public class TableManager
}
/**
* Called when our game has been created, we take this opportunity to clean
* up the table and transition it to "in play" mode.
* Called when our game has been created, we take this opportunity to clean up the table and
* transition it to "in play" mode.
*/
protected void gameCreated (Table table, GameObject gameobj)
{
@@ -365,8 +335,7 @@ public class TableManager
}
}
// add an object death listener to unmap the table when the game
// finally goes away
// add an object death listener to unmap the table when the game finally goes away
gameobj.addListener(_gameListener);
// and then update the lobby object that contains the table
@@ -374,18 +343,16 @@ public class TableManager
}
/**
* Called when a game created from a table managed by this table
* manager was destroyed. We remove the associated table.
* Called when a game created from a table managed by this table manager was destroyed. We
* remove the associated table.
*/
protected void unmapTable (int gameOid)
{
Table table = _goidMap.get(gameOid);
if (table != null) {
purgeTable(table);
} else {
Log.warning("Requested to unmap table that wasn't mapped " +
"[gameOid=" + gameOid + "].");
Log.warning("Requested to unmap table that wasn't mapped [gameOid=" + gameOid + "].");
}
}
@@ -396,8 +363,7 @@ public class TableManager
{
Table table = _goidMap.get(gameOid);
if (table == null) {
Log.warning("Unable to find table for running game " +
"[gameOid=" + gameOid + "].");
Log.warning("Unable to find table for running game [gameOid=" + gameOid + "].");
return;
}
@@ -437,13 +403,13 @@ public class TableManager
// remove this occupant from the table
if (!pender.clearOccupantByOid(bodyOid)) {
Log.warning("Attempt to remove body from mapped table failed " +
"[table=" + pender + ", bodyOid=" + bodyOid + "].");
Log.warning("Attempt to remove body from mapped table failed [table=" + pender +
", bodyOid=" + bodyOid + "].");
return;
}
// either update or delete the table depending on whether or not
// we just removed the last occupant
// either update or delete the table depending on whether or not we just removed the last
// occupant
if (pender.isEmpty()) {
purgeTable(pender);
} else {
@@ -451,15 +417,15 @@ public class TableManager
}
}
/** A reference to the place object in which we're managing tables. */
protected PlaceObject _plobj;
/** A reference to the distributed object in which we're managing tables. */
protected DObject _dobj;
/** A reference to our distributed object casted to a table lobby object. */
protected TableLobbyObject _tlobj;
/** The class of table we instantiate. */
protected Class<? extends Table> _tableClass = Table.class;
/** A reference to our place object casted to a table lobby object. */
protected TableLobbyObject _tlobj;
/** The table of pending tables. */
protected HashIntMap<Table> _tables = new HashIntMap<Table>();
@@ -488,9 +454,7 @@ public class TableManager
maybeCheckOccupants(event);
}
/**
* Check to see if the set event causes us to update the table.
*/
/** Check to see if the set event causes us to update the table. */
protected void maybeCheckOccupants (NamedEvent event) {
if (GameObject.OCCUPANTS.equals(event.getName())) {
updateOccupants(event.getTargetOid());
@@ -499,5 +463,5 @@ public class TableManager
} // END: class GameDeathListener
/** A listener that prunes tables after the game dies. */
protected ChangeListener _gameListener = new GameListener();
protected GameListener _gameListener = new GameListener();
}