fixed game creation screen e8b2d641 #16

main
Jonah Bauer 3 years ago
parent c328377478
commit ebcefda582

@ -25,6 +25,9 @@ public class ClientSocket extends WebSocketClient {
@Override @Override
public void onMessage(String s) { public void onMessage(String s) {
if (WizardGame.DEBUG) {
System.out.println("Received: " + s);
}
ServerMessage message = ServerMessage.parse(s); ServerMessage message = ServerMessage.parse(s);
Gdx.app.postRunnable(() -> getClient().onMessage(message)); Gdx.app.postRunnable(() -> getClient().onMessage(message));
} }
@ -39,7 +42,15 @@ public class ClientSocket extends WebSocketClient {
e.printStackTrace(); e.printStackTrace();
close(CloseFrame.ABNORMAL_CLOSE, e.getMessage()); 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() { private Client getClient() {
return getAttachment(); return getAttachment();
} }

@ -6,10 +6,8 @@ import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import eu.jonahbauer.wizard.client.libgdx.WizardGame; 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.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; import eu.jonahbauer.wizard.common.model.Configuration;
public class CreateGameScreen extends MenuScreen { public class CreateGameScreen extends MenuScreen {
@ -32,7 +30,7 @@ public class CreateGameScreen extends MenuScreen {
game.setScreen(new LobbyScreen(game)); game.setScreen(new LobbyScreen(game));
sfxClick(); sfxClick();
} else if (actor == buttonContinue) { } else if (actor == buttonContinue) {
if( sessionName.getText() == null ) { if (sessionName.getText() == null) {
System.out.println("Please select a session name."); System.out.println("Please select a session name.");
sessionName.clear(); sessionName.clear();
} }
@ -40,7 +38,7 @@ public class CreateGameScreen extends MenuScreen {
userName = "Mallory"; userName = "Mallory";
System.out.println("Your name will be " + userName + "."); System.out.println("Your name will be " + userName + ".");
} }
try{ try {
selectedTimeOut = Long.parseLong(timeOut.getText()); selectedTimeOut = Long.parseLong(timeOut.getText());
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
System.out.println("Please select a valid timeout."); System.out.println("Please select a valid timeout.");
@ -48,6 +46,7 @@ public class CreateGameScreen extends MenuScreen {
} }
int selected = configurations.getSelectedIndex(); int selected = configurations.getSelectedIndex();
Configuration config = Configuration.values()[selected]; 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)); game.getClient().send(new CreateSessionMessage(sessionName.getText(), playerName.getText(), selectedTimeOut, config));
} }
} }
@ -114,12 +113,4 @@ public class CreateGameScreen extends MenuScreen {
buttonContinue.addListener(listener); buttonContinue.addListener(listener);
buttonBack.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()));
}
} }

@ -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.WizardGame;
import eu.jonahbauer.wizard.client.libgdx.actors.AutoFocusScrollPane; import eu.jonahbauer.wizard.client.libgdx.actors.AutoFocusScrollPane;
import eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession; 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.client.libgdx.state.Menu;
import eu.jonahbauer.wizard.common.messages.client.JoinSessionMessage; import eu.jonahbauer.wizard.common.messages.client.JoinSessionMessage;
import eu.jonahbauer.wizard.common.messages.data.SessionData; import eu.jonahbauer.wizard.common.messages.data.SessionData;
@ -48,7 +46,7 @@ public class LobbyScreen extends MenuScreen {
if (selectedSession != null) { if (selectedSession != null) {
var name = playerName.getText(); var name = playerName.getText();
var session = sessionData.get(selectedSession); 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)); game.getClient().send(new JoinSessionMessage(selectedSession, name));
} }
sfxClick(); sfxClick();

