added more logic for the server state diagram
This commit is contained in:
@@ -29,6 +29,8 @@ public class Game {
|
||||
private Boolean allRanked = false;
|
||||
private Boolean movablePieces = false;
|
||||
|
||||
private Boolean allReady = false;
|
||||
|
||||
private static final int AMOUNT_OF_TURBO_CARDS = 16;
|
||||
private static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12;
|
||||
|
||||
@@ -397,6 +399,25 @@ public void setStartPlayer(Player startPlayer) {
|
||||
this.startPlayer = startPlayer;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the all ready state.
|
||||
*
|
||||
* @return the all ready state
|
||||
*/
|
||||
public Boolean allReady() {
|
||||
return allReady;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method sets the all ready state.
|
||||
*
|
||||
* @param allReady the new all ready state
|
||||
*/
|
||||
public void setAllReady(Boolean allReady) {
|
||||
this.allReady = allReady;
|
||||
if (allReady) notifyObservers();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method notifies the observers.
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
package pp.mdga.message.client;
|
||||
|
||||
import pp.mdga.game.Color;
|
||||
|
||||
public class DeselectTSK extends ClientMessage {
|
||||
private final Color color;
|
||||
|
||||
public DeselectTSK(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "null";
|
||||
|
||||
@@ -19,6 +19,7 @@ public String getName() {
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ServerInterpreter interpreter) {
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class DetermineStartPlayer extends ServerState {
|
||||
private final List<Player> player = new ArrayList<>();
|
||||
@@ -17,9 +18,9 @@ public DetermineStartPlayer(ServerState parent, ServerGameLogic logic) {
|
||||
|
||||
@Override
|
||||
public void receivedRequestDice(RequestDice msg, int from) {
|
||||
logic.send();
|
||||
|
||||
broadcastUpdate(new Dice());
|
||||
final Random random = new Random();
|
||||
final int dice = random.nextInt(6) + 1;
|
||||
broadcastUpdate(new Dice(dice, new ArrayList<>()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
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;
|
||||
@@ -26,7 +29,7 @@ public Lobby(ServerState parent, ServerGameLogic logic) {
|
||||
*/
|
||||
@Override
|
||||
public void receivedDeselectTSK(DeselectTSK msg, int from) {
|
||||
broadcastUpdate(new UpdateTSK());
|
||||
broadcastUpdate(new UpdateTSK(logic.getPlayerById(from).getName(), msg.getColor()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +39,7 @@ public void receivedDeselectTSK(DeselectTSK msg, int from) {
|
||||
*/
|
||||
@Override
|
||||
public void receivedNotReady(LobbyNotReady msg, int from) {
|
||||
broadcastUpdate(new UpdateReady());
|
||||
broadcastUpdate(new UpdateReady(getPlayerColor(from), false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,7 +49,24 @@ public void receivedNotReady(LobbyNotReady msg, int from) {
|
||||
*/
|
||||
@Override
|
||||
public void receivedReady(LobbyReady msg, int from) {
|
||||
broadcastUpdate(new UpdateReady());
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +76,7 @@ public void receivedReady(LobbyReady msg, int from) {
|
||||
*/
|
||||
@Override
|
||||
public void receivedSelectTSK(SelectTSK msg, int from) {
|
||||
broadcastUpdate(new UpdateTSK());
|
||||
broadcastUpdate(new UpdateTSK(logic.getPlayerById(from).getName(), msg.getColor()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +86,7 @@ public void receivedSelectTSK(SelectTSK msg, int from) {
|
||||
*/
|
||||
@Override
|
||||
public void receivedStartGame(ClientStartGame msg, int from) {
|
||||
if (Boolean.TRUE.equals(logic.getGame().allRanked())) {
|
||||
if (Boolean.TRUE.equals(logic.getGame().allReady())) {
|
||||
broadcastUpdate(new ServerStartGame());
|
||||
parent.gotoState(new GameState(parent, logic));
|
||||
}
|
||||
|
||||
@@ -125,6 +125,20 @@ public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the player representing the client with the specified connection ID.
|
||||
*
|
||||
* @param id the ID of the client
|
||||
* @return the player associated with the client ID, or null if not found
|
||||
*/
|
||||
public Player getPlayerById(int id) {
|
||||
for (var entry : game.getPlayers().entrySet())
|
||||
if (entry.getValue().getId() == id)
|
||||
return entry.getValue();
|
||||
LOGGER.log(Logger.Level.ERROR, "no player found with connection {0}", id); //NON-NLS
|
||||
return null;
|
||||
}
|
||||
|
||||
public ServerState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -204,9 +204,9 @@ public void sentPossibleCard(PossibleCard msg, int from) {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a RankingResponce message is sent.
|
||||
* This method is called when a RankingResponse message is sent.
|
||||
*
|
||||
* @param msg the RankingResponce message
|
||||
* @param msg the RankingResponse message
|
||||
*/
|
||||
@Override
|
||||
public void sentRankingResponse(RankingResponce msg, int from) {
|
||||
|
||||
@@ -29,7 +29,7 @@ public void exit() {
|
||||
this.getParent().getParent().exit();
|
||||
} else {
|
||||
// todo: next player
|
||||
broadcastUpdate(new ActivePlayer());
|
||||
broadcastUpdate(new ActivePlayer(null));
|
||||
parent.gotoState(new Animation(parent, logic));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user