Lobby has now a parameter that indicates if the lobby is still open or if the game has already started.
only the admin can start a game and only one game per lobby can be started
This commit is contained in:
parent
5b0004f8ff
commit
1ba30663be
@ -131,7 +131,10 @@ public class Game implements Runnable {
|
||||
}
|
||||
if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) || gameOverCheck.equals(
|
||||
ClientGameInfoHandler.gameOverHumansWin)) {
|
||||
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck); //ToDo(Seraina): adjust for lobby
|
||||
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck);
|
||||
lobby.removeGameFromRunningGames(this);
|
||||
lobby.addGameToFinishedGames(this);
|
||||
lobby.setLobbyIsOpen(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,6 +90,10 @@ public class MessageFormatter {
|
||||
System.out.println("invalid vote");
|
||||
}
|
||||
stringBuilder.append(Protocol.votedFor + "$" + position + "$");
|
||||
break;
|
||||
case "/s":
|
||||
stringBuilder.append(Protocol.startANewGame);
|
||||
|
||||
break;
|
||||
default:
|
||||
s = msg;
|
||||
|
||||
@ -135,7 +135,6 @@ public class ClientHandler implements Runnable {
|
||||
try {
|
||||
getLobby().getGame().getGameState().changeUsername(helper,newName);
|
||||
} catch (NullPointerException e) {
|
||||
LOGGER.warn("No game has been started yet in this lobby");
|
||||
}
|
||||
}
|
||||
|
||||
@ -306,15 +305,27 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new Game instance and starts its run method in a new thread
|
||||
* Initializes a new Game instance and starts its run method in a new thread.
|
||||
* Puts the game in the corresponding lobby. Only the admin of this lobby can start a new
|
||||
* game.
|
||||
*/
|
||||
public void startNewGame() {
|
||||
try {
|
||||
Lobby l = getLobby();
|
||||
Game game = new Game(6,1, l.getLobbyClients().size(), l);
|
||||
if (l.getLobbyIsOpen()) {
|
||||
if (l.getAdmin().equals(this)) {
|
||||
Game game = new Game(6, 1, l.getLobbyClients().size(), l);
|
||||
l.setGame(game);
|
||||
Thread t = new Thread(game);
|
||||
t.start();
|
||||
l.addGameToRunningGames(game);
|
||||
l.setLobbyIsOpen(false);
|
||||
} else {
|
||||
sendAnnouncementToClient("Only the admin can start the game");
|
||||
}
|
||||
} else {
|
||||
sendAnnouncementToClient("The game has already started");
|
||||
}
|
||||
} catch (TrainOverflow e) {
|
||||
LOGGER.warn(e.getMessage());
|
||||
} catch (NullPointerException e) {
|
||||
@ -379,7 +390,11 @@ public class ClientHandler implements Runnable {
|
||||
public void joinLobby(int i) {
|
||||
Lobby l = Lobby.getLobbyFromID(i);
|
||||
if (l != null) {
|
||||
if (l.getLobbyIsOpen()) {
|
||||
l.addPlayer(this);
|
||||
} else {
|
||||
sendAnnouncementToClient("The game in Lobby " + l.getLobbyID() + " has already started");
|
||||
}
|
||||
} else {
|
||||
sendAnnouncementToClient("Invalid Lobby nr.");
|
||||
sendAnnouncementToClient("use LISTL to list lobbies");
|
||||
@ -411,7 +426,11 @@ public class ClientHandler implements Runnable {
|
||||
sendAnnouncementToClient("No open Lobbies.");
|
||||
} else {
|
||||
for (Lobby l : Lobby.lobbies) {
|
||||
sendAnnouncementToClient("Lobby nr. " + l.getLobbyID() + ":");
|
||||
String lobbyStatus = "closed";
|
||||
if(l.getLobbyIsOpen()) {
|
||||
lobbyStatus = "open";
|
||||
}
|
||||
sendAnnouncementToClient("Lobby nr. " + l.getLobbyID() + ": (" + lobbyStatus + ")");
|
||||
for (ClientHandler c : l.getLobbyClients()) {
|
||||
if (c.equals(l.getAdmin())) {
|
||||
sendAnnouncementToClient(" -" + c.getClientUserName() + " (admin)");
|
||||
|
||||
@ -17,8 +17,20 @@ public class Lobby {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
public static HashSet<Lobby> lobbies = new HashSet<>();
|
||||
public Game game;
|
||||
public boolean gameIsOngoing; //TODO(Seraina): getter and setter
|
||||
public static HashSet<Game> runningGames = new HashSet<>();
|
||||
public static HashSet<Game> finishedGames = new HashSet<>();
|
||||
/**
|
||||
* The currently ongoing game, is set, when a game is started
|
||||
*/
|
||||
private Game game;
|
||||
/**
|
||||
* true by default
|
||||
* true if game has not started yet, false if game has. If true, potential players can still join the game.
|
||||
* Should be set to true again, after a game is finished.
|
||||
* Games can only be started if Lobby is open.
|
||||
*/
|
||||
private boolean lobbyIsOpen = true;
|
||||
|
||||
|
||||
private static final int MAX_NO_OF_CLIENTS = 6;
|
||||
|
||||
@ -96,6 +108,14 @@ public class Lobby {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static HashSet<Game> getFinishedGames() {
|
||||
return finishedGames;
|
||||
}
|
||||
|
||||
public static HashSet<Game> getRunningGames() {
|
||||
return runningGames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the game that the clients in this lobby are in
|
||||
*
|
||||
@ -105,6 +125,10 @@ public class Lobby {
|
||||
return game;
|
||||
}
|
||||
|
||||
public boolean getLobbyIsOpen() {
|
||||
return lobbyIsOpen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the game of this lobby to a certain game
|
||||
*
|
||||
@ -114,10 +138,16 @@ public class Lobby {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
public void setLobbyIsOpen(boolean lobbyIsOpen) {
|
||||
this.lobbyIsOpen = lobbyIsOpen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the lobby that the client is in. If the client is not in any
|
||||
* lobby, it returns -1.
|
||||
*/
|
||||
|
||||
|
||||
public static int clientIsInLobby(ClientHandler h) {
|
||||
for (Lobby l: lobbies) {
|
||||
for (ClientHandler clientHandler: l.getLobbyClients()) {
|
||||
@ -172,6 +202,18 @@ public class Lobby {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addGameToRunningGames(Game game) {
|
||||
runningGames.add(game);
|
||||
}
|
||||
|
||||
public void removeGameFromRunningGames(Game game) {
|
||||
runningGames.remove(game);
|
||||
}
|
||||
|
||||
public void addGameToFinishedGames(Game game) {
|
||||
finishedGames.add(game);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the lobby.
|
||||
*
|
||||
@ -179,6 +221,7 @@ public class Lobby {
|
||||
public void closeLobby() {
|
||||
lobbies.remove(this);
|
||||
ClientHandler.broadcastAnnouncementToAll("Lobby nr. " + this.getLobbyID() + " has been closed.");
|
||||
|
||||
/*
|
||||
Todo: theoretically, this is enough to close a lobby.
|
||||
ClientHandlers dont have to manually be removed from the lobby
|
||||
|
||||
Reference in New Issue
Block a user