diff --git a/Meilenstein II/Protocol.txt b/Meilenstein II/Protocol.txt index 4471166..d53d36c 100644 --- a/Meilenstein II/Protocol.txt +++ b/Meilenstein II/Protocol.txt @@ -7,6 +7,7 @@ Implemented: * PINGB Pingback from client to server. * NAMEC$name Change name to whatever is specified * STGAM start the Game + * CVOTE$position Client has voted for position Future / planned: * CRTGM Create a new game diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java index 3702db6..380f4da 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java @@ -88,7 +88,7 @@ public class Game implements Runnable { } while (i < order.length) { int index = order[i]; - if (passengerTrain[index].getIsGhost()) { //if there is a ghost + if (passengerTrain[index].getIsGhost()) { //if they are a ghost GhostNPC ghostNPC = new GhostNPC(passengerTrain[index].getPosition(), "NPC" + passengerTrain[index].getPosition(),passengerTrain[index].getIsOG() ,this); gameState.getPassengerTrain()[index] = ghostNPC; } else { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java index 70e00e1..495cfb0 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java @@ -81,19 +81,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() + "|"; } } } @@ -105,4 +105,18 @@ public class GameState { return stringBuilder.toString(); } + public String humanToString() { + Passenger[] array = passengerTrain; + 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() + "|"; + } + + for (int i = 0; i < array.length; i++) { + stringBuilder.append(print[i]); + } + return stringBuilder.toString(); + } + } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java index 3613ce6..68576d9 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java @@ -31,7 +31,7 @@ public class ServerGameInfoHandler { msg = Protocol.serverRequestsGhostVote + "$" + game.gameState.toString(); break; case "Vote for a ghost to kick off!": - msg = Protocol.serverRequestsHumanVote; + msg = Protocol.serverRequestsHumanVote + "$" + game.gameState.humanToString(); break; default: msg = Protocol.printToClientConsole + "$" + msg; diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java index 7be4084..9dedf84 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java @@ -53,6 +53,12 @@ public class VoteHandler { } } + try { // waits 20 seconds before votes get collected + Thread.sleep(30*1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + for (Passenger passenger : passengers) { // collecting the votes - distribute them among the vote counters for all players // Note: Each voting collects votes for all players even though some might not be concerned @@ -62,7 +68,7 @@ public class VoteHandler { for (int i = 0; i < votesForPlayers.length; i++) { if (passenger.getVote() == i) { votesForPlayers[i]++; - LOGGER.info(passengers[i] + " has received the most votes"); + LOGGER.info(passengers[i] + " has received a vote"); } } } @@ -126,6 +132,12 @@ public class VoteHandler { } } + try { // waits 20 seconds before votes get collected + Thread.sleep(30*1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + for (Passenger passenger : passengers) { // collecting the votes - distribute them among the vote counters for all players // TODO: Perhaps the vote results should be handled by ClientGameInfoHandler diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java index a8d527c..ef40ce9 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java @@ -70,6 +70,10 @@ public class Passenger { hasVoted = voted; } + public void setVote(int vote) { + this.vote = vote; + } + public void setIsOg() { isOG = true; } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java index e809771..9771c76 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java @@ -76,8 +76,24 @@ public class Client { * Tells user to enter a position to vote for passenger at that position */ - public void voteGetter() { + public void voteGetter(String msg) { + Scanner userInput = new Scanner(System.in); //TODO(Seraina): implement + System.out.println(msg); + System.out.println("Please enter your vote"); + int vote; + String input = ""; + try { + input = userInput.nextLine(); + vote = Integer.parseInt(input); + LOGGER.info("input is: " + vote); + } catch (Exception e) { + LOGGER.warn(e.getMessage()); + System.out.println("Invalid vote"); + input = String.valueOf(Integer.MAX_VALUE); + } finally { + sendMsgToServer(Protocol.votedFor + "$" + input); + } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java index 658a0fd..c19a11a 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java @@ -93,10 +93,15 @@ public class Protocol { public static final String listLobbies = "LISTL"; /** - * A Client decides to start the game + * A Client decides to start the game. */ public static final String startANewGame = "STGAM"; + /** + * Client informs server that they have voted and delivers this vote in the form of "CVOTE$position" + */ + public static final String votedFor = "CVOTE"; + //SERVER TO CLIENT COMMANDS @@ -122,7 +127,8 @@ public class Protocol { public static final String serverConfirmQuit = "QUITC"; /** - * The server requests the client (who should be a ghost) to vote on the victim. + * The server requests the client (who should be a ghost) to vote on the victim. in the format GVOTR$string + * the current train will be shown as a string to the client */ public static final String serverRequestsGhostVote = "GVOTR"; diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java index 7fefb40..f8306ac 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java @@ -69,6 +69,11 @@ public class JServerProtocolParser { //TODO: add action LOGGER.debug(Protocol.listLobbies + " command recieved from: " + h.getClientUserName()); break; + case Protocol.votedFor: + int vote = Integer.parseInt(msg.substring(6)); + if(vote != Integer.MAX_VALUE) { + //TODO(SERAINA): do smt + } case Protocol.startANewGame: try {