java.lang.Object
model.logic.server.MyServer
model.logic.server.HostServer
- All Implemented Interfaces:
Serializable
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassManage each guest turn, update models and if player is found initiate clientHandler. -
Field Summary
Fields inherited from class model.logic.server.MyServer
clientHandler, port, stop -
Constructor Summary
ConstructorsConstructorDescriptionHostServer(int port, ClientHandler clientHandler) The HostServer function is a constructor for the HostServer class. -
Method Summary
Modifier and TypeMethodDescriptionvoidbroadcastUpdate(String messageForPlayers) The broadcastUpdate function is used to send a message to all players in the game.voidclose()The close function closes the server socket and all the client sockets.voidThe connectClients function is responsible for connecting clients to the server.booleanThe isGameRunning function returns a boolean value that indicates whether the game is running or not.voidplayGame()The playGame function is the main function of the game.voidremovePlayer(int playerId) The removePlayer function removes a player from the game.voidThe resetTimerTask function is used to reset the timerTask variable.protected voidThe runServer function is the main function of the server.voidThe sendUpdatedModel function is used to send the updated model to all the clients.voidThe startGame function is called when the game is ready to start.voidstopGame()The stopGame function is called when the game ends.
-
Constructor Details
-
HostServer
The HostServer function is a constructor for the HostServer class. It takes in two parameters: an integer representing the port number, and a ClientHandler object. The function initializes two HashMaps, one to store clients and their corresponding client handlers, and another to store clients' model receivers.- Parameters:
port- port Specify the port number that the server will be listening onclientHandler- clientHandler Create a new instance of the clientHandler class
-
-
Method Details
-
runServer
The runServer function is the main function of the server. It creates a new MyServerSocket object, which is a subclass of ServerSocket that allows for multiple clients to connect at once. The runServer function then calls two other functions: connectClients and playGame. The first connects all clients to the server, while the second runs through each round of gameplay until one player wins or there's a tie. -
connectClients
public void connectClients()The connectClients function is responsible for connecting clients to the server. It does this by accepting connections from clients and adding them to a list of connected players. The function also adds the player's name to a list of players in GameManager, which will be used later on when creating the game board. -
sendUpdatedModel
public void sendUpdatedModel()The sendUpdatedModel function is used to send the updated model to all the clients. It does this by creating a new thread that iterates through each client and sends them an object output stream containing the GameManager's current gameModel. This function is called whenever there are changes made to the gameModel, such as when a player moves or when a card is played. -
resetTimerTask
public void resetTimerTask()The resetTimerTask function is used to reset the timerTask variable. This function is called when a new game begins, and it resets the timerTask variable so that it can be used again. -
playGame
public void playGame()The playGame function is the main function of the game. It starts by checking if a game is running, and if not, it starts one. Then it enters a while loop that runs as long as there's an active game running. The while loop contains two try-catch blocks: One for accepting new players to join the server (if they're allowed), and one for receiving messages from clients (such as when they disconnect). If a player disconnects, their ID will be sent to this class through MySocket's getPlayerSocket().getInputStream(), which will then be read by Scanner scanner = new Scan -
broadcastUpdate
The broadcastUpdate function is used to send a message to all players in the game.- Parameters:
messageForPlayers- messageForPlayers Send a message to all the clients
-
removePlayer
public void removePlayer(int playerId) The removePlayer function removes a player from the game.- Parameters:
playerId- playerId Find the player in the clients map
-
close
public void close()The close function closes the server socket and all the client sockets. It also clears both clients hashmaps. -
isGameRunning
public boolean isGameRunning()The isGameRunning function returns a boolean value that indicates whether the game is running or not.- Returns:
- A boolean value
-
startGame
public void startGame()The startGame function is called when the game is ready to start. It sets the gameIsRunning variable to true, and then sends an updated model to all the players in this Game's playerList. -
stopGame
public void stopGame()The stopGame function is called when the game ends. It sets the gameIsRunning boolean to false, cancels any turnTimer that may be running, resets the timerTask and closes all of its sockets.
-