Added a thread to voteGetter so it would interfere with Pinger. Added to do for jonas

This commit is contained in:
Seraina 2022-04-09 23:10:11 +02:00
parent 159310c7ca
commit 362b965742
2 changed files with 42 additions and 22 deletions

View File

@ -77,30 +77,50 @@ 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(String msg) { public void voteGetter(final String msg) {
int msgIndex = msg.indexOf('$'); /*TODO(Jonas): find a way to integrate this with userInput listener, so we can still send the
String position = msg.substring(0, msgIndex);; * position to the server. This way doesnt work, after a game is finished it thinks you still
msg = msg.substring(msgIndex + 1); * want to vote when entering /c msg
Scanner userInput = new Scanner(System.in); */
//TODO(Seraina): implement new Thread(new Runnable() {
System.out.println(msg); @Override
System.out.println("Please enter your vote"); public void run() {
int vote; int msgIndex = msg.indexOf('$');
String input = ""; String position = msg.substring(0, msgIndex);
try { String justMsg = msg.substring(msgIndex + 1);
input = userInput.nextLine(); BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
vote = Integer.parseInt(input); while (socket.isConnected() && !socket.isClosed()) {
LOGGER.info("input is: " + vote); try {
} catch (Exception e) { if (bfr.ready()) {
LOGGER.warn(e.getMessage()); System.out.println(justMsg);
System.out.println("Invalid vote"); System.out.println("Please enter your vote");
input = String.valueOf(Integer.MAX_VALUE); String msgInput = bfr.readLine();
} int vote;
sendMsgToServer(Protocol.votedFor + "$" + position + "$" + input); try {
LOGGER.debug("msg to server is: " + Protocol.votedFor + "$" + position + "$" + input); vote = Integer.parseInt(msgInput);
LOGGER.info("input is: " + vote);
} catch (Exception e) {
LOGGER.warn(e.getMessage());
System.out.println("Invalid vote");
msgInput = String.valueOf(Integer.MAX_VALUE);
}
sendMsgToServer(Protocol.votedFor + "$" + position + "$" + msgInput);
LOGGER.debug(
"msg to server is: " + Protocol.votedFor + "$" + position + "$" + msgInput);
Thread.sleep(5);
}
//LOGGER.debug("just checked next line");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
} }
/** /**
* Starts a thread which listens for incoming chat messages / other messages that the user * Starts a thread which listens for incoming chat messages / other messages that the user
* has to see * has to see

View File

@ -98,7 +98,7 @@ public class Protocol {
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" * Client informs server that they have voted and delivers this vote in the form of "CVOTE$position$vote"
*/ */
public static final String votedFor = "CVOTE"; public static final String votedFor = "CVOTE";