|
|
|
@ -76,7 +76,7 @@ public final class Game extends BaseState {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<ClientState> onEnter(Client client) {
|
|
|
|
|
var out = handlePendingMessages();
|
|
|
|
|
var out = handlePendingMessages(client);
|
|
|
|
|
if (out.isPresent()) return out;
|
|
|
|
|
|
|
|
|
|
gameScreen = new GameScreen(client.getGame(), self, players);
|
|
|
|
@ -85,44 +85,47 @@ public final class Game extends BaseState {
|
|
|
|
|
return super.onEnter(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<ClientState> onErrorDismissed(Client client) {
|
|
|
|
|
return Optional.of(new Menu());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<editor-fold desc="onMessage">
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
|
|
|
|
|
try {
|
|
|
|
|
if (message instanceof GameMessage game) {
|
|
|
|
|
var observerMessage = game.getObserverMessage();
|
|
|
|
|
return onMessage(observerMessage);
|
|
|
|
|
return onMessage(client, observerMessage);
|
|
|
|
|
} else if (message instanceof NackMessage nack) {
|
|
|
|
|
return onNackMessage(nack);
|
|
|
|
|
return onNackMessage(client, nack);
|
|
|
|
|
} else if (message instanceof AckMessage) {
|
|
|
|
|
onAckMessage();
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
} else {
|
|
|
|
|
return unexpectedMessage(message);
|
|
|
|
|
return unexpectedMessage(client, message);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
executeDelayedFinishInteraction();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Optional<ClientState> onMessage(ObserverMessage message) {
|
|
|
|
|
if (finishing != 0) return onMessageWhileFinishing(message);
|
|
|
|
|
private Optional<ClientState> onMessage(Client client, ObserverMessage message) {
|
|
|
|
|
if (finishing != 0) return onMessageWhileFinishing(client, message);
|
|
|
|
|
|
|
|
|
|
if (message instanceof StateMessage state) {
|
|
|
|
|
switch (state.getState()) {
|
|
|
|
|
case "starting_round" -> {
|
|
|
|
|
return onStartRound();
|
|
|
|
|
return onStartRound(client);
|
|
|
|
|
}
|
|
|
|
|
case "starting_trick" -> onStartTrick();
|
|
|
|
|
case "juggling" -> onJuggle();
|
|
|
|
|
case "finishing_round" -> onFinishingRound();
|
|
|
|
|
case "finished" -> {
|
|
|
|
|
onFinished();
|
|
|
|
|
return returnToSession();
|
|
|
|
|
return onFinished();
|
|
|
|
|
}
|
|
|
|
|
case "error" -> {
|
|
|
|
|
onError();
|
|
|
|
|
return returnToSession();
|
|
|
|
|
return onError(client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (message instanceof HandMessage hand) {
|
|
|
|
@ -142,12 +145,12 @@ public final class Game extends BaseState {
|
|
|
|
|
} else if (message instanceof TimeoutMessage) {
|
|
|
|
|
onTimeoutMessage();
|
|
|
|
|
} else {
|
|
|
|
|
return unexpectedMessage(new GameMessage(message));
|
|
|
|
|
return unexpectedMessage(client, new GameMessage(message));
|
|
|
|
|
}
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Optional<ClientState> onMessageWhileFinishing(ObserverMessage message) {
|
|
|
|
|
private Optional<ClientState> onMessageWhileFinishing(Client client, ObserverMessage message) {
|
|
|
|
|
if (finishing == 1) { // last "finishing_round" has been received
|
|
|
|
|
if (message instanceof ScoreMessage score) {
|
|
|
|
|
onScoreMessage(score.getPoints(), false);
|
|
|
|
@ -166,13 +169,13 @@ public final class Game extends BaseState {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unexpectedMessage(new GameMessage(message));
|
|
|
|
|
return unexpectedMessage(client, new GameMessage(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Optional<ClientState> onStartRound() {
|
|
|
|
|
private Optional<ClientState> onStartRound(Client client) {
|
|
|
|
|
if (isLastRound()) {
|
|
|
|
|
log.fatal("Cannot start round {} with {} players", round + 1, players.size());
|
|
|
|
|
return unexpectedMessage(new GameMessage(new StateMessage("starting_round")));
|
|
|
|
|
return unexpectedMessage(client, new GameMessage(new StateMessage("starting_round")));
|
|
|
|
|
}
|
|
|
|
|
log.info("Round {} is starting...", round + 1);
|
|
|
|
|
|
|
|
|
@ -204,15 +207,18 @@ public final class Game extends BaseState {
|
|
|
|
|
if (isLastRound()) finishing = 1; // start finish procedure
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onFinished() {
|
|
|
|
|
private Optional<ClientState> onFinished() {
|
|
|
|
|
log.info("The game has finished.");
|
|
|
|
|
if (gameScreen != null) {
|
|
|
|
|
gameScreen.showScoreOverlay(true);
|
|
|
|
|
}
|
|
|
|
|
return returnToSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onError() {
|
|
|
|
|
private Optional<ClientState> onError(Client client) {
|
|
|
|
|
log.error("The game has finished with an error.");
|
|
|
|
|
showErrorScreen(client, "The game has finished with an error.");
|
|
|
|
|
return returnToSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onHandMessage(@NotNull UUID player, @Unmodifiable @NotNull List<@NotNull Card> hand) {
|
|
|
|
@ -332,7 +338,7 @@ public final class Game extends BaseState {
|
|
|
|
|
if (gameScreen != null) gameScreen.timeout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Optional<ClientState> onNackMessage(@NotNull NackMessage nack) {
|
|
|
|
|
private Optional<ClientState> onNackMessage(Client client, @NotNull NackMessage nack) {
|
|
|
|
|
sending.set(false);
|
|
|
|
|
|
|
|
|
|
if (isActive() && currentInteraction.action() == JUGGLE_CARD && juggleCard != null) {
|
|
|
|
@ -348,7 +354,7 @@ public final class Game extends BaseState {
|
|
|
|
|
}
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
} else {
|
|
|
|
|
return unexpectedMessage(nack);
|
|
|
|
|
return unexpectedMessage(client, nack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -514,10 +520,10 @@ public final class Game extends BaseState {
|
|
|
|
|
}
|
|
|
|
|
//</editor-fold>
|
|
|
|
|
|
|
|
|
|
private Optional<ClientState> handlePendingMessages() {
|
|
|
|
|
private Optional<ClientState> handlePendingMessages(Client client) {
|
|
|
|
|
if (pendingMessages != null) {
|
|
|
|
|
for (var message : pendingMessages) {
|
|
|
|
|
var result = onMessage(message);
|
|
|
|
|
var result = onMessage(client, message);
|
|
|
|
|
if (result.isPresent()) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|