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 37f1e24..ae0f8ed 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 @@ -6,6 +6,7 @@ import eu.jonahbauer.wizard.common.machine.TimeoutContext; import eu.jonahbauer.wizard.common.messages.client.ClientMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import lombok.Getter; +import lombok.Setter; import lombok.extern.log4j.Log4j2; import java.util.Optional; @@ -20,6 +21,10 @@ public class Client extends TimeoutContext { @Getter private ClientSocket socket; + @Getter + @Setter + public boolean isError = false; + public Client(WizardGame game) { super(new Menu()); this.game = game; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/ErrorScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/ErrorScreen.java index 1874f2f..0ce1c89 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/ErrorScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/ErrorScreen.java @@ -5,29 +5,29 @@ import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.WizardGame; import eu.jonahbauer.wizard.client.libgdx.listeners.KeyboardFocusManager; +import eu.jonahbauer.wizard.client.libgdx.state.BaseState; +import eu.jonahbauer.wizard.client.libgdx.state.Lobby; import eu.jonahbauer.wizard.client.libgdx.state.Session; import eu.jonahbauer.wizard.common.messages.server.NackMessage; public class ErrorScreen extends MenuScreen { private final String labelText; private TextButton buttonBack; - private final MenuScreen prevScreen; private final ChangeListener listener = new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { if (actor == buttonBack) { - game.setScreen(prevScreen); sfxClick(); } } }; - public ErrorScreen(WizardGame game, MenuScreen prevScreen, NackMessage message) { + public ErrorScreen(WizardGame game, Client client, NackMessage message) { super(game); - this.prevScreen = prevScreen; labelText = switch (message.getCode()) { case NackMessage.GAME_ALREADY_STARTED -> messages.get("menu.error.game_already_started"); case NackMessage.SESSION_FULL -> messages.get("menu.error.session_full"); @@ -61,6 +61,8 @@ public class ErrorScreen extends MenuScreen { content.addActor(label); content.addActor(buttonBack); + buttonBack.addListener(listener); + stage.addActor(content); stage.addCaptureListener(new KeyboardFocusManager(buttonBack)); } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java index 0dc35a5..4dc25b0 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java @@ -7,6 +7,7 @@ import eu.jonahbauer.wizard.client.libgdx.WizardGame; import eu.jonahbauer.wizard.client.libgdx.listeners.KeyboardFocusManager; import eu.jonahbauer.wizard.client.libgdx.listeners.ResetErrorListener; import eu.jonahbauer.wizard.client.libgdx.state.Lobby; +import eu.jonahbauer.wizard.client.libgdx.state.Menu; import eu.jonahbauer.wizard.common.messages.data.SessionData; import lombok.extern.log4j.Log4j2; diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java index f0c919c..9c3c636 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java @@ -28,4 +28,6 @@ public final class AwaitingConnection extends Awaiting { log.error("Connection could not be established. (code={}, reason={}, remote={})", code, reason, remote); return Optional.of(new Menu()); } + + //public Optional showErrorScreen(Client client, ) } 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 a36e3a2..8b7c6f8 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 @@ -1,6 +1,9 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; +import eu.jonahbauer.wizard.client.libgdx.WizardGame; +import eu.jonahbauer.wizard.client.libgdx.screens.ErrorScreen; +import eu.jonahbauer.wizard.common.messages.server.NackMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import lombok.extern.log4j.Log4j2; @@ -34,4 +37,12 @@ public abstract class BaseState implements ClientState { log.fatal("Unexpected message {}. Returning to menu.", message); return Optional.of(new Menu()); } + + public Optional showErrorScreen(Client client, NackMessage nack) { + WizardGame game = client.getGame(); + client.setError(true); + game.setScreen(new ErrorScreen(game, client, nack)); + + return Optional.of(new Menu(client)); + } } 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 f76ac72..8242e6b 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 @@ -1,9 +1,8 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; -import eu.jonahbauer.wizard.client.libgdx.screens.CreateGameScreen; -import eu.jonahbauer.wizard.client.libgdx.screens.LobbyScreen; -import eu.jonahbauer.wizard.client.libgdx.screens.RejoinScreen; +import eu.jonahbauer.wizard.client.libgdx.WizardGame; +import eu.jonahbauer.wizard.client.libgdx.screens.*; import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage; import eu.jonahbauer.wizard.common.messages.client.JoinSessionMessage; import eu.jonahbauer.wizard.common.messages.client.RejoinMessage; @@ -18,6 +17,8 @@ import static eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession.Sourc public final class Lobby extends BaseState { private final Map sessions = new HashMap<>(); + + private Client client; private LobbyScreen lobbyScreen; @@ -28,6 +29,7 @@ public final class Lobby extends BaseState { @Override public Optional onEnter(Client client) { showListScreen(client); + this.client = client; return super.onEnter(client); } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Menu.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Menu.java index eb1a1e7..52f0ffc 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Menu.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Menu.java @@ -21,10 +21,17 @@ public final class Menu extends BaseState { if (client.getSocket() != null && client.getSocket().isOpen()) { client.getSocket().close(CloseFrame.GOING_AWAY); } - showMenuScreen(client); return super.onEnter(client); } + public Menu(Client client) { + if(client.isError) { + //showErrorScreen(client); + } + } + + public Menu() {} + @Override public Optional onMessage(Client client, ServerMessage message) { // it is possible that there are messages still queued after