From 159310c7caa2d1a845157d2e96034939d92771f5 Mon Sep 17 00:00:00 2001 From: Seraina Date: Sat, 9 Apr 2022 22:04:00 +0200 Subject: [PATCH 1/2] Created new methods in ClientHandler for the start of a game and for incoming Vote --- .../multiplayer/server/ClientHandler.java | 41 +++++++++++++++++++ .../server/JServerProtocolParser.java | 29 ++----------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java index d9ed5fa..7083c84 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java @@ -1,6 +1,9 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.server; import ch.unibas.dmi.dbis.cs108.BudaLogConfig; +import ch.unibas.dmi.dbis.cs108.gamelogic.Game; +import ch.unibas.dmi.dbis.cs108.gamelogic.TrainOverflow; +import ch.unibas.dmi.dbis.cs108.gamelogic.VoteHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger; import java.io.*; @@ -182,6 +185,44 @@ public class ClientHandler implements Runnable { } } + /** + * Takes a msg of the form position$vote and extracts vote and position from it and saves it + * in VoteHandler.getClientVoteData + * @param msg the messaged to decode + */ + public void decodeVote(String msg){ + int msgIndex = msg.indexOf('$'); + int vote = Integer.MAX_VALUE; + int position = 0; + LOGGER.debug("Message is " + msg); + try { + position = Integer.parseInt(msg.substring(0,msgIndex)); + vote = Integer.parseInt(msg.substring(msgIndex + 1)); + LOGGER.debug("Vote is:" + vote); + } catch (Exception e) { + LOGGER.warn("Invalid vote " + e.getMessage()); + } + LOGGER.debug("Vote is:" + vote); + if(vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid + VoteHandler.getClientVoteData().setVote(position,vote); + LOGGER.debug("Player vote: " + vote); + VoteHandler.getClientVoteData().setHasVoted(position,true); + } + } + + /** + * Initializes a new Game instance and starts its run method in a new thread + */ + public void startNewGame() { + try { + Game game = new Game(this,6,1, ClientHandler.getConnectedClients().size()); + Thread t = new Thread(game); + t.start(); + } catch (TrainOverflow e) { + LOGGER.warn(e.getMessage()); + } + } + /** * Removes & disconnects the client. To be used if a severe connection loss is detected (i.e. if trying to * send / receive a message throws an exception, not just if ping-pong detects a connection loss). 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 de6ad69..df365a4 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 @@ -78,34 +78,11 @@ public class JServerProtocolParser { case Protocol.votedFor: LOGGER.debug("Made it here"); msg = msg.substring(6); - int msgIndex = msg.indexOf('$'); - int vote = Integer.MAX_VALUE; - int position = 0; - LOGGER.debug("Message is " + msg); - try { - position = Integer.parseInt(msg.substring(0,msgIndex)); - vote = Integer.parseInt(msg.substring(msgIndex + 1)); - LOGGER.debug("Vote is:" + vote); - } catch (Exception e) { - LOGGER.warn("Invalid vote " + e.getMessage()); - } - LOGGER.debug("Vote is:" + vote); - if(vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid - VoteHandler.getClientVoteData().setVote(position,vote); - LOGGER.debug("Player vote: " + vote); - VoteHandler.getClientVoteData().setHasVoted(position,true); - } + h.decodeVote(msg); break; case Protocol.startANewGame: - try { - - Game game = new Game(h,6,1, ClientHandler.getConnectedClients().size()); - Thread t = new Thread(game); - t.start(); - } catch (TrainOverflow e) { - LOGGER.warn(e.getMessage()); - } - + h.startNewGame(); + break; default: System.out.println("Received unknown command"); } From 362b96574229246aaa7f7b89156963380d7ac886 Mon Sep 17 00:00:00 2001 From: Seraina Date: Sat, 9 Apr 2022 23:10:11 +0200 Subject: [PATCH 2/2] Added a thread to voteGetter so it would interfere with Pinger. Added to do for jonas --- .../dbis/cs108/multiplayer/client/Client.java | 62 ++++++++++++------- .../cs108/multiplayer/helpers/Protocol.java | 2 +- 2 files changed, 42 insertions(+), 22 deletions(-) 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 db6c3eb..27e8a7a 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 @@ -77,30 +77,50 @@ public class Client { * Tells user to enter a position to vote for passenger at that position */ - public void voteGetter(String msg) { - int msgIndex = msg.indexOf('$'); - String position = msg.substring(0, msgIndex);; - msg = msg.substring(msgIndex + 1); - 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); - } - sendMsgToServer(Protocol.votedFor + "$" + position + "$" + input); - LOGGER.debug("msg to server is: " + Protocol.votedFor + "$" + position + "$" + input); + public void voteGetter(final String msg) { + /*TODO(Jonas): find a way to integrate this with userInput listener, so we can still send the + * 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() { + @Override + public void run() { + int msgIndex = msg.indexOf('$'); + String position = msg.substring(0, msgIndex); + String justMsg = msg.substring(msgIndex + 1); + BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); + while (socket.isConnected() && !socket.isClosed()) { + try { + if (bfr.ready()) { + System.out.println(justMsg); + System.out.println("Please enter your vote"); + String msgInput = bfr.readLine(); + int vote; + try { + 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 * has to see 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 c19a11a..e40e3ff 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 @@ -98,7 +98,7 @@ public class Protocol { 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";