95 lines
2.6 KiB
Java
95 lines
2.6 KiB
Java
package pp.mdga.server;
|
|
|
|
import pp.mdga.game.Color;
|
|
import pp.mdga.game.Player;
|
|
import pp.mdga.message.client.*;
|
|
import pp.mdga.message.server.ServerMessage;
|
|
import pp.mdga.message.server.ServerStartGame;
|
|
import pp.mdga.message.server.UpdateReady;
|
|
import pp.mdga.message.server.UpdateTSK;
|
|
|
|
/**
|
|
* Represents the lobby state of the server.
|
|
*/
|
|
public class Lobby extends ServerState {
|
|
/**
|
|
* Constructs a new Lobby state.
|
|
*
|
|
* @param parent the parent state
|
|
* @param logic the server game logic
|
|
*/
|
|
public Lobby(ServerState parent, ServerGameLogic logic) {
|
|
super(parent, logic);
|
|
}
|
|
|
|
/**
|
|
* Handles the DeselectTSK message.
|
|
*
|
|
* @param msg the DeselectTSK message
|
|
*/
|
|
@Override
|
|
public void receivedDeselectTSK(DeselectTSK msg, int from) {
|
|
broadcastUpdate(new UpdateTSK(logic.getPlayerById(from).getName(), msg.getColor()));
|
|
}
|
|
|
|
/**
|
|
* Handles the LobbyNotReady message.
|
|
*
|
|
* @param msg the LobbyNotReady message
|
|
*/
|
|
@Override
|
|
public void receivedNotReady(LobbyNotReady msg, int from) {
|
|
broadcastUpdate(new UpdateReady(getPlayerColor(from), false));
|
|
}
|
|
|
|
/**
|
|
* Handles the LobbyReady message.
|
|
*
|
|
* @param msg the LobbyReady message
|
|
*/
|
|
@Override
|
|
public void receivedReady(LobbyReady msg, int from) {
|
|
broadcastUpdate(new UpdateReady(getPlayerColor(from), true));
|
|
}
|
|
|
|
/**
|
|
* Helper method to get the color associated with a player ID.
|
|
*
|
|
* @param playerId The ID of the player.
|
|
* @return The Color associated with the player, or null if not found.
|
|
*/
|
|
private Color getPlayerColor(int playerId) {
|
|
Player player = logic.getPlayerById(playerId);
|
|
|
|
for (var entry : logic.getGame().getPlayers().entrySet()) {
|
|
if (entry.getValue().equals(player)) {
|
|
return entry.getKey();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Handles the SelectTSK message.
|
|
*
|
|
* @param msg the SelectTSK message
|
|
*/
|
|
@Override
|
|
public void receivedSelectTSK(SelectTSK msg, int from) {
|
|
broadcastUpdate(new UpdateTSK(logic.getPlayerById(from).getName(), msg.getColor()));
|
|
}
|
|
|
|
/**
|
|
* Handles the ClientStartGame message.
|
|
*
|
|
* @param msg the ClientStartGame message
|
|
*/
|
|
@Override
|
|
public void receivedStartGame(ClientStartGame msg, int from) {
|
|
if (Boolean.TRUE.equals(logic.getGame().allReady())) {
|
|
broadcastUpdate(new ServerStartGame());
|
|
parent.gotoState(new GameState(parent, logic));
|
|
}
|
|
}
|
|
}
|