Class Client

java.lang.Object
java.lang.Thread
it.polimi.ingsw.eriantys.client.Client
All Implemented Interfaces:
Runnable

public class Client extends Thread
This is the main class for the client-side application. It starts the Socket, connects to the server and handles received and sent messages.
  • Constructor Details

    • Client

      public Client(String serverAddress, int serverPort, boolean gui) throws IOException
      Constructs a Client object, starting the Socket and either the CommandLineInterface or the GraphicalUserInterface.
      Parameters:
      serverAddress - the server IP address
      serverPort - the port number identifying the server socket
      gui - whether to start the GUI (if true) or the CLI (if false)
      Throws:
      IOException - if the Socket could not be opened, the connection to the server could not be established, or a problem occurs while opening configuration files
  • Method Details

    • main

      public static void main(String[] args) throws IOException
      The method is called to launch the client-side application (in either CLI or GUI mode).
      Parameters:
      args - the command line arguments
      Throws:
      IOException - if a Client object cannot be constructed
    • run

      public void run()
      Starts the user interface thread and then loops to receive and handle messages from the server.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • write

      public void write(Message message)
      Sends a message to the server.
      Parameters:
      message - the message to send
    • setRunning

      public void setRunning(boolean running)
      Synchronized method to set the running attribute.
      Parameters:
      running - the value to set
    • getUsername

      public String getUsername()
      A getter for the username chosen by the player.
      Returns:
      the chosen username
    • setUsername

      public void setUsername(String username)
      A setter for the username attribute.
      Parameters:
      username - the username chosen by the player
    • setGameId

      public void setGameId(int gameId)
      A setter for the gameId attribute.
      Parameters:
      gameId - the identifier of the game the player joined
    • getTowerColor

      public String getTowerColor()
      A getter for the towerColor attribute.
      Returns:
      the TowerColor literal selected by the player
    • setTowerColor

      public void setTowerColor(String towerColor)
      Sets the tower color selected by the player and tries to send the selection to the server.
      Parameters:
      towerColor - the selected TowerColor literal
    • getWizard

      public String getWizard()
      A getter for the wizard attribute.
      Returns:
      the Wizard literal selected by the player
    • setWizard

      public void setWizard(String wizard)
      Sets the wizard selected by the player and tries to send the selection to the server.
      Parameters:
      wizard - the selected Wizard literal
    • getBoardStatus

      public BoardStatus getBoardStatus()
      A getter for the last BoardStatus received from the server. If the boardStatus is null, an error is shown via UI.
      Returns:
      the latest board status
    • setBoardStatus

      public void setBoardStatus(BoardStatus boardStatus)
      A setter for the boardStatus attribute.
      Parameters:
      boardStatus - the board status to set
    • getAvailableCards

      public List<String> getAvailableCards()
      A getter for the list of the assistant cards that the player is allowed to play.
      Returns:
      the list of available assistant cards
    • setAvailableCards

      public void setAvailableCards(List<String> availableCards)
      A setter for the availableCards attribute.
      Parameters:
      availableCards - the list of the assistant cards that the player is allowed to play
    • getCharacterCard

      public Integer getCharacterCard()
      A getter for the characterCard attribute.
      Returns:
      the character card selected by the player
    • askHelp

      public void askHelp()
      Sends a HelpRequest message to the server.
    • sendHandshake

      public void sendHandshake(String username)
      Checks if the username is valid and sends a Handshake message to the server.
      Parameters:
      username - the username selected by the player
    • sendReconnect

      public void sendReconnect()
      Checks if a reconnection is available and sends a Reconnect message to the server.
    • askLobbies

      public void askLobbies()
      Sends a LobbiesRequest message to the server.
    • joinLobby

      public void joinLobby(String lobbyIdArg)
      Checks if the lobby id is a valid integer and sends a JoinLobby message to the server.
      Parameters:
      lobbyIdArg - the identifier of the lobby to join
    • createLobby

      public void createLobby(String numPlayersArg, String expertModeArg)
      Checks if the parameters are valid and sends a LobbyCreation message to the server.
      Parameters:
      numPlayersArg - the desired size of the lobby
      expertModeArg - whether to use expert mode in the lobby
    • putReconnectSettings

      public void putReconnectSettings(AcceptedJoinLobby message)
      Saves the reconnection settings.
      Parameters:
      message - the message containing the information about the joined lobby
    • removeReconnectSettings

      public void removeReconnectSettings()
      Removes the reconnection settings.
    • hasReconnectSettings

      public boolean hasReconnectSettings()
      Checks if a reconnection is available.
      Returns:
      true if reconnection settings are found
    • leaveLobby

      public void leaveLobby()
      If the player is in a lobby, sends a LeaveLobby message to the server.
    • playAssistantCard

      public void playAssistantCard(String card)
      Sends a PlayAssistantCard message to the server.
      Parameters:
      card - the AssistantCard literal
    • moveStudent

      public void moveStudent(String color, String destination)
      Sends a MoveStudent message to the server.
      Parameters:
      color - the Color literal of the student to move
      destination - where to place the student
    • moveMotherNature

      public void moveMotherNature(String island)
      Sends a MotherNatureDestination message to the server.
      Parameters:
      island - the identifier of the island where to put Mother Nature
    • chooseCloud

      public void chooseCloud(int cloud)
      Sends a SelectCloud message to the server.
      Parameters:
      cloud - the identifier of the selected cloud
    • setCharacterCard

      public void setCharacterCard(Integer card)
      Sets the characterCard attribute to save the selected character card while choosing the requested arguments.
      Parameters:
      card - the selected card
    • playCharacterCard

      public void playCharacterCard(String[] sourceColors, String[] destinationColors, String targetColor, String targetIsland)
      Builds the JsonObject containing the given arguments to play the selected character card, sends a PlayCharacterCard message to the server and then clears the characterCard attribute setting it to null.
      Parameters:
      sourceColors - an array of colors selected as a source for the character card effect
      destinationColors - an array of colors selected as a destination for the character card effect
      targetColor - the single color selected for the character card effect
      targetIsland - the island selected for the character card effect