com.threerings.parlor.game.server

## Class GameManager

- java.lang.Object
- - com.threerings.crowd.server.PlaceManager
  - - com.threerings.parlor.game.server.GameManager

All Implemented Interfaces:
  com.threerings.crowd.chat.server.SpeakHandler.SpeakerValidator, ParlorCodes, GameCodes, PlayManager, com.threerings.presents.data.InvocationCodes, com.threerings.presents.dobj.ChangeListener, com.threerings.presents.dobj.MessageListener

Direct Known Subclasses:
  CardGameManager, PuzzleManager

    public class GameManager
    extends com.threerings.crowd.server.PlaceManager
    implements ParlorCodes, GameCodes, PlayManager

The game manager handles the server side management of a game. It manipulates the game state in accordance with the logic of the game flow and generally manages the whole game playing process.

The game manager extends the place manager because games are implicitly played in a location, the players of the game implicitly bodies in that location.

### Nested Class Summary

### Nested classes/interfaces inherited from class com.threerings.crowd.server.PlaceManager

`com.threerings.crowd.server.PlaceManager.DelegateOp, com.threerings.crowd.server.PlaceManager.MessageHandler`

### Field Summary

### Fields inherited from interface com.threerings.parlor.data.ParlorCodes

`ALREADY_AT_TABLE, BANNED_FROM_TABLE, GAME_ALREADY_STARTED, INVALID_TABLE_POSITION, INVITATION_ACCEPTED, INVITATION_COUNTERED, INVITATION_REFUSED, INVITEE_NOT_ONLINE, MUST_BE_CREATOR, NO_SELF_BOOT, NO_SUCH_TABLE, NOT_AT_TABLE, PARLOR_GROUP, TABLE_POSITION_OCCUPIED`

### Fields inherited from interface com.threerings.parlor.game.data.GameCodes

`GAME_CHAT_TYPE, GAME_MESSAGE_BUNDLE, PLAYER_KNOCKED_OUT, WINNERS_AND_LOSERS`

### Fields inherited from interface com.threerings.presents.data.InvocationCodes

`ACCESS_DENIED, E_ACCESS_DENIED, E_INTERNAL_ERROR, GLOBAL_GROUP, INTERNAL_ERROR`

### Constructor Summary

Constructors
Constructor and Description
`GameManager()`

### Method Summary

All Methods Instance Methods Concrete Methods
`int` `addPlayer(com.threerings.util.Name player)` Adds the given player to the game at the first available player index.
`boolean` `addPlayerAt(com.threerings.util.Name player, int pidx)` Adds the given player to the game at the specified player index.
`boolean` `allPlayersReady()` Returns true if all (non-AI) players have delivered their `playerReady(com.threerings.crowd.data.BodyObject)` notifications, false if they have not.
`boolean` `cancelGame()` Sets the state of the game to `GameObject.CANCELLED`.
`com.threerings.crowd.data.BodyObject` `checkWritePermission(com.threerings.presents.data.ClientObject client, int playerId)` Make sure that the given caller is a player or an agent and can write to the data of the given playerId.
`void` `endGame()` Called when the game is known to be over.
`void` `endPlayerGame(int pidx)` Ends the game for the given player.
`GameConfig` `getGameConfig()` Returns the configuration object for the game being managed by this manager.
`int` `getGameId()` Returns the unique numeric identifier for our managed game.
`int` `getMatchType()` A convenience method for getting the game type.
`com.threerings.crowd.data.BodyObject` `getPlayer(int playerIdx)` Returns the user object for the player with the specified index or null if the player at that index is not online.
`int` `getPlayerCount()` Returns the number of players in the game.
`com.threerings.util.Name` `getPlayerDisplayName(int index)` Returns the name that should be shown in the client for the player with the specified index or null if no player exists at that index.
`int` `getPlayerIndex(com.threerings.util.Name username)` Returns the player index of the given user in the game, or `-1` if the player is not involved in the game.
`com.threerings.util.Name` `getPlayerName(int index)` Returns the name of the player with the specified index or null if no player exists at that index.
`int` `getPlayerOid(int index)` Returns the user object oid of the player with the specified index.
`int` `getPlayerPersistentId(com.threerings.crowd.data.BodyObject body)` Convenience for getting the persistent id from a body.
`int` `getPlayerPersistentId(com.threerings.util.Name name)` Returns the persistent user id for the supplied player name.
`int` `getPlayerSlots()` Returns the number of players allowed in this game.
`int` `getPresentPlayerIndex(int bodyOid)` Get the player index of the specified oid, or -1 if the oid is not a player or is a player that is not presently in the game.
`int` `getSessionId()` Returns the unique session identifier for this game session.
`boolean` `isActivePlayer(int pidx)` Returns whether the player at the specified player index is actively playing the game
`boolean` `isAgent(com.threerings.presents.data.ClientObject client)` Return true if the given client is a server-side agent in this place.
`boolean` `isAI(int pidx)` Returns whether the player at the specified player index is an AI.
`boolean` `isPlayer(com.threerings.presents.data.ClientObject client)` Return true if the given client is a player in this place.
`void` `occupantInRoom(com.threerings.crowd.data.BodyObject caller)` Called by the client when an occupant has arrived in the game room and has loaded their bits.
`boolean` `playerIsReady(int pidx)` Returns true if the player at the specified slot is ready (or if there is meant to be no player in that slot), false if there is meant to be a player in the specified slot and they have not yet reported that they are ready.
`void` `playerReady(com.threerings.crowd.data.BodyObject caller)` Called by the client when the player is ready for the game to start.
`boolean` `removePlayer(com.threerings.util.Name player)` Removes the given player from the game.
`void` `replacePlayer(int pidx, com.threerings.util.Name player)` Replaces the player at the specified index and calls `playerWasReplaced(int, com.threerings.util.Name, com.threerings.util.Name)` to let derived classes and delegates know what's going on.
`void` `resetGame()` Called when the game is to be reset to its starting state in preparation for a new game without actually ending the current game.
`void` `setAI(int pidx, GameAI ai)` Sets the specified player as an AI with the specified configuration.
`boolean` `shouldConcludeGame()` Returns whether game conclusion antics such as rating updates should be performed when an in-play game is ended.
`boolean` `startGame()` This is called when the game is ready to start (all players involved have delivered their "am ready" notifications).
`void` `systemMessage(String msgbundle, String msg)` Sends a system message to the players in the game room.
`void` `systemMessage(String msgbundle, String msg, boolean waitForStart)` Sends a system message to the players in the game room.