@ -1,7 +1,6 @@
package eu.jonahbauer.wizard.client.libgdx.screens; package eu.jonahbauer.wizard.client.libgdx.screens;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; 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.LeaveSessionMessage;
import eu.jonahbauer.wizard.common.messages.client.ReadyMessage; import eu.jonahbauer.wizard.common.messages.client.ReadyMessage;
import eu.jonahbauer.wizard.common.messages.data.PlayerData; 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; import java.util.UUID;
@ -132,10 +131,10 @@ public class WaitingScreen extends MenuScreen {
this.players.invalidateHierarchy(); this.players.invalidateHierarchy();
} }
public void setSession(SessionData session) { public void setSession(UUID uuid, String name, Configuration configuration) {
this.labelSessionName.setText(session.getName()); this.labelSessionName.setText(name);
this.labelSessionUUID.setText(session.getUuid().toString()); this.labelSessionUUID.setText(uuid.toString());
this.labelSessionConfiguration.setText(session.getConfiguration().toString()); this.labelSessionConfiguration.setText(configuration.toString());
} }
public void setPlayerName(String name) { public void setPlayerName(String name) {

@ -2,7 +2,6 @@ package eu.jonahbauer.wizard.client.libgdx.state;
import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.Client;
import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen;
import eu.jonahbauer.wizard.client.libgdx.screens.LobbyScreen;
import java.util.Optional; import java.util.Optional;
@ -23,7 +22,7 @@ public class AwaitingConnection extends Awaiting {
@Override @Override
public Optional<ClientState> onClose(Client client, int code, String reason, boolean remote) { public Optional<ClientState> 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()); return Optional.of(new Menu());
} }
} }

@ -2,18 +2,21 @@ package eu.jonahbauer.wizard.client.libgdx.state;
import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.Client;
import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; 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.messages.server.*;
import eu.jonahbauer.wizard.common.model.Configuration;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
@Getter @Getter
@RequiredArgsConstructor @RequiredArgsConstructor
public final class AwaitingJoinSession extends Awaiting { 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; private final String playerName;
@Override @Override
@ -26,7 +29,8 @@ public final class AwaitingJoinSession extends Awaiting {
@Override @Override
public Optional<ClientState> onMessage(Client client, ServerMessage message) { public Optional<ClientState> onMessage(Client client, ServerMessage message) {
if (message instanceof SessionJoinedMessage joined) { 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) { } else if (message instanceof NackMessage nack) {
switch (nack.getCode()) { switch (nack.getCode()) {
case NackMessage.GAME_ALREADY_STARTED -> System.out.println("Error: Game has already started."); case NackMessage.GAME_ALREADY_STARTED -> System.out.println("Error: Game has already started.");

@ -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<ClientState> onEnter(Client client) {
createScreen = new CreateGameScreen(client.getGame());
client.getGame().setScreen(createScreen);
return super.onEnter(client);
}
@Override
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
if (message instanceof SessionCreatedMessage created) {
return Optional.empty();
} else {
return unexpectedMessage(client, message);
}
}
}

@ -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.GameScreen;
import eu.jonahbauer.wizard.client.libgdx.screens.WaitingScreen; import eu.jonahbauer.wizard.client.libgdx.screens.WaitingScreen;
import eu.jonahbauer.wizard.common.messages.data.PlayerData; 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.messages.server.*;
import eu.jonahbauer.wizard.common.model.Configuration; import eu.jonahbauer.wizard.common.model.Configuration;
import lombok.Getter; import lombok.Getter;
@ -21,20 +20,22 @@ public final class Session extends BaseState {
private final UUID self; private final UUID self;
private final String secret; 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 final String playerName;
private boolean ready; private boolean ready;
@Setter @Setter
private Boolean nextReady; private Boolean nextReady;
public Session(SessionJoinedMessage joined, SessionData session, String playerName) { public Session(SessionJoinedMessage joined, String sessionName, Configuration configuration, String playerName) {
if (!joined.getSession().equals(session.getUuid())) throw new RuntimeException();
this.self = joined.getPlayer(); this.self = joined.getPlayer();
this.secret = joined.getSecret(); this.secret = joined.getSecret();
this.session = session; this.session = joined.getSession();
this.sessionName = sessionName;
this.configuration = configuration;
this.joined = joined; this.joined = joined;
this.playerName = playerName; this.playerName = playerName;
} }
@ -46,7 +47,7 @@ public final class Session extends BaseState {
sessionScreen.setPlayers(joined.getPlayers().toArray(new PlayerData[0])); sessionScreen.setPlayers(joined.getPlayers().toArray(new PlayerData[0]));
sessionScreen.setReady(false); sessionScreen.setReady(false);
sessionScreen.setPlayerName(playerName); sessionScreen.setPlayerName(playerName);
sessionScreen.setSession(session); sessionScreen.setSession(session, sessionName, configuration);
joined = null; joined = null;
return super.onEnter(client); return super.onEnter(client);

Loading…
Cancel
Save