added running flag to SessionData
This commit is contained in:
@@ -53,6 +53,11 @@ public class Session implements Observer, SessionMBean {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return game != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates the given player with this session and notifies all other players in the
|
||||
* session with a {@link PlayerJoinedMessage}, the joining player with a {@link SessionJoinedMessage} and all
|
||||
@@ -63,7 +68,7 @@ public class Session implements Observer, SessionMBean {
|
||||
* @return the players uuid
|
||||
*/
|
||||
public synchronized UUID join(Player player, String name) {
|
||||
if (game != null) {
|
||||
if (isRunning()) {
|
||||
throw new NackException(NackMessage.GAME_ALREADY_STARTED, "Game has already started.");
|
||||
} else if (players.size() == MAX_PLAYERS) {
|
||||
throw new NackException(NackMessage.SESSION_FULL, "Session is full.");
|
||||
@@ -80,7 +85,7 @@ public class Session implements Observer, SessionMBean {
|
||||
sessionPlayer.setPlayer(player);
|
||||
|
||||
notifyJoined(sessionPlayer.toData());
|
||||
Lobby.getInstance().notifyPlayers(new SessionModifiedMessage(toData()));
|
||||
notifyLobby();
|
||||
|
||||
return sessionPlayer.getUuid();
|
||||
}
|
||||
@@ -115,13 +120,13 @@ public class Session implements Observer, SessionMBean {
|
||||
}
|
||||
|
||||
public synchronized void leave(UUID player) {
|
||||
if (game == null) {
|
||||
if (!isRunning()) {
|
||||
if (players.remove(player) != null) {
|
||||
if (players.size() == 0) {
|
||||
Lobby.getInstance().removeSession(uuid);
|
||||
} else {
|
||||
notifyPlayers(new PlayerLeftMessage(player));
|
||||
Lobby.getInstance().notifyPlayers(new SessionModifiedMessage(toData()));
|
||||
notifyLobby();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -139,7 +144,7 @@ public class Session implements Observer, SessionMBean {
|
||||
var player = players.get(uuid);
|
||||
if (player == null) {
|
||||
throw new NackException(NackMessage.PLAYER_NOT_FOUND, "Who are you?");
|
||||
} else if (game != null) {
|
||||
} else if (isRunning()) {
|
||||
throw new NackException(NackMessage.GAME_ALREADY_STARTED, "Game has already started.");
|
||||
}
|
||||
|
||||
@@ -164,7 +169,7 @@ public class Session implements Observer, SessionMBean {
|
||||
var player = players.get(uuid);
|
||||
if (player == null) {
|
||||
throw new NackException(NackMessage.PLAYER_NOT_FOUND, "Who are you?");
|
||||
} else if (game == null) {
|
||||
} else if (!isRunning()) {
|
||||
throw new NackException(NackMessage.GAME_NOT_YET_STARTED, "Game hat not yet started.");
|
||||
} else {
|
||||
try {
|
||||
@@ -178,10 +183,11 @@ public class Session implements Observer, SessionMBean {
|
||||
}
|
||||
}
|
||||
|
||||
protected void startGame() {
|
||||
protected synchronized void startGame() {
|
||||
notifyPlayers(new StartingGameMessage());
|
||||
messages.add(Pair.of(null, new StartingGameMessage()));
|
||||
game = new Game(configuration, timeout, 10 * 60 * 1000, this);
|
||||
notifyLobby();
|
||||
game.start(List.copyOf(players.keySet()));
|
||||
CompletableFuture.runAsync(() -> {
|
||||
while (true) {
|
||||
@@ -195,20 +201,23 @@ public class Session implements Observer, SessionMBean {
|
||||
break;
|
||||
}
|
||||
}
|
||||
players.forEach((id, player) -> player.setReady(false));
|
||||
synchronized (this) {
|
||||
game = null;
|
||||
messages.clear();
|
||||
for (SessionPlayer player : List.copyOf(players.values())) {
|
||||
if (!player.isConnected()) {
|
||||
leave(player.getUuid());
|
||||
}
|
||||
}
|
||||
}
|
||||
finishGame();
|
||||
});
|
||||
}
|
||||
|
||||
protected void notifyJoined(PlayerData joined) {
|
||||
protected synchronized void finishGame() {
|
||||
players.forEach((id, player) -> player.setReady(false));
|
||||
game = null;
|
||||
messages.clear();
|
||||
for (SessionPlayer player : List.copyOf(players.values())) {
|
||||
if (!player.isConnected()) {
|
||||
leave(player.getUuid());
|
||||
}
|
||||
}
|
||||
notifyLobby();
|
||||
}
|
||||
|
||||
protected synchronized void notifyJoined(PlayerData joined) {
|
||||
var message = new PlayerJoinedMessage(joined);
|
||||
for (SessionPlayer player : players.values()) {
|
||||
if (player.getUuid().equals(joined.getUuid())) {
|
||||
@@ -224,14 +233,14 @@ public class Session implements Observer, SessionMBean {
|
||||
}
|
||||
}
|
||||
|
||||
protected void notifyPlayers(ServerMessage message) {
|
||||
protected synchronized void notifyPlayers(ServerMessage message) {
|
||||
for (SessionPlayer player : players.values()) {
|
||||
player.send(message);
|
||||
}
|
||||
}
|
||||
|
||||
public SessionData toData() {
|
||||
return new SessionData(uuid, name, players.size(), configuration);
|
||||
return new SessionData(uuid, name, players.size(), configuration, isRunning());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -266,6 +275,10 @@ public class Session implements Observer, SessionMBean {
|
||||
}
|
||||
}
|
||||
|
||||
protected void notifyLobby() {
|
||||
Lobby.getInstance().notifyPlayers(new SessionModifiedMessage(toData()));
|
||||
}
|
||||
|
||||
public void close() {
|
||||
for (var sessionPlayer : getPlayers().values()) {
|
||||
var player = sessionPlayer.getPlayer();
|
||||
@@ -276,11 +289,6 @@ public class Session implements Observer, SessionMBean {
|
||||
}
|
||||
|
||||
//<editor-fold desc="JMX" defaulstate="collapsed">
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return game != null;
|
||||
}
|
||||
|
||||
public boolean isDebug() {
|
||||
return false;
|
||||
}
|
||||
|
@@ -3,9 +3,7 @@ package eu.jonahbauer.wizard.server.debug;
|
||||
import eu.jonahbauer.wizard.common.messages.player.PlayerMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.NackMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.SessionModifiedMessage;
|
||||
import eu.jonahbauer.wizard.common.model.Configuration;
|
||||
import eu.jonahbauer.wizard.server.Lobby;
|
||||
import eu.jonahbauer.wizard.server.NackException;
|
||||
import eu.jonahbauer.wizard.server.Session;
|
||||
import eu.jonahbauer.wizard.server.machine.Player;
|
||||
@@ -51,7 +49,7 @@ public class DebugSession extends Session {
|
||||
sessionPlayer.setPlayer(player);
|
||||
|
||||
notifyJoined(sessionPlayer.toData());
|
||||
Lobby.getInstance().notifyPlayers(new SessionModifiedMessage(toData()));
|
||||
notifyLobby();
|
||||
|
||||
return sessionPlayer.getUuid();
|
||||
}
|
||||
|
Reference in New Issue
Block a user