Coverage Summary for Class: Leaderboard (io.github.unisim)

Class Class, % Method, % Branch, % Line, %
Leaderboard 100% (1/1) 22.2% (2/9) 14.3% (2/14) 28% (14/50)


 package io.github.unisim;
 
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.files.FileHandle;
 import com.badlogic.gdx.utils.Json;
 import com.badlogic.gdx.utils.JsonValue;
 
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
 /**
  * A leaderboard to store, sort and display player scores
  */
 public class Leaderboard implements Json.Serializable {
     private static final String LEADERBOARD_FILE = "Leaderboard.txt";
 
     private FileHandle file;
     private List<Score> scores;
 
     /**
      * Constructor: Initialises the leaderboard and loads player names + scores from
      * the file.
      * Creates a new file if the leaderboard file doesn't exist.
      */
     public Leaderboard() {
         Json json = new Json();
         try {
             file = Gdx.files.local(LEADERBOARD_FILE);
         } catch (Exception e) {
             System.out.println("Creating new file due to error: " + e.getMessage());
             file = Gdx.files.local(LEADERBOARD_FILE);
         }
 
         // Initialises file if it doesn't exist
         if (!file.exists()) {
             file.writeString("[]", false); // Create an empty JSON array if file does not exist
         }
 
         scores = new ArrayList<>();
         loadScores(json); // Load scores from the file
     }
 
     /**
      * Finds player score based on their player namer
      * 
      * @param playerName - Player Name - String
      * @return - Returns score of the player or null
      */
     private Score findScoreByPlayer(String playerName) {
         for (Score score : scores) {
             if (score.getPlayerName().equalsIgnoreCase(GameState.settings.getPlayerName())) {
                 return score;
             }
         }
         return null; // Null if no player score found
     }
 
     /**
      * Adds/updates player score
      * 
      * @param playerName - Player Name - String
      * @param points     - Player Score - Integer
      */
     public void addScore(String playerName, int points) {
         Score existingScore = findScoreByPlayer(playerName);
         if (existingScore != null) {
             // Replace the score if the new one is higher
             if (points > existingScore.getScore()) {
                 existingScore.setScore(points);
             }
         } else {
             // Add a new score if the player doesn't already exist
             Score newScore = new Score(playerName, points);
             scores.add(newScore);
         }
         Collections.sort(scores);
         saveScores();
     }
 
     /**
      * Returns top N scores from the leaderboard
      * 
      * @param count - Number of scores to return - Integer
      * @return - Top N scores - List
      */
     public List<Score> getTopScores(int count) {
         try {
             return scores.subList(0, Math.min(count, scores.size()));
         } catch (Exception e) {
             file.writeString("[]", false);
             scores = new ArrayList<>();
             return scores.subList(0, Math.min(count, scores.size()));
         }
     }
 
     /**
      * Saves scores to the JSON file
      */
     private void saveScores() {
         Json json = new Json();
         file.writeString(json.toJson(scores), false);
     }
 
     /**
      * Loads scores from the JSON file
      * 
      * @param json - JSON object used for serialisation
      */
     @SuppressWarnings("unchecked")
     private void loadScores(Json json) {
         if (file.exists()) {
             try {
                 String jsonString = file.readString();
                 scores = json.fromJson(ArrayList.class, Score.class, jsonString);
             } catch (Exception e) {
                 System.out.println("Error loading scores: " + e.getMessage());
                 scores = new ArrayList<>();
             }
         }
     }
 
     /**
      * Prints the leaderboard for debugging
      */
     public void printLeaderboard() {
         for (Score score : scores) {
             System.out.println(score.getPlayerName() + ": " + score.getScore());
         }
     }
 
     /**
      * Serialises the scores to JSON
      * 
      * @param json - JSON object used for serialisation
      */
     @Override
     public void write(Json json) {
         json.writeValue("scores", scores);
     }
 
     /**
      * Deserialises the scores from JSON
      * 
      * @param json     - JSON object used for serialisation
      * @param jsonData - JSON value containing the data
      */
     @SuppressWarnings("unchecked")
     @Override
     public void read(Json json, JsonValue jsonData) {
         scores = json.readValue("scores", ArrayList.class, Score.class, jsonData);
     }
 }