diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java index f412902..043ee9d 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java @@ -119,7 +119,6 @@ public class Game implements Runnable { lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck); lobby.removeGameFromRunningGames(this); lobby.addGameToFinishedGames(this); - lobby.setLobbyIsOpen(true); return; } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java index 9a69e26..daa9e6d 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java @@ -319,7 +319,6 @@ public class ClientHandler implements Runnable { Thread t = new Thread(game); t.start(); l.addGameToRunningGames(game); - l.setLobbyIsOpen(false); } else { sendAnnouncementToClient("Only the admin can start the game"); } @@ -383,9 +382,8 @@ public class ClientHandler implements Runnable { } /** - * The client wants to join the lobby with the index i. - * //todo: needs more doc. - * @param i + * The client wants to join the lobby with the index i. If the lobby is closed, the client will be notified. + * @param i the number of the lobby to join */ public void joinLobby(int i) { Lobby l = Lobby.getLobbyFromID(i); @@ -393,7 +391,7 @@ public class ClientHandler implements Runnable { if (l.getLobbyIsOpen()) { l.addPlayer(this); } else { - sendAnnouncementToClient("The game in Lobby " + l.getLobbyID() + " has already started"); + sendAnnouncementToClient("The game in Lobby " + l.getLobbyID() + " has already started, or the lobby is already full."); } } else { sendAnnouncementToClient("Invalid Lobby nr."); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Lobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Lobby.java index 815564a..303ca94 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Lobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Lobby.java @@ -23,6 +23,8 @@ public class Lobby { * 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. @@ -31,6 +33,8 @@ public class Lobby { */ private boolean lobbyIsOpen = true; + private boolean gameIsRunning = false; + private static final int MAX_NO_OF_CLIENTS = 6; @@ -119,6 +123,11 @@ public class Lobby { } public boolean getLobbyIsOpen() { + if (lobbyClients.size() >= MAX_NO_OF_CLIENTS || gameIsRunning ) { + setLobbyIsOpen(false); + } else { + setLobbyIsOpen(true); + } return lobbyIsOpen; } @@ -135,12 +144,11 @@ public class Lobby { 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()) { @@ -158,8 +166,7 @@ public class Lobby { * @param client who wants to join the lobby. */ public synchronized boolean addPlayer(ClientHandler client) { - if (lobbyClients.size() < MAX_NO_OF_CLIENTS) { - //todo: check that game hasn't started yet + if (getLobbyIsOpen()) { if (clientIsInLobby(client) == -1) { lobbyClients.add(client); ClientHandler.broadcastAnnouncementToAll(client.getClientUserName() + " has joined lobby " + this.getLobbyID()); @@ -170,7 +177,7 @@ public class Lobby { client.sendAnnouncementToClient("You are already in lobby nr. " + clientIsInLobby(client)); } } else { - client.sendAnnouncementToClient("This lobby is full. Please try joining a different lobby or create a new lobby"); + client.sendAnnouncementToClient("This lobby is closed. Please try joining a different lobby or create a new lobby"); } return false; } @@ -195,11 +202,19 @@ public class Lobby { return false; } + /** + * Adds game to list of running games and sets its lobby's gameIsRunning to true. + */ public void addGameToRunningGames(Game game) { + game.getLobby().gameIsRunning = true; runningGames.add(game); } + /** + * Removes game from list of running games and sets its lobby's gameIsRunning to false. + */ public void removeGameFromRunningGames(Game game) { + game.getLobby().gameIsRunning = false; runningGames.remove(game); } @@ -213,7 +228,7 @@ public class Lobby { */ public void closeLobby() { lobbies.remove(this); - ClientHandler.broadcastAnnouncementToAll("Lobby nr. " + this.getLobbyID() + " has been closed."); + //ClientHandler.broadcastAnnouncementToAll("Lobby nr. " + this.getLobbyID() + " has been closed."); /* Todo: theoretically, this is enough to close a lobby.