Package client

Source Code of client.Client

package client;

import game.dice.Dice;
import game.gamesheet.GameSheet;
import game.leaderboard.Leaderboard;

import java.util.Date;
import java.util.HashMap;

import server.constants.CommandList;
import server.protocol.PlayerList;
import client.frame.GameplayFrame;
import client.frame.StartupFrame;
import client.frame.YahtzeeFrame;
import client.frame.timer.ActTimeCountdown;
/**
* Defines a "Toolbox of business logic" for the application. Houses pointers to all of the vital instruments
* of the game. Also contains the functions for transitioning the client between game states.
*
* @author Priidu Neemre
*/
public class Client {
 
  public static final int CLIENT_NAME_MAXLENGTH = 15;
  public static final int CLIENT_NAME_MINLENGTH = 3;
  public static final int MAX_NO_OF_PLAYERS = 4;
  public static final int MIN_NO_OF_PLAYERS = 1;
 
  public static final int RELOAD_WITH_RESTART = 0;
  public static final int RELOAD_WITHOUT_RESTART = 1;
 
  public static final int TURN_TIME_MILLIS = 180000;
  public static final int TIMEUNIT_MILLIS = 1000;
 
  //Business-logic data variables of the client instance
  private Dice dice;
  private GameSheet gameSheet;
  private Leaderboard leaderboard;
  private ActTimeCountdown turnTimer;
  String clientName;
 
  private int noOfPlayers;
  private PlayerList joinedList;
 
  //Technical variables of the client session instance
  private Connection conn;
  private ClientState state;
  private YahtzeeFrame currentFrame;

  public Client(){
    setState(ClientState.PROCESS_LAUNCHED);
    conn = new Connection(ClientMain.INET_ADDRESS, ClientMain.COMM_PORT, this);
    joinedList = new PlayerList(new HashMap<String, Date>());
    currentFrame = new StartupFrame(this);
  }
 
  /**
   * Reloads(resets) the client and its data fields accoringly to the inputed condition code.
   *
   * @param conditionCode  a code specifying the type of reload being applied (for ex. <code>RELOAD_WITH_RESTART</code>
   * or <code>RELOAD_WITHOUT_RESTART</code> )
   */
  public void reloadClient(int conditionCode){
    if(conn.isConnectionCreated())conn.disconnect();
    if(turnTimer != null){  //Check, whether the turnTimer has been initialized
      turnTimer.resetCountdown();
      turnTimer.stop();
    }
    dice = null;
    gameSheet = null;
    clientName = null;
    turnTimer = null;
    joinedList.reset();
   
    if(conditionCode == RELOAD_WITH_RESTART){
      setState(ClientState.PROCESS_LAUNCHED);
      conn = new Connection(ClientMain.INET_ADDRESS, ClientMain.COMM_PORT, this);
      setCurrentFrame(new StartupFrame(this));
    }else{
      System.exit(0);
    }
  }
 
  //[START]CLIENT STATE TRANSITION functions
  /**
   * Transitions the client from the <code>PROCESS_LAUNCHED</code> client state to the <code>INITIATING_CONNECTION</code>
   * client state (forward transition). 
   *
   * @param tempName  the wished player name specified by the client
   * @param players  the number of players that the game should have, also specified by the client
   */
  public void fPrcoessLaunchedTOInitiatingConnection(String tempName, int players){
    setState(ClientState.INITIATING_CONNECTION);
    setClientName(tempName);
    setNoOfPlayers(players);
    getConnection().connect();
    getConnection().broadcast(CommandList.JOINCMD + CommandList.CMD_DELIM + getNoOfPlayers()
        + CommandList.CMD_DELIM  + tempName);
  }
 
  /**
   * Transitions the client from the <code>INITIATING_CONNECTION</code> client state to the <code>GAME_IN_PROGRESS</code>
   * client state (forward transition). 
   */
  public void fInitiatingConnectionTOGameInProgress(){
    getJoinedList().reset();
    setState(ClientState.GAME_IN_PROGRESS);
    setCurrentFrame(new GameplayFrame(this));
  }
 
  /**
   * Transitions the client from the <code>GAME_IN_PROGRESS</code> client state to the <code>GAME_FINISHED</code>
   * client state (forward transition). 
   */
  public void fGameInProgressTOGameFinished(){
    setState(ClientState.GAME_FINISHED);
  }
 
  /**
   * Transitions the client from the <code>INITIATING_CONNECTION</code> client state back to the to the
   * <code>PROCESS_LANCHED</code> client state (backward transition). 
   */
  public void bInitiatingConnectionTOProcessLaunched(){
    setState(ClientState.PROCESS_LAUNCHED);
    setClientName(null);
    setNoOfPlayers(0);
  }
  //[END]CLIENT STATE TRANSITION functions

  //[START]GETTERS AND SETTERS
  public Dice getDice(){
    return dice;
  }
  public void setDice(Dice dice){
    this.dice = dice;
  }
  public GameSheet getGameSheet(){
    return gameSheet;
  }
  public void setGameSheet(GameSheet gameSheet){
    this.gameSheet = gameSheet;
  }
  public Connection getConnection(){
    return conn;
  }
  public YahtzeeFrame getCurrentFrame(){
    return currentFrame;
  }
  public void setCurrentFrame(YahtzeeFrame currentFrame){
    this.currentFrame = currentFrame;
  }
  public ClientState getState(){
    return state;
  }
  public void setState(ClientState state){
    this.state = state;
  }
  public String getClientName(){
    return clientName;
  }
  public void setClientName(String clientName){
    this.clientName = clientName;
  }
  public Leaderboard getLeaderboard(){
    return leaderboard;
  }
  public void setLeaderboard(Leaderboard leaderboard){
    this.leaderboard = leaderboard;
  }
  public ActTimeCountdown getTurnTimer(){
    return turnTimer;
  }
  public void setTurnTimer(ActTimeCountdown turnTimer){
    this.turnTimer = turnTimer;
  }
  public PlayerList getJoinedList(){
    return joinedList;
  }
  public void setJoinedList(PlayerList joinedList){
    this.joinedList = joinedList;
  }
  public int getNoOfPlayers(){
    return noOfPlayers;
  }
  public void setNoOfPlayers(int players){
    noOfPlayers = players;
  }
  //[END]GETTERS AND SETTERS
}
TOP

Related Classes of client.Client

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.