|
|
@ -53,6 +53,11 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
this.configuration = configuration;
|
|
|
|
this.configuration = configuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public boolean isRunning() {
|
|
|
|
|
|
|
|
return game != null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Associates the given player with this session and notifies all other players in the
|
|
|
|
* 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
|
|
|
|
* 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
|
|
|
|
* @return the players uuid
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public synchronized UUID join(Player player, String name) {
|
|
|
|
public synchronized UUID join(Player player, String name) {
|
|
|
|
if (game != null) {
|
|
|
|
if (isRunning()) {
|
|
|
|
throw new NackException(NackMessage.GAME_ALREADY_STARTED, "Game has already started.");
|
|
|
|
throw new NackException(NackMessage.GAME_ALREADY_STARTED, "Game has already started.");
|
|
|
|
} else if (players.size() == MAX_PLAYERS) {
|
|
|
|
} else if (players.size() == MAX_PLAYERS) {
|
|
|
|
throw new NackException(NackMessage.SESSION_FULL, "Session is full.");
|
|
|
|
throw new NackException(NackMessage.SESSION_FULL, "Session is full.");
|
|
|
@ -80,7 +85,7 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
sessionPlayer.setPlayer(player);
|
|
|
|
sessionPlayer.setPlayer(player);
|
|
|
|
|
|
|
|
|
|
|
|
notifyJoined(sessionPlayer.toData());
|
|
|
|
notifyJoined(sessionPlayer.toData());
|
|
|
|
Lobby.getInstance().notifyPlayers(new SessionModifiedMessage(toData()));
|
|
|
|
notifyLobby();
|
|
|
|
|
|
|
|
|
|
|
|
return sessionPlayer.getUuid();
|
|
|
|
return sessionPlayer.getUuid();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -115,13 +120,13 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized void leave(UUID player) {
|
|
|
|
public synchronized void leave(UUID player) {
|
|
|
|
if (game == null) {
|
|
|
|
if (!isRunning()) {
|
|
|
|
if (players.remove(player) != null) {
|
|
|
|
if (players.remove(player) != null) {
|
|
|
|
if (players.size() == 0) {
|
|
|
|
if (players.size() == 0) {
|
|
|
|
Lobby.getInstance().removeSession(uuid);
|
|
|
|
Lobby.getInstance().removeSession(uuid);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
notifyPlayers(new PlayerLeftMessage(player));
|
|
|
|
notifyPlayers(new PlayerLeftMessage(player));
|
|
|
|
Lobby.getInstance().notifyPlayers(new SessionModifiedMessage(toData()));
|
|
|
|
notifyLobby();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -139,7 +144,7 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
var player = players.get(uuid);
|
|
|
|
var player = players.get(uuid);
|
|
|
|
if (player == null) {
|
|
|
|
if (player == null) {
|
|
|
|
throw new NackException(NackMessage.PLAYER_NOT_FOUND, "Who are you?");
|
|
|
|
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.");
|
|
|
|
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);
|
|
|
|
var player = players.get(uuid);
|
|
|
|
if (player == null) {
|
|
|
|
if (player == null) {
|
|
|
|
throw new NackException(NackMessage.PLAYER_NOT_FOUND, "Who are you?");
|
|
|
|
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.");
|
|
|
|
throw new NackException(NackMessage.GAME_NOT_YET_STARTED, "Game hat not yet started.");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -178,10 +183,11 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void startGame() {
|
|
|
|
protected synchronized void startGame() {
|
|
|
|
notifyPlayers(new StartingGameMessage());
|
|
|
|
notifyPlayers(new StartingGameMessage());
|
|
|
|
messages.add(Pair.of(null, new StartingGameMessage()));
|
|
|
|
messages.add(Pair.of(null, new StartingGameMessage()));
|
|
|
|
game = new Game(configuration, timeout, 10 * 60 * 1000, this);
|
|
|
|
game = new Game(configuration, timeout, 10 * 60 * 1000, this);
|
|
|
|
|
|
|
|
notifyLobby();
|
|
|
|
game.start(List.copyOf(players.keySet()));
|
|
|
|
game.start(List.copyOf(players.keySet()));
|
|
|
|
CompletableFuture.runAsync(() -> {
|
|
|
|
CompletableFuture.runAsync(() -> {
|
|
|
|
while (true) {
|
|
|
|
while (true) {
|
|
|
@ -195,8 +201,12 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finishGame();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected synchronized void finishGame() {
|
|
|
|
players.forEach((id, player) -> player.setReady(false));
|
|
|
|
players.forEach((id, player) -> player.setReady(false));
|
|
|
|
synchronized (this) {
|
|
|
|
|
|
|
|
game = null;
|
|
|
|
game = null;
|
|
|
|
messages.clear();
|
|
|
|
messages.clear();
|
|
|
|
for (SessionPlayer player : List.copyOf(players.values())) {
|
|
|
|
for (SessionPlayer player : List.copyOf(players.values())) {
|
|
|
@ -204,11 +214,10 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
leave(player.getUuid());
|
|
|
|
leave(player.getUuid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyLobby();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void notifyJoined(PlayerData joined) {
|
|
|
|
protected synchronized void notifyJoined(PlayerData joined) {
|
|
|
|
var message = new PlayerJoinedMessage(joined);
|
|
|
|
var message = new PlayerJoinedMessage(joined);
|
|
|
|
for (SessionPlayer player : players.values()) {
|
|
|
|
for (SessionPlayer player : players.values()) {
|
|
|
|
if (player.getUuid().equals(joined.getUuid())) {
|
|
|
|
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()) {
|
|
|
|
for (SessionPlayer player : players.values()) {
|
|
|
|
player.send(message);
|
|
|
|
player.send(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public SessionData toData() {
|
|
|
|
public SessionData toData() {
|
|
|
|
return new SessionData(uuid, name, players.size(), configuration);
|
|
|
|
return new SessionData(uuid, name, players.size(), configuration, isRunning());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -266,6 +275,10 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void notifyLobby() {
|
|
|
|
|
|
|
|
Lobby.getInstance().notifyPlayers(new SessionModifiedMessage(toData()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
public void close() {
|
|
|
|
for (var sessionPlayer : getPlayers().values()) {
|
|
|
|
for (var sessionPlayer : getPlayers().values()) {
|
|
|
|
var player = sessionPlayer.getPlayer();
|
|
|
|
var player = sessionPlayer.getPlayer();
|
|
|
@ -276,11 +289,6 @@ public class Session implements Observer, SessionMBean {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//<editor-fold desc="JMX" defaulstate="collapsed">
|
|
|
|
//<editor-fold desc="JMX" defaulstate="collapsed">
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public boolean isRunning() {
|
|
|
|
|
|
|
|
return game != null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isDebug() {
|
|
|
|
public boolean isDebug() {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|