implemented Lobby.gameIsRunning, Lobby.getLobbyIsOpen updates on method call and considers both the number of players in the lobby as well as whether a game has already started in that lobby.

This commit is contained in:
Jonas 2022-04-16 12:45:24 +02:00
parent b30f44df6c
commit b454cbee96
3 changed files with 24 additions and 12 deletions

View File

@ -119,7 +119,6 @@ public class Game implements Runnable {
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck);
lobby.removeGameFromRunningGames(this);
lobby.addGameToFinishedGames(this);
lobby.setLobbyIsOpen(true);
return;
}
}

View File

@ -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.");

View File

@ -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.