Created new methods in ClientHandler for the start of a game and for incoming Vote

This commit is contained in:
Seraina 2022-04-09 22:04:00 +02:00
parent fcd93c4d58
commit 159310c7ca
2 changed files with 44 additions and 26 deletions

View File

@ -1,6 +1,9 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.server; package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; 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.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger;
import java.io.*; 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 * 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). * send / receive a message throws an exception, not just if ping-pong detects a connection loss).

View File

@ -78,34 +78,11 @@ public class JServerProtocolParser {
case Protocol.votedFor: case Protocol.votedFor:
LOGGER.debug("Made it here"); LOGGER.debug("Made it here");
msg = msg.substring(6); msg = msg.substring(6);
int msgIndex = msg.indexOf('$'); h.decodeVote(msg);
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);
}
break; break;
case Protocol.startANewGame: case Protocol.startANewGame:
try { h.startNewGame();
break;
Game game = new Game(h,6,1, ClientHandler.getConnectedClients().size());
Thread t = new Thread(game);
t.start();
} catch (TrainOverflow e) {
LOGGER.warn(e.getMessage());
}
default: default:
System.out.println("Received unknown command"); System.out.println("Received unknown command");
} }