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.HumanNPC;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanPlayer; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanPlayer;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; 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.ClientHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby; import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby;
import java.util.HashSet; import java.util.HashSet;
@ -64,6 +65,17 @@ public class Game implements Runnable {
isOngoing = ongoing; 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 * 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 * 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( if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) || gameOverCheck.equals(
ClientGameInfoHandler.gameOverHumansWin)) { ClientGameInfoHandler.gameOverHumansWin)) {
if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) && getOgGhost().getIsPlayer()) {
OgGhostHighScore.addOgGhostWinner(getOgGhost().getName());
}
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck); lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck);
lobby.removeGameFromRunningGames(this); lobby.removeGameFromRunningGames(this);
lobby.addGameToFinishedGames(this); lobby.addGameToFinishedGames(this);

View File

@ -93,7 +93,7 @@ public class OgGhostHighScore {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
ogGhostWinners = new ArrayList<>(); 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 already present, it reads what's already in the file into the ogGhostWinners array.
if (!ogGhostFile.createNewFile()) { if (!ogGhostFile.createNewFile()) {
BufferedReader buffreader = new BufferedReader(new FileReader(ogGhostFile)); BufferedReader buffreader = new BufferedReader(new FileReader(ogGhostFile));
@ -103,17 +103,6 @@ public class OgGhostHighScore {
line = buffreader.readLine(); line = buffreader.readLine();
} }
} }
/*
addOgGhostWinner("Seraina");
ogGhostWinners.add("Jonas, the ultimate winner");
writeOgGhostWinnersToFile();
System.out.println(formatGhostHighscoreList());
*/
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

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

View File

@ -138,6 +138,13 @@ public class Protocol {
*/ */
public static final String votedFor = "CVOTE"; public static final String votedFor = "CVOTE";
/**
* Client requests high score list.
*/
public static final String highScoreList = "HSCOR";
//SERVER TO CLIENT COMMANDS //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.Game;
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 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.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger; 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: case Protocol.listGames:
h.listGames(); h.listGames();
break; break;
case Protocol.highScoreList:
h.sendHighScoreList();
break;
default: default:
System.out.println("Received unknown command"); 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.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.highscore.OgGhostHighScore;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@ -30,6 +31,7 @@ public class Server {
public void startServer() { public void startServer() {
try { try {
System.out.println("Port " + gamePort + " is open."); System.out.println("Port " + gamePort + " is open.");
OgGhostHighScore.main(null);
while (!serverSocket.isClosed()) { while (!serverSocket.isClosed()) {
Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept();
ClientHandler nextClient = new ClientHandler(socket, socket.getInetAddress()); ClientHandler nextClient = new ClientHandler(socket, socket.getInetAddress());