Merge remote-tracking branch 'origin/master'

# Conflicts:
#	build.gradle
This commit is contained in:
Seraina 2022-04-18 16:25:37 +02:00
commit c3aef4dd54
16 changed files with 109 additions and 116 deletions

View File

@ -7,11 +7,11 @@ import java.net.InetAddress;
public class NightTrainToBudapest { public class NightTrainToBudapest {
public static void main(String[] args){ public static void main(String[] args) {
try{ try {
String clientOrServer = args[0]; String clientOrServer = args[0];
if (clientOrServer.equalsIgnoreCase("client")) { if (clientOrServer.equalsIgnoreCase("client")) {
String addrString = args[1].substring(0,args[1].indexOf(":")); String addrString = args[1].substring(0, args[1].indexOf(":"));
InetAddress addr = InetAddress.getByName(addrString); InetAddress addr = InetAddress.getByName(addrString);
int port = Integer.parseInt(args[1].substring(args[1].indexOf(":") + 1)); int port = Integer.parseInt(args[1].substring(args[1].indexOf(":") + 1));
String username = null; String username = null;

View File

@ -12,42 +12,46 @@ import org.apache.logging.log4j.Logger;
/** /**
* Handles all communications Server to Client concerning game state or game state related requests * Handles all communications Server to Client concerning game state or game state related requests
* - Needs a possibility to only send to Ghosts * - Needs a possibility to only send to Ghosts - and only humans
* - and only humans
*/ */
public class ServerGameInfoHandler { public class ServerGameInfoHandler {
public static final Logger LOGGER = LogManager.getLogger(ServerGameInfoHandler.class); public static final Logger LOGGER = LogManager.getLogger(ServerGameInfoHandler.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/** /**
* Gets a string msg from somewhere and formats it into protocol messages * Gets a string msg from somewhere and formats it into protocol messages
* @param msg the message to be formatted *
* @param msg the message to be formatted
* @param passenger the passenger getting the message * @param passenger the passenger getting the message
* @param game the game in wich the passenger lives * @param game the game in wich the passenger lives
* @return a message in a protocol format * @return a message in a protocol format
*/ */
public static String format(String msg, Passenger passenger, Game game) { public static String format(String msg, Passenger passenger, Game game) {
switch (msg) { switch (msg) {
case ClientGameInfoHandler.ghostVoteRequest: case ClientGameInfoHandler.ghostVoteRequest:
msg = Protocol.serverRequestsGhostVote + "$" + passenger.getPosition() +"$" + game.gameState.toString(); msg = Protocol.serverRequestsGhostVote + "$" + passenger.getPosition() + "$"
+ game.gameState.toString();
break; break;
case ClientGameInfoHandler.humanVoteRequest: case ClientGameInfoHandler.humanVoteRequest:
msg = Protocol.serverRequestsHumanVote + "$" + passenger.getPosition() +"$"+ game.gameState.humanToString(); msg = Protocol.serverRequestsHumanVote + "$" + passenger.getPosition() + "$"
+ game.gameState.humanToString();
break; break;
default: default:
msg = Protocol.printToClientConsole + "$"+ msg; msg = Protocol.printToClientConsole + "$" + msg;
} }
LOGGER.debug(msg); LOGGER.debug(msg);
return msg; return msg;
} }
/** /**
* //TODO(Seraina): Smart implementation that sends all relevant things to spectator, so they won't get bored * //TODO(Seraina): Smart implementation that sends all relevant things to spectator, so they
* Formartiert Nachrichten die für einen Spectator gedacht sind. * won't get bored Formartiert Nachrichten die für einen Spectator gedacht sind.
* @param msg the message to be formatted *
* @param msg the message to be formatted
* @param passenger the passenger getting the message * @param passenger the passenger getting the message
* @param game the game in wich the passenger lives * @param game the game in wich the passenger lives
* @return a message in a protocol format * @return a message in a protocol format
*/ */
public static String spectatorFormat(String msg, Passenger passenger, Game game) { public static String spectatorFormat(String msg, Passenger passenger, Game game) {
@ -61,7 +65,7 @@ public class ServerGameInfoHandler {
msg = Protocol.printToClientConsole + "$Humans are voting:" + game.gameState.toString(); msg = Protocol.printToClientConsole + "$Humans are voting:" + game.gameState.toString();
break; break;
default: default:
msg = Protocol.printToClientConsole + "$"+ msg; msg = Protocol.printToClientConsole + "$" + msg;
} }
LOGGER.debug(msg); LOGGER.debug(msg);
return msg; return msg;
@ -69,6 +73,7 @@ public class ServerGameInfoHandler {
/** /**
* Chooses for an NPC what they want to say, so they don't sound the same all the time * Chooses for an NPC what they want to say, so they don't sound the same all the time
*
* @return a String saying that sm heard sm noise * @return a String saying that sm heard sm noise
*/ */
public static String noiseRandomizer() { public static String noiseRandomizer() {
@ -77,7 +82,7 @@ public class ServerGameInfoHandler {
String c = "I heard smt strange tonight"; String c = "I heard smt strange tonight";
String d = "Me, noise!"; String d = "Me, noise!";
String e = "Uuuuh, spoky sounds"; String e = "Uuuuh, spoky sounds";
int number = (int)(Math.random()*5); int number = (int) (Math.random() * 5);
switch (number) { switch (number) {
case 0: case 0:
return a; return a;
@ -94,8 +99,9 @@ public class ServerGameInfoHandler {
/** /**
* decides which action an GhostNpc needs to take, based on a message * decides which action an GhostNpc needs to take, based on a message
* @param npc the GhostNpc needing to do smt *
* @param msg the msg containing the information on what to do * @param npc the GhostNpc needing to do smt
* @param msg the msg containing the information on what to do
* @param game the game the GhostNpc lives in (in gameState.passengerTrain) * @param game the game the GhostNpc lives in (in gameState.passengerTrain)
*/ */
public static void ghostNpcParser(GhostNPC npc, String msg, Game game) { public static void ghostNpcParser(GhostNPC npc, String msg, Game game) {
@ -111,14 +117,16 @@ public class ServerGameInfoHandler {
/** /**
* decides which action an HumanNpc needs to take, based on a message * decides which action an HumanNpc needs to take, based on a message
* @param npc the HumanNpc needing to do smt *
* @param msg the msg containing the information on what to do * @param npc the HumanNpc needing to do smt
* @param msg the msg containing the information on what to do
* @param game the game the HumanNpc lives in (in gameState.passengerTrain) * @param game the game the HumanNpc lives in (in gameState.passengerTrain)
*/ */
public static void humanNpcParser(HumanNPC npc, String msg, Game game) { public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
switch (msg) { switch (msg) {
case ClientGameInfoHandler.noiseNotification: case ClientGameInfoHandler.noiseNotification:
String outMsg = npc.getName() + ": " + noiseRandomizer();; String outMsg = npc.getName() + ": " + noiseRandomizer();
;
game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg); game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg);
break; break;
case ClientGameInfoHandler.humanVoteRequest: case ClientGameInfoHandler.humanVoteRequest:

View File

@ -71,7 +71,7 @@ public class Client {
} }
public void changeUsername (String newName) { public void changeUsername(String newName) {
ChatController.getClient().setUsername(newName); ChatController.getClient().setUsername(newName);
} }
@ -105,6 +105,7 @@ public class Client {
/** /**
* Tells user to enter a position to vote for passenger at that position * Tells user to enter a position to vote for passenger at that position
*
* @param msg the message containing the position * @param msg the message containing the position
*/ */
public void positionSetter(String msg) { public void positionSetter(String msg) {

View File

@ -7,15 +7,15 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
public class JClientProtocolParser { public class JClientProtocolParser {
public static final Logger LOGGER = LogManager.getLogger(JClientProtocolParser.class); public static final Logger LOGGER = LogManager.getLogger(JClientProtocolParser.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/** /**
* Used by the client to parse an incoming protocol message. * Used by the client to parse an incoming protocol message. For documentation on the individual
* For documentation on the individual Protocol messages, view the Protocol.java * Protocol messages, view the Protocol.java class or hover over the commands (e.g.
* class or hover over the commands (e.g. Protocol.chatMsgToAll) with your mouse * Protocol.chatMsgToAll) with your mouse in this class.
* in this class.
* *
* @param msg the encoded message that needs to be parsed * @param msg the encoded message that needs to be parsed
* @param c this Client(required so this method can access the Client's methods) * @param c this Client(required so this method can access the Client's methods)

View File

@ -11,10 +11,10 @@ public class MessageFormatter {
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/** /**
* Takes a given client input and reformats it to where the JServerProtocolParser.parse() method can * Takes a given client input and reformats it to where the JServerProtocolParser.parse() method
* handle it (see Protocol.java). May need to be redesigned once the game uses a GUI. * can handle it (see Protocol.java). May need to be redesigned once the game uses a GUI.
* *
* @param msg the Messaged to be reformatted * @param msg the Messaged to be reformatted
* @param position the position the client is in * @param position the position the client is in
* @return the reformatted message in the form HEADR$msg * @return the reformatted message in the form HEADR$msg
*/ */

View File

@ -6,6 +6,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
public class ClientModel { public class ClientModel {
public static final Logger LOGGER = LogManager.getLogger(ClientModel.class); public static final Logger LOGGER = LogManager.getLogger(ClientModel.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);

View File

@ -1,11 +0,0 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
public class CmdProperty extends SimpleBooleanProperty {
private String cmd;
//TODO private void updateCmd();
}

View File

@ -6,13 +6,15 @@ import javafx.application.Application;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
public class GUI implements Runnable{ public class GUI implements Runnable {
public static final Logger LOGGER = LogManager.getLogger(GUI.class); public static final Logger LOGGER = LogManager.getLogger(GUI.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
ChatApp chatApp; ChatApp chatApp;
private String name; private String name;
public GUI (ChatApp chatApp) {
public GUI(ChatApp chatApp) {
this.chatApp = chatApp; this.chatApp = chatApp;
} }

View File

@ -14,6 +14,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
public class ChatApp extends Application { public class ChatApp extends Application {
public static final Logger LOGGER = LogManager.getLogger(ChatApp.class); public static final Logger LOGGER = LogManager.getLogger(ChatApp.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);

View File

@ -112,7 +112,11 @@ public class ChatController implements Initializable {
client.getClient().sendMsgToServer(cmd.toString() + msg); client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg); LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Label l = new Label(client.getUsername() + " (you): " + msg); Label l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER)); if (cmd.contains(whisper)) {
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
l.setBackground(Background.fill(Color.LAVENDER));
}
vBoxChatMessages.getChildren().add(l); vBoxChatMessages.getChildren().add(l);
chatMsgField.clear(); chatMsgField.clear();
} else { } else {

View File

@ -1,31 +0,0 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat;
import java.io.BufferedReader;
import java.io.BufferedWriter;
/**
* To get the chat messages to the chat window.
*/
public class ChatIncomingThread implements Runnable {
BufferedReader inputReader;
ChatApp chatApp;
public ChatIncomingThread(BufferedReader inputReader, ChatApp chatApp) {
this.inputReader = inputReader;
this.chatApp = chatApp;
}
/**
* When an object implementing interface {@code Runnable} is used to create a thread, starting the
* thread causes the object's {@code run} method to be called in that separately executing
* thread.
* <p>
* The general contract of the method {@code run} is that it may take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
}
}

View File

@ -156,6 +156,7 @@ public class ClientHandler implements Runnable {
/** /**
* Returns the Lobby this ClientHandler is in. If this ClientHandler is not in a Lobby, it returns * Returns the Lobby this ClientHandler is in. If this ClientHandler is not in a Lobby, it returns
* null. * null.
*
* @return the Lobby this clientHandler lives in * @return the Lobby this clientHandler lives in
*/ */
public Lobby getLobby() { public Lobby getLobby() {
@ -176,7 +177,7 @@ public class ClientHandler implements Runnable {
Lobby l = getLobby(); Lobby l = getLobby();
if (l != null) { if (l != null) {
for (ClientHandler client : l.getLobbyClients()) { for (ClientHandler client : l.getLobbyClients()) {
if(client.getClientUserName().equals(this.getClientUserName())){ if (client.getClientUserName().equals(this.getClientUserName())) {
continue; continue;
} }
client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg); client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg);
@ -184,7 +185,7 @@ public class ClientHandler implements Runnable {
} else { } else {
//send msg to all clients who are not in a lobby. //send msg to all clients who are not in a lobby.
for (ClientHandler client : connectedClients) { for (ClientHandler client : connectedClients) {
if(client.getClientUserName().equals(this.getClientUserName())){ if (client.getClientUserName().equals(this.getClientUserName())) {
continue; continue;
} }
if (Lobby.clientIsInLobby(client) == -1) { if (Lobby.clientIsInLobby(client) == -1) {
@ -209,14 +210,14 @@ public class ClientHandler implements Runnable {
} }
/** /**
* Broadcasts a chat Message to all clients across all lobbies and clients who are not in a lobby in * Broadcasts a chat Message to all clients across all lobbies and clients who are not in a lobby
* the form "Username: @msg" * in the form "Username: @msg"
* *
* @param msg the Message to be broadcast * @param msg the Message to be broadcast
*/ */
public void broadcastChatMessageToAll(String msg) { public void broadcastChatMessageToAll(String msg) {
for (ClientHandler client : connectedClients) { for (ClientHandler client : connectedClients) {
if(client.getClientUserName().equals(this.getClientUserName())){ if (client.getClientUserName().equals(this.getClientUserName())) {
continue; continue;
} }
client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg); client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg);
@ -267,12 +268,15 @@ public class ClientHandler implements Runnable {
* sent the message that it has been sent. Syntax: * sent the message that it has been sent. Syntax:
* *
* @param target MUST NOT BE NULL! * @param target MUST NOT BE NULL!
* @param msg the message being whisperd * @param msg the message being whisperd
*/ */
public void whisper(String msg, ClientHandler target) { public void whisper(String msg, ClientHandler target) {
target.sendMsgToClient( target.sendMsgToClient(
Protocol.printToClientChat + "$" + this.getClientUserName() + " whispers: " + msg); Protocol.printToClientChat + "$" + this.getClientUserName() + " whispers: " + msg);
sendMsgToClient( /*sendMsgToClient(
Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": "
+ msg);*/ // no confirmation messge needed. will be colorcoded in gui
LOGGER.info(
Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": " Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": "
+ msg); + msg);
} }
@ -375,6 +379,7 @@ public class ClientHandler implements Runnable {
* Sends an announcement to just this client. Essentially the same as broadcastAnnouncementToAll * Sends an announcement to just this client. Essentially the same as broadcastAnnouncementToAll
* except it only sends an announcement to just this client instead of everyone. Can be used for * except it only sends an announcement to just this client instead of everyone. Can be used for
* private non-chat messages (e.g. "You are now a ghost"). * private non-chat messages (e.g. "You are now a ghost").
*
* @param msg the message being announced * @param msg the message being announced
*/ */
public void sendAnnouncementToClient(String msg) { public void sendAnnouncementToClient(String msg) {

View File

@ -30,7 +30,8 @@ public class JServerProtocolParser {
String header = ""; //"header" is the first 5 characters, i.e. the protocol part String header = ""; //"header" is the first 5 characters, i.e. the protocol part
try { try {
header = msg.substring(0, 5); header = msg.substring(0, 5);
if(!header.equals(Protocol.pingBack) &&!header.equals(Protocol.pingFromClient)) { //for debuging without constant pings if (!header.equals(Protocol.pingBack) && !header.equals(
Protocol.pingFromClient)) { //for debuging without constant pings
LOGGER.debug("got message: " + msg + "."); LOGGER.debug("got message: " + msg + ".");
} }
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {

View File

@ -26,10 +26,9 @@ public class Lobby {
/** /**
* true by default * true by default true if game has not started yet, false if game has. If true, potential players
* true if game has not started yet, false if game has. If true, potential players can still join the game. * can still join the game. Should be set to true again, after a game is finished. Games can only
* Should be set to true again, after a game is finished. * be started if Lobby is open.
* Games can only be started if Lobby is open.
*/ */
private boolean lobbyIsOpen = true; private boolean lobbyIsOpen = true;
@ -40,8 +39,8 @@ public class Lobby {
/** /**
* The Person who created the game and can configure it and decide to start once enough lobbyClients * The Person who created the game and can configure it and decide to start once enough
* have entered the lobby. * lobbyClients have entered the lobby.
*/ */
private final ClientHandler admin; private final ClientHandler admin;
@ -54,8 +53,8 @@ public class Lobby {
/** /**
* Constructor. Sets the admin to who created the lobby. Adds the admin to the list of lobbyClients. * Constructor. Sets the admin to who created the lobby. Adds the admin to the list of
* Increases the number of lobbyClients from 0 to 1. * lobbyClients. Increases the number of lobbyClients from 0 to 1.
* *
* @param admin the Client who called CRTGM * @param admin the Client who called CRTGM
*/ */
@ -83,6 +82,7 @@ public class Lobby {
/** /**
* getter for the lobby ID * getter for the lobby ID
*
* @return lobbyID as set in constructor. * @return lobbyID as set in constructor.
*/ */
public int getLobbyID() { public int getLobbyID() {
@ -99,14 +99,14 @@ public class Lobby {
} }
/** /**
* Returns the lobby with the desired LobbyID. * Returns the lobby with the desired LobbyID. For example, getLobbyFromID(5) returns the lobby
* For example, getLobbyFromID(5) returns the lobby whose LobbyID is 5. * whose LobbyID is 5. If no such lobby exists, it returns null.
* If no such lobby exists, it returns null. *
* @param i the Lobby ID you are looking for * @param i the Lobby ID you are looking for
* @return the Lobby with i as its ID * @return the Lobby with i as its ID
*/ */
public static Lobby getLobbyFromID(int i) { public static Lobby getLobbyFromID(int i) {
for (Lobby l: lobbies) { for (Lobby l : lobbies) {
if (l.getLobbyID() == i) { if (l.getLobbyID() == i) {
return l; return l;
} }
@ -125,7 +125,7 @@ public class Lobby {
} }
public boolean getLobbyIsOpen() { public boolean getLobbyIsOpen() {
if (lobbyClients.size() >= MAX_NO_OF_CLIENTS || gameIsRunning ) { if (lobbyClients.size() >= MAX_NO_OF_CLIENTS || gameIsRunning) {
setLobbyIsOpen(false); setLobbyIsOpen(false);
} else { } else {
setLobbyIsOpen(true); setLobbyIsOpen(true);
@ -148,14 +148,15 @@ public class Lobby {
/** /**
* 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
* lobby, it returns -1. * returns -1.
*
* @param h ClientHandler that the corresponding Lobby is searched for * @param h ClientHandler that the corresponding Lobby is searched for
* @return the Lobby ID * @return the Lobby ID
*/ */
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()) {
if (h.equals(clientHandler)) { if (h.equals(clientHandler)) {
return l.getLobbyID(); return l.getLobbyID();
} }
@ -167,6 +168,7 @@ public class Lobby {
/** /**
* Adds a player to the lobby. Returns true if successful. * Adds a player to the lobby. Returns true if successful.
* TODO: add an appropriate response. Currently hardcoded. * TODO: add an appropriate response. Currently hardcoded.
*
* @param client who wants to join the lobby. * @param client who wants to join the lobby.
* @return true if successful * @return true if successful
*/ */
@ -174,7 +176,8 @@ public class Lobby {
if (getLobbyIsOpen()) { if (getLobbyIsOpen()) {
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());
//LOGGER.debug(client.getClientUserName() + " has been added to Lobby with ID: " + lobbyID //LOGGER.debug(client.getClientUserName() + " has been added to Lobby with ID: " + lobbyID
// + ". Current number of lobbyClients in this lobby: " + lobbyClients.size()); // + ". Current number of lobbyClients in this lobby: " + lobbyClients.size());
return true; return true;
@ -182,7 +185,8 @@ 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 closed. 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;
} }
@ -198,10 +202,12 @@ public class Lobby {
public synchronized boolean removePlayer(ClientHandler player) { public synchronized boolean removePlayer(ClientHandler player) {
//if the player who leaves the lobby is the admin, the lobby is closed. //if the player who leaves the lobby is the admin, the lobby is closed.
if (player.equals(getAdmin())) { if (player.equals(getAdmin())) {
ClientHandler.broadcastAnnouncementToAll(player.getClientUserName() + " has closed lobby nr. " + this.getLobbyID()); ClientHandler.broadcastAnnouncementToAll(
player.getClientUserName() + " has closed lobby nr. " + this.getLobbyID());
closeLobby(); closeLobby();
} else if (this.getLobbyClients().remove(player)){ } else if (this.getLobbyClients().remove(player)) {
ClientHandler.broadcastAnnouncementToAll(player.getClientUserName() + " has left lobby nr. " + this.getLobbyID()); ClientHandler.broadcastAnnouncementToAll(
player.getClientUserName() + " has left lobby nr. " + this.getLobbyID());
return true; return true;
} }
return false; return false;
@ -209,6 +215,7 @@ public class Lobby {
/** /**
* Adds game to list of running games and sets its lobby's gameIsRunning to true. * Adds game to list of running games and sets its lobby's gameIsRunning to true.
*
* @param game the game to be added * @param game the game to be added
*/ */
public void addGameToRunningGames(Game game) { public void addGameToRunningGames(Game game) {
@ -218,6 +225,7 @@ public class Lobby {
/** /**
* Removes game from list of running games and sets its lobby's gameIsRunning to false. * Removes game from list of running games and sets its lobby's gameIsRunning to false.
*
* @param game the game to be removed * @param game the game to be removed
*/ */
public void removeGameFromRunningGames(Game game) { public void removeGameFromRunningGames(Game game) {
@ -225,13 +233,12 @@ public class Lobby {
runningGames.remove(game); runningGames.remove(game);
} }
public void addGameToFinishedGames(Game game) { public void addGameToFinishedGames(Game game) {
finishedGames.add(game); finishedGames.add(game);
} }
/** /**
* Closes the lobby. * Closes the lobby.
*
*/ */
public void closeLobby() { public void closeLobby() {
lobbies.remove(this); lobbies.remove(this);

View File

@ -11,6 +11,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
public class Server { public class Server {
public static final Logger LOGGER = LogManager.getLogger(Server.class); public static final Logger LOGGER = LogManager.getLogger(Server.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);

View File

@ -3,11 +3,12 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import java.util.Random; import java.util.Random;
/** /**
* This class is responsible for checking names for duplicates and assigning suffixes in case * This class is responsible for checking names for duplicates and assigning suffixes in case of
* of duplicate names. * duplicate names.
*/ */
public class nameDuplicateChecker { public class nameDuplicateChecker {
static final String[] suffixes = new String[] {
static final String[] suffixes = new String[]{
" from London", " from London",
" of Prussia", " of Prussia",
" of Zagreb", " of Zagreb",
@ -48,19 +49,22 @@ public class nameDuplicateChecker {
} }
/** /**
* Adjusts the name to avoid conflicts and returns it as a String. Namely: * Adjusts the name to avoid conflicts and returns it as a String. Namely: If that name is already
* If that name is already used by some other ClientHandler, it returns the name with some suffix. * used by some other ClientHandler, it returns the name with some suffix. Also, any ":" or "$"
* Also, any ":" or "$" are removed, so they can be used for whisper chat. * are removed, so they can be used for whisper chat. Also, if the name is empty, it assigns a
* Also, if the name is empty, it assigns a default value ("U.N. Owen"). * default value ("U.N. Owen").
*
* @param name the name that is checked for * @param name the name that is checked for
* @return returns either just the name or added some suffix * @return returns either just the name or added some suffix
*/ */
public static String checkName(String name) { public static String checkName(String name) {
String tempname = name; //if this line is used, only duplicate names get a suffix. String tempname = name; //if this line is used, only duplicate names get a suffix.
//String tempname = extendName(name); //if this line is used, all clients get a suffix //String tempname = extendName(name); //if this line is used, all clients get a suffix
tempname = tempname.replace("$",""); tempname = tempname.replace("$", "");
tempname = tempname.replace(":",""); tempname = tempname.replace(":", "");
if (tempname.equalsIgnoreCase("")) {tempname = "U.N. Owen";} if (tempname.equalsIgnoreCase("")) {
tempname = "U.N. Owen";
}
String rtrn = tempname; String rtrn = tempname;
while (isTaken(rtrn)) { //todo: handle the (very unlikely) case that all names are taken. while (isTaken(rtrn)) { //todo: handle the (very unlikely) case that all names are taken.
rtrn = extendName(tempname); rtrn = extendName(tempname);