Package it.polimi.ingsw.Client.CLI
Class CliWriter
java.lang.Object
it.polimi.ingsw.Client.CLI.CliWriter
- All Implemented Interfaces:
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
Modifier and TypeFieldDescriptionprivate 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 oneprivate final SocketWrapper
socket wrapper to connect the Client to the Serverprivate final BufferedReader
used to acquire text from command line -
Constructor Summary
ConstructorDescriptionCliWriter
(SocketWrapper socketWrapper, ClientView clientView, BufferedReader bufferedReader, CyclicBarrier cyclicBarrier) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
This method prints selected characterCard to the command line to explain card's effect to the playerprivate 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).private PlayerActionRequest
createPlayerActionByPawnColour
(int selected, PlayerBoard currentPlayer) Support method that create the PlayerActionRequest after that user type a PawnColour.private void
elaborateInput
(String userInput) This method, given a String, executes the proper method according to User's requestprivate void
endTurn()
Executes the endTurn command to end currentPlayer's action turn and move on to the next playerprivate 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
getInt()
Support method to read an integer from command line, this method does not accept 'enter' as inputprivate OptionalValue<Integer>
Support method to read an integer from command line, this method accepts 'enter' as inputprivate 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 takeprivate 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 pawnprivate 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 gameprivate void
print all the available actions basing on current game phasevoid
run()
private void
Executes the startGame command, it can be executed only by lobby's admin and whether the lobby is full.
-
Field Details
-
cyclicBarrier
used to synchronize CliWriter and CliReader, useful when the first one needs to wait the second one -
socketWrapper
socket wrapper to connect the Client to the Server -
clientView
used to store the client's game data -
stdIn
used to acquire text from command line
-
-
Constructor Details
-
CliWriter
public CliWriter(SocketWrapper socketWrapper, ClientView clientView, BufferedReader bufferedReader, CyclicBarrier cyclicBarrier)
-
-
Method Details
-
run
public void run() -
elaborateInput
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
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
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
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
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
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
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
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 pawnFinally, it creates the moveStudent request and then sends it to the Server.
- Throws:
IOException
- if an I/O error occurs
-
moveMotherNature
Executes the moveMotherNature command, this method asks the user to type the amount of steps motherNature should takeFinally, it creates the moveMotherNature request and then sends it to the Server.
- Throws:
IOException
- if an I/O error occurs
-
chooseCloud
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
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
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
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
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 gamecurrentPlayer
- 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
-