|
|
|
@ -2,8 +2,14 @@ package eu.jonahbauer.wizard.server;
|
|
|
|
|
|
|
|
|
|
import eu.jonahbauer.wizard.common.messages.data.PlayerData;
|
|
|
|
|
import eu.jonahbauer.wizard.common.messages.data.SessionData;
|
|
|
|
|
import eu.jonahbauer.wizard.common.messages.observer.ObserverMessage;
|
|
|
|
|
import eu.jonahbauer.wizard.common.messages.player.PlayerMessage;
|
|
|
|
|
import eu.jonahbauer.wizard.common.messages.server.*;
|
|
|
|
|
import eu.jonahbauer.wizard.common.model.Configuration;
|
|
|
|
|
import eu.jonahbauer.wizard.core.machine.Game;
|
|
|
|
|
import eu.jonahbauer.wizard.core.messages.Observer;
|
|
|
|
|
import eu.jonahbauer.wizard.core.model.Configurations;
|
|
|
|
|
import eu.jonahbauer.wizard.core.model.GameConfiguration;
|
|
|
|
|
import eu.jonahbauer.wizard.server.machine.Player;
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
|
import lombok.Data;
|
|
|
|
@ -17,7 +23,7 @@ import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
@EqualsAndHashCode(of = "uuid")
|
|
|
|
|
public class Session {
|
|
|
|
|
public class Session implements Observer {
|
|
|
|
|
private static final int MIN_PLAYERS = 3;
|
|
|
|
|
private static final int MAX_PLAYERS = 6;
|
|
|
|
|
|
|
|
|
@ -29,6 +35,8 @@ public class Session {
|
|
|
|
|
@Getter(AccessLevel.NONE)
|
|
|
|
|
private final Map<UUID, SessionPlayer> players = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
private Game game;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Session(UUID uuid, String name, long timeout, Configuration configuration) {
|
|
|
|
|
this.uuid = uuid;
|
|
|
|
@ -48,9 +56,9 @@ public class Session {
|
|
|
|
|
*/
|
|
|
|
|
public synchronized UUID join(Player player, String name) {
|
|
|
|
|
if (players.size() == MAX_PLAYERS) {
|
|
|
|
|
throw new NackException(NackMessage.BAD_REQUEST, "Session is full.");
|
|
|
|
|
throw new NackException(NackMessage.SESSION_FULL, "Session is full.");
|
|
|
|
|
} else if (players.values().stream().anyMatch(p -> p.getName().equalsIgnoreCase(name))) {
|
|
|
|
|
throw new NackException(NackMessage.BAD_REQUEST, "Name is already taken.");
|
|
|
|
|
throw new NackException(NackMessage.NAME_TAKEN, "Name is already taken.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Lobby.getInstance().leave(player);
|
|
|
|
@ -80,16 +88,19 @@ public class Session {
|
|
|
|
|
public synchronized void ready(UUID player, boolean ready) {
|
|
|
|
|
var sessionPlayer = players.get(player);
|
|
|
|
|
if (sessionPlayer == null) {
|
|
|
|
|
throw new NackException(NackMessage.BAD_REQUEST, "Who are you?");
|
|
|
|
|
throw new NackException(NackMessage.PLAYER_NOT_FOUND, "Who are you?");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sessionPlayer.isReady() != ready) {
|
|
|
|
|
sessionPlayer.setReady(ready);
|
|
|
|
|
sessionPlayer.getPlayer().send(new AckMessage());
|
|
|
|
|
notifyPlayers(new PlayerModifiedMessage(sessionPlayer.toData()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (players.size() >= MIN_PLAYERS && players.values().stream().allMatch(SessionPlayer::isReady)) {
|
|
|
|
|
// TODO start game
|
|
|
|
|
game = new Game(Configurations.get(configuration), this);
|
|
|
|
|
notifyPlayers(new StartingGameMessage());
|
|
|
|
|
game.start(players.keySet().stream().toList());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -119,6 +130,26 @@ public class Session {
|
|
|
|
|
return new SessionData(uuid, name, players.size(), configuration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Forwards the message sent by the player to the game.
|
|
|
|
|
*
|
|
|
|
|
* @param player the player
|
|
|
|
|
* @param message the players message
|
|
|
|
|
*/
|
|
|
|
|
public void handlePlayerMessage(UUID player, PlayerMessage message) {
|
|
|
|
|
game.onMessage(player, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void notify(ObserverMessage message) {
|
|
|
|
|
notifyPlayers(new GameMessage(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void notify(UUID player, ObserverMessage message) {
|
|
|
|
|
players.get(player).getPlayer().send(new GameMessage(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
@EqualsAndHashCode(of = "uuid")
|
|
|
|
|
private static class SessionPlayer {
|
|
|
|
|