public class Player extends Object implements com.sun.sgs.app.ClientSessionListener, com.sun.sgs.app.ManagedObject, Serializable
Player for each user, and the user may not be logged in
more than once.
In addition to managing the details about a player (their name, characters,
channel, current uid, etc.), this is also the point where all incoming
messages from the client arrive. This means that message receipt and
processing is all done while blocking on the Player rather
than some general processing logic. This model helps distribute
synchronization and also protect against a player trying to disrupt
the system.
| Modifier and Type | Field and Description |
|---|---|
static String |
NAME_PREFIX
The standard namespace prefix for all players.
|
| Modifier and Type | Method and Description |
|---|---|
void |
broadcastBoardUpdate(Collection<BoardSpace> updates)
Sends a graphical update of specific spaces to the client.
|
void |
disconnected(boolean graceful)
Removes this
Player from the current game. |
PlayerCharacterManager |
getCharacterManager()
Returns the
CharacterManager that this Player
uses to manage their Characters. |
com.sun.sgs.app.ClientSession |
getCurrentSession()
Returns this
Player's current UserID. |
static Player |
getInstance(String name)
Looks for an existing instance of
Player for the given
name, and creates one if an instance doesn't already exist. |
String |
getName()
Returns the user's name.
|
boolean |
isPlaying()
Returns whether this
Player is currently logged in and
playing. |
void |
leaveCurrentLevel()
This is used to handle leaving dungeons before we can actually call
the
moveToGame (eg, when we die). |
void |
moveToGame(Game game)
Moves the player into the provided
Game. |
void |
notifyUnhandledCommand(String channelName,
Commands.Command command)
Notifies the client that they have sent an unhandled command to
the specified channel.
|
void |
receivedMessage(ByteBuffer message)
Called when data arrives from the a user.
|
void |
sendBoard(Board board)
Sends a complete
Board to the client. |
void |
sendCharacter(PlayerCharacter character)
Sends the statistics of the given character to the client.
|
void |
sendCharacterStats(int id,
CharacterStats stats)
Sends a set of character statistics to the player.
|
void |
sendServerMessage(String message)
Sends a server text message (different from a client chat message) to
the client.
|
void |
setCurrentSession(com.sun.sgs.app.ClientSession session)
Sets the current
Session for this Player, which
changes from session to session. |
String |
toString() |
void |
userJoinedChannel(com.sun.sgs.app.Channel newChannel)
Called when the player joins a channel.
|
public static final String NAME_PREFIX
public static Player getInstance(String name)
Player for the given
name, and creates one if an instance doesn't already exist. This
also takes care of registering the player.name - the account name of the userPlayer with the given namepublic boolean isPlaying()
Player is currently logged in and
playing.public String getName()
public PlayerCharacterManager getCharacterManager()
CharacterManager that this Player
uses to manage their Characters. A Player may
only play as one Character at a time.public void setCurrentSession(com.sun.sgs.app.ClientSession session)
Session for this Player, which
changes from session to session. Typically this is called when
the player first logs again, and not again until the player
logs out and logs back in.session - the Session associated with the player.public com.sun.sgs.app.ClientSession getCurrentSession()
Player's current UserID. Note
that this is only meaningful if isPlaying returns
true. Otherwise this will return null.null if the player is
not currently playingpublic void moveToGame(Game game)
Game. This
causes the Player to leave the game they are
currently playing (if they are currently playing a game) and
notify the new game that they are joining. If the reference
provided is null, then this Player is removed from
the current game and sets itself as not playing any games.
When the Player is first started, it is not
playing any games. In practice, this method is only called with
a value of null when the associated client logs out of the
server.
game - the new Game, or null if the player is
only being removed from a gamepublic void leaveCurrentLevel()
moveToGame (eg, when we die). It handles multiple
calls while on the same level cleanly (which is done by paranoid
checking when leaving dungeons).public void userJoinedChannel(com.sun.sgs.app.Channel newChannel)
newChannel - the new channelpublic void receivedMessage(ByteBuffer message)
Player sends
a message directly to the server (i.e., not a broadcast message like a
chat comment). This method, therefore, is the handler for all client
messages, and so we hold the lock on the Player and not
some more generally shared logic while we're processing messages.receivedMessage in interface com.sun.sgs.app.ClientSessionListenermessage - the data sent to the playerpublic void disconnected(boolean graceful)
Player from the current game.disconnected in interface com.sun.sgs.app.ClientSessionListenergraceful - whether the diconnection was gracefulpublic void sendBoard(Board board)
Board to the client.board - the Board to sendpublic void broadcastBoardUpdate(Collection<BoardSpace> updates)
updates - the updates to sendpublic void notifyUnhandledCommand(String channelName, Commands.Command command)
channelName - the name of the channel that the unhandled
command was sent oncommand - the unhandled commandpublic void sendCharacter(PlayerCharacter character)
character - the character who's statistics will be sentpublic void sendCharacterStats(int id,
CharacterStats stats)
id - the character's identifierstats - the character statisticspublic void sendServerMessage(String message)
message - the message to sendCopyright © 2013. All Rights Reserved.