parent
d5bece96d1
commit
e8b2d64119
@ -1,5 +1,6 @@
|
||||
subprojects {
|
||||
dependencies {
|
||||
implementation(project(":wizard-common"))
|
||||
implementation(JavaWebSocket.id)
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx;
|
||||
|
||||
import eu.jonahbauer.wizard.client.libgdx.state.ClientState;
|
||||
import eu.jonahbauer.wizard.client.libgdx.state.Menu;
|
||||
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.extern.log4j.Log4j2;
|
||||
|
||||
@Log4j2
|
||||
public class Client extends TimeoutContext<ClientState, Client> {
|
||||
|
||||
@Getter
|
||||
private final WizardGame game;
|
||||
|
||||
@Getter
|
||||
private ClientSocket socket;
|
||||
|
||||
public Client(WizardGame game) {
|
||||
super(new Menu());
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleError(Throwable t) {
|
||||
// TODO better error handling
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
public void setSocket(ClientSocket socket) {
|
||||
this.socket = socket;
|
||||
if (socket != null) {
|
||||
this.socket.setAttachment(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void onOpen() {
|
||||
execute(s -> s.onOpen(this));
|
||||
}
|
||||
|
||||
public void onClose(int code, String reason, boolean remote) {
|
||||
execute(s -> s.onClose(this, code, reason, remote));
|
||||
}
|
||||
|
||||
public void onMessage(ServerMessage message) {
|
||||
log.debug(message.toString());
|
||||
execute(s -> s.onMessage(this, message));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onTransition(ClientState from, ClientState to) {
|
||||
System.out.println("Transistion from " + from.getClass().getSimpleName() + " to " + to.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
public void send(ClientMessage message) {
|
||||
getSocket().send(message.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.framing.CloseFrame;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import java.net.URI;
|
||||
|
||||
public class ClientSocket extends WebSocketClient {
|
||||
public ClientSocket(URI serverUri) {
|
||||
super(serverUri);
|
||||
|
||||
if ("wss".equals(getURI().getScheme())) {
|
||||
setSocketFactory(SSLSocketFactory.getDefault());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake serverHandshake) {
|
||||
Gdx.app.postRunnable(() -> getClient().onOpen());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String s) {
|
||||
ServerMessage message = ServerMessage.parse(s);
|
||||
Gdx.app.postRunnable(() -> getClient().onMessage(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(int i, String s, boolean b) {
|
||||
Gdx.app.postRunnable(() -> getClient().onClose(i, s, b));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
e.printStackTrace();
|
||||
close(CloseFrame.ABNORMAL_CLOSE, e.getMessage());
|
||||
}
|
||||
|
||||
private Client getClient() {
|
||||
return getAttachment();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx.screens;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
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 eu.jonahbauer.wizard.client.libgdx.WizardGame;
|
||||
|
||||
public class LoadingScreen extends MenuScreen {
|
||||
private final String key;
|
||||
|
||||
@Deprecated
|
||||
public LoadingScreen(WizardGame game) {
|
||||
this(game, "menu.loading.loading");
|
||||
}
|
||||
|
||||
public LoadingScreen(WizardGame game, String key) {
|
||||
super(game);
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
super.show();
|
||||
|
||||
var label = new Label(game.messages.get(key), game.data.skin);
|
||||
|
||||
var content = new VerticalGroup();
|
||||
content.setPosition(WizardGame.WIDTH * 0.5f, WizardGame.HEIGHT*0.5f);
|
||||
content.addActor(label);
|
||||
|
||||
game.data.stage.addActor(content);
|
||||
|
||||
Gdx.input.setInputProcessor(game.data.stage);
|
||||
}
|
||||
|
||||
}
|
@ -1,122 +1,173 @@
|
||||
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;
|
||||
import eu.jonahbauer.wizard.client.libgdx.WizardGame;
|
||||
import eu.jonahbauer.wizard.client.libgdx.actors.AutoFocusScrollPane;
|
||||
import eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinLobby;
|
||||
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;
|
||||
|
||||
public class WaitingScreen extends MenuScreen {
|
||||
|
||||
|
||||
private TextButton buttonLeave;
|
||||
private TextButton buttonReady;
|
||||
|
||||
private SessionData session;
|
||||
private UUID myUUID;
|
||||
private String myName;
|
||||
private boolean myReady;
|
||||
|
||||
private Label labelSessionName;
|
||||
private Label labelSessionUUID;
|
||||
private Label labelSessionConfiguration;
|
||||
private Label labelPlayerName;
|
||||
|
||||
private List<PlayerData> players;
|
||||
|
||||
{
|
||||
// sample data
|
||||
session = new SessionData(UUID.randomUUID(), "Session X", -1, Configuration.DEFAULT);
|
||||
myUUID = UUID.randomUUID();
|
||||
myName = "Max Mustermann";
|
||||
}
|
||||
|
||||
|
||||
private AutoFocusScrollPane listContainer;
|
||||
|
||||
private final ChangeListener listener = new ChangeListener() {
|
||||
@Override
|
||||
public void changed(ChangeEvent event, Actor actor) {
|
||||
if (actor == buttonLeave) {
|
||||
game.setScreen(new LobbyScreen(game, data));
|
||||
game.getClient().transition(new AwaitingJoinLobby());
|
||||
game.getClient().send(new LeaveSessionMessage());
|
||||
sfxClick();
|
||||
} else if (actor == buttonReady) {
|
||||
ready(!myReady);
|
||||
var state = (Session) game.getClient().getState();
|
||||
state.setNextReady(!state.isReady());
|
||||
game.getClient().send(new ReadyMessage(!state.isReady()));
|
||||
buttonReady.setDisabled(true);
|
||||
sfxClick();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public WaitingScreen(WizardGame game, Data data) {
|
||||
super(game, data);
|
||||
public WaitingScreen(WizardGame game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
super.show();
|
||||
|
||||
buttonLeave = new TextButton(game.messages.get("menu.waiting.leave"), data.skin);
|
||||
buttonLeave = new TextButton(game.messages.get("menu.waiting.leave"), game.data.skin);
|
||||
buttonLeave.setPosition(WizardGame.WIDTH * 0.275f, BUTTON_BAR_Y);
|
||||
buttonReady = new TextButton(game.messages.get("menu.waiting.ready"), data.skin);
|
||||
buttonReady = new TextButton(game.messages.get("menu.waiting.ready"), game.data.skin);
|
||||
buttonReady.setPosition(WizardGame.WIDTH * 0.725f - buttonReady.getWidth(), BUTTON_BAR_Y);
|
||||
|
||||
players = new List<>(data.skin) {
|
||||
players = new List<>(game.data.skin) {
|
||||
@Override
|
||||
public String toString(PlayerData player) {
|
||||
return player.getName();
|
||||
}
|
||||
};
|
||||
|
||||
var listContainer = new AutoFocusScrollPane(players, data.skin);
|
||||
listContainer = new AutoFocusScrollPane(players, game.data.skin);
|
||||
listContainer.layout();
|
||||
|
||||
var content = new HorizontalGroup().grow().space(20);
|
||||
content.setPosition(0.25f * WizardGame.WIDTH, 0.3f * WizardGame.HEIGHT);
|
||||
content.setSize(0.5f * WizardGame.WIDTH, 400);
|
||||
content.addActor(new Container<>(listContainer).width(0.2f * WizardGame.WIDTH).height(400));
|
||||
content.addActor(createInfoTable());
|
||||
content.layout();
|
||||
|
||||
Gdx.input.setInputProcessor(data.stage);
|
||||
data.stage.addActor(buttonLeave);
|
||||
data.stage.addActor(buttonReady);
|
||||
data.stage.addActor(content);
|
||||
Gdx.input.setInputProcessor(game.data.stage);
|
||||
game.data.stage.addActor(buttonLeave);
|
||||
game.data.stage.addActor(buttonReady);
|
||||
game.data.stage.addActor(content);
|
||||
|
||||
buttonLeave.addListener(listener);
|
||||
buttonReady.addListener(listener);
|
||||
|
||||
// sample data
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
players.getItems().add(new PlayerData(UUID.randomUUID(), "Player " + i, false));
|
||||
}
|
||||
players.invalidateHierarchy();
|
||||
}
|
||||
|
||||
private void ready(boolean ready) {
|
||||
this.myReady = ready;
|
||||
public void setReady(boolean ready) {
|
||||
buttonReady.setDisabled(false);
|
||||
buttonReady.setText(game.messages.get(ready ? "menu.waiting.not_ready" : "menu.waiting.ready"));
|
||||
}
|
||||
|
||||
|
||||
public void addPlayer(PlayerData player) {
|
||||
this.players.getItems().add(player);
|
||||
var items = this.players.getItems();
|
||||
for (int i = 0; i < items.size; i++) {
|
||||
if (items.get(i).getUuid().equals(player.getUuid())) {
|
||||
items.set(i, player);
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.players.invalidateHierarchy();
|
||||
}
|
||||
|
||||
public void removePlayer(UUID player) {
|
||||
var items = this.players.getItems();
|
||||
for (int i = 0; i < items.size; i++) {
|
||||
if (items.get(i).getUuid().equals(player)) {
|
||||
items.removeIndex(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.players.invalidateHierarchy();
|
||||
}
|
||||
|
||||
public void modifyPlayer(PlayerData data) {
|
||||
var items = this.players.getItems();
|
||||
for (int i = 0; i < items.size; i++) {
|
||||
if (items.get(i).getUuid().equals(data.getUuid())) {
|
||||
items.set(i, data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.players.invalidateHierarchy();
|
||||
}
|
||||
|
||||
public void setPlayers(PlayerData... players) {
|
||||
var items = this.players.getItems();
|
||||
items.clear();
|
||||
items.addAll(players);
|
||||
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 setPlayerName(String name) {
|
||||
this.labelPlayerName.setText(name);
|
||||
}
|
||||
|
||||
private Table createInfoTable() {
|
||||
float infoTableWidth = 0.3f * WizardGame.WIDTH - 20;
|
||||
|
||||
var labelSessionName = new Label(session.getName(), data.skin, "textfield");
|
||||
var labelSessionUUID = new Label(session.getUuid().toString(), data.skin, "textfield");
|
||||
var labelSessionConfiguration = new Label(session.getConfiguration().toString(), data.skin, "textfield");
|
||||
var labelPlayerName = new Label(myName, data.skin, "textfield");
|
||||
|
||||
|
||||
labelSessionName = new Label("", game.data.skin, "textfield");
|
||||
labelSessionUUID = new Label("", game.data.skin, "textfield");
|
||||
labelSessionConfiguration = new Label("", game.data.skin, "textfield");
|
||||
labelPlayerName = new Label("", game.data.skin, "textfield");
|
||||
|
||||
labelSessionName.setEllipsis(true);
|
||||
labelSessionUUID.setEllipsis(true);
|
||||
labelSessionConfiguration.setEllipsis(true);
|
||||
labelPlayerName.setEllipsis(true);
|
||||
|
||||
|
||||
var infoTable = new Table().center().left();
|
||||
infoTable.columnDefaults(0).growX().width(infoTableWidth);
|
||||
infoTable.setSize(infoTableWidth, 400);
|
||||
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.session_name.label"), data.skin)).row();
|
||||
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.session_name.label"), game.data.skin)).row();
|
||||
infoTable.add(labelSessionName).row();
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.session_uuid.label"), data.skin)).row();
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.session_uuid.label"), game.data.skin)).row();
|
||||
infoTable.add(labelSessionUUID).row();
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.session_configuration.label"), data.skin)).row();
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.session_configuration.label"), game.data.skin)).row();
|
||||
infoTable.add(labelSessionConfiguration).row();
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.player_name.label"), data.skin)).row();
|
||||
infoTable.add(new Label(game.messages.get("menu.waiting.player_name.label"), game.data.skin)).row();
|
||||
infoTable.add(labelPlayerName).row();
|
||||
|
||||
|
||||
return infoTable;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx.state;
|
||||
|
||||
import eu.jonahbauer.wizard.client.libgdx.Client;
|
||||
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class Awaiting extends BaseState implements ClientState {
|
||||
@Override
|
||||
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
|
||||
return unexpectedMessage(client, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Optional<ClientState> onEnter(Client client) {
|
||||
client.timeout(this, 10_000);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onTimeout(Client client) {
|
||||
System.out.println("Timed out. Returning to menu");
|
||||
return Optional.of(new Menu());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
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;
|
||||
|
||||
public class AwaitingConnection extends Awaiting {
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onEnter(Client client) {
|
||||
System.out.println("Awaiting connection...");
|
||||
client.getGame().setScreen(new LoadingScreen(client.getGame()));
|
||||
return super.onEnter(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onOpen(Client client) {
|
||||
System.out.println("Connection established.");
|
||||
return Optional.of(new AwaitingJoinLobby());
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
return Optional.of(new Menu());
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
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.server.ServerMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.SessionListMessage;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public final class AwaitingJoinLobby extends Awaiting {
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onEnter(Client client) {
|
||||
System.out.println("Waiting for session list...");
|
||||
client.getGame().setScreen(new LoadingScreen(client.getGame()));
|
||||
return super.onEnter(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
|
||||
if (message instanceof SessionListMessage list) {
|
||||
return Optional.of(new Lobby(list));
|
||||
} else {
|
||||
return super.onMessage(client, message);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
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 lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public final class AwaitingJoinSession extends Awaiting {
|
||||
|
||||
private final SessionData session;
|
||||
private final String playerName;
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onEnter(Client client) {
|
||||
System.out.println("Waiting for acknowledgment...");
|
||||
client.getGame().setScreen(new LoadingScreen(client.getGame()));
|
||||
return super.onEnter(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
|
||||
if (message instanceof SessionJoinedMessage joined) {
|
||||
return Optional.of(new Session(joined, session, playerName));
|
||||
} else if (message instanceof NackMessage nack) {
|
||||
switch (nack.getCode()) {
|
||||
case NackMessage.GAME_ALREADY_STARTED -> System.out.println("Error: Game has already started.");
|
||||
case NackMessage.SESSION_FULL -> System.out.println("Error: The session is full.");
|
||||
case NackMessage.SESSION_NOT_FOUND -> System.out.println("Error: Session not found.");
|
||||
case NackMessage.NAME_TAKEN -> System.out.println("Error: Name already taken.");
|
||||
default -> { return super.onMessage(client, message); }
|
||||
}
|
||||
return Optional.of(new AwaitingJoinLobby());
|
||||
} else if (message instanceof SessionModifiedMessage || message instanceof SessionRemovedMessage) {
|
||||
// drop
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return super.onMessage(client, message);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx.state;
|
||||
|
||||
import eu.jonahbauer.wizard.client.libgdx.Client;
|
||||
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class BaseState implements ClientState {
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onEnter(Client context) {
|
||||
return ClientState.super.onEnter(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onOpen(Client client) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onClose(Client client, int code, String reason, boolean remote) {
|
||||
if (remote) {
|
||||
System.out.println("Lost connection " + code + " " + reason);
|
||||
} else {
|
||||
System.out.println("Connection closed " + code + " " + reason);
|
||||
}
|
||||
return Optional.of(new Menu());
|
||||
}
|
||||
|
||||
protected static Optional<ClientState> unexpectedMessage(Client client, ServerMessage message) {
|
||||
// return to menu on unexpected message
|
||||
System.out.println("Fatal: Unexpected message " + message + ". Returning to menu.");
|
||||
return Optional.of(new Menu());
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx.state;
|
||||
|
||||
import eu.jonahbauer.wizard.client.libgdx.Client;
|
||||
import eu.jonahbauer.wizard.common.machine.TimeoutState;
|
||||
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ClientState extends TimeoutState<ClientState, Client> {
|
||||
Optional<ClientState> onOpen(Client client);
|
||||
|
||||
Optional<ClientState> onMessage(Client client, ServerMessage message);
|
||||
|
||||
Optional<ClientState> onClose(Client client, int code, String reason, boolean remote);
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx.state;
|
||||
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
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.common.messages.data.SessionData;
|
||||
import eu.jonahbauer.wizard.common.messages.server.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public final class Lobby extends BaseState {
|
||||
|
||||
private SessionListMessage list;
|
||||
private LobbyScreen lobbyScreen;
|
||||
private CreateGameScreen createScreen;
|
||||
|
||||
public Lobby(SessionListMessage list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onEnter(Client client) {
|
||||
lobbyScreen = new LobbyScreen(client.getGame());
|
||||
client.getGame().setScreen(lobbyScreen);
|
||||
lobbyScreen.setSessions(list.getSessions().toArray(new SessionData[0]));
|
||||
|
||||
list = null;
|
||||
return super.onEnter(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
|
||||
if (message instanceof SessionCreatedMessage created) {
|
||||
lobbyScreen.addSession(created.getSession());
|
||||
return Optional.empty();
|
||||
} else if (message instanceof SessionRemovedMessage removed) {
|
||||
lobbyScreen.removeSession(removed.getSession());
|
||||
return Optional.empty();
|
||||
} else if (message instanceof SessionModifiedMessage modified) {
|
||||
lobbyScreen.modifySession(modified.getSession());
|
||||
return Optional.empty();
|
||||
} else if (message instanceof SessionListMessage list) {
|
||||
lobbyScreen.setSessions(list.getSessions().toArray(new SessionData[0]));
|
||||
return Optional.empty();
|
||||
} else if (message instanceof SessionJoinedMessage joined) {
|
||||
//TODO find solution
|
||||
createScreen = new CreateGameScreen(client.getGame());
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return unexpectedMessage(client, message);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx.state;
|
||||
|
||||
import eu.jonahbauer.wizard.client.libgdx.Client;
|
||||
import eu.jonahbauer.wizard.client.libgdx.screens.MainMenuScreen;
|
||||
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
|
||||
import lombok.SneakyThrows;
|
||||
import org.java_websocket.framing.CloseFrame;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public final class Menu extends BaseState {
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Optional<ClientState> onEnter(Client client) {
|
||||
if (client.getSocket() != null && client.getSocket().isOpen()) {
|
||||
client.getSocket().close(CloseFrame.GOING_AWAY);
|
||||
}
|
||||
client.getGame().setScreen(new MainMenuScreen(client.getGame()));
|
||||
return super.onEnter(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
|
||||
// it is possible that there are messages still queued after
|
||||
// returning to the menu as a result of a previous message
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onClose(Client client, int code, String reason, boolean remote) {
|
||||
super.onClose(client, code, reason, remote);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package eu.jonahbauer.wizard.client.libgdx.state;
|
||||
|
||||
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;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
public final class Session extends BaseState {
|
||||
|
||||
private SessionJoinedMessage joined;
|
||||
private WaitingScreen sessionScreen;
|
||||
|
||||
private final UUID self;
|
||||
private final String secret;
|
||||
|
||||
private final SessionData session;
|
||||
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();
|
||||
|
||||
this.self = joined.getPlayer();
|
||||
this.secret = joined.getSecret();
|
||||
this.session = session;
|
||||
this.joined = joined;
|
||||
this.playerName = playerName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onEnter(Client client) {
|
||||
sessionScreen = new WaitingScreen(client.getGame());
|
||||
client.getGame().setScreen(sessionScreen);
|
||||
sessionScreen.setPlayers(joined.getPlayers().toArray(new PlayerData[0]));
|
||||
sessionScreen.setReady(false);
|
||||
sessionScreen.setPlayerName(playerName);
|
||||
sessionScreen.setSession(session);
|
||||
|
||||
joined = null;
|
||||
return super.onEnter(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ClientState> onMessage(Client client, ServerMessage message) {
|
||||
if (message instanceof PlayerJoinedMessage join) {
|
||||
sessionScreen.addPlayer(join.getPlayer());
|
||||
return Optional.empty();
|
||||
} else if (message instanceof PlayerLeftMessage leave) {
|
||||
sessionScreen.removePlayer(leave.getPlayer());
|
||||
return Optional.empty();
|
||||
} else if (message instanceof PlayerModifiedMessage modified) {
|
||||
sessionScreen.modifyPlayer(modified.getPlayer());
|
||||
return Optional.empty();
|
||||
} else if (message instanceof StartingGameMessage) {
|
||||
client.getGame().setScreen(new GameScreen(client.getGame()));
|
||||
return Optional.empty();
|
||||
} else if (nextReady != null && message instanceof NackMessage nack) {
|
||||
// TODO display error
|
||||
System.out.println("Error: " + nack.getMessage());
|
||||
sessionScreen.setReady(!nextReady);
|
||||
nextReady = null;
|
||||
return Optional.empty();
|
||||
} else if (nextReady != null && message instanceof AckMessage) {
|
||||
sessionScreen.setReady(nextReady);
|
||||
ready = nextReady;
|
||||
nextReady = null;
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return unexpectedMessage(client, message);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue