|
|
@ -9,6 +9,7 @@ import eu.jonahbauer.wizard.common.model.Configuration;
|
|
|
|
import eu.jonahbauer.wizard.core.machine.Game;
|
|
|
|
import eu.jonahbauer.wizard.core.machine.Game;
|
|
|
|
import eu.jonahbauer.wizard.core.messages.Observer;
|
|
|
|
import eu.jonahbauer.wizard.core.messages.Observer;
|
|
|
|
import eu.jonahbauer.wizard.core.model.Configurations;
|
|
|
|
import eu.jonahbauer.wizard.core.model.Configurations;
|
|
|
|
|
|
|
|
import eu.jonahbauer.wizard.core.util.Pair;
|
|
|
|
import eu.jonahbauer.wizard.server.machine.Player;
|
|
|
|
import eu.jonahbauer.wizard.server.machine.Player;
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.Data;
|
|
|
|
import lombok.Data;
|
|
|
@ -16,10 +17,7 @@ import lombok.EqualsAndHashCode;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
@ -40,6 +38,7 @@ public class Session implements Observer {
|
|
|
|
private final Map<UUID, SessionPlayer> players = new HashMap<>();
|
|
|
|
private final Map<UUID, SessionPlayer> players = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
private Game game;
|
|
|
|
private Game game;
|
|
|
|
|
|
|
|
private final List<Pair<UUID, ServerMessage>> messages = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Session(UUID uuid, String name, long timeout, Configuration configuration) {
|
|
|
|
public Session(UUID uuid, String name, long timeout, Configuration configuration) {
|
|
|
@ -79,6 +78,34 @@ public class Session implements Observer {
|
|
|
|
return sessionPlayer.getUuid();
|
|
|
|
return sessionPlayer.getUuid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized PlayerData rejoin(Player player, UUID uuid, String secret) {
|
|
|
|
|
|
|
|
if (!players.containsKey(uuid)) {
|
|
|
|
|
|
|
|
throw new NackException(NackMessage.ILLEGAL_ARGUMENT, "An error occurred.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SessionPlayer sessionPlayer = players.get(uuid);
|
|
|
|
|
|
|
|
if (sessionPlayer.isConnected()) {
|
|
|
|
|
|
|
|
throw new NackException(NackMessage.ILLEGAL_ARGUMENT, "An error occurred.");
|
|
|
|
|
|
|
|
} else if (!sessionPlayer.getSecret().equals(secret)) {
|
|
|
|
|
|
|
|
throw new NackException(NackMessage.ILLEGAL_ARGUMENT, "An error occurred.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sessionPlayer.setPlayer(player);
|
|
|
|
|
|
|
|
player.send(new SessionJoinedMessage(
|
|
|
|
|
|
|
|
this.uuid,
|
|
|
|
|
|
|
|
uuid,
|
|
|
|
|
|
|
|
players.values().stream().map(SessionPlayer::toData).toList(),
|
|
|
|
|
|
|
|
secret
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
for (Pair<UUID, ServerMessage> message : messages) {
|
|
|
|
|
|
|
|
if (message.getKey() == null || message.getKey().equals(uuid)) {
|
|
|
|
|
|
|
|
player.send(message.getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sessionPlayer.toData();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized void leave(UUID player) {
|
|
|
|
public synchronized void leave(UUID player) {
|
|
|
|
if (game == null) {
|
|
|
|
if (game == null) {
|
|
|
|
if (players.remove(player) != null) {
|
|
|
|
if (players.remove(player) != null) {
|
|
|
@ -149,6 +176,7 @@ public class Session implements Observer {
|
|
|
|
|
|
|
|
|
|
|
|
private void startGame() {
|
|
|
|
private void startGame() {
|
|
|
|
notifyPlayers(new StartingGameMessage());
|
|
|
|
notifyPlayers(new StartingGameMessage());
|
|
|
|
|
|
|
|
messages.add(Pair.of(null, new StartingGameMessage()));
|
|
|
|
game = new Game(Configurations.get(configuration).withTimeout(timeout), this);
|
|
|
|
game = new Game(Configurations.get(configuration).withTimeout(timeout), this);
|
|
|
|
game.start(List.copyOf(players.keySet()));
|
|
|
|
game.start(List.copyOf(players.keySet()));
|
|
|
|
CompletableFuture.runAsync(() -> {
|
|
|
|
CompletableFuture.runAsync(() -> {
|
|
|
@ -166,6 +194,7 @@ public class Session implements Observer {
|
|
|
|
players.forEach((id, player) -> player.setReady(false));
|
|
|
|
players.forEach((id, player) -> player.setReady(false));
|
|
|
|
synchronized (this) {
|
|
|
|
synchronized (this) {
|
|
|
|
game = null;
|
|
|
|
game = null;
|
|
|
|
|
|
|
|
messages.clear();
|
|
|
|
for (SessionPlayer player : List.copyOf(players.values())) {
|
|
|
|
for (SessionPlayer player : List.copyOf(players.values())) {
|
|
|
|
if (!player.isConnected()) {
|
|
|
|
if (!player.isConnected()) {
|
|
|
|
leave(player.getUuid());
|
|
|
|
leave(player.getUuid());
|
|
|
@ -202,13 +231,17 @@ public class Session implements Observer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void notify(ObserverMessage message) {
|
|
|
|
public synchronized void notify(ObserverMessage message) {
|
|
|
|
notifyPlayers(new GameMessage(message));
|
|
|
|
var gameMessage = new GameMessage(message);
|
|
|
|
|
|
|
|
notifyPlayers(gameMessage);
|
|
|
|
|
|
|
|
messages.add(Pair.of(null, gameMessage));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void notify(UUID player, ObserverMessage message) {
|
|
|
|
public synchronized void notify(UUID player, ObserverMessage message) {
|
|
|
|
players.get(player).send(new GameMessage(message));
|
|
|
|
var gameMessage = new GameMessage(message);
|
|
|
|
|
|
|
|
players.get(player).send(gameMessage);
|
|
|
|
|
|
|
|
messages.add(Pair.of(player, gameMessage));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
@Data
|
|
|
|