Package it.polimi.ingsw.eriantys.client
Class Client
java.lang.Object
java.lang.Thread
it.polimi.ingsw.eriantys.client.Client
- All Implemented Interfaces:
Runnable
This is the main class for the client-side application.
It starts the
Socket, connects to the server and handles received and sent messages.-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aClientobject, starting theSocketand either theCommandLineInterfaceor theGraphicalUserInterface. -
Method Summary
Modifier and TypeMethodDescriptionvoidaskHelp()Sends aHelpRequestmessage to the server.voidSends aLobbiesRequestmessage to the server.voidchooseCloud(int cloud) Sends aSelectCloudmessage to the server.voidcreateLobby(String numPlayersArg, String expertModeArg) Checks if the parameters are valid and sends aLobbyCreationmessage to the server.A getter for the list of the assistant cards that the player is allowed to play.A getter for the lastBoardStatusreceived from the server.A getter for thecharacterCardattribute.A getter for thetowerColorattribute.A getter for the username chosen by the player.A getter for thewizardattribute.booleanChecks if a reconnection is available.voidChecks if the lobby id is a valid integer and sends aJoinLobbymessage to the server.voidIf the player is in a lobby, sends aLeaveLobbymessage to the server.static voidThe method is called to launch the client-side application (in either CLI or GUI mode).voidmoveMotherNature(String island) Sends aMotherNatureDestinationmessage to the server.voidmoveStudent(String color, String destination) Sends aMoveStudentmessage to the server.voidplayAssistantCard(String card) Sends aPlayAssistantCardmessage to the server.voidplayCharacterCard(String[] sourceColors, String[] destinationColors, String targetColor, String targetIsland) Builds theJsonObjectcontaining the given arguments to play the selected character card, sends aPlayCharacterCardmessage to the server and then clears thecharacterCardattribute setting it tonull.voidputReconnectSettings(AcceptedJoinLobby message) Saves the reconnection settings.voidRemoves the reconnection settings.voidrun()Starts the user interface thread and then loops to receive and handle messages from the server.voidsendHandshake(String username) Checks if the username is valid and sends aHandshakemessage to the server.voidChecks if a reconnection is available and sends aReconnectmessage to the server.voidsetAvailableCards(List<String> availableCards) A setter for theavailableCardsattribute.voidsetBoardStatus(BoardStatus boardStatus) A setter for theboardStatusattribute.voidsetCharacterCard(Integer card) Sets thecharacterCardattribute to save the selected character card while choosing the requested arguments.voidsetGameId(int gameId) A setter for thegameIdattribute.voidsetRunning(boolean running) Synchronized method to set therunningattribute.voidsetTowerColor(String towerColor) Sets the tower color selected by the player and tries to send the selection to the server.voidsetUsername(String username) A setter for theusernameattribute.voidSets the wizard selected by the player and tries to send the selection to the server.voidSends a message to the server.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Constructor Details
-
Client
Constructs aClientobject, starting theSocketand either theCommandLineInterfaceor theGraphicalUserInterface.- Parameters:
serverAddress- the server IP addressserverPort- the port number identifying the server socketgui- whether to start the GUI (iftrue) or the CLI (iffalse)- Throws:
IOException- if theSocketcould not be opened, the connection to the server could not be established, or a problem occurs while opening configuration files
-
-
Method Details
-
main
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 aClientobject cannot be constructed
-
run
public void run()Starts the user interface thread and then loops to receive and handle messages from the server. -
write
Sends a message to the server.- Parameters:
message- the message to send
-
setRunning
public void setRunning(boolean running) Synchronized method to set therunningattribute.- Parameters:
running- the value to set
-
getUsername
A getter for the username chosen by the player.- Returns:
- the chosen username
-
setUsername
A setter for theusernameattribute.- Parameters:
username- the username chosen by the player
-
setGameId
public void setGameId(int gameId) A setter for thegameIdattribute.- Parameters:
gameId- the identifier of the game the player joined
-
getTowerColor
A getter for thetowerColorattribute.- Returns:
- the
TowerColorliteral selected by the player
-
setTowerColor
Sets the tower color selected by the player and tries to send the selection to the server.- Parameters:
towerColor- the selectedTowerColorliteral
-
getWizard
A getter for thewizardattribute.- Returns:
- the
Wizardliteral selected by the player
-
setWizard
Sets the wizard selected by the player and tries to send the selection to the server.- Parameters:
wizard- the selectedWizardliteral
-
getBoardStatus
A getter for the lastBoardStatusreceived from the server. If theboardStatusisnull, an error is shown via UI.- Returns:
- the latest board status
-
setBoardStatus
A setter for theboardStatusattribute.- Parameters:
boardStatus- the board status to set
-
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
A setter for theavailableCardsattribute.- Parameters:
availableCards- the list of the assistant cards that the player is allowed to play
-
getCharacterCard
A getter for thecharacterCardattribute.- Returns:
- the character card selected by the player
-
askHelp
public void askHelp()Sends aHelpRequestmessage to the server. -
sendHandshake
Checks if the username is valid and sends aHandshakemessage to the server.- Parameters:
username- the username selected by the player
-
sendReconnect
public void sendReconnect()Checks if a reconnection is available and sends aReconnectmessage to the server. -
askLobbies
public void askLobbies()Sends aLobbiesRequestmessage to the server. -
joinLobby
Checks if the lobby id is a valid integer and sends aJoinLobbymessage to the server.- Parameters:
lobbyIdArg- the identifier of the lobby to join
-
createLobby
Checks if the parameters are valid and sends aLobbyCreationmessage to the server.- Parameters:
numPlayersArg- the desired size of the lobbyexpertModeArg- whether to use expert mode in the lobby
-
putReconnectSettings
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:
trueif reconnection settings are found
-
leaveLobby
public void leaveLobby()If the player is in a lobby, sends aLeaveLobbymessage to the server. -
playAssistantCard
Sends aPlayAssistantCardmessage to the server.- Parameters:
card- theAssistantCardliteral
-
moveStudent
Sends aMoveStudentmessage to the server.- Parameters:
color- theColorliteral of the student to movedestination- where to place the student
-
moveMotherNature
Sends aMotherNatureDestinationmessage to the server.- Parameters:
island- the identifier of the island where to put Mother Nature
-
chooseCloud
public void chooseCloud(int cloud) Sends aSelectCloudmessage to the server.- Parameters:
cloud- the identifier of the selected cloud
-
setCharacterCard
Sets thecharacterCardattribute 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 theJsonObjectcontaining the given arguments to play the selected character card, sends aPlayCharacterCardmessage to the server and then clears thecharacterCardattribute setting it tonull.- Parameters:
sourceColors- an array of colors selected as a source for the character card effectdestinationColors- an array of colors selected as a destination for the character card effecttargetColor- the single color selected for the character card effecttargetIsland- the island selected for the character card effect
-