/z now lists all games opened and closed

also fixed two bugs in VoteHandler (Array out of Bound in GhostNPC and in ClientVoteData)
This commit is contained in:
Seraina
2022-04-15 18:47:05 +02:00
parent 1ba30663be
commit 7c70b276cd
12 changed files with 105 additions and 48 deletions

View File

@@ -65,6 +65,9 @@ public class MessageFormatter {
case "/p":
stringBuilder.append(Protocol.listPlayersInLobby);
break;
case "/z":
stringBuilder.append(Protocol.listGames);
break;
case "/j":
stringBuilder.append(Protocol.joinLobby + "$");
try {
@@ -93,7 +96,6 @@ public class MessageFormatter {
break;
case "/s":
stringBuilder.append(Protocol.startANewGame);
break;
default:
s = msg;

View File

@@ -122,10 +122,17 @@ public class Protocol {
/**
* A Client decides to start the game.
* A Client decides to start the game. The game is started in the lobby the message came from.
* Only one game can be started per lobby at a time.
*/
public static final String startANewGame = "STGAM";
/**
* Client request to see a list of all games, ongoing and finished.
*/
public static final String listGames = "LISTG";
/**
* Client informs server that they have voted and delivers this vote in the form of "CVOTE$position$vote"
*/

View File

@@ -423,7 +423,7 @@ public class ClientHandler implements Runnable {
*/
public void listLobbies() {
if (Lobby.lobbies.isEmpty()) {
sendAnnouncementToClient("No open Lobbies.");
sendAnnouncementToClient("No Lobbies.");
} else {
for (Lobby l : Lobby.lobbies) {
String lobbyStatus = "closed";
@@ -475,6 +475,32 @@ public class ClientHandler implements Runnable {
}
}
/**
* Lists all Games currenty running and already finished and displays it to the client handled by this
*/
public void listGames() {
if (Lobby.runningGames.isEmpty() && Lobby.finishedGames.isEmpty()) {
sendAnnouncementToClient("No Games");
} else {
sendAnnouncementToClient("Running Games:");
try {
for (Game runningGame : Lobby.runningGames) {
sendAnnouncementToClient(" - " + runningGame.getName() + ", Lobby" + runningGame.getLobby().getLobbyID());
}
} catch (Exception e) {
sendAnnouncementToClient(" - No running Games");
}
sendAnnouncementToClient("Finished Games");
try {
for (Game finishedGame : Lobby.finishedGames) {
sendAnnouncementToClient(" - " + finishedGame.getName() + ", Lobby" + finishedGame.getLobby().getLobbyID());
}
} catch (Exception e) {
sendAnnouncementToClient(" - No finished Games");
}
}
}
/**
* Closes the client's socket, in, and out. and removes from global list of clients.
*/

View File

@@ -111,6 +111,9 @@ public class JServerProtocolParser {
case Protocol.startANewGame:
h.startNewGame();
break;
case Protocol.listGames:
h.listGames();
break;
default:
System.out.println("Received unknown command");
}

View File

@@ -108,13 +108,6 @@ 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