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
ConstructorDescriptionConstructs aClient
object, starting theSocket
and either theCommandLineInterface
or theGraphicalUserInterface
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
askHelp()
Sends aHelpRequest
message to the server.void
Sends aLobbiesRequest
message to the server.void
chooseCloud
(int cloud) Sends aSelectCloud
message to the server.void
createLobby
(String numPlayersArg, String expertModeArg) Checks if the parameters are valid and sends aLobbyCreation
message to the server.A getter for the list of the assistant cards that the player is allowed to play.A getter for the lastBoardStatus
received from the server.A getter for thecharacterCard
attribute.A getter for thetowerColor
attribute.A getter for the username chosen by the player.A getter for thewizard
attribute.boolean
Checks if a reconnection is available.void
Checks if the lobby id is a valid integer and sends aJoinLobby
message to the server.void
If the player is in a lobby, sends aLeaveLobby
message to the server.static void
The method is called to launch the client-side application (in either CLI or GUI mode).void
moveMotherNature
(String island) Sends aMotherNatureDestination
message to the server.void
moveStudent
(String color, String destination) Sends aMoveStudent
message to the server.void
playAssistantCard
(String card) Sends aPlayAssistantCard
message to the server.void
playCharacterCard
(String[] sourceColors, String[] destinationColors, String targetColor, String targetIsland) Builds theJsonObject
containing the given arguments to play the selected character card, sends aPlayCharacterCard
message to the server and then clears thecharacterCard
attribute setting it tonull
.void
putReconnectSettings
(AcceptedJoinLobby message) Saves the reconnection settings.void
Removes the reconnection settings.void
run()
Starts the user interface thread and then loops to receive and handle messages from the server.void
sendHandshake
(String username) Checks if the username is valid and sends aHandshake
message to the server.void
Checks if a reconnection is available and sends aReconnect
message to the server.void
setAvailableCards
(List<String> availableCards) A setter for theavailableCards
attribute.void
setBoardStatus
(BoardStatus boardStatus) A setter for theboardStatus
attribute.void
setCharacterCard
(Integer card) Sets thecharacterCard
attribute to save the selected character card while choosing the requested arguments.void
setGameId
(int gameId) A setter for thegameId
attribute.void
setRunning
(boolean running) Synchronized method to set therunning
attribute.void
setTowerColor
(String towerColor) Sets the tower color selected by the player and tries to send the selection to the server.void
setUsername
(String username) A setter for theusername
attribute.void
Sets the wizard selected by the player and tries to send the selection to the server.void
Sends 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 aClient
object, starting theSocket
and either theCommandLineInterface
or 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 theSocket
could 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 aClient
object 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 therunning
attribute.- Parameters:
running
- the value to set
-
getUsername
A getter for the username chosen by the player.- Returns:
- the chosen username
-
setUsername
A setter for theusername
attribute.- Parameters:
username
- the username chosen by the player
-
setGameId
public void setGameId(int gameId) A setter for thegameId
attribute.- Parameters:
gameId
- the identifier of the game the player joined
-
getTowerColor
A getter for thetowerColor
attribute.- Returns:
- the
TowerColor
literal 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 selectedTowerColor
literal
-
getWizard
A getter for thewizard
attribute.- Returns:
- the
Wizard
literal selected by the player
-
setWizard
Sets the wizard selected by the player and tries to send the selection to the server.- Parameters:
wizard
- the selectedWizard
literal
-
getBoardStatus
A getter for the lastBoardStatus
received from the server. If theboardStatus
isnull
, an error is shown via UI.- Returns:
- the latest board status
-
setBoardStatus
A setter for theboardStatus
attribute.- 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 theavailableCards
attribute.- Parameters:
availableCards
- the list of the assistant cards that the player is allowed to play
-
getCharacterCard
A getter for thecharacterCard
attribute.- Returns:
- the character card selected by the player
-
askHelp
public void askHelp()Sends aHelpRequest
message to the server. -
sendHandshake
Checks if the username is valid and sends aHandshake
message to the server.- Parameters:
username
- the username selected by the player
-
sendReconnect
public void sendReconnect()Checks if a reconnection is available and sends aReconnect
message to the server. -
askLobbies
public void askLobbies()Sends aLobbiesRequest
message to the server. -
joinLobby
Checks if the lobby id is a valid integer and sends aJoinLobby
message to the server.- Parameters:
lobbyIdArg
- the identifier of the lobby to join
-
createLobby
Checks if the parameters are valid and sends aLobbyCreation
message 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:
true
if reconnection settings are found
-
leaveLobby
public void leaveLobby()If the player is in a lobby, sends aLeaveLobby
message to the server. -
playAssistantCard
Sends aPlayAssistantCard
message to the server.- Parameters:
card
- theAssistantCard
literal
-
moveStudent
Sends aMoveStudent
message to the server.- Parameters:
color
- theColor
literal of the student to movedestination
- where to place the student
-
moveMotherNature
Sends aMotherNatureDestination
message to the server.- Parameters:
island
- the identifier of the island where to put Mother Nature
-
chooseCloud
public void chooseCloud(int cloud) Sends aSelectCloud
message to the server.- Parameters:
cloud
- the identifier of the selected cloud
-
setCharacterCard
Sets thecharacterCard
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 theJsonObject
containing the given arguments to play the selected character card, sends aPlayCharacterCard
message to the server and then clears thecharacterCard
attribute 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
-