Class Player

java.lang.Object
io.sockit.gameserver.Player

public abstract class Player
extends Object
This class represents a player in a game room. When a client session joins a room, the session is considered to be a spectator. A player instance gets created when a session takes seat to start playing in the room. Each Game should provide an implementation of this class as follows
 
    public class TicTacToePlayer extends Player{

        CellState playerToken;// X or 0
        
        public TicTacToePlayer(Session session, int seatNo) {
                super(session, seatNo);
                playerToken = seatNo == 1 ? CellState.x : CellState.o;
        }
        
        {@literal @}Override
        protected JsonObject prepareJsonForSelfClient() {
                JsonObjectBuilder json = Json.createObjectBuilder();
                json.add("playerToken", playerToken.toString());                
                return json.build();
        }

        {@literal @}Override
        protected boolean canJoinGamePlay() {
                return true;
        }
       //..........
    }
 
 
  • Field Summary

    Fields 
    Modifier and Type Field Description
    Room room
    the room of the player
    int seatNo
    The seat number of the player
    Session session
    The client session of the player.
  • Constructor Summary

    Constructors 
    Constructor Description
    Player​(Session session, int seatNo)
    Creates a Player whose session is the specified session and whose seat number is the specified seat number
  • Method Summary

    Modifier and Type Method Description
    protected abstract boolean canJoinGamePlay()
    called in startGamePlay() to check if player is allowed to play (takePart) in the new game play
    protected abstract boolean canPlayTurn()
    Called to check if player can play turn or not.
    void exitGamePlay()
    Causes player to exit the current game play.
    boolean isActive()
    Checks if the player is still active in the current game play.
    boolean isCurTurnPlayer()
    Checks if the current turn is the player's or not
    void leaveSeat()
    Causes player to leave seat.
    protected String onCantJoinGamePlay()
    EventHandler/CallBack when player can't join the game play.
    protected void onRoomDestroyed()
    EventHandler/CallBack called when Room is destroyed.
    protected void onShutDown()
    EventHandler/CallBack called when the Game Engine is shutdown.
    protected abstract JsonObject prepareJsonForOtherClients()
    Converts playerData to json that will be sent to other clients.
    protected abstract JsonObject prepareJsonForSelfClient()
    Converts playerData to json that will be sent to own client.
    protected abstract void resetData()
    Callback called when game play has ended to reset player data
    boolean wasActive()
    checks whether player was or is active in the current game play - returns true if a player was active when the game play began.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • session

      public final Session session
      The client session of the player.
    • seatNo

      public final int seatNo
      The seat number of the player
    • room

      public final Room room
      the room of the player
  • Constructor Details

    • Player

      public Player​(Session session, int seatNo)
      Creates a Player whose session is the specified session and whose seat number is the specified seat number
      Parameters:
      session - - the player's client session
      seatNo - - the seat number of the player
  • Method Details

    • leaveSeat

      public final void leaveSeat()
      Causes player to leave seat. After calling this method the client session becomes a spectator and the player instance is destroyed.
    • exitGamePlay

      public final void exitGamePlay()
      Causes player to exit the current game play. Note player still remains seated and will take part/play in the next game play after the current game play ends. In a game like poker exitGamePlay() can be called when the player folds
    • isActive

      public final boolean isActive()
      Checks if the player is still active in the current game play. For example in a game like poker player will remain active till the player folds or the game ends
      Returns:
      boolean - true if the player is active in the current game play
    • wasActive

      public final boolean wasActive()
      checks whether player was or is active in the current game play - returns true if a player was active when the game play began.
      Returns:
      boolean - true if the player was active when the game play began
    • isCurTurnPlayer

      public final boolean isCurTurnPlayer()
      Checks if the current turn is the player's or not
      Returns:
      boolean - true if the current turn is the player's
    • prepareJsonForOtherClients

      protected abstract JsonObject prepareJsonForOtherClients()
      Converts playerData to json that will be sent to other clients. This method is called whenever a player's data has to sent to other clients. This method should be overridden in the actual Player class
      Returns:
      JsonObject - the player data as json
    • prepareJsonForSelfClient

      protected abstract JsonObject prepareJsonForSelfClient()
      Converts playerData to json that will be sent to own client. This method is called whenever a player's data has to sent to own client. This method should be overridden in the actual Player class
      Returns:
      JsonObject - the player data as json
    • onShutDown

      protected void onShutDown()
      EventHandler/CallBack called when the Game Engine is shutdown.
    • onRoomDestroyed

      protected void onRoomDestroyed()
      EventHandler/CallBack called when Room is destroyed. In a game like poker you may restore the player's chips in hand from the chips on table in this event
    • resetData

      protected abstract void resetData()
      Callback called when game play has ended to reset player data
    • onCantJoinGamePlay

      protected String onCantJoinGamePlay()
      EventHandler/CallBack when player can't join the game play. You may force player to leave seat in this event. Should return the reason why player cant join if you want to send the cantJoinGamePlay message to the client else return null
      Returns:
      String - the reason why the player can't join the game play or null if no message should be sent to the client
    • canJoinGamePlay

      protected abstract boolean canJoinGamePlay()
      called in startGamePlay() to check if player is allowed to play (takePart) in the new game play
      Returns:
      boolean - true if the player is allowed to join the game play
    • canPlayTurn

      protected abstract boolean canPlayTurn()
      Called to check if player can play turn or not. For example in poker if player is all in or folded then player cant play turn and the turn should go to the next player.
      Returns:
      boolean - true if the player can play the turn.