Fixed some bugs with the noise handeling, seems not work now

Still need to handle connection loss and name-change in gamelogic
This commit is contained in:
Seraina 2022-04-13 12:57:49 +02:00
parent fc250da14b
commit bc136d7b68
6 changed files with 60 additions and 27 deletions

View File

@ -17,7 +17,7 @@ public class ClientGameInfoHandler {
//relevant: //relevant:
public static final String ghostVoteRequest = "Vote on who to ghostify!"; public static final String ghostVoteRequest = "Vote on who to ghostify!";
public static final String humanVoteRequest = "Vote for a ghost to kick off!"; public static final String humanVoteRequest = "Vote for a ghost to kick off!";
public static final String noiseNotification = "noise"; public static final String noiseNotification = "You heard some noise";
public static final String gameOverHumansWin = "Game over: humans win!"; public static final String gameOverHumansWin = "Game over: humans win!";
public static final String gameOverGhostsWin = "Game over: ghosts win!"; public static final String gameOverGhostsWin = "Game over: ghosts win!";

View File

@ -95,19 +95,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 +130,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++) {

View File

@ -41,6 +41,31 @@ public class ServerGameInfoHandler {
return msg; return msg;
} }
/**
* 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() {
String a = "I heard some noise tonight";
String b = "noise";
String c = "I heard smt strange tonight";
String d = "Me, noise!";
String e = "Uuuuh, spoky noises";
int number = (int)(Math.random()*4);
switch (number) {
case 0:
return a;
case 1:
return d;
case 2:
return c;
case 3:
return e;
default:
return b;
}
}
/** /**
* 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 npc the GhostNpc needing to do smt
@ -50,14 +75,12 @@ public class ServerGameInfoHandler {
public static void ghostNpcParser(GhostNPC npc, String msg, Game game) { public static void ghostNpcParser(GhostNPC npc, String msg, Game game) {
switch (msg) { switch (msg) {
case ClientGameInfoHandler.noiseNotification: case ClientGameInfoHandler.noiseNotification:
//TODO(Seraina & Alex): noise handling String outMsg = npc.getName() + ": " + noiseRandomizer();
game.getClientHandler().broadcastChatMessage(ClientGameInfoHandler.noiseNotification); game.getClientHandler().broadcastNpcChatMessage(outMsg);
break; break;
case ClientGameInfoHandler.ghostVoteRequest: case ClientGameInfoHandler.ghostVoteRequest:
npc.vote(game); npc.vote(game);
} }
} }
/** /**
@ -69,7 +92,8 @@ public class ServerGameInfoHandler {
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:
game.getClientHandler().broadcastChatMessage(ClientGameInfoHandler.noiseNotification); String outMsg = npc.getName() + ": " + noiseRandomizer();;
game.getClientHandler().broadcastNpcChatMessage(outMsg);
break; break;
case ClientGameInfoHandler.humanVoteRequest: case ClientGameInfoHandler.humanVoteRequest:
npc.vote(); npc.vote();

View File

@ -67,4 +67,8 @@ public class Train {
return false; return false;
} }
public static void main(String[] args) {
System.out.println("Hallo");
}
} }

View File

@ -78,23 +78,18 @@ public class Client {
*/ */
public void voteGetter(final String msg) { public void voteGetter(final String msg) {
/*TODO(Jonas): find a way to integrate this with userInput listener, so we can still send the /*TODO(Seraina): Find out what happens if there is no input*/
* position to the server. This way doesnt work, after a game is finished it thinks you still
* want to vote when entering /c msg
*/
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
int msgIndex = msg.indexOf('$'); int msgIndex = msg.indexOf('$');
String position = msg.substring(0, msgIndex); String position = msg.substring(0, msgIndex);
String justMsg = msg.substring(msgIndex + 1); String justMsg = msg.substring(msgIndex + 1);
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); Scanner input = new Scanner(System.in);
while (socket.isConnected() && !socket.isClosed()) {
try {
if (bfr.ready()) {
System.out.println(justMsg); System.out.println(justMsg);
try {
System.out.println("Please enter your vote"); System.out.println("Please enter your vote");
String msgInput = bfr.readLine(); String msgInput = input.nextLine();
int vote; int vote;
try { try {
vote = Integer.parseInt(msgInput); vote = Integer.parseInt(msgInput);
@ -109,13 +104,12 @@ public class Client {
"msg to server is: " + Protocol.votedFor + "$" + position + "$" + msgInput); "msg to server is: " + Protocol.votedFor + "$" + position + "$" + msgInput);
Thread.sleep(5); Thread.sleep(5);
}
//LOGGER.debug("just checked next line"); //LOGGER.debug("just checked next line");
} catch (IOException | InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
}).start(); }).start();
} }

View File

@ -158,6 +158,17 @@ public class ClientHandler implements Runnable {
} }
} }
/**
* Broadcasts a pseudo chat Message from a NPC to all active clients
*
* @param msg the Message to be broadcast
*/
public void broadcastNpcChatMessage(String msg) {
for (ClientHandler client : connectedClients) {
client.sendMsgToClient(Protocol.printToClientConsole + "$" + msg);
}
}
/** /**
* Broadcasts a non-chat Message to all active clients. This can be used for server * Broadcasts a non-chat Message to all active clients. This can be used for server
* messages / announcements rather than chat messages. The message will be printed to the user * messages / announcements rather than chat messages. The message will be printed to the user