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 static void main(String[] args){
try{
public static void main(String[] args) {
try {
String clientOrServer = args[0];
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);
int port = Integer.parseInt(args[1].substring(args[1].indexOf(":") + 1));
String username = null;

View File

@ -12,16 +12,17 @@ import org.apache.logging.log4j.Logger;
/**
* Handles all communications Server to Client concerning game state or game state related requests
* - Needs a possibility to only send to Ghosts
* - and only humans
* - Needs a possibility to only send to Ghosts - and only humans
*/
public class ServerGameInfoHandler {
public static final Logger LOGGER = LogManager.getLogger(ServerGameInfoHandler.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/**
* Gets a string msg from somewhere and formats it into protocol messages
*
* @param msg the message to be formatted
* @param passenger the passenger getting the message
* @param game the game in wich the passenger lives
@ -30,21 +31,24 @@ public class ServerGameInfoHandler {
public static String format(String msg, Passenger passenger, Game game) {
switch (msg) {
case ClientGameInfoHandler.ghostVoteRequest:
msg = Protocol.serverRequestsGhostVote + "$" + passenger.getPosition() +"$" + game.gameState.toString();
msg = Protocol.serverRequestsGhostVote + "$" + passenger.getPosition() + "$"
+ game.gameState.toString();
break;
case ClientGameInfoHandler.humanVoteRequest:
msg = Protocol.serverRequestsHumanVote + "$" + passenger.getPosition() +"$"+ game.gameState.humanToString();
msg = Protocol.serverRequestsHumanVote + "$" + passenger.getPosition() + "$"
+ game.gameState.humanToString();
break;
default:
msg = Protocol.printToClientConsole + "$"+ msg;
msg = Protocol.printToClientConsole + "$" + msg;
}
LOGGER.debug(msg);
return msg;
}
/**
* //TODO(Seraina): Smart implementation that sends all relevant things to spectator, so they won't get bored
* Formartiert Nachrichten die für einen Spectator gedacht sind.
* //TODO(Seraina): Smart implementation that sends all relevant things to spectator, so they
* won't get bored Formartiert Nachrichten die für einen Spectator gedacht sind.
*
* @param msg the message to be formatted
* @param passenger the passenger getting the message
* @param game the game in wich the passenger lives
@ -61,7 +65,7 @@ public class ServerGameInfoHandler {
msg = Protocol.printToClientConsole + "$Humans are voting:" + game.gameState.toString();
break;
default:
msg = Protocol.printToClientConsole + "$"+ msg;
msg = Protocol.printToClientConsole + "$" + msg;
}
LOGGER.debug(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
*
* @return a String saying that sm heard sm noise
*/
public static String noiseRandomizer() {
@ -77,7 +82,7 @@ public class ServerGameInfoHandler {
String c = "I heard smt strange tonight";
String d = "Me, noise!";
String e = "Uuuuh, spoky sounds";
int number = (int)(Math.random()*5);
int number = (int) (Math.random() * 5);
switch (number) {
case 0:
return a;
@ -94,6 +99,7 @@ public class ServerGameInfoHandler {
/**
* 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 game the game the GhostNpc lives in (in gameState.passengerTrain)
@ -111,6 +117,7 @@ public class ServerGameInfoHandler {
/**
* 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 game the game the HumanNpc lives in (in gameState.passengerTrain)
@ -118,7 +125,8 @@ public class ServerGameInfoHandler {
public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
switch (msg) {
case ClientGameInfoHandler.noiseNotification:
String outMsg = npc.getName() + ": " + noiseRandomizer();;
String outMsg = npc.getName() + ": " + noiseRandomizer();
;
game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg);
break;
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);
}
@ -105,6 +105,7 @@ public class Client {
/**
* Tells user to enter a position to vote for passenger at that position
*
* @param msg the message containing the position
*/
public void positionSetter(String msg) {

View File

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

View File

@ -11,8 +11,8 @@ public class MessageFormatter {
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/**
* Takes a given client input and reformats it to where the JServerProtocolParser.parse() method can
* handle it (see Protocol.java). May need to be redesigned once the game uses a GUI.
* Takes a given client input and reformats it to where the JServerProtocolParser.parse() method
* 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 position the position the client is in

View File

@ -6,6 +6,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ClientModel {
public static final Logger LOGGER = LogManager.getLogger(ClientModel.class);
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.Logger;
public class GUI implements Runnable{
public class GUI implements Runnable {
public static final Logger LOGGER = LogManager.getLogger(GUI.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
ChatApp chatApp;
private String name;
public GUI (ChatApp chatApp) {
public GUI(ChatApp chatApp) {
this.chatApp = chatApp;
}

View File

@ -14,6 +14,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ChatApp extends Application {
public static final Logger LOGGER = LogManager.getLogger(ChatApp.class);
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);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Label l = new Label(client.getUsername() + " (you): " + msg);
if (cmd.contains(whisper)) {
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
l.setBackground(Background.fill(Color.LAVENDER));
}
vBoxChatMessages.getChildren().add(l);
chatMsgField.clear();
} 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
* null.
*
* @return the Lobby this clientHandler lives in
*/
public Lobby getLobby() {
@ -176,7 +177,7 @@ public class ClientHandler implements Runnable {
Lobby l = getLobby();
if (l != null) {
for (ClientHandler client : l.getLobbyClients()) {
if(client.getClientUserName().equals(this.getClientUserName())){
if (client.getClientUserName().equals(this.getClientUserName())) {
continue;
}
client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg);
@ -184,7 +185,7 @@ public class ClientHandler implements Runnable {
} else {
//send msg to all clients who are not in a lobby.
for (ClientHandler client : connectedClients) {
if(client.getClientUserName().equals(this.getClientUserName())){
if (client.getClientUserName().equals(this.getClientUserName())) {
continue;
}
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
* the form "Username: @msg"
* Broadcasts a chat Message to all clients across all lobbies and clients who are not in a lobby
* in the form "Username: @msg"
*
* @param msg the Message to be broadcast
*/
public void broadcastChatMessageToAll(String msg) {
for (ClientHandler client : connectedClients) {
if(client.getClientUserName().equals(this.getClientUserName())){
if (client.getClientUserName().equals(this.getClientUserName())) {
continue;
}
client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg);
@ -272,7 +273,10 @@ public class ClientHandler implements Runnable {
public void whisper(String msg, ClientHandler target) {
target.sendMsgToClient(
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() + ": "
+ msg);
}
@ -375,6 +379,7 @@ public class ClientHandler implements Runnable {
* 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
* private non-chat messages (e.g. "You are now a ghost").
*
* @param msg the message being announced
*/
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
try {
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 + ".");
}
} catch (IndexOutOfBoundsException e) {

View File

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

View File

@ -11,6 +11,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Server {
public static final Logger LOGGER = LogManager.getLogger(Server.class);
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;
/**
* This class is responsible for checking names for duplicates and assigning suffixes in case
* of duplicate names.
* This class is responsible for checking names for duplicates and assigning suffixes in case of
* duplicate names.
*/
public class nameDuplicateChecker {
static final String[] suffixes = new String[] {
static final String[] suffixes = new String[]{
" from London",
" of Prussia",
" of Zagreb",
@ -48,19 +49,22 @@ public class nameDuplicateChecker {
}
/**
* Adjusts the name to avoid conflicts and returns it as a String. Namely:
* If that name is already used by some other ClientHandler, it returns the name with some suffix.
* Also, any ":" or "$" are removed, so they can be used for whisper chat.
* Also, if the name is empty, it assigns a default value ("U.N. Owen").
* Adjusts the name to avoid conflicts and returns it as a String. Namely: If that name is already
* used by some other ClientHandler, it returns the name with some suffix. Also, any ":" or "$"
* are removed, so they can be used for whisper chat. Also, if the name is empty, it assigns a
* default value ("U.N. Owen").
*
* @param name the name that is checked for
* @return returns either just the name or added some suffix
*/
public static String checkName(String name) {
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
tempname = tempname.replace("$","");
tempname = tempname.replace(":","");
if (tempname.equalsIgnoreCase("")) {tempname = "U.N. Owen";}
tempname = tempname.replace("$", "");
tempname = tempname.replace(":", "");
if (tempname.equalsIgnoreCase("")) {
tempname = "U.N. Owen";
}
String rtrn = tempname;
while (isTaken(rtrn)) { //todo: handle the (very unlikely) case that all names are taken.
rtrn = extendName(tempname);