### Methods inherited from class com.threerings.crowd.server.PlaceManager

`addDelegate, applyToDelegates, applyToOccupants, bodyWillEnter, bodyWillLeave, checkPermissions, getConfig, getLocation, getPlaceObject, init, isValidSpeaker, messageReceived, ratifyBodyEntry, registerMessageHandler, shutdown, startup, toString, updateOccupantInfo, where`

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### GameManager

    public GameManager()

### Method Detail

#### getGameConfig

    public GameConfig getGameConfig()

Returns the configuration object for the game being managed by this manager.

#### getGameId

    public int getGameId()

Returns the unique numeric identifier for our managed game. See `GameConfig.getGameId()`.

#### getMatchType

    public int getMatchType()

A convenience method for getting the game type.

#### addPlayer

    public int addPlayer(com.threerings.util.Name player)

Adds the given player to the game at the first available player index. This should only be called before the game is started, and is most likely to be used to add players to party games.

Parameters:
  `player` - the username of the player to add to this game.
Returns:
  the player index at which the player was added, or `-1` if the player could not be added to the game.

#### addPlayerAt

    public boolean addPlayerAt(com.threerings.util.Name player,
                               int pidx)

Adds the given player to the game at the specified player index. This should only be called before the game is started, and is most likely to be used to add players to party games.

Parameters:
  `player` - the username of the player to add to this game.
  `pidx` - the player index at which the player is to be added.
Returns:
  true if the player was added successfully, false if not.

#### removePlayer

    public boolean removePlayer(com.threerings.util.Name player)

Removes the given player from the game. This is most likely to be used to allow players involved in a party game to leave the game early-on if they realize they'd rather not play for some reason.

Parameters:
  `player` - the username of the player to remove from this game.
Returns:
  true if the player was successfully removed, false if not.

#### replacePlayer

    public void replacePlayer(int pidx,
                              com.threerings.util.Name player)

Replaces the player at the specified index and calls `playerWasReplaced(int, com.threerings.util.Name, com.threerings.util.Name)` to let derived classes and delegates know what's going on.

#### getPlayer

    public com.threerings.crowd.data.BodyObject getPlayer(int playerIdx)

Returns the user object for the player with the specified index or null if the player at that index is not online.

#### setAI

    public void setAI(int pidx,
                      GameAI ai)

Sets the specified player as an AI with the specified configuration. It is assumed that this will be set soon after the player names for all AIs present in the game. (It should be done before human players start trickling into the game.)

Parameters:
  `pidx` - the player index of the AI.
  `ai` - the AI configuration.

#### getPlayerName

    public com.threerings.util.Name getPlayerName(int index)

Returns the name of the player with the specified index or null if no player exists at that index.

#### getPlayerDisplayName

    public com.threerings.util.Name getPlayerDisplayName(int index)

Returns the name that should be shown in the client for the player with the specified index or null if no player exists at that index. This may be different than their username as returned by `getPlayerName(int)`, which is the player's unique name in the server.

#### getPlayerIndex

    public int getPlayerIndex(com.threerings.util.Name username)

Returns the player index of the given user in the game, or `-1` if the player is not involved in the game.

#### getPresentPlayerIndex

    public int getPresentPlayerIndex(int bodyOid)

