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 6e7d98b..80b273a 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 @@ -25,6 +25,9 @@ public class ClientSocket extends WebSocketClient { @Override public void onMessage(String s) { + if (WizardGame.DEBUG) { + System.out.println("Received: " + s); + } ServerMessage message = ServerMessage.parse(s); Gdx.app.postRunnable(() -> getClient().onMessage(message)); } @@ -39,7 +42,15 @@ public class ClientSocket extends WebSocketClient { e.printStackTrace(); close(CloseFrame.ABNORMAL_CLOSE, e.getMessage()); } - + + @Override + public void send(String text) { + if (WizardGame.DEBUG) { + System.out.println("Sending: " + text); + } + super.send(text); + } + private Client getClient() { return getAttachment(); } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java index c3937b5..c811c82 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java @@ -6,10 +6,8 @@ import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.utils.Array; import eu.jonahbauer.wizard.client.libgdx.WizardGame; -import eu.jonahbauer.wizard.client.libgdx.state.Session; +import eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession; import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage; -import eu.jonahbauer.wizard.common.messages.data.SessionData; -import eu.jonahbauer.wizard.common.messages.server.SessionJoinedMessage; import eu.jonahbauer.wizard.common.model.Configuration; public class CreateGameScreen extends MenuScreen { @@ -32,7 +30,7 @@ public class CreateGameScreen extends MenuScreen { game.setScreen(new LobbyScreen(game)); sfxClick(); } else if (actor == buttonContinue) { - if( sessionName.getText() == null ) { + if (sessionName.getText() == null) { System.out.println("Please select a session name."); sessionName.clear(); } @@ -40,7 +38,7 @@ public class CreateGameScreen extends MenuScreen { userName = "Mallory"; System.out.println("Your name will be " + userName + "."); } - try{ + try { selectedTimeOut = Long.parseLong(timeOut.getText()); } catch (NumberFormatException e) { System.out.println("Please select a valid timeout."); @@ -48,6 +46,7 @@ public class CreateGameScreen extends MenuScreen { } int selected = configurations.getSelectedIndex(); Configuration config = Configuration.values()[selected]; + game.getClient().transition(new AwaitingJoinSession(null, sessionName.getText(), config, playerName.getText())); game.getClient().send(new CreateSessionMessage(sessionName.getText(), playerName.getText(), selectedTimeOut, config)); } } @@ -114,12 +113,4 @@ public class CreateGameScreen extends MenuScreen { buttonContinue.addListener(listener); buttonBack.addListener(listener); } - - public void performTransition(SessionJoinedMessage joined) { - int selected = configurations.getSelectedIndex(); - Configuration config = Configuration.values()[selected]; - System.out.println("Transition from Lobby to Session"); - SessionData sessionData = new SessionData(joined.getSession(), sessionName.getText(), joined.getPlayers().size(), config); - game.getClient().transition(new Session(joined, sessionData, playerName.getText())); - } } 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 6a7e1ea..8b82517 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,8 +7,6 @@ import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import eu.jonahbauer.wizard.client.libgdx.WizardGame; import eu.jonahbauer.wizard.client.libgdx.actors.AutoFocusScrollPane; import eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession; -import eu.jonahbauer.wizard.client.libgdx.state.Creation; -import eu.jonahbauer.wizard.client.libgdx.state.Lobby; import eu.jonahbauer.wizard.client.libgdx.state.Menu; import eu.jonahbauer.wizard.common.messages.client.JoinSessionMessage; import eu.jonahbauer.wizard.common.messages.data.SessionData; @@ -48,7 +46,7 @@ public class LobbyScreen extends MenuScreen { if (selectedSession != null) { var name = playerName.getText(); var session = sessionData.get(selectedSession); - game.getClient().transition(new AwaitingJoinSession(session, name)); + game.getClient().transition(new AwaitingJoinSession(session.getUuid(), session.getName(), session.getConfiguration(), name)); game.getClient().send(new JoinSessionMessage(selectedSession, name)); } sfxClick(); 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 4ce30a9..91e5408 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 @@ -1,7 +1,6 @@ package eu.jonahbauer.wizard.client.libgdx.screens; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; @@ -12,7 +11,7 @@ import eu.jonahbauer.wizard.client.libgdx.state.Session; import eu.jonahbauer.wizard.common.messages.client.LeaveSessionMessage; import eu.jonahbauer.wizard.common.messages.client.ReadyMessage; import eu.jonahbauer.wizard.common.messages.data.PlayerData; -import eu.jonahbauer.wizard.common.messages.data.SessionData; +import eu.jonahbauer.wizard.common.model.Configuration; import java.util.UUID; @@ -132,10 +131,10 @@ public class WaitingScreen extends MenuScreen { this.players.invalidateHierarchy(); } - public void setSession(SessionData session) { - this.labelSessionName.setText(session.getName()); - this.labelSessionUUID.setText(session.getUuid().toString()); - this.labelSessionConfiguration.setText(session.getConfiguration().toString()); + public void setSession(UUID uuid, String name, Configuration configuration) { + this.labelSessionName.setText(name); + this.labelSessionUUID.setText(uuid.toString()); + this.labelSessionConfiguration.setText(configuration.toString()); } public void setPlayerName(String name) { 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 b4394f2..9eda580 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 @@ -2,7 +2,6 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; -import eu.jonahbauer.wizard.client.libgdx.screens.LobbyScreen; import java.util.Optional; @@ -23,7 +22,7 @@ public class AwaitingConnection extends Awaiting { @Override public Optional onClose(Client client, int code, String reason, boolean remote) { - System.out.println("Connection could not be established. (code=%d, reason=%s)" + code + reason); + System.out.printf("Connection could not be established. (code=%d, reason=%s)%n", code, reason); return Optional.of(new Menu()); } } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java index 2799268..a77b0be 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java @@ -2,18 +2,21 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; -import eu.jonahbauer.wizard.common.messages.data.SessionData; import eu.jonahbauer.wizard.common.messages.server.*; +import eu.jonahbauer.wizard.common.model.Configuration; import lombok.Getter; import lombok.RequiredArgsConstructor; import java.util.Optional; +import java.util.UUID; @Getter @RequiredArgsConstructor public final class AwaitingJoinSession extends Awaiting { - - private final SessionData session; + + private final UUID session; + private final String sessionName; + private final Configuration configuration; private final String playerName; @Override @@ -26,7 +29,8 @@ public final class AwaitingJoinSession extends Awaiting { @Override public Optional onMessage(Client client, ServerMessage message) { if (message instanceof SessionJoinedMessage joined) { - return Optional.of(new Session(joined, session, playerName)); + if (session != null && !session.equals(joined.getSession())) throw new RuntimeException(); + return Optional.of(new Session(joined, sessionName, configuration, playerName)); } else if (message instanceof NackMessage nack) { switch (nack.getCode()) { case NackMessage.GAME_ALREADY_STARTED -> System.out.println("Error: Game has already started."); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Creation.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Creation.java deleted file mode 100644 index bee76f3..0000000 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Creation.java +++ /dev/null @@ -1,34 +0,0 @@ -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.common.messages.server.ServerMessage; -import eu.jonahbauer.wizard.common.messages.server.SessionCreatedMessage; - -import java.util.Optional; - -public final class Creation extends BaseState { - - private CreateGameScreen createScreen; - - public Creation() { - } - - @Override - public Optional onEnter(Client client) { - createScreen = new CreateGameScreen(client.getGame()); - client.getGame().setScreen(createScreen); - - return super.onEnter(client); - } - - @Override - public Optional onMessage(Client client, ServerMessage message) { - if (message instanceof SessionCreatedMessage created) { - - return Optional.empty(); - } else { - return unexpectedMessage(client, message); - } - } -} \ No newline at end of file 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 24ee8a8..0887f89 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 @@ -4,7 +4,6 @@ import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.screens.GameScreen; import eu.jonahbauer.wizard.client.libgdx.screens.WaitingScreen; import eu.jonahbauer.wizard.common.messages.data.PlayerData; -import eu.jonahbauer.wizard.common.messages.data.SessionData; import eu.jonahbauer.wizard.common.messages.server.*; import eu.jonahbauer.wizard.common.model.Configuration; import lombok.Getter; @@ -21,20 +20,22 @@ public final class Session extends BaseState { private final UUID self; private final String secret; - - private final SessionData session; + + private final UUID session; + private final String sessionName; + private final Configuration configuration; private final String playerName; private boolean ready; @Setter private Boolean nextReady; - public Session(SessionJoinedMessage joined, SessionData session, String playerName) { - if (!joined.getSession().equals(session.getUuid())) throw new RuntimeException(); - + public Session(SessionJoinedMessage joined, String sessionName, Configuration configuration, String playerName) { this.self = joined.getPlayer(); this.secret = joined.getSecret(); - this.session = session; + this.session = joined.getSession(); + this.sessionName = sessionName; + this.configuration = configuration; this.joined = joined; this.playerName = playerName; } @@ -46,7 +47,7 @@ public final class Session extends BaseState { sessionScreen.setPlayers(joined.getPlayers().toArray(new PlayerData[0])); sessionScreen.setReady(false); sessionScreen.setPlayerName(playerName); - sessionScreen.setSession(session); + sessionScreen.setSession(session, sessionName, configuration); joined = null; return super.onEnter(client);