Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
85fca301ff
@ -3,6 +3,7 @@ package ch.unibas.dmi.dbis.cs108;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Server;
|
||||
import java.net.InetAddress;
|
||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.IParameterRenderer;
|
||||
|
||||
@ -2,9 +2,13 @@ package ch.unibas.dmi.dbis.cs108.gamelogic;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
|
||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostNPC;
|
||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Human;
|
||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanNPC;
|
||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@ -72,6 +76,41 @@ public class GameState {
|
||||
return train;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public void changeUsername(String oldName, String newName){
|
||||
for (Passenger passenger : passengerTrain) {
|
||||
String name = passenger.getName();
|
||||
if (name.equals(oldName)) {
|
||||
passenger.setName(newName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void handleClientDisconnect(ClientHandler disconnectedClient) {
|
||||
for(Passenger passenger : passengerTrain) {
|
||||
if (passenger.getIsPlayer()) {
|
||||
if (passenger.getClientHandler().equals(disconnectedClient)) {
|
||||
String name = passenger.getName() + "-NPC";
|
||||
if (passenger.getIsGhost()) {
|
||||
GhostNPC ghostNPC = new GhostNPC(passenger.getPosition(),name, passenger.getIsOG());
|
||||
addNewPassenger(ghostNPC, passenger.getPosition());
|
||||
} else { //is a human
|
||||
HumanNPC humanNPC = new HumanNPC(passenger.getPosition(),name);
|
||||
addNewPassenger(humanNPC, passenger.getPosition());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -95,19 +134,19 @@ public class GameState {
|
||||
String[] print = new String[6];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i].getKickedOff()) {
|
||||
print[i] = "-| kicked off: " + array[i].getPosition() + "|-";
|
||||
print[i] = "| kicked off: " + array[i].getPosition() + " |";
|
||||
} else {
|
||||
if (array[i].getIsPlayer()) {
|
||||
if (array[i].getIsGhost()) {
|
||||
print[i] = "-| ghostPlayer: " + array[i].getPosition() + "|-";
|
||||
print[i] = "| ghostPlayer: " + array[i].getPosition() + " |";
|
||||
} else {
|
||||
print[i] = "-| humanPlayer: " + array[i].getPosition() + "|";
|
||||
print[i] = "-| humanPlayer: " + array[i].getPosition() + " |";
|
||||
}
|
||||
} else {
|
||||
if (array[i].getIsGhost()) {
|
||||
print[i] = "-| ghostNPC: " + array[i].getPosition() + "|-";
|
||||
print[i] = "| ghostNPC: " + array[i].getPosition() + " |";
|
||||
} else {
|
||||
print[i] = "-| humanNPC: " + array[i].getPosition() + "|-";
|
||||
print[i] = "| humanNPC: " + array[i].getPosition() + " |";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -130,7 +169,7 @@ public class GameState {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String[] print = new String[6];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
print[i] = "-| " + array[i].getName() + ": " + array[i].getPosition() + "|-";
|
||||
print[i] = "| " + array[i].getName() + ": " + array[i].getPosition() + " |";
|
||||
}
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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.
|
||||
|
||||
Reference in New Issue
Block a user