Minigame Leaderboard - UQcsse3200/2024-studio-2 GitHub Wiki

1. Overview/Description

The Leaderboard feature provides players with a visual representation of their scores and the scores of other players in the minigame. It allows players to see their current ranking and compare their performance with others. This feature enhances the competitive aspect of the game by motivating players to improve their scores. The leaderboard displays three different types of rankings: Bird, Snake, and Fish, each representing distinct score categories or game modes. image

2. Features:

There are some features of mini map, including:

  • Real-Time Updating: Player scores are updated dynamically as the game progresses, reflecting changes immediately on the leaderboard.
  • Rank Sorting: The scores are sorted from highest to lowest, displaying the top-ranked players first.
  • Highlighting: The top players' scores are highlighted.
  • Refresh Button: Allows users to manually refresh the leaderboard to ensure the latest scores are displayed.

3. Implementation:

3.1 PlayFab Leaderboard set up

In order to save and load players' scores to the leaderboard, we will create 2 functions in the PlayFab class. WE need to get the leaderboard firstly, and update the minigame usernames and highscores:

public static void getLeaderboard(String gameName){
        GetLeaderboardRequest request = new GetLeaderboardRequest();
        request.StatisticName = gameName;
        request.MaxResultsCount = 10;
        PlayFabResult<GetLeaderboardResult> result = PlayFabClientAPI.GetLeaderboard(request);
        // Clean the arrays before updating them
        minigameUsernames.clear();
        minigameHighscores.clear();

        if (result.Result != null) {
            // Update UI with leaderboard data
            List<PlayerLeaderboardEntry> leaderboard = result.Result.Leaderboard;
            for (int i = 0; i < leaderboard.size(); i++) {
                minigameUsernames.add(leaderboard.get(i).DisplayName);
                minigameHighscores.add(String.valueOf(leaderboard.get(i).StatValue));
                System.out.println("User " + i + ": " + minigameUsernames.get(i) + " - " + minigameHighscores.get(i));
            }
        } else {
            logger.error("Failed to retrieve leaderboard: " + result.Error.errorMessage);
        }
    }

Then, there is a function to submit the score to PlayFab Leaderboard service, which will be automatically updated with the highest scores.

public static void submitScore(String gameName, int score) {
        if (!isLogin) {
            logger.info("You need to login to put your score to the leaderboard.");
            return;
        }
        UpdatePlayerStatisticsRequest request = new UpdatePlayerStatisticsRequest();

        StatisticUpdate statUpdate = new StatisticUpdate();
        statUpdate.StatisticName = gameName;
        statUpdate.Value = score;

        ArrayList<StatisticUpdate> statisticUpdates = new ArrayList<>();
        statisticUpdates.add(statUpdate);

        request.Statistics = statisticUpdates;

        PlayFabResult<UpdatePlayerStatisticsResult> result = PlayFabClientAPI.UpdatePlayerStatistics(request);

        if (result.Result != null) {
            logger.info("Player score submitted successfully.");
        } else {
            logger.error("Failed to submit score: " + result.Error.errorMessage);
        }
    }

3.2 Initialize the minigame leaderboard

We need to instantiate playFab with the titleId then set the current index of the leaderboard to 0.

public MinigameLeaderboard() {
        super();
        playFab = new PlayFab("DBB26");
        gameName = new ArrayList<>();
        gameName.add("Snake");
        gameName.add("Bird");
        gameName.add("Fish");
        currentIdx = 0;
}

3.3 Refresh leaderboard

The leaderboard will refresh when you click to the refresh button. If the given string is "current", it won't update the index and update the leaderboard based on current index. Otherwise, if the given string is "Bird", "Snake" and "Fish" it will update current index and update the leaderboard.

    public void refreshLeaderboard(String name) {
        if (!name.equals("Current")) {
            currentIdx = gameName.indexOf(name);
        }
        PlayFab.submitScore(gameName.get(currentIdx), 0);
        PlayFab.getLeaderboard(gameName.get(currentIdx));
        updateLeaderboard();
        updateUI();
    }

4. UML

image