Moved all the fields in added in client handler into a separate class ClientVoteData cuz it didn't make sense there

This commit is contained in:
Seraina 2022-04-09 16:56:42 +02:00
parent 45597eee3c
commit 1de2e739de
9 changed files with 71 additions and 39 deletions

View File

@ -0,0 +1,32 @@
package ch.unibas.dmi.dbis.cs108.gamelogic;
import java.util.Arrays;
public class ClientVoteData {
private int[] vote; //saves vote of clientHandler for later transmission to passenger, by default MAX_VALUE, index corresponds to Passenger position
private boolean[] hasVoted; //saves hasVoted status of clientHandler for later transmission to passenger, by default false, index corresponds to Passenger position
public ClientVoteData() {
int[] h = new int[6];
Arrays.fill(h,Integer.MAX_VALUE);
this.vote = h;
this.hasVoted = new boolean[6];
}
public int[] getVote() {
return vote;
}
public boolean[] getHasVoted() {
return hasVoted;
}
public void setVote(int position, int vote) {
this.vote[position] = vote;
}
public void setHasVoted(int position, boolean hasVoted) {
this.hasVoted[position] = hasVoted;
}
}

View File

@ -109,6 +109,7 @@ public class Game implements Runnable {
} else { } else {
LOGGER.info("DAY"); LOGGER.info("DAY");
gameOverCheck = voteHandler.humanVote(gameState.getPassengerTrain(), this); gameOverCheck = voteHandler.humanVote(gameState.getPassengerTrain(), this);
setDay(false);
} }
if (gameOverCheck.equals("Game over: ghosts win!") || gameOverCheck.equals( if (gameOverCheck.equals("Game over: ghosts win!") || gameOverCheck.equals(
"Game over: humans win!")) { "Game over: humans win!")) {

View File

@ -4,6 +4,7 @@ import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Human; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Human;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -24,6 +25,7 @@ public class GameState {
private Passenger[] passengerTrain; private Passenger[] passengerTrain;
/** /**
* Constructs a GameState instance where nrOfPlayers >= nrOfUsers. Fills passengerTrain with * Constructs a GameState instance where nrOfPlayers >= nrOfUsers. Fills passengerTrain with
* only humans * only humans
@ -65,6 +67,8 @@ public class GameState {
return train; return train;
} }
/** /**
* Takes a given Passenger and puts it into the passengerTrain at a certain position * Takes a given Passenger and puts it into the passengerTrain at a certain position
* @param passenger the new passenger being put into the train * @param passenger the new passenger being put into the train

View File

@ -2,9 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostPlayer;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -23,6 +21,15 @@ public class VoteHandler {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
private ClientVoteData clientVoteData;
public ClientVoteData getClientVoteData() {
return clientVoteData;
}
public static void setClientVoteData(ClientVoteData clientVoteData) {
clientVoteData = clientVoteData;
}
/** /**
* Handles the ghost vote during nighttime: passengers who are ghosts are being asked on who to * Handles the ghost vote during nighttime: passengers who are ghosts are being asked on who to
@ -65,7 +72,7 @@ public class VoteHandler {
// 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
// (i.e. ghosts during ghost vote). Those players will then get 0 votes so it doesn't matter. // (i.e. ghosts during ghost vote). Those players will then get 0 votes so it doesn't matter.
// TODO: Perhaps the vote results should be handled by ClientGameInfoHandler // TODO: Perhaps the vote results should be handled by ClientGameInfoHandler
passenger.getVoteFromClientHandler(); passenger.getVoteFromGameState(clientVoteData);
if (passenger.getHasVoted()) { if (passenger.getHasVoted()) {
for (int i = 0; i < votesForPlayers.length; i++) { for (int i = 0; i < votesForPlayers.length; i++) {
if (passenger.getVote() == i) { if (passenger.getVote() == i) {
@ -142,7 +149,7 @@ public class VoteHandler {
} }
for (Passenger passenger : passengers) { for (Passenger passenger : passengers) {
passenger.getVoteFromClientHandler(); passenger.getVoteFromGameState(clientVoteData);
// 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
if (passenger.getHasVoted()) { if (passenger.getHasVoted()) {

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur; package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.ClientVoteData;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler; import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
@ -44,11 +45,11 @@ public class GhostPlayer extends Ghost {
* Sets clientHandler fields to default: vote = Integer.MAX_VALUE , hasVoted = false * Sets clientHandler fields to default: vote = Integer.MAX_VALUE , hasVoted = false
*/ */
@Override @Override
public void getVoteFromClientHandler() { public void getVoteFromGameState(ClientVoteData clientVoteData) {
vote = clientHandler.getVote()[position]; vote = clientVoteData.getVote()[position];
hasVoted = clientHandler.getHasVoted()[position]; hasVoted = clientVoteData.getHasVoted()[position];
clientHandler.setVote(position,Integer.MAX_VALUE); clientVoteData.setVote(position,Integer.MAX_VALUE);
clientHandler.setHasVoted(position,false); clientVoteData.setHasVoted(position,false);
LOGGER.info("Ghost at Pos: " + position + " has voted for: " + vote); LOGGER.info("Ghost at Pos: " + position + " has voted for: " + vote);
/* /*
* if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry * if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry

View File

@ -1,9 +1,11 @@
package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur; package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.ClientVoteData;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler; import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -41,13 +43,14 @@ public class HumanPlayer extends Human {
* Sets clientHandler fields to default: vote = Integer.MAX_VALUE , hasVoted = false * Sets clientHandler fields to default: vote = Integer.MAX_VALUE , hasVoted = false
*/ */
@Override @Override
public void getVoteFromClientHandler() { public void getVoteFromGameState(ClientVoteData clientVoteData) {
LOGGER.debug(Arrays.toString(clientVoteData.getVote()));
LOGGER.info("method was called by: " + position); LOGGER.info("method was called by: " + position);
vote = clientHandler.getVote()[position]; vote = clientVoteData.getVote()[position];
LOGGER.info("Human at Pos: " + position + " has voted for: " + vote); LOGGER.info("Human at Pos: " + position + " has voted for: " + vote);
hasVoted = clientHandler.getHasVoted()[position]; hasVoted = clientVoteData.getHasVoted()[position];
clientHandler.setVote(position,Integer.MAX_VALUE); clientVoteData.setVote(position,Integer.MAX_VALUE);
clientHandler.setHasVoted(position,false); clientVoteData.setHasVoted(position,false);
/* /*
* if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry * if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry
*/ */

View File

@ -1,9 +1,9 @@
package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur; package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.ClientVoteData;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler; import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -112,7 +112,7 @@ public class Passenger {
/** /**
* When called by NPC nothing should happen, because clientHandler = null * When called by NPC nothing should happen, because clientHandler = null
*/ */
public void getVoteFromClientHandler() { public void getVoteFromGameState(ClientVoteData clientVoteData) {
LOGGER.debug("a NPC called this method hopefully: " + position); LOGGER.debug("a NPC called this method hopefully: " + position);
} }

View File

@ -36,8 +36,6 @@ public class ClientHandler implements Runnable {
public static HashSet<ClientHandler> disconnectedClients = new HashSet<>(); //todo: implement re-connection public static HashSet<ClientHandler> disconnectedClients = new HashSet<>(); //todo: implement re-connection
public static HashSet<ClientHandler> lobby = new HashSet<>(); public static HashSet<ClientHandler> lobby = new HashSet<>();
public static HashSet<ClientHandler> ghostClients = new HashSet<>(); public static HashSet<ClientHandler> ghostClients = new HashSet<>();
private int[] vote; //saves vote of clientHandler for later transmission to passenger, by default MAX_VALUE, index corresponds to Passenger position
private boolean[] hasVoted; //saves hasVoted status of clientHandler for later transmission to passenger, by default false, index corresponds to Passenger position
/** /**
* Implements the login logic in client-server architecture. * Implements the login logic in client-server architecture.
@ -52,10 +50,6 @@ public class ClientHandler implements Runnable {
this.in = new BufferedReader(new InputStreamReader((socket.getInputStream()))); this.in = new BufferedReader(new InputStreamReader((socket.getInputStream())));
this.loggedIn = false; this.loggedIn = false;
this.clientUserName = nameDuplicateChecker.checkName("U.N. Owen"); this.clientUserName = nameDuplicateChecker.checkName("U.N. Owen");
int[] h = new int[1000];
Arrays.fill(h,Integer.MAX_VALUE);
this.vote = h;
this.hasVoted = new boolean[1000];
connectedClients.add(this); connectedClients.add(this);
serverPinger = new ServerPinger(socket, this); serverPinger = new ServerPinger(socket, this);
Thread sP = new Thread(serverPinger); Thread sP = new Thread(serverPinger);
@ -94,14 +88,6 @@ public class ClientHandler implements Runnable {
return loggedIn; return loggedIn;
} }
public int[] getVote() {
return vote;
}
public boolean[] getHasVoted() {
return hasVoted;
}
public String getClientUserName() { public String getClientUserName() {
return clientUserName; return clientUserName;
} }
@ -111,13 +97,6 @@ public class ClientHandler implements Runnable {
this.loggedIn = loggedIn; this.loggedIn = loggedIn;
} }
public void setVote(int position, int vote) {
this.vote[position] = vote;
}
public void setHasVoted(int position, boolean hasVoted) {
this.hasVoted[position] = hasVoted;
}
@Override @Override
public void run() { public void run() {

View File

@ -2,8 +2,11 @@ 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.ClientVoteData;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.GameState;
import ch.unibas.dmi.dbis.cs108.gamelogic.TrainOverflow; import ch.unibas.dmi.dbis.cs108.gamelogic.TrainOverflow;
import ch.unibas.dmi.dbis.cs108.gamelogic.VoteHandler;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
@ -77,6 +80,7 @@ public class JServerProtocolParser {
int msgIndex = msg.indexOf('$'); int msgIndex = msg.indexOf('$');
int vote = Integer.MAX_VALUE; int vote = Integer.MAX_VALUE;
int position = 0; int position = 0;
ClientVoteData clientVoteData = new ClientVoteData();
LOGGER.debug("Message is " + msg.substring(6)); LOGGER.debug("Message is " + msg.substring(6));
try { try {
position = Integer.parseInt(msg.substring(0,msgIndex)); position = Integer.parseInt(msg.substring(0,msgIndex));
@ -85,10 +89,11 @@ public class JServerProtocolParser {
LOGGER.warn("Invalid vote " + e.getMessage()); LOGGER.warn("Invalid vote " + e.getMessage());
} }
if(vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid if(vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid
h.setVote(position,vote); clientVoteData.setVote(position,vote);
LOGGER.debug("Player vote: " + vote); LOGGER.debug("Player vote: " + vote);
h.setHasVoted(position,true); clientVoteData.setHasVoted(position,true);
} }
VoteHandler.setClientVoteData(clientVoteData);
break; break;
case Protocol.startANewGame: case Protocol.startANewGame:
try { try {