Game can now handle a name change and an exit by a client
This commit is contained in:
parent
a0a82f8f8c
commit
44a534bafa
@ -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.client.Client;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Server;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Server;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.IParameterRenderer;
|
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.BudaLogConfig;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
|
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.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.gamelogic.klassenstruktur.Passenger;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@ -72,6 +76,41 @@ public class GameState {
|
|||||||
return train;
|
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];
|
String[] print = new String[6];
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
if (array[i].getKickedOff()) {
|
if (array[i].getKickedOff()) {
|
||||||
print[i] = "-| kicked off: " + array[i].getPosition() + "|-";
|
print[i] = "| kicked off: " + array[i].getPosition() + " |";
|
||||||
} else {
|
} else {
|
||||||
if (array[i].getIsPlayer()) {
|
if (array[i].getIsPlayer()) {
|
||||||
if (array[i].getIsGhost()) {
|
if (array[i].getIsGhost()) {
|
||||||
print[i] = "-| ghostPlayer: " + array[i].getPosition() + "|-";
|
print[i] = "| ghostPlayer: " + array[i].getPosition() + " |";
|
||||||
} else {
|
} else {
|
||||||
print[i] = "-| humanPlayer: " + array[i].getPosition() + " |";
|
print[i] = "-| humanPlayer: " + array[i].getPosition() + " |";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (array[i].getIsGhost()) {
|
if (array[i].getIsGhost()) {
|
||||||
print[i] = "-| ghostNPC: " + array[i].getPosition() + "|-";
|
print[i] = "| ghostNPC: " + array[i].getPosition() + " |";
|
||||||
} else {
|
} else {
|
||||||
print[i] = "-| humanNPC: " + array[i].getPosition() + "|-";
|
print[i] = "| humanNPC: " + array[i].getPosition() + " |";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,7 +169,7 @@ public class GameState {
|
|||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
String[] print = new String[6];
|
String[] print = new String[6];
|
||||||
for (int i = 0; i < array.length; i++) {
|
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++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
|||||||
@ -200,7 +200,7 @@ public class Client {
|
|||||||
}
|
}
|
||||||
Socket socket;
|
Socket socket;
|
||||||
try {
|
try {
|
||||||
socket = new Socket(hostname, 42069);
|
socket = new Socket(hostname, 1873);
|
||||||
Client client = new Client(socket, null);
|
Client client = new Client(socket, null);
|
||||||
client.chatListener();
|
client.chatListener();
|
||||||
Thread cP = new Thread(client.clientPinger);
|
Thread cP = new Thread(client.clientPinger);
|
||||||
|
|||||||
@ -132,6 +132,11 @@ public class ClientHandler implements Runnable {
|
|||||||
String helper = this.getClientUserName();
|
String helper = this.getClientUserName();
|
||||||
this.clientUserName = nameDuplicateChecker.checkName(newName);
|
this.clientUserName = nameDuplicateChecker.checkName(newName);
|
||||||
broadcastAnnouncementToAll(helper + " has changed their nickname to " + clientUserName);
|
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
|
* @param msg the Message to be broadcast
|
||||||
*/
|
*/
|
||||||
@ -307,6 +312,7 @@ public class ClientHandler implements Runnable {
|
|||||||
try {
|
try {
|
||||||
Lobby l = getLobby();
|
Lobby l = getLobby();
|
||||||
Game game = new Game(6,1, l.getLobbyClients().size(), l);
|
Game game = new Game(6,1, l.getLobbyClients().size(), l);
|
||||||
|
l.setGame(game);
|
||||||
Thread t = new Thread(game);
|
Thread t = new Thread(game);
|
||||||
t.start();
|
t.start();
|
||||||
} catch (TrainOverflow e) {
|
} catch (TrainOverflow e) {
|
||||||
@ -388,6 +394,10 @@ public class ClientHandler implements Runnable {
|
|||||||
Lobby l = Lobby.getLobbyFromID(Lobby.clientIsInLobby(this));
|
Lobby l = Lobby.getLobbyFromID(Lobby.clientIsInLobby(this));
|
||||||
if (l != null) {
|
if (l != null) {
|
||||||
l.removePlayer(this);
|
l.removePlayer(this);
|
||||||
|
Game game = l.getGame();
|
||||||
|
if(game != null) {
|
||||||
|
l.getGame().getGameState().handleClientDisconnect(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -450,6 +460,11 @@ public class ClientHandler implements Runnable {
|
|||||||
* Closes the client's socket, in, and out. and removes from global list of clients.
|
* Closes the client's socket, in, and out. and removes from global list of clients.
|
||||||
*/
|
*/
|
||||||
public void disconnectClient() {
|
public void disconnectClient() {
|
||||||
|
Lobby l = getLobby();
|
||||||
|
Game g = l.getGame();
|
||||||
|
if (l != null && g != null) {
|
||||||
|
l.getGame().getGameState().handleClientDisconnect(this);
|
||||||
|
}
|
||||||
socket = this.getSocket();
|
socket = this.getSocket();
|
||||||
in = this.getIn();
|
in = this.getIn();
|
||||||
out = this.getOut();
|
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.BudaLogConfig;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -16,6 +17,8 @@ public class Lobby {
|
|||||||
public static final Logger LOGGER = LogManager.getLogger();
|
public static final Logger LOGGER = LogManager.getLogger();
|
||||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||||
public static HashSet<Lobby> lobbies = new HashSet<>();
|
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;
|
private static final int MAX_NO_OF_CLIENTS = 6;
|
||||||
|
|
||||||
@ -93,6 +96,24 @@ public class Lobby {
|
|||||||
return null;
|
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
|
* Returns the ID of the lobby that the client is in. If the client is not in any
|
||||||
* lobby, it returns -1.
|
* lobby, it returns -1.
|
||||||
|
|||||||
Reference in New Issue
Block a user