Started communication between voteHandler and user

- implemented a new method in client, that collects votes
- started ading parser entries
- added timer to votehandler(20 s)
This commit is contained in:
Seraina 2022-04-09 00:13:34 +02:00
parent 316619d067
commit d373242486
9 changed files with 69 additions and 11 deletions

View File

@ -7,6 +7,7 @@ Implemented:
* PINGB Pingback from client to server. * PINGB Pingback from client to server.
* NAMEC$name Change name to whatever is specified * NAMEC$name Change name to whatever is specified
* STGAM start the Game * STGAM start the Game
* CVOTE$position Client has voted for position
Future / planned: Future / planned:
* CRTGM Create a new game * CRTGM Create a new game

View File

@ -88,7 +88,7 @@ public class Game implements Runnable {
} }
while (i < order.length) { while (i < order.length) {
int index = order[i]; 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); GhostNPC ghostNPC = new GhostNPC(passengerTrain[index].getPosition(), "NPC" + passengerTrain[index].getPosition(),passengerTrain[index].getIsOG() ,this);
gameState.getPassengerTrain()[index] = ghostNPC; gameState.getPassengerTrain()[index] = ghostNPC;
} else { } else {

View File

@ -81,19 +81,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() + "|";
} }
} }
} }
@ -105,4 +105,18 @@ public class GameState {
return stringBuilder.toString(); 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();
}
} }

View File

@ -31,7 +31,7 @@ public class ServerGameInfoHandler {
msg = Protocol.serverRequestsGhostVote + "$" + game.gameState.toString(); msg = Protocol.serverRequestsGhostVote + "$" + game.gameState.toString();
break; break;
case "Vote for a ghost to kick off!": case "Vote for a ghost to kick off!":
msg = Protocol.serverRequestsHumanVote; msg = Protocol.serverRequestsHumanVote + "$" + game.gameState.humanToString();
break; break;
default: default:
msg = Protocol.printToClientConsole + "$" + msg; msg = Protocol.printToClientConsole + "$" + msg;

View File

@ -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) { for (Passenger passenger : passengers) {
// collecting the votes - distribute them among the vote counters for all players // 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 // 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++) { for (int i = 0; i < votesForPlayers.length; i++) {
if (passenger.getVote() == i) { if (passenger.getVote() == i) {
votesForPlayers[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) { for (Passenger passenger : passengers) {
// collecting the votes - distribute them among the vote counters for all players // collecting the votes - distribute them among the vote counters for all players
// TODO: Perhaps the vote results should be handled by ClientGameInfoHandler // TODO: Perhaps the vote results should be handled by ClientGameInfoHandler

View File

@ -70,6 +70,10 @@ public class Passenger {
hasVoted = voted; hasVoted = voted;
} }
public void setVote(int vote) {
this.vote = vote;
}
public void setIsOg() { public void setIsOg() {
isOG = true; isOG = true;
} }

View File

@ -76,8 +76,24 @@ public class Client {
* Tells user to enter a position to vote for passenger at that position * 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 //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);
}
} }

View File

@ -93,10 +93,15 @@ public class Protocol {
public static final String listLobbies = "LISTL"; 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"; 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 //SERVER TO CLIENT COMMANDS
@ -122,7 +127,8 @@ public class Protocol {
public static final String serverConfirmQuit = "QUITC"; 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"; public static final String serverRequestsGhostVote = "GVOTR";

View File

@ -69,6 +69,11 @@ public class JServerProtocolParser {
//TODO: add action //TODO: add action
LOGGER.debug(Protocol.listLobbies + " command recieved from: " + h.getClientUserName()); LOGGER.debug(Protocol.listLobbies + " command recieved from: " + h.getClientUserName());
break; break;
case Protocol.votedFor:
int vote = Integer.parseInt(msg.substring(6));
if(vote != Integer.MAX_VALUE) {
//TODO(SERAINA): do smt
}
case Protocol.startANewGame: case Protocol.startANewGame:
try { try {