#16 started useful error handling

main
Johannes Teubler 3 years ago
parent e3b99bc2fc
commit 254c4a99c4

@ -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<ClientState, Client> {
@Getter
private ClientSocket socket;
@Getter
@Setter
public boolean isError = false;
public Client(WizardGame game) {
super(new Menu());
this.game = game;

@ -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));
}

@ -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;

@ -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<ClientState> showErrorScreen(Client client, )
}

@ -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<ClientState> 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));
}
}

@ -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;
@ -19,6 +18,8 @@ import static eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession.Sourc
public final class Lobby extends BaseState {
private final Map<UUID, SessionData> sessions = new HashMap<>();
private Client client;
private LobbyScreen lobbyScreen;
public Lobby(Collection<SessionData> list) {
@ -28,6 +29,7 @@ public final class Lobby extends BaseState {
@Override
public Optional<ClientState> onEnter(Client client) {
showListScreen(client);
this.client = client;
return super.onEnter(client);
}

@ -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<ClientState> onMessage(Client client, ServerMessage message) {
// it is possible that there are messages still queued after

Loading…
Cancel
Save