diff --git a/wizard-client/build.gradle.kts b/wizard-client/build.gradle.kts index 78fb591..e69de29 100644 --- a/wizard-client/build.gradle.kts +++ b/wizard-client/build.gradle.kts @@ -1,6 +0,0 @@ -subprojects { - dependencies { - implementation(project(":wizard-common")) - implementation(JavaWebSocket.id) - } -} \ No newline at end of file diff --git a/wizard-client/wizard-client-cli/build.gradle.kts b/wizard-client/wizard-client-cli/build.gradle.kts index 6e7fc21..98bc700 100644 --- a/wizard-client/wizard-client-cli/build.gradle.kts +++ b/wizard-client/wizard-client-cli/build.gradle.kts @@ -3,6 +3,8 @@ plugins { } dependencies { + implementation(project(":wizard-common")) + implementation(JLine.id) implementation(Jansi.id) implementation(JavaWebSocket.id) diff --git a/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/commands/MenuCommand.java b/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/commands/MenuCommand.java index 958aac7..22b95a7 100644 --- a/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/commands/MenuCommand.java +++ b/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/commands/MenuCommand.java @@ -3,12 +3,8 @@ package eu.jonahbauer.wizard.client.cli.commands; import eu.jonahbauer.wizard.client.cli.Client; import eu.jonahbauer.wizard.client.cli.ClientSocket; import eu.jonahbauer.wizard.client.cli.state.AwaitingConnection; -import eu.jonahbauer.wizard.client.cli.state.Lobby; -import eu.jonahbauer.wizard.common.messages.server.SessionListMessage; import java.net.URI; -import java.util.List; -import java.util.concurrent.CompletableFuture; import static picocli.CommandLine.*; diff --git a/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/AwaitingJoinLobby.java b/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/AwaitingJoinLobby.java index 60e0644..005719d 100644 --- a/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/AwaitingJoinLobby.java +++ b/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/AwaitingJoinLobby.java @@ -17,7 +17,7 @@ public final class AwaitingJoinLobby extends Awaiting { @Override public Optional onMessage(Client client, ServerMessage message) { if (message instanceof SessionListMessage list) { - return Optional.of(new Lobby(list)); + return Optional.of(new Lobby(list.getSessions())); } else { return super.onMessage(client, message); } diff --git a/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/Lobby.java b/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/Lobby.java index 619ba71..44a0919 100644 --- a/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/Lobby.java +++ b/wizard-client/wizard-client-cli/src/main/java/eu/jonahbauer/wizard/client/cli/state/Lobby.java @@ -12,8 +12,8 @@ public final class Lobby extends BaseState { @Getter private final Map sessions = new HashMap<>(); - public Lobby(SessionListMessage list) { - list.getSessions().forEach(session -> sessions.put(session.getUuid(), session)); + public Lobby(Collection sessions) { + sessions.forEach(session -> this.sessions.put(session.getUuid(), session)); } @Override diff --git a/wizard-client/wizard-client-libgdx/build.gradle.kts b/wizard-client/wizard-client-libgdx/build.gradle.kts index 81e9c2b..2a26863 100644 --- a/wizard-client/wizard-client-libgdx/build.gradle.kts +++ b/wizard-client/wizard-client-libgdx/build.gradle.kts @@ -14,5 +14,7 @@ project(":wizard-client:wizard-client-libgdx:core") { dependencies { api(LibGDX.api) + implementation(project(":wizard-common")) + implementation(JavaWebSocket.id) } } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java index fd9f364..37f1e24 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java @@ -28,7 +28,11 @@ public class Client extends TimeoutContext { @Override protected void handleError(Throwable t) { // TODO better error handling - t.printStackTrace(); + log.error("", t); + + var menu = new Menu(); + forceTransition(menu); + menu.onEnter(this); } public void setSocket(ClientSocket socket) { @@ -39,15 +43,27 @@ public class Client extends TimeoutContext { } public void onOpen() { - execute(s -> s.onOpen(this)); + try { + execute(s -> s.onOpen(this)); + } catch (Throwable t) { + handleError(t); + } } public void onClose(int code, String reason, boolean remote) { - execute(s -> s.onClose(this, code, reason, remote)); + try { + execute(s -> s.onClose(this, code, reason, remote)); + } catch (Throwable t) { + handleError(t); + } } public void onMessage(ServerMessage message) { - execute(s -> s.onMessage(this, message)); + try { + execute(s -> s.onMessage(this, message)); + } catch (Throwable t) { + handleError(t); + } } @Override diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java index ece6726..6bf09f3 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java @@ -39,7 +39,7 @@ public class ClientSocket extends WebSocketClient { @Override public void onError(Exception e) { - e.printStackTrace(); + log.error("", e); close(CloseFrame.ABNORMAL_CLOSE, e.getMessage()); } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/ChangePredictionOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/ChangePredictionOverlay.java similarity index 94% rename from wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/ChangePredictionOverlay.java rename to wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/ChangePredictionOverlay.java index a4a8160..f7a6e21 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/ChangePredictionOverlay.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/ChangePredictionOverlay.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup; import eu.jonahbauer.wizard.client.libgdx.actors.game.CardActor; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/InteractionOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/InteractionOverlay.java new file mode 100644 index 0000000..e51cd73 --- /dev/null +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/InteractionOverlay.java @@ -0,0 +1,5 @@ +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; + +public interface InteractionOverlay { + void close(); +} diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/MakePredictionOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/MakePredictionOverlay.java similarity index 96% rename from wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/MakePredictionOverlay.java rename to wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/MakePredictionOverlay.java index 7c4acb3..cfe71a8 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/MakePredictionOverlay.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/MakePredictionOverlay.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/Overlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/Overlay.java similarity index 97% rename from wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/Overlay.java rename to wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/Overlay.java index 2ece58b..66b0f73 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/Overlay.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/Overlay.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.TextureAtlas; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/PickTrumpOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/PickTrumpOverlay.java similarity index 97% rename from wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/PickTrumpOverlay.java rename to wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/PickTrumpOverlay.java index c3d47f6..3495d36 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/PickTrumpOverlay.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/PickTrumpOverlay.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/PlayColoredCardOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/PlayColoredCardOverlay.java similarity index 98% rename from wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/PlayColoredCardOverlay.java rename to wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/PlayColoredCardOverlay.java index e584258..47b5a8e 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/PlayColoredCardOverlay.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/PlayColoredCardOverlay.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/StartRoundOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/StartRoundOverlay.java similarity index 95% rename from wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/StartRoundOverlay.java rename to wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/StartRoundOverlay.java index 6b99a5c..bdb2cdc 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/StartRoundOverlay.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/StartRoundOverlay.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Group; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/TrumpOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/TrumpOverlay.java similarity index 97% rename from wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/TrumpOverlay.java rename to wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/TrumpOverlay.java index 045e546..e079209 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/TrumpOverlay.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actions/overlay/TrumpOverlay.java @@ -1,4 +1,4 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; +package eu.jonahbauer.wizard.client.libgdx.actions.overlay; import com.badlogic.gdx.math.Interpolation; import com.badlogic.gdx.scenes.scene2d.Actor; @@ -85,7 +85,7 @@ public class TrumpOverlay extends Overlay { ); private final @Nullable String player; - private final @NotNull Card card; + private final @Nullable Card card; private final @Nullable Card.Suit suit; private final @NotNull CardActor trumpCardActor; @@ -93,7 +93,7 @@ public class TrumpOverlay extends Overlay { private boolean animateCard = true; - public TrumpOverlay(@NotNull GameScreen gameScreen, @Nullable String player, @NotNull Card card, @Nullable Card.Suit suit) { + public TrumpOverlay(@NotNull GameScreen gameScreen, @Nullable String player, @Nullable Card card, @Nullable Card.Suit suit) { super(gameScreen, Long.MAX_VALUE); this.player = player; this.card = card; @@ -140,7 +140,7 @@ public class TrumpOverlay extends Overlay { } else { trumpCardActor.remove(); cardGroup.addActor(trumpCardActor); - trumpCardActor.setCard(card); + trumpCardActor.setCard(card != null ? card : Card.HIDDEN); } trumpSuitActor.remove(); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/InteractionOverlay.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/InteractionOverlay.java deleted file mode 100644 index 870d80f..0000000 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/actors/game/overlay/InteractionOverlay.java +++ /dev/null @@ -1,5 +0,0 @@ -package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay; - -public interface InteractionOverlay { - void close(); -} diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/GameScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/GameScreen.java index 8fbe247..05575c0 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/GameScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/GameScreen.java @@ -16,11 +16,11 @@ import com.badlogic.gdx.utils.I18NBundle; import eu.jonahbauer.wizard.client.libgdx.AnimationTimings; import eu.jonahbauer.wizard.client.libgdx.GameAtlas; import eu.jonahbauer.wizard.client.libgdx.WizardGame; +import eu.jonahbauer.wizard.client.libgdx.actions.overlay.*; import eu.jonahbauer.wizard.client.libgdx.actors.game.CardActor; import eu.jonahbauer.wizard.client.libgdx.actors.game.CardStack; import eu.jonahbauer.wizard.client.libgdx.actors.game.CardsGroup; import eu.jonahbauer.wizard.client.libgdx.actors.game.PadOfTruth; -import eu.jonahbauer.wizard.client.libgdx.actors.game.overlay.*; import eu.jonahbauer.wizard.client.libgdx.state.Game; import eu.jonahbauer.wizard.common.messages.observer.UserInputMessage; import eu.jonahbauer.wizard.common.model.Card; @@ -451,7 +451,7 @@ public class GameScreen extends MenuScreen { } // - public void showTrumpOverlay(@Nullable UUID player, @NotNull Card trumpCard, @Nullable Card.Suit trumpSuit) { + public void showTrumpOverlay(@Nullable UUID player, @Nullable Card trumpCard, @Nullable Card.Suit trumpSuit) { if (trumpCardActor == null) { trumpCardActor = new CardActor(Card.HIDDEN, atlas); } @@ -489,7 +489,7 @@ public class GameScreen extends MenuScreen { delay(AnimationTimings.OVERLAY_HOLD), parallel( targeting(padOfTruth, scaleTo(PadOfTruth.COLLAPSED_SCALE, PadOfTruth.COLLAPSED_SCALE, AnimationTimings.OVERLAY_SHARED_ELEMENT)), - targeting(padOfTruth, moveTo(WizardGame.WIDTH - 10 - PadOfTruth.EXTENDED_WIDTH, 10)) + targeting(padOfTruth, moveTo(WizardGame.WIDTH - 10 - PadOfTruth.EXTENDED_WIDTH, 10, AnimationTimings.OVERLAY_SHARED_ELEMENT)) ), run(() -> padOfTruth.setEnabled(true)) )); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/MenuScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/MenuScreen.java index 64ea37a..509e887 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/MenuScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/MenuScreen.java @@ -8,7 +8,7 @@ import eu.jonahbauer.wizard.client.libgdx.WizardGame; public abstract class MenuScreen implements Screen { protected final float BUTTON_BAR_Y = WizardGame.HEIGHT * 0.15f; - protected WizardGame game; + protected final WizardGame game; public MenuScreen(WizardGame game) { this.game = game; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/WaitingScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/WaitingScreen.java index 793139c..b29104c 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/WaitingScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/WaitingScreen.java @@ -29,9 +29,7 @@ public class WaitingScreen extends MenuScreen { private Label labelPlayerName; private List players; - - private AutoFocusScrollPane listContainer; - + private final ChangeListener listener = new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { @@ -81,7 +79,7 @@ public class WaitingScreen extends MenuScreen { } }; - listContainer = new AutoFocusScrollPane(players, game.data.skin); + var listContainer = new AutoFocusScrollPane(players, game.data.skin); listContainer.layout(); var content = new HorizontalGroup().grow().space(20); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java index 1ba1553..47de96c 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java @@ -11,7 +11,7 @@ import java.util.Optional; public abstract class Awaiting extends BaseState implements ClientState { @Override public Optional onMessage(Client client, ServerMessage message) { - return unexpectedMessage(client, message); + return unexpectedMessage(message); } @Override diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java index d568bc7..a36e3a2 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java @@ -29,7 +29,7 @@ public abstract class BaseState implements ClientState { return Optional.of(new Menu()); } - protected static Optional unexpectedMessage(Client client, ServerMessage message) { + protected static Optional unexpectedMessage(ServerMessage message) { // return to menu on unexpected message log.fatal("Unexpected message {}. Returning to menu.", message); return Optional.of(new Menu()); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Game.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Game.java index b311cee..5c100f5 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Game.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Game.java @@ -1,7 +1,7 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; -import eu.jonahbauer.wizard.client.libgdx.actors.game.overlay.InteractionOverlay; +import eu.jonahbauer.wizard.client.libgdx.actions.overlay.InteractionOverlay; import eu.jonahbauer.wizard.client.libgdx.screens.GameScreen; import eu.jonahbauer.wizard.client.libgdx.util.Pair; import eu.jonahbauer.wizard.common.messages.client.InteractionMessage; @@ -14,10 +14,15 @@ import eu.jonahbauer.wizard.common.messages.server.GameMessage; import eu.jonahbauer.wizard.common.messages.server.NackMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import eu.jonahbauer.wizard.common.model.Card; +import eu.jonahbauer.wizard.common.model.Configuration; import lombok.Data; import lombok.Getter; import lombok.experimental.Accessors; import lombok.extern.log4j.Log4j2; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Range; +import org.jetbrains.annotations.Unmodifiable; import java.lang.ref.WeakReference; import java.time.Instant; @@ -34,6 +39,7 @@ public final class Game extends BaseState { private final UUID self; private final UUID session; private final String sessionName; + private final Configuration configuration; private final String secret; private final LinkedHashMap players; @@ -59,10 +65,11 @@ public final class Game extends BaseState { private Card juggleCard; private boolean werewolf; - public Game(UUID self, UUID session, String sessionName, String secret, LinkedHashMap players) { + public Game(UUID self, UUID session, String sessionName, Configuration configuration, String secret, LinkedHashMap players) { this.self = self; this.session = session; this.sessionName = sessionName; + this.configuration = configuration; this.secret = secret; this.players = players; } @@ -117,12 +124,12 @@ public final class Game extends BaseState { } return Optional.empty(); } else if (message instanceof NackMessage nack) { - return onNackMessage(client, message, nack); + return onNackMessage(nack); } else if (message instanceof AckMessage) { onAckMessage(); return Optional.empty(); } else { - return unexpectedMessage(client, message); + return unexpectedMessage(message); } } finally { if (pendingClearActivePlayer > 0 && --pendingClearActivePlayer == 0) { @@ -165,7 +172,7 @@ public final class Game extends BaseState { log.error("The game has finished with an error."); } - private void onHandMessage(UUID player, List hand) { + private void onHandMessage(@NotNull UUID player, @Unmodifiable @NotNull List<@NotNull Card> hand) { checkPlayer(player); log.info("{} hand cards are: {}", nameOf(player, true, true), hand); @@ -178,7 +185,7 @@ public final class Game extends BaseState { juggling = false; } - private void onPredictionMessage(UUID player, int prediction) { + private void onPredictionMessage(@NotNull UUID player, @Range(from = 0, to = Integer.MAX_VALUE) int prediction) { checkPlayer(player); checkActivePlayer(player, MAKE_PREDICTION, CHANGE_PREDICTION); log.info("{} predicted: {}", nameOf(player, true, false), prediction); @@ -190,13 +197,13 @@ public final class Game extends BaseState { gameScreen.addPrediction(round, player, prediction, changed); } - private void onTrumpMessage(Card trumpCard, Card.Suit trumpSuit) { + private void onTrumpMessage(@Nullable Card trumpCard, @Nullable Card.Suit trumpSuit) { if (trumpCard == null) { log.info("There is no trump in this round."); } else { log.info("The trump suit is {} ({}).", trumpSuit != null ? trumpSuit : "yet to be determined.", trumpCard); } - + var player = currentInteraction != null && currentInteraction.action() == PICK_TRUMP ? currentInteraction.player() : null; finishInteraction(); this.trumpCard = trumpCard; @@ -205,11 +212,11 @@ public final class Game extends BaseState { werewolf = true; } else { werewolf = false; - gameScreen.showTrumpOverlay(null, trumpCard, trumpSuit); + gameScreen.showTrumpOverlay(player, trumpCard, trumpSuit); } } - private void onTrickMessage(UUID player, List cards) { + private void onTrickMessage(@NotNull UUID player, @Unmodifiable @NotNull List<@NotNull Card> cards) { checkPlayer(player); log.info("This trick {} goes to {}.", cards, nameOf(player)); @@ -219,7 +226,7 @@ public final class Game extends BaseState { gameScreen.finishTrick(player); } - private void onCardMessage(UUID player, Card card) { + private void onCardMessage(@NotNull UUID player, @NotNull Card card) { checkPlayer(player); checkActivePlayer(player, PLAY_CARD); log.info("{} played {}.", nameOf(player, true, false), card); @@ -242,14 +249,14 @@ public final class Game extends BaseState { gameScreen.playCard(player, handCard); } - private void onScoreMessage(Map points) { + private void onScoreMessage(@Unmodifiable Map<@NotNull UUID, @NotNull Integer> points) { log.info("The scores are as follows: " + points); points.forEach((player, p) -> scores.merge(player, p, Integer::sum)); gameScreen.addScores(round, points); gameScreen.showScoreOverlay(); } - private void onUserInputMessage(UUID player, UserInputMessage.Action action, long timeout) { + private void onUserInputMessage(@Nullable UUID player, @NotNull UserInputMessage.Action action, long timeout) { checkPlayer(player); log.info( "Waiting for input {} from {}. (times out at {})", @@ -264,7 +271,7 @@ public final class Game extends BaseState { currentInteraction = new Interaction(player, action, timeout); gameScreen.setActivePlayer(player, action, timeout); - if (werewolf && action == PICK_TRUMP) { + if (player != null && werewolf && action == PICK_TRUMP) { gameScreen.swapTrumpCard(player); } @@ -289,7 +296,7 @@ public final class Game extends BaseState { gameScreen.timeout(); } - private Optional onNackMessage(Client client, ServerMessage message, NackMessage nack) { + private Optional onNackMessage(@NotNull NackMessage nack) { sending.set(false); if (isActive() && currentInteraction.action() == JUGGLE_CARD && juggleCard != null) { @@ -303,7 +310,7 @@ public final class Game extends BaseState { gameScreen.ready(false); return Optional.empty(); } else { - return unexpectedMessage(client, message); + return unexpectedMessage(nack); } } @@ -321,7 +328,7 @@ public final class Game extends BaseState { // // - public Optional onCardClicked(Client client, Card card) { + public Optional onCardClicked(Client client, @NotNull Card card) { if (isActive()) { if (currentInteraction.action() == PLAY_CARD) { if (card == Card.CLOUD || card == Card.JUGGLER) { @@ -342,7 +349,7 @@ public final class Game extends BaseState { return Optional.empty(); } - public Optional onSuitClicked(Client client, Card.Suit suit) { + public Optional onSuitClicked(Client client, @NotNull Card.Suit suit) { if (isActive() && currentInteraction.action() == PICK_TRUMP) { send(client, new PickTrumpMessage(suit)); } else { @@ -371,7 +378,7 @@ public final class Game extends BaseState { private Optional returnToSession() { return Optional.of(new Session( - new SessionData(session, sessionName, -1, null), + new SessionData(session, sessionName, -1, configuration), players.entrySet().stream() .map(entry -> new PlayerData(entry.getKey(), entry.getValue(), false)) .toList(), diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Lobby.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Lobby.java index 001200b..95b801e 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Lobby.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Lobby.java @@ -63,7 +63,7 @@ public final class Lobby extends BaseState { } return Optional.empty(); } else { - return unexpectedMessage(client, message); + return unexpectedMessage(message); } } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java index 509ff95..6d8aec6 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java @@ -24,7 +24,7 @@ public final class Session extends BaseState { private final UUID session; private final String sessionName; private final Configuration configuration; - private final List players = new ArrayList<>(); + private final Map players = new LinkedHashMap<>(); private boolean sending; @@ -32,7 +32,7 @@ public final class Session extends BaseState { this.session = session.getUuid(); this.sessionName = session.getName(); this.configuration = session.getConfiguration(); - this.players.addAll(players); + players.forEach(player -> this.players.put(player.getUuid(), player)); this.self = self; this.secret = secret; @@ -42,7 +42,7 @@ public final class Session extends BaseState { public Optional onEnter(Client client) { sessionScreen = new WaitingScreen(client.getGame()); client.getGame().setScreen(sessionScreen); - sessionScreen.setPlayers(players.toArray(new PlayerData[0])); + sessionScreen.setPlayers(players.values().toArray(new PlayerData[0])); sessionScreen.setReady(false); sessionScreen.setPlayerName(getName()); sessionScreen.setSession(session, sessionName, configuration); @@ -53,19 +53,20 @@ public final class Session extends BaseState { @Override public Optional onMessage(Client client, ServerMessage message) { if (message instanceof PlayerJoinedMessage join) { - sessionScreen.addPlayer(join.getPlayer()); - players.add(join.getPlayer()); + var player = join.getPlayer(); + sessionScreen.addPlayer(player); + players.put(player.getUuid(), player); return Optional.empty(); } else if (message instanceof PlayerLeftMessage leave) { - sessionScreen.removePlayer(leave.getPlayer()); - players.remove(new PlayerData(leave.getPlayer(), null, false)); + var player = leave.getPlayer(); + sessionScreen.removePlayer(player); + players.remove(player); return Optional.empty(); } else if (message instanceof PlayerModifiedMessage modified) { var player = modified.getPlayer(); sessionScreen.modifyPlayer(player); - players.remove(player); - players.add(player); + players.put(player.getUuid(), player); if (self.equals(player.getUuid())) { sessionScreen.setReady(player.isReady()); @@ -74,8 +75,8 @@ public final class Session extends BaseState { return Optional.empty(); } else if (message instanceof StartingGameMessage) { var players = new LinkedHashMap(); - this.players.forEach(player -> players.put(player.getUuid(), player.getName())); - return Optional.of(new Game(self, session, sessionName, secret, players)); + this.players.forEach((uuid, player) -> players.put(uuid, player.getName())); + return Optional.of(new Game(self, session, sessionName, configuration, secret, players)); } else if (sending && message instanceof NackMessage nack) { // TODO display error log.error(nack.getMessage()); @@ -87,7 +88,7 @@ public final class Session extends BaseState { sessionScreen.setSending(false); return Optional.empty(); } else { - return unexpectedMessage(client, message); + return unexpectedMessage(message); } } @@ -112,7 +113,7 @@ public final class Session extends BaseState { } private boolean isReady() { - for (PlayerData player : players) { + for (PlayerData player : players.values()) { if (self.equals(player.getUuid())) { return player.isReady(); } @@ -121,7 +122,7 @@ public final class Session extends BaseState { } private String getName() { - for (PlayerData player : players) { + for (PlayerData player : players.values()) { if (self.equals(player.getUuid())) { return player.getName(); } diff --git a/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/ScoreMessage.java b/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/ScoreMessage.java index 0b75979..d701e75 100644 --- a/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/ScoreMessage.java +++ b/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/ScoreMessage.java @@ -3,7 +3,6 @@ package eu.jonahbauer.wizard.common.messages.observer; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NonNull; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Unmodifiable; import java.util.Map; diff --git a/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/StateMessage.java b/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/StateMessage.java index 4f44c21..3c67542 100644 --- a/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/StateMessage.java +++ b/wizard-common/src/main/java/eu/jonahbauer/wizard/common/messages/observer/StateMessage.java @@ -4,7 +4,6 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import org.jetbrains.annotations.NotNull; /** * A {@link StateMessage} is sent whenever the game changes its internal state. diff --git a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/GameData.java b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/GameData.java index 4e221c9..28adf96 100644 --- a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/GameData.java +++ b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/GameData.java @@ -152,34 +152,6 @@ public final class GameData { return new GameData(newValues, newPresent); } - /** - * @see #with(Key, Object) - */ - public GameData with(@NotNull Key key1, T1 value1, Key key2, T2 value2, Key key3, T3 value3) { - int index1 = key1.index(); - int index2 = key2.index(); - int index3 = key3.index(); - - if (present[index1] - && present[index2] - && present[index3] - && Objects.equals(values[index1], value1) - && Objects.equals(values[index2], value2) - && Objects.equals(values[index3], value3)) { - return this; - } - - Object[] newValues = Arrays.copyOf(this.values, SIZE); - boolean[] newPresent = Arrays.copyOf(this.present, SIZE); - newValues[index1] = value1; - newPresent[index1] = true; - newValues[index2] = value2; - newPresent[index2] = true; - newValues[index3] = value3; - newPresent[index3] = true; - return new GameData(newValues, newPresent); - } - /** * Returns {@code this} if this map contains a mapping for the specified key or throws a * {@link NoSuchElementException} otherwise. diff --git a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/PlayingCard.java b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/PlayingCard.java index fd6c718..b7e87e3 100644 --- a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/PlayingCard.java +++ b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/PlayingCard.java @@ -35,9 +35,6 @@ public final class PlayingCard extends TrickState { @Override public Optional onMessage(Game game, UUID player, PlayerMessage message) { if (get(CURRENT_PLAYER).equals(player) && message instanceof PlayCardMessage cardMessage) { - if (cardMessage.getCard() == null) { - throw new IllegalArgumentException("Card must not be null."); - } return transition(game, cardMessage.getCard()); } else { return super.onMessage(game, player, message); diff --git a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/StartingTrick.java b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/StartingTrick.java index 48a0e0c..a37f8c5 100644 --- a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/StartingTrick.java +++ b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/machine/states/trick/StartingTrick.java @@ -3,8 +3,6 @@ package eu.jonahbauer.wizard.core.machine.states.trick; import eu.jonahbauer.wizard.core.machine.Game; import eu.jonahbauer.wizard.core.machine.GameState; import eu.jonahbauer.wizard.core.machine.states.GameData; -import eu.jonahbauer.wizard.core.machine.states.SyncState; -import eu.jonahbauer.wizard.core.machine.states.round.RoundState; import java.util.Optional; diff --git a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/model/Configurations.java b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/model/Configurations.java index f21ca29..b7a8d9d 100644 --- a/wizard-core/src/main/java/eu/jonahbauer/wizard/core/model/Configurations.java +++ b/wizard-core/src/main/java/eu/jonahbauer/wizard/core/model/Configurations.java @@ -7,7 +7,6 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.util.HashMap; -import java.util.Locale; import java.util.Map; import java.util.NoSuchElementException; diff --git a/wizard-core/src/test/java/eu/jonahbauer/wizard/core/machine/states/round/DeterminingTrumpTest.java b/wizard-core/src/test/java/eu/jonahbauer/wizard/core/machine/states/round/DeterminingTrumpTest.java index 7f85963..678735f 100644 --- a/wizard-core/src/test/java/eu/jonahbauer/wizard/core/machine/states/round/DeterminingTrumpTest.java +++ b/wizard-core/src/test/java/eu/jonahbauer/wizard/core/machine/states/round/DeterminingTrumpTest.java @@ -24,7 +24,6 @@ import static eu.jonahbauer.wizard.core.machine.GameTestUtils.doFinish; import static eu.jonahbauer.wizard.core.machine.states.GameData.*; import static eu.jonahbauer.wizard.core.machine.states.GameData.TypedValue.entry; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.*; public class DeterminingTrumpTest { diff --git a/wizard-server/src/main/java/eu/jonahbauer/wizard/server/Lobby.java b/wizard-server/src/main/java/eu/jonahbauer/wizard/server/Lobby.java index 0a374e7..d353995 100644 --- a/wizard-server/src/main/java/eu/jonahbauer/wizard/server/Lobby.java +++ b/wizard-server/src/main/java/eu/jonahbauer/wizard/server/Lobby.java @@ -1,6 +1,5 @@ package eu.jonahbauer.wizard.server; -import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import eu.jonahbauer.wizard.common.messages.server.SessionCreatedMessage; import eu.jonahbauer.wizard.common.messages.server.SessionListMessage; @@ -26,17 +25,12 @@ public class Lobby { private Lobby() {} - public Session createSession(CreateSessionMessage create) { + public Session createSession(@NotNull String name, long timeout, @NotNull Configuration configuration) { lock.readLock().lock(); try { Session session; do { - session = new Session( - UUID.randomUUID(), - create.getSessionName(), - create.getTimeout(), - create.getConfiguration() - ); + session = new Session(UUID.randomUUID(), name, timeout, configuration); } while (sessions.putIfAbsent(session.getUuid(), session) != null); notifyPlayers(new SessionCreatedMessage(session.toData())); diff --git a/wizard-server/src/main/java/eu/jonahbauer/wizard/server/NackException.java b/wizard-server/src/main/java/eu/jonahbauer/wizard/server/NackException.java index a60bcc0..f4c67fd 100644 --- a/wizard-server/src/main/java/eu/jonahbauer/wizard/server/NackException.java +++ b/wizard-server/src/main/java/eu/jonahbauer/wizard/server/NackException.java @@ -1,9 +1,11 @@ package eu.jonahbauer.wizard.server; import eu.jonahbauer.wizard.common.messages.server.NackMessage; +import lombok.Getter; import org.intellij.lang.annotations.MagicConstant; public class NackException extends RuntimeException { + @Getter(onMethod = @__({@MagicConstant(valuesFromClass = NackMessage.class)})) private final int code; public NackException(@MagicConstant(valuesFromClass = NackMessage.class) int code, String message) { @@ -11,11 +13,6 @@ public class NackException extends RuntimeException { this.code = code; } - @MagicConstant(valuesFromClass = NackMessage.class) - public int getCode() { - return code; - } - public NackMessage toMessage() { return new NackMessage(code, getMessage()); } diff --git a/wizard-server/src/main/java/eu/jonahbauer/wizard/server/machine/states/LobbyState.java b/wizard-server/src/main/java/eu/jonahbauer/wizard/server/machine/states/LobbyState.java index 8b7449e..e280c2a 100644 --- a/wizard-server/src/main/java/eu/jonahbauer/wizard/server/machine/states/LobbyState.java +++ b/wizard-server/src/main/java/eu/jonahbauer/wizard/server/machine/states/LobbyState.java @@ -26,7 +26,11 @@ public class LobbyState implements ClientState { Lobby.getInstance().leave(player); try { player.buffer(); - var session = Lobby.getInstance().createSession(create); + var session = Lobby.getInstance().createSession( + create.getSessionName(), + create.getTimeout(), + create.getConfiguration() + ); var uuid = session.join(player, create.getPlayerName()); return Optional.of(new SessionState(session.getUuid(), uuid, create.getPlayerName())); } catch (NackException nack) {