Get the player index of the specified oid, or -1 if the oid is not a player or is a player that is not presently in the game.

#### getPlayerOid

    public int getPlayerOid(int index)

Returns the user object oid of the player with the specified index.

#### getPlayerPersistentId

    public int getPlayerPersistentId(com.threerings.util.Name name)

Returns the persistent user id for the supplied player name.

#### getPlayerPersistentId

    public int getPlayerPersistentId(com.threerings.crowd.data.BodyObject body)

Convenience for getting the persistent id from a body.

#### getPlayerCount

    public int getPlayerCount()

Returns the number of players in the game.

#### getPlayerSlots

    public int getPlayerSlots()

Returns the number of players allowed in this game.

#### isAI

    public boolean isAI(int pidx)

Returns whether the player at the specified player index is an AI.

#### isActivePlayer

    public boolean isActivePlayer(int pidx)

Returns whether the player at the specified player index is actively playing the game

#### getSessionId

    public int getSessionId()

Returns the unique session identifier for this game session.

#### systemMessage

    public void systemMessage(String msgbundle,
                              String msg)

Sends a system message to the players in the game room.

#### systemMessage

    public void systemMessage(String msgbundle,
                              String msg,
                              boolean waitForStart)

Sends a system message to the players in the game room.

Parameters:
  `waitForStart` - if true, the message will not be sent until the game has started.

#### startGame

    public boolean startGame()

This is called when the game is ready to start (all players involved have delivered their "am ready" notifications). It calls `gameWillStart()`, sets the necessary wheels in motion and then calls `gameDidStart()`. Derived classes should override one or both of the calldown functions (rather than this function) if they need to do things before or after the game starts.

Returns:
  true if the game was started, false if it could not be started because it was already in play or because all players have not yet reported in.

#### endPlayerGame

    public void endPlayerGame(int pidx)

Ends the game for the given player.

#### endGame

    public void endGame()

Called when the game is known to be over. This will call some calldown functions to determine the winner of the game and then transition the game to the `GameObject.GAME_OVER` state.

#### cancelGame

    public boolean cancelGame()

Sets the state of the game to `GameObject.CANCELLED`.

Returns:
  true if the game was cancelled, false if it was already over or cancelled.

#### shouldConcludeGame

    public boolean shouldConcludeGame()

Returns whether game conclusion antics such as rating updates should be performed when an in-play game is ended. Derived classes may wish to override this method to customize the conditions under which the game is concluded.

#### resetGame

    public void resetGame()

Called when the game is to be reset to its starting state in preparation for a new game without actually ending the current game. It calls `gameWillReset()` followed by the standard game start processing (`gameWillStart()` and `gameDidStart()`). Derived classes should override these calldown functions (rather than this function) if they need to do things before or after the game resets.

#### occupantInRoom

    public void occupantInRoom(com.threerings.crowd.data.BodyObject caller)

Called by the client when an occupant has arrived in the game room and has loaded their bits. Most games will simply call `playerReady(com.threerings.crowd.data.BodyObject)` but games that wish to delay their actual start until players take some action must report ASAP with a call to `occupantInRoom(com.threerings.crowd.data.BodyObject)` to let the server know that they have arrived and will later be calling `playerReady(com.threerings.crowd.data.BodyObject)` when they are ready for the game to actually start.

#### playerReady

    public void playerReady(com.threerings.crowd.data.BodyObject caller)

Called by the client when the player is ready for the game to start. This method is dispatched dynamically by `PlaceManager.messageReceived(com.threerings.presents.dobj.MessageEvent)`.

#### allPlayersReady

    public boolean allPlayersReady()

Returns true if all (non-AI) players have delivered their `playerReady(com.threerings.crowd.data.BodyObject)` notifications, false if they have not.

#### playerIsReady

    public boolean playerIsReady(int pidx)

Returns true if the player at the specified slot is ready (or if there is meant to be no player in that slot), false if there is meant to be a player in the specified slot and they have not yet reported that they are ready.

#### isPlayer

    public boolean isPlayer(com.threerings.presents.data.ClientObject client)

Description copied from interface: `PlayManager`
Return true if the given client is a player in this place.

Specified by:
  `isPlayer` in interface `PlayManager`

#### isAgent

    public boolean isAgent(com.threerings.presents.data.ClientObject client)

Description copied from interface: `PlayManager`
Return true if the given client is a server-side agent in this place.

Specified by:
  `isAgent` in interface `PlayManager`

#### checkWritePermission

    public com.threerings.crowd.data.BodyObject checkWritePermission(com.threerings.presents.data.ClientObject client,
                                                                     int playerId)

Description copied from interface: `PlayManager`
Make sure that the given caller is a player or an agent and can write to the data of the given playerId.

Specified by:
  `checkWritePermission` in interface `PlayManager`
Returns:
  the resolved player object to write to