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:
parent
b30f44df6c
commit
b454cbee96
@ -119,7 +119,6 @@ public class Game implements Runnable {
|
|||||||
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck);
|
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck);
|
||||||
lobby.removeGameFromRunningGames(this);
|
lobby.removeGameFromRunningGames(this);
|
||||||
lobby.addGameToFinishedGames(this);
|
lobby.addGameToFinishedGames(this);
|
||||||
lobby.setLobbyIsOpen(true);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -319,7 +319,6 @@ public class ClientHandler implements Runnable {
|
|||||||
Thread t = new Thread(game);
|
Thread t = new Thread(game);
|
||||||
t.start();
|
t.start();
|
||||||
l.addGameToRunningGames(game);
|
l.addGameToRunningGames(game);
|
||||||
l.setLobbyIsOpen(false);
|
|
||||||
} else {
|
} else {
|
||||||
sendAnnouncementToClient("Only the admin can start the game");
|
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.
|
* The client wants to join the lobby with the index i. If the lobby is closed, the client will be notified.
|
||||||
* //todo: needs more doc.
|
* @param i the number of the lobby to join
|
||||||
* @param i
|
|
||||||
*/
|
*/
|
||||||
public void joinLobby(int i) {
|
public void joinLobby(int i) {
|
||||||
Lobby l = Lobby.getLobbyFromID(i);
|
Lobby l = Lobby.getLobbyFromID(i);
|
||||||
@ -393,7 +391,7 @@ public class ClientHandler implements Runnable {
|
|||||||
if (l.getLobbyIsOpen()) {
|
if (l.getLobbyIsOpen()) {
|
||||||
l.addPlayer(this);
|
l.addPlayer(this);
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
sendAnnouncementToClient("Invalid Lobby nr.");
|
sendAnnouncementToClient("Invalid Lobby nr.");
|
||||||
|
|||||||
@ -23,6 +23,8 @@ public class Lobby {
|
|||||||
* The currently ongoing game, is set, when a game is started
|
* The currently ongoing game, is set, when a game is started
|
||||||
*/
|
*/
|
||||||
private Game game;
|
private Game game;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* true by default
|
* true by default
|
||||||
* true if game has not started yet, false if game has. If true, potential players can still join the game.
|
* 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 lobbyIsOpen = true;
|
||||||
|
|
||||||
|
private boolean gameIsRunning = false;
|
||||||
|
|
||||||
|
|
||||||
private static final int MAX_NO_OF_CLIENTS = 6;
|
private static final int MAX_NO_OF_CLIENTS = 6;
|
||||||
|
|
||||||
@ -119,6 +123,11 @@ public class Lobby {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean getLobbyIsOpen() {
|
public boolean getLobbyIsOpen() {
|
||||||
|
if (lobbyClients.size() >= MAX_NO_OF_CLIENTS || gameIsRunning ) {
|
||||||
|
setLobbyIsOpen(false);
|
||||||
|
} else {
|
||||||
|
setLobbyIsOpen(true);
|
||||||
|
}
|
||||||
return lobbyIsOpen;
|
return lobbyIsOpen;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,12 +144,11 @@ public class Lobby {
|
|||||||
this.lobbyIsOpen = lobbyIsOpen;
|
this.lobbyIsOpen = lobbyIsOpen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the ID of the lobby that the client is in. If the client is not in any
|
* Returns the ID of the lobby that the client is in. If the client is not in any
|
||||||
* lobby, it returns -1.
|
* lobby, it returns -1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public static int clientIsInLobby(ClientHandler h) {
|
public static int clientIsInLobby(ClientHandler h) {
|
||||||
for (Lobby l: lobbies) {
|
for (Lobby l: lobbies) {
|
||||||
for (ClientHandler clientHandler: l.getLobbyClients()) {
|
for (ClientHandler clientHandler: l.getLobbyClients()) {
|
||||||
@ -158,8 +166,7 @@ public class Lobby {
|
|||||||
* @param client who wants to join the lobby.
|
* @param client who wants to join the lobby.
|
||||||
*/
|
*/
|
||||||
public synchronized boolean addPlayer(ClientHandler client) {
|
public synchronized boolean addPlayer(ClientHandler client) {
|
||||||
if (lobbyClients.size() < MAX_NO_OF_CLIENTS) {
|
if (getLobbyIsOpen()) {
|
||||||
//todo: check that game hasn't started yet
|
|
||||||
if (clientIsInLobby(client) == -1) {
|
if (clientIsInLobby(client) == -1) {
|
||||||
lobbyClients.add(client);
|
lobbyClients.add(client);
|
||||||
ClientHandler.broadcastAnnouncementToAll(client.getClientUserName() + " has joined lobby " + this.getLobbyID());
|
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));
|
client.sendAnnouncementToClient("You are already in lobby nr. " + clientIsInLobby(client));
|
||||||
}
|
}
|
||||||
} else {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
@ -195,11 +202,19 @@ public class Lobby {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds game to list of running games and sets its lobby's gameIsRunning to true.
|
||||||
|
*/
|
||||||
public void addGameToRunningGames(Game game) {
|
public void addGameToRunningGames(Game game) {
|
||||||
|
game.getLobby().gameIsRunning = true;
|
||||||
runningGames.add(game);
|
runningGames.add(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes game from list of running games and sets its lobby's gameIsRunning to false.
|
||||||
|
*/
|
||||||
public void removeGameFromRunningGames(Game game) {
|
public void removeGameFromRunningGames(Game game) {
|
||||||
|
game.getLobby().gameIsRunning = false;
|
||||||
runningGames.remove(game);
|
runningGames.remove(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,7 +228,7 @@ public class Lobby {
|
|||||||
*/
|
*/
|
||||||
public void closeLobby() {
|
public void closeLobby() {
|
||||||
lobbies.remove(this);
|
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.
|
Todo: theoretically, this is enough to close a lobby.
|
||||||
|
|||||||
Reference in New Issue
Block a user