Class CliWriter

java.lang.Object
it.polimi.ingsw.Client.CLI.CliWriter
All Implemented Interfaces:
Runnable

public class CliWriter extends Object implements Runnable
This class takes Client's commands from terminal, provides support to the user and sends the command to the Server by using the SocketWrapper
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final ClientView
    used to store the client's game data
    (package private) final CyclicBarrier
    used to synchronize CliWriter and CliReader, useful when the first one needs to wait the second one
    private final SocketWrapper
    socket wrapper to connect the Client to the Server
    private final BufferedReader
    used to acquire text from command line
  • Constructor Summary

    Constructors
    Constructor
    Description
    CliWriter(SocketWrapper socketWrapper, ClientView clientView, BufferedReader bufferedReader, CyclicBarrier cyclicBarrier)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    This method prints selected characterCard to the command line to explain card's effect to the player
    private boolean
    Support method responsible for checking whether the user has the rights to perform the requested action.
    private void
    Executes the chooseCloud command, this method asks the user to type a number corresponding to Island's id.
    private void
    Executes the createLobby command; during the creation the user is able to choose lobby's visibility (private or public) and lobby's dimension (this dimension will also be game's number of players).
    createPlayerActionByPawnColour(int selected, PlayerBoard currentPlayer)
    Support method that create the PlayerActionRequest after that user type a PawnColour.
    private void
    This method, given a String, executes the proper method according to User's request
    private void
    Executes the endTurn command to end currentPlayer's action turn and move on to the next player
    private int
    Support method used to acquire chosen characterCard from command line; it requires user to type the characterCard number (not the index inside Game's list of 3 characterCards but the "id" o the card between 1 and 12).
    private int
    Support method to read an integer from command line, this method does not accept 'enter' as input
    Support method to read an integer from command line, this method accepts 'enter' as input
    private void
    Executes the joinLobby command by knowing lobby's UUID, it allows to access both open and private lobbies.
    private void
    Executes the moveMotherNature command, this method asks the user to type the amount of steps motherNature should take
    private void
    Executes the moveStudent command, the method requires from the user to type pawn's index inside entrance then it allows the user to choose the destination: type enter to move the pawn directly to the dining room type a number that will be interpreted as the island's id on which to move the pawn
    private void
    Executes the playAssistantCard command The method shows the available assistantCard at that moment (removing the used cards and cards picked by other players in the same turn to avoid playing cards with same value).
    private void
    This method, after checking the gameMode, takes CharacterCard's number and extracts the characteCard; basing on CharacterCard type the methods create dynamically the CharacterCardInput by requesting the user only the right input.
    private void
    Method responsible for print actions during all the game
    private void
    print all the available actions basing on current game phase
    void
    run()
     
    private void
    Executes the startGame command, it can be executed only by lobby's admin and whether the lobby is full.

    Methods inherited from class java.lang.Object

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

    • cyclicBarrier

      final CyclicBarrier cyclicBarrier
      used to synchronize CliWriter and CliReader, useful when the first one needs to wait the second one
    • socketWrapper

      private final SocketWrapper socketWrapper
      socket wrapper to connect the Client to the Server
    • clientView

      private final ClientView clientView
      used to store the client's game data
    • stdIn

      private final BufferedReader stdIn
      used to acquire text from command line
  • Constructor Details

  • Method Details

    • run

      public void run()
      Specified by:
      run in interface Runnable
    • elaborateInput

      private void elaborateInput(String userInput) throws IOException
      This method, given a String, executes the proper method according to User's request
      Parameters:
      userInput - : text containing the command
      Throws:
      IOException - the integer read from command line
    • playCharacterCard

      private void playCharacterCard() throws IOException
      This method, after checking the gameMode, takes CharacterCard's number and extracts the characteCard; basing on CharacterCard type the methods create dynamically the CharacterCardInput by requesting the user only the right input.
      Throws:
      IOException - input has gone wrong
    • characterCardInfo

      private void characterCardInfo() throws IOException
      This method prints selected characterCard to the command line to explain card's effect to the player
      Throws:
      IOException - input has gone wrong
    • checkActionRequest

      private boolean checkActionRequest()
      Support method responsible for checking whether the user has the rights to perform the requested action. This method will only be executed to check actions available only when the match has started
      Returns:
      true if the check fails
    • printActions

      private void printActions()
      Method responsible for print actions during all the game
    • createLobby

      private void createLobby() throws IOException
      Executes the createLobby command; during the creation the user is able to choose lobby's visibility (private or public) and lobby's dimension (this dimension will also be game's number of players).

      Finally, it creates the createLobby request and then sends it to the server.

      Throws:
      IOException - if an I/O error occurs
    • joinLobby

      private void joinLobby() throws IOException
      Executes the joinLobby command by knowing lobby's UUID, it allows to access both open and private lobbies.

      Finally, it creates the joinLobby request and then sends it to the Server.

      Throws:
      IOException - if an I/O error occurs
    • startGame

      private void startGame() throws IOException
      Executes the startGame command, it can be executed only by lobby's admin and whether the lobby is full. The admin can choose the game mode between simple and advanced.

      Finally, it creates the startGameRequest and then sends it to the Server.

      Throws:
      IOException - if an I/O error occurs
    • playAssistantCard

      private void playAssistantCard() throws IOException
      Executes the playAssistantCard command The method shows the available assistantCard at that moment (removing the used cards and cards picked by other players in the same turn to avoid playing cards with same value).

      Finally, it creates the playAssistantCard request and then sends it to the Server.

      Throws:
      IOException - if an I/O error occurs
    • moveStudent

      private void moveStudent() throws IOException
      Executes the moveStudent command, the method requires from the user to type pawn's index inside entrance then it allows the user to choose the destination: type enter to move the pawn directly to the dining room type a number that will be interpreted as the island's id on which to move the pawn

      Finally, it creates the moveStudent request and then sends it to the Server.

      Throws:
      IOException - if an I/O error occurs
    • moveMotherNature

      private void moveMotherNature() throws IOException
      Executes the moveMotherNature command, this method asks the user to type the amount of steps motherNature should take

      Finally, it creates the moveMotherNature request and then sends it to the Server.

      Throws:
      IOException - if an I/O error occurs
    • chooseCloud

      private void chooseCloud() throws IOException
      Executes the chooseCloud command, this method asks the user to type a number corresponding to Island's id. Finally, it creates the chooseCloudTile request and then sends it to the Server.
      Throws:
      IOException - if an I/O error occurs
    • endTurn

      private void endTurn() throws IOException
      Executes the endTurn command to end currentPlayer's action turn and move on to the next player
      Throws:
      IOException
    • printGameActions

      private void printGameActions()
      print all the available actions basing on current game phase
    • getCharacterCardIndex

      private int getCharacterCardIndex() throws IOException
      Support method used to acquire chosen characterCard from command line; it requires user to type the characterCard number (not the index inside Game's list of 3 characterCards but the "id" o the card between 1 and 12). The methods also checks if the chosen characterCard's number is present among the 3 CharacterCards available during the game.
      Returns:
      int representing characterCard's number
      Throws:
      IOException - if an I/O error occurs
    • getInt

      private int getInt() throws IOException
      Support method to read an integer from command line, this method does not accept 'enter' as input
      Returns:
      the integer read from command line
      Throws:
      IOException - if an I/O error occurs
    • getOptionalInt

      private OptionalValue<Integer> getOptionalInt() throws IOException
      Support method to read an integer from command line, this method accepts 'enter' as input
      Returns:
      the integer read from command line or Optional.empty if the user pressed "Enter"
      Throws:
      IOException - if an I/O error occurs
    • createPlayerActionByPawnColour

      private PlayerActionRequest createPlayerActionByPawnColour(int selected, PlayerBoard currentPlayer) throws IOException
      Support method that create the PlayerActionRequest after that user type a PawnColour.
      Parameters:
      selected - selected CharacterCard's index among the 3 available during the game
      currentPlayer - current player in turn, necessary to create PlayCharacterCard action
      Returns:
      PlayerActionRequest with the selected PawnColour as parameter
      Throws:
      IOException - if an I/O error occurs