|
|
|
@ -68,6 +68,8 @@ public final class Game extends BaseState {
|
|
|
|
|
private Card juggleCard;
|
|
|
|
|
private boolean werewolf;
|
|
|
|
|
|
|
|
|
|
private int finishing = 0;
|
|
|
|
|
|
|
|
|
|
public void init(List<ObserverMessage> messages) {
|
|
|
|
|
pendingMessages = messages;
|
|
|
|
|
}
|
|
|
|
@ -104,11 +106,16 @@ public final class Game extends BaseState {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Optional<ClientState> onMessage(ObserverMessage message) {
|
|
|
|
|
if (finishing != 0) return onMessageWhileFinishing(message);
|
|
|
|
|
|
|
|
|
|
if (message instanceof StateMessage state) {
|
|
|
|
|
switch (state.getState()) {
|
|
|
|
|
case "starting_round" -> onStartRound();
|
|
|
|
|
case "starting_round" -> {
|
|
|
|
|
return onStartRound();
|
|
|
|
|
}
|
|
|
|
|
case "starting_trick" -> onStartTrick();
|
|
|
|
|
case "juggling" -> onJuggle();
|
|
|
|
|
case "finishing_round" -> onFinishingRound();
|
|
|
|
|
case "finished" -> {
|
|
|
|
|
onFinished();
|
|
|
|
|
return returnToSession();
|
|
|
|
@ -129,20 +136,44 @@ public final class Game extends BaseState {
|
|
|
|
|
} else if (message instanceof CardMessage card) {
|
|
|
|
|
onCardMessage(card.getPlayer(), card.getCard());
|
|
|
|
|
} else if (message instanceof ScoreMessage score) {
|
|
|
|
|
onScoreMessage(score.getPoints());
|
|
|
|
|
onScoreMessage(score.getPoints(), true);
|
|
|
|
|
} else if (message instanceof UserInputMessage input) {
|
|
|
|
|
onUserInputMessage(input.getPlayer(), input.getAction(), input.getTimeout());
|
|
|
|
|
} else if (message instanceof TimeoutMessage) {
|
|
|
|
|
onTimeoutMessage();
|
|
|
|
|
} else {
|
|
|
|
|
log.fatal("Unknown observer message type {}.", message.getClass());
|
|
|
|
|
// TODO user feedback
|
|
|
|
|
return Optional.of(new Menu());
|
|
|
|
|
return unexpectedMessage(new GameMessage(message));
|
|
|
|
|
}
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onStartRound() {
|
|
|
|
|
private Optional<ClientState> onMessageWhileFinishing(ObserverMessage message) {
|
|
|
|
|
if (finishing == 1) { // last "finishing_round" has been received
|
|
|
|
|
if (message instanceof ScoreMessage score) {
|
|
|
|
|
onScoreMessage(score.getPoints(), false);
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
} else if (message instanceof StateMessage state && "finishing".equals(state.getState())) {
|
|
|
|
|
finishing++;
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
}
|
|
|
|
|
} else if (finishing == 2) { // "finishing" has been received
|
|
|
|
|
if (message instanceof ScoreMessage) {
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
} else if (message instanceof StateMessage state && "finished".equals(state.getState())) {
|
|
|
|
|
onFinished();
|
|
|
|
|
finishing++;
|
|
|
|
|
return returnToSession();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unexpectedMessage(new GameMessage(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Optional<ClientState> onStartRound() {
|
|
|
|
|
if (isLastRound()) {
|
|
|
|
|
log.fatal("Cannot start round {} with {} players", round + 1, players.size());
|
|
|
|
|
return unexpectedMessage(new GameMessage(new StateMessage("starting_round")));
|
|
|
|
|
}
|
|
|
|
|
log.info("Round {} is starting...", round + 1);
|
|
|
|
|
|
|
|
|
|
round ++;
|
|
|
|
@ -153,6 +184,7 @@ public final class Game extends BaseState {
|
|
|
|
|
trick = -1;
|
|
|
|
|
|
|
|
|
|
if (gameScreen != null) gameScreen.startRound(round);
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onStartTrick() {
|
|
|
|
@ -168,8 +200,15 @@ public final class Game extends BaseState {
|
|
|
|
|
juggleCard = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onFinishingRound() {
|
|
|
|
|
if (isLastRound()) finishing = 1; // start finish procedure
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onFinished() {
|
|
|
|
|
log.info("The game has finished.");
|
|
|
|
|
if (gameScreen != null) {
|
|
|
|
|
gameScreen.showScoreOverlay(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onError() {
|
|
|
|
@ -256,12 +295,12 @@ public final class Game extends BaseState {
|
|
|
|
|
if (gameScreen != null) gameScreen.playCard(player, handCard, card);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onScoreMessage(@Unmodifiable Map<@NotNull UUID, @NotNull Integer> points) {
|
|
|
|
|
private void onScoreMessage(@Unmodifiable Map<@NotNull UUID, @NotNull Integer> points, boolean showOverlay) {
|
|
|
|
|
log.info("The scores are as follows: " + points);
|
|
|
|
|
scores.put(round, points);
|
|
|
|
|
if (gameScreen != null) {
|
|
|
|
|
gameScreen.addScores(round, points);
|
|
|
|
|
gameScreen.showScoreOverlay();
|
|
|
|
|
if (showOverlay) gameScreen.showScoreOverlay(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -392,7 +431,8 @@ public final class Game extends BaseState {
|
|
|
|
|
players.entrySet().stream()
|
|
|
|
|
.map(entry -> new PlayerData(entry.getKey(), entry.getValue(), false))
|
|
|
|
|
.toList(),
|
|
|
|
|
self
|
|
|
|
|
self,
|
|
|
|
|
true
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -509,6 +549,10 @@ public final class Game extends BaseState {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isLastRound() {
|
|
|
|
|
return round + 1 >= 60 / getPlayers().size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<editor-fold desc="Logging" defaultState="collapsed">
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|