Merge remote-tracking branch 'origin/master'

This commit is contained in:
Sebastian Lenzlinger
2022-04-14 16:26:07 +02:00
4 changed files with 86 additions and 7 deletions

View File

@@ -132,6 +132,11 @@ public class ClientHandler implements Runnable {
String helper = this.getClientUserName();
this.clientUserName = nameDuplicateChecker.checkName(newName);
broadcastAnnouncementToAll(helper + " has changed their nickname to " + clientUserName);
try {
getLobby().getGame().getGameState().changeUsername(helper,newName);
} catch (NullPointerException e) {
LOGGER.warn("No game has been started yet in this lobby");
}
}
/**
@@ -186,7 +191,7 @@ public class ClientHandler implements Runnable {
}
/**
* Broadcasts a pseudo chat Message from a NPC to all active clients
* Broadcasts a pseudo chat Message from a NPC to all active clients in the corresponding lobby
*
* @param msg the Message to be broadcast
*/
@@ -307,10 +312,13 @@ public class ClientHandler implements Runnable {
try {
Lobby l = getLobby();
Game game = new Game(6,1, l.getLobbyClients().size(), l);
l.setGame(game);
Thread t = new Thread(game);
t.start();
} catch (TrainOverflow e) {
LOGGER.warn(e.getMessage());
} catch (NullPointerException e) {
LOGGER.warn("Client is not in a lobby");
}
}
@@ -386,6 +394,10 @@ public class ClientHandler implements Runnable {
Lobby l = Lobby.getLobbyFromID(Lobby.clientIsInLobby(this));
if (l != null) {
l.removePlayer(this);
Game game = l.getGame();
if(game != null) {
l.getGame().getGameState().handleClientDisconnect(this);
}
}
}
@@ -448,6 +460,12 @@ public class ClientHandler implements Runnable {
* Closes the client's socket, in, and out. and removes from global list of clients.
*/
public void disconnectClient() {
Lobby l = getLobby();
if (l != null) {
Game g = l.getGame();
if(g != null)
l.getGame().getGameState().handleClientDisconnect(this);
}
socket = this.getSocket();
in = this.getIn();
out = this.getOut();

View File

@@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import java.util.HashSet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -16,6 +17,8 @@ public class Lobby {
public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
public static HashSet<Lobby> lobbies = new HashSet<>();
public Game game;
public boolean gameIsOngoing; //TODO(Seraina): getter and setter
private static final int MAX_NO_OF_CLIENTS = 6;
@@ -93,6 +96,24 @@ public class Lobby {
return null;
}
/**
* Returns the game that the clients in this lobby are in
*
* @return the game associated with this lobby
*/
public Game getGame() {
return game;
}
/**
* Sets the game of this lobby to a certain game
*
* @param game The game to be set as this lobbys game
*/
public void setGame(Game game) {
this.game = game;
}
/**
* Returns the ID of the lobby that the client is in. If the client is not in any
* lobby, it returns -1.