made OgGhostHighScore.java, see documentation for details
This commit is contained in:
parent
b94c1ab3f4
commit
5fbee579a9
@ -19,8 +19,8 @@ public class GameState {
|
||||
**/
|
||||
public final int nrOfPlayers; //sets the length of the train
|
||||
public final int nrOfGhosts; // sets how many Ghosts we start witch
|
||||
public final int nrOfUsers; // safes how many clients are active in this Game
|
||||
public final Train train; // safes who sits where
|
||||
public final int nrOfUsers; // saves how many clients are active in this Game
|
||||
public final Train train; // saves who sits where
|
||||
/**
|
||||
* contains all Passengers on train, needs to be updated
|
||||
*/
|
||||
|
||||
@ -54,7 +54,7 @@ public class VoteHandler {
|
||||
|
||||
int currentMax = ghostVoteEvaluation(passengers, votesForPlayers, game.getGameState().getClientVoteData(), game);
|
||||
|
||||
LOGGER.debug("Most votes: " + currentMax + " vote");
|
||||
LOGGER.debug("Most votes: " + currentMax + " vote(s)");
|
||||
|
||||
// ghostify the player with most votes
|
||||
int ghostPosition = 0;
|
||||
|
||||
@ -0,0 +1,121 @@
|
||||
package ch.unibas.dmi.dbis.cs108.highscore;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* used for logging OG ghost highscore
|
||||
*/
|
||||
public class OgGhostHighScore {
|
||||
|
||||
public static final Logger LOGGER = LogManager.getLogger(OgGhostHighScore.class);
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
static ArrayList<String> ogGhostWinners = new ArrayList<>();
|
||||
static final File ogGhostFile = new File("OgGhostWinners.txt");
|
||||
|
||||
/**
|
||||
* Writes the current state of the ogGhostWinners String[] to the ogGhostFile, then closes
|
||||
* the fileWriter.
|
||||
*/
|
||||
static void writeOgGhostWinnersToFile() {
|
||||
try {
|
||||
FileWriter fileWriter = new FileWriter(ogGhostFile, false);
|
||||
for (String name : ogGhostWinners) {
|
||||
fileWriter.write(name);
|
||||
fileWriter.write(System.lineSeparator());
|
||||
}
|
||||
fileWriter.close();
|
||||
} catch (Exception e) {
|
||||
LOGGER.debug("Exception while trying to write ogGhostWinners to file.");
|
||||
LOGGER.debug(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* adds the given name to the list of og ghost winners and updates the file listing the og ghost
|
||||
* winners via writeOgGhostWinnersToFile
|
||||
*/
|
||||
public static void addOgGhostWinner(String name){
|
||||
ogGhostWinners.add(name);
|
||||
writeOgGhostWinnersToFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* outputs the highscore list as it could be shown to clients.
|
||||
* @return
|
||||
*/
|
||||
public static String formatGhostHighscoreList() {
|
||||
|
||||
//create the hashMap which lists all names along with their number of appearances
|
||||
//int max = 0;
|
||||
HashMap<String, Integer> hm = new HashMap<>();
|
||||
for (String name: ogGhostWinners) {
|
||||
if (hm.containsKey(name)) {
|
||||
hm.replace(name, hm.get(name) + 1);
|
||||
} else {
|
||||
hm.put(name, 1);
|
||||
}
|
||||
//if (max < hm.get(name)) max = hm.get(name);
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
||||
//add the 5 highest scoring peeps to the StringBuilder sb
|
||||
for (int i = 0; i < 5; i++) {
|
||||
//find first place among the remaining members of hm. ("remaining" because we remove people once theyre listed)
|
||||
if (!hm.isEmpty()) {
|
||||
String firstplace = (String) hm.keySet().toArray()[0]; //choose one candidate for first place just so we dont get null pointer
|
||||
for (String name: hm.keySet()) {
|
||||
if (hm.get(name) > hm.get(firstplace)) firstplace = name;
|
||||
}
|
||||
sb.append(firstplace).append(": ").append(hm.get(firstplace)).append(" wins.")
|
||||
.append(System.lineSeparator());
|
||||
hm.remove(firstplace);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* reads the highscore file (or if not yet present create it) and reads the ogGhostWinners;
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
ogGhostWinners = new ArrayList<>();
|
||||
//if not already present, the following creates the file and enters the if statement.
|
||||
//if already present, it reads what's already in the file into the ogGhostWinners array.
|
||||
if (!ogGhostFile.createNewFile()) {
|
||||
BufferedReader buffreader = new BufferedReader(new FileReader(ogGhostFile));
|
||||
String line = buffreader.readLine();
|
||||
while (line != null) {
|
||||
ogGhostWinners.add(line);
|
||||
line = buffreader.readLine();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
addOgGhostWinner("Seraina");
|
||||
ogGhostWinners.add("Jonas, the ultimate winner");
|
||||
|
||||
writeOgGhostWinnersToFile();
|
||||
System.out.println(formatGhostHighscoreList());
|
||||
*/
|
||||
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,105 @@
|
||||
package ch.unibas.dmi.dbis.cs108.gamelogic;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.*;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Server;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class GameStateTests {
|
||||
|
||||
/*
|
||||
* Streams to store system.out and system.err content
|
||||
*/
|
||||
private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
private ByteArrayOutputStream errStream = new ByteArrayOutputStream();
|
||||
|
||||
/*
|
||||
* Here we store the previous pointers to system.out / system.err
|
||||
*/
|
||||
private PrintStream outBackup;
|
||||
private PrintStream errBackup;
|
||||
|
||||
/**
|
||||
* This method is executed before each test.
|
||||
* It redirects System.out and System.err to our variables {@link #outStream} and {@link #errStream}.
|
||||
* This allows us to test their content later.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void redirectStdOutStdErr() {
|
||||
System.out.println("im here");
|
||||
outBackup = System.out;
|
||||
errBackup = System.err;
|
||||
System.setOut(new PrintStream(outStream));
|
||||
System.setErr(new PrintStream(errStream));
|
||||
outBackup.println("this should");
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is run after each test.
|
||||
* It redirects System.out / System.err back to the normal streams.
|
||||
*/
|
||||
@AfterEach
|
||||
public void reestablishStdOutStdErr() {
|
||||
System.setOut(outBackup);
|
||||
System.setErr(errBackup);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a normal JUnit-Test. It executes the HelloWorld-Method and verifies that it actually wrote "Hello World" to stdout
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
|
||||
/* old test. Todo: delete
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Server.main(1837);
|
||||
}
|
||||
}).start();
|
||||
outBackup.println("here now");
|
||||
String toTest = outStream.toString();
|
||||
outBackup.println(toTest);
|
||||
//toTest = removeNewline(toTest);
|
||||
assertTrue(toTest.contains("Port"));
|
||||
*/
|
||||
try {
|
||||
int totalNumberOfPlayers = 6;
|
||||
int numberOfHumanPlayers = 3;
|
||||
Passenger[] passengers = new Passenger[totalNumberOfPlayers];
|
||||
VoteHandler v = new VoteHandler();
|
||||
Lobby l = null;
|
||||
|
||||
Game game = new Game(6, 1, 3, l);
|
||||
passengers[0] = new GhostNPC(0,"0", true);
|
||||
passengers[1] = new GhostNPC(1,"1", false);
|
||||
passengers[2] = new GhostNPC(2,"2", false);
|
||||
passengers[3] = new HumanNPC(3,"3");
|
||||
passengers[4] = new HumanNPC(4,"4");
|
||||
passengers[5] = new HumanNPC(5,"5");
|
||||
|
||||
int[] votesForPlayers = {0, 0, 0, 0, 0, 0};
|
||||
int maxVotes = v.ghostVoteEvaluation(passengers, votesForPlayers, null, game);
|
||||
assertEquals(2, maxVotes);
|
||||
|
||||
} catch (Exception ignored) {
|
||||
outBackup.println("exception.");
|
||||
}
|
||||
outBackup.println("done");
|
||||
|
||||
}
|
||||
|
||||
private static String removeNewline(String str) {
|
||||
return str.replace("\n", "").replace("\r", "");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user