Restructured table creation to allow it to be customized. Though I now

realize I will probably have to change it further.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3252 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2004-11-30 00:32:43 +00:00
parent 9dc8c542b8
commit dd4ce23b93
@@ -1,5 +1,5 @@
// //
// $Id: TableManager.java,v 1.11 2004/08/27 02:20:14 mdb Exp $ // $Id$
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -57,7 +57,7 @@ import com.threerings.parlor.game.GameManager;
* to the table manager for the associated invocation services. * to the table manager for the associated invocation services.
*/ */
public class TableManager public class TableManager
implements ParlorCodes, PlaceRegistry.CreationObserver, OidListListener implements ParlorCodes, OidListListener
{ {
/** /**
* Creates a table manager that will work in tandem with the specified * Creates a table manager that will work in tandem with the specified
@@ -178,20 +178,10 @@ public class TableManager
// if the table is sufficiently full, start the game automatically // if the table is sufficiently full, start the game automatically
if (table.shouldBeStarted()) { if (table.shouldBeStarted()) {
// create the game manager // fill the players array into the game config
GameManager gmgr = table.config.players = table.getPlayers();
createGameManager(table.config, table.getPlayers()); // and create the game
createGame(table);
// and map this table to this game manager so that we can
// update the table with the game in progress once it's
// created
_pendingTables.put(gmgr, table);
// clear out the occupant to table mappings because this game
// is underway
for (int i = 0; i < table.bodyOids.length; i++) {
_boidMap.remove(table.bodyOids[i]);
}
} else { } else {
// make a mapping from this occupant to this table // make a mapping from this occupant to this table
@@ -268,61 +258,50 @@ public class TableManager
/** /**
* Called when we're ready to create a game (either an invitation has * 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 * been accepted or a table is ready to start. If there is a problem
* creating the game manager, it will be reported in the logs. * creating the game manager, it should be reported in the logs.
*
* @return a reference to the newly created game manager or null if
* something choked during the creation or initialization process.
*/ */
protected GameManager createGameManager (GameConfig config, Name[] players) protected void createGame (final Table table)
throws InvocationException
{ {
GameManager gmgr = null; PlaceRegistry.CreationObserver obs =
new PlaceRegistry.CreationObserver() {
public void placeCreated (PlaceObject plobj, PlaceManager pmgr) {
gameCreated(table, plobj);
}
};
try { try {
Log.info("Creating game manager [config=" + config + "]."); CrowdServer.plreg.createPlace(table.config, obs);
} catch (InstantiationException ie) {
// configure the game config with the players array Log.warning("Failed to create manager for game " +
config.players = players; "[config=" + table.config + "]: " + ie);
throw new InvocationException(INTERNAL_ERROR);
// create the game manager and begin it's initialization
// process. the game manager will take care of notifying the
// players that the game has been created once it has been
// started up (which is done by the place registry once the
// game object creation has completed)
gmgr = (GameManager)CrowdServer.plreg.createPlace(config, this);
} catch (Exception e) {
Log.warning("Unable to create game manager [config=" + config +
", players=" + StringUtil.toString(players) + "].");
Log.logStackTrace(e);
} }
return gmgr;
} }
/** /**
* Called by the place registry when the game object has been created * Called when our game has been created, we take this opportunity to
* and provided to the game manager. We use this to map game object * clean up the table and transition it to "in play" mode.
* oids back to table records when we create games from tables.
*/ */
public void placeCreated (PlaceObject plobj, PlaceManager pmgr) protected void gameCreated (Table table, PlaceObject plobj)
{ {
// see if this place manager is associated with a table // update the table with the newly created game object
Table table = (Table)_pendingTables.remove(pmgr); table.gameOid = plobj.getOid();
if (table != null) {
// update the table with the newly created game object
table.gameOid = plobj.getOid();
// add an object death listener to unmap the table when the // clear the occupant to table mappings as this game is underway
// game finally goes away for (int i = 0; i < table.bodyOids.length; i++) {
plobj.addListener(new ObjectDeathListener() { _boidMap.remove(table.bodyOids[i]);
public void objectDestroyed (ObjectDestroyedEvent event) {
unmapTable(event.getTargetOid());
}
});
// and then update the lobby object that contains the table
_tlobj.updateTables(table);
} }
// add an object death listener to unmap the table when the game
// finally goes away
plobj.addListener(new ObjectDeathListener() {
public void objectDestroyed (ObjectDestroyedEvent event) {
unmapTable(event.getTargetOid());
}
});
// and then update the lobby object that contains the table
_tlobj.updateTables(table);
} }
/** /**
@@ -393,8 +372,4 @@ public class TableManager
/** A mapping from body oid to table. */ /** A mapping from body oid to table. */
protected HashIntMap _boidMap = new HashIntMap(); protected HashIntMap _boidMap = new HashIntMap();
/** A table of tables that have games that have been created but not
* yet started. */
protected HashMap _pendingTables = new HashMap();
} }