Did some cleanup with the loggers, only set vital information at info level

This commit is contained in:
Seraina 2022-04-09 18:35:13 +02:00
parent d3a0c00dea
commit ac80a88b98
9 changed files with 45 additions and 29 deletions

View File

@ -58,6 +58,8 @@ public class Game implements Runnable {
return nrOfUsers; return nrOfUsers;
} }
public boolean getIsDay() {return isDay;}
public void setDay(boolean day) { public void setDay(boolean day) {
isDay = day; isDay = day;
} }

View File

@ -81,6 +81,11 @@ public class GameState {
} }
/**
* Converts the data in this passengerTrain into a human-readable string,
* where one can see who is a ghost and who is a human, who is a player and who an NPC
* @return a String that displays passengerTrain
*/
public String toString() { public String toString() {
Passenger[] array = passengerTrain; Passenger[] array = passengerTrain;
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
@ -111,6 +116,12 @@ public class GameState {
return stringBuilder.toString(); return stringBuilder.toString();
} }
/**
* Converts the data in this passengerTrain into a human-readable string, but it is anonymised for
* human players, so it is not obvious who is a human and who a ghost, only names and positions
* are displayed
* @return the String displaying an anonymised passengerTrain
*/
public String humanToString() { public String humanToString() {
Passenger[] array = passengerTrain; Passenger[] array = passengerTrain;
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();

View File

@ -34,9 +34,9 @@ public class ServerGameInfoHandler {
msg = Protocol.serverRequestsHumanVote + "$" + p.getPosition() +"$"+ game.gameState.humanToString(); msg = Protocol.serverRequestsHumanVote + "$" + p.getPosition() +"$"+ game.gameState.humanToString();
break; break;
default: default:
msg = Protocol.printToClientConsole + "$" + p.getPosition() +"$"+ msg; msg = Protocol.printToClientConsole + "$"+ msg;
} }
LOGGER.info(msg); LOGGER.debug(msg);
return msg; return msg;
} }

View File

@ -72,17 +72,16 @@ public class VoteHandler {
} }
for (Passenger passenger : passengers) { for (Passenger passenger : passengers) { //TODO: could be outsourced to a method to increase readability
// 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
// (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.getVoteFromGameState(clientVoteData); passenger.getVoteFromGameState(clientVoteData, game);
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) {
votesForPlayers[i]++; votesForPlayers[i]++;
LOGGER.info(passengers[i] + " has received a vote");
} }
} }
} }
@ -96,18 +95,18 @@ public class VoteHandler {
currentMax = votesForPlayer; currentMax = votesForPlayer;
} }
} }
LOGGER.info("Most votes: " + currentMax + " vote"); LOGGER.debug("Most votes: " + currentMax + " vote");
// ghostify the player with most votes // ghostify the player with most votes
int ghostPosition = 0; int ghostPosition = 0;
for (int i = 0; i < votesForPlayers.length; i++) { for (int i = 0; i < votesForPlayers.length; i++) {
if (votesForPlayers[i] == currentMax) { // if player at position i has most votes if (votesForPlayers[i] == currentMax) { // if player at position i has most votes
ghostPosition = i; ghostPosition = i;
LOGGER.info("Most votes for Passenger " + i); LOGGER.debug("Most votes for Passenger " + i);
} }
} }
LOGGER.debug("ghostPosition: " + ghostPosition); LOGGER.info("Most votes for: " + ghostPosition);
GhostifyHandler gh = new GhostifyHandler(); GhostifyHandler gh = new GhostifyHandler();
Ghost g = gh.ghost(passengers[ghostPosition], game); Ghost g = gh.ghost(passengers[ghostPosition], game);
passengers[ghostPosition] = g; passengers[ghostPosition] = g;
@ -154,7 +153,7 @@ public class VoteHandler {
} }
for (Passenger passenger : passengers) { for (Passenger passenger : passengers) {
passenger.getVoteFromGameState(clientVoteData); passenger.getVoteFromGameState(clientVoteData, game);
// 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

@ -45,7 +45,7 @@ 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 getVoteFromGameState(ClientVoteData clientVoteData) { public void getVoteFromGameState(ClientVoteData clientVoteData, Game game) {
vote = clientVoteData.getVote()[position]; vote = clientVoteData.getVote()[position];
hasVoted = clientVoteData.getHasVoted()[position]; hasVoted = clientVoteData.getHasVoted()[position];
clientVoteData.setVote(position,Integer.MAX_VALUE); clientVoteData.setVote(position,Integer.MAX_VALUE);

View File

@ -43,20 +43,22 @@ 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 getVoteFromGameState(ClientVoteData clientVoteData) { public void getVoteFromGameState(ClientVoteData clientVoteData, Game game) {
if(game.getIsDay()) {
LOGGER.debug(Arrays.toString(clientVoteData.getVote())); LOGGER.debug(Arrays.toString(clientVoteData.getVote()));
LOGGER.info("method was called by: " + position); LOGGER.debug("method was called by: " + position);
vote = clientVoteData.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 = clientVoteData.getHasVoted()[position]; hasVoted = clientVoteData.getHasVoted()[position];
LOGGER.debug(Arrays.toString(clientVoteData.getVote())); LOGGER.debug(Arrays.toString(clientVoteData.getVote()));
//clientVoteData.setVote(position,Integer.MAX_VALUE); clientVoteData.setVote(position, Integer.MAX_VALUE);
//clientVoteData.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
*/ */
if(vote == Integer.MAX_VALUE) { if (vote == Integer.MAX_VALUE) {
hasVoted = false; hasVoted = false;
} }
} }
}
} }

View File

@ -112,8 +112,8 @@ 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 getVoteFromGameState(ClientVoteData clientVoteData) { public void getVoteFromGameState(ClientVoteData clientVoteData,Game game) {
//LOGGER.debug("a NPC called this method hopefully: " + position); LOGGER.debug("a NPC called this method hopefully: " + position);
} }
} }

View File

@ -97,7 +97,7 @@ public class Client {
input = String.valueOf(Integer.MAX_VALUE); input = String.valueOf(Integer.MAX_VALUE);
} }
sendMsgToServer(Protocol.votedFor + "$" + position + "$" + input); sendMsgToServer(Protocol.votedFor + "$" + position + "$" + input);
LOGGER.info("msg to server is: " + Protocol.votedFor + "$" + position + "$" + input); LOGGER.debug("msg to server is: " + Protocol.votedFor + "$" + position + "$" + input);
} }

View File

@ -43,12 +43,14 @@ public class JClientProtocolParser {
c.disconnectFromServer(); c.disconnectFromServer();
break; break;
case Protocol.serverRequestsGhostVote: case Protocol.serverRequestsGhostVote:
System.out.println("Ghost received Vote request"); LOGGER.debug("Ghost received Vote request");
System.out.println("Ghost Vote:");
c.voteGetter(msg.substring(6)); c.voteGetter(msg.substring(6));
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input //TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
break; break;
case Protocol.serverRequestsHumanVote: case Protocol.serverRequestsHumanVote:
LOGGER.info("Human received Vote request"); LOGGER.debug("Human received Vote request");
System.out.println("Human Vote:");
c.voteGetter(msg.substring(6)); c.voteGetter(msg.substring(6));
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input //TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
break; break;