High score fully implemented.

This commit is contained in:
Jonas 2022-04-28 15:29:36 +02:00
parent b769991c36
commit 96812a04ae
7 changed files with 39 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostPlayer;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanNPC;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanPlayer;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import ch.unibas.dmi.dbis.cs108.highscore.OgGhostHighScore;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby;
import java.util.HashSet;
@ -64,6 +65,17 @@ public class Game implements Runnable {
isOngoing = ongoing;
}
Passenger getOgGhost(){
int[] order = gameState.getTrain().getOrderOfTrain();
Passenger[] passengerTrain = gameState.getPassengerTrain();
for (int i = 0; i < 6; i++) {
if (passengerTrain[i].getIsOG()) {
return passengerTrain[i];
}
}
return null;
}
/**
* Starts a new game, creates a passenger array and saves it in gameState, sets the OG
* currently at gameState.train[3] fills the passengerTrain moving from left to rigth in the
@ -121,6 +133,9 @@ public class Game implements Runnable {
}
if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) || gameOverCheck.equals(
ClientGameInfoHandler.gameOverHumansWin)) {
if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) && getOgGhost().getIsPlayer()) {
OgGhostHighScore.addOgGhostWinner(getOgGhost().getName());
}
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck);
lobby.removeGameFromRunningGames(this);
lobby.addGameToFinishedGames(this);

View File

@ -93,7 +93,7 @@ public class OgGhostHighScore {
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 not already present, the following creates the file.
//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));
@ -103,17 +103,6 @@ public class OgGhostHighScore {
line = buffreader.readLine();
}
}
/*
addOgGhostWinner("Seraina");
ogGhostWinners.add("Jonas, the ultimate winner");
writeOgGhostWinnersToFile();
System.out.println(formatGhostHighscoreList());
*/
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -98,6 +98,9 @@ public class MessageFormatter {
case "/s":
stringBuilder.append(Protocol.startANewGame);
break;
case "/h":
stringBuilder.append(Protocol.highScoreList);
break;
default:
s = msg;
}

View File

@ -138,6 +138,13 @@ public class Protocol {
*/
public static final String votedFor = "CVOTE";
/**
* Client requests high score list.
*/
public static final String highScoreList = "HSCOR";
//SERVER TO CLIENT COMMANDS

View File

@ -4,6 +4,7 @@ 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.highscore.OgGhostHighScore;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger;
@ -594,4 +595,11 @@ public class ClientHandler implements Runnable {
}
public void sendHighScoreList() {
String list = OgGhostHighScore.formatGhostHighscoreList();
String[] listarray = list.split("\\R");
for (String s: listarray) {
sendAnnouncementToClient(s);
}
}
}

View File

@ -109,6 +109,9 @@ public class JServerProtocolParser {
case Protocol.listGames:
h.listGames();
break;
case Protocol.highScoreList:
h.sendHighScoreList();
break;
default:
System.out.println("Received unknown command");
}

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.highscore.OgGhostHighScore;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
@ -30,6 +31,7 @@ public class Server {
public void startServer() {
try {
System.out.println("Port " + gamePort + " is open.");
OgGhostHighScore.main(null);
while (!serverSocket.isClosed()) {
Socket socket = serverSocket.accept();
ClientHandler nextClient = new ClientHandler(socket, socket.getInetAddress());