Developer Console

Leaderboards and Tournaments Tutorial - Submit Score and Get Leaderboard

Return to Tutorial Overview


To submit a score, we call Submit Score, and provide the match id and the player's score:

gameOnApi.submitScoreUsingPUT(
        gameOnSession.matchId!!,
        SubmitScoreRequest(score.toLong()),
        sessionId,
        apiKey)

Getting the leaderboard is also not too difficult, but we need to use some Android UI controls to display the table of players. To keep this tutorial as simple as possible, we use a single column RecyclerView. First, the code to get the leaderboard:

val response = gameOnApi.getMatchLeaderboardUsingGET(
                gameOnSession.matchId!!,
                "",
                "",
                "32",
                apiKey,
                "",
                "",
                "",
                "",
                sessionId,
                "",
                "")

There are several fields here that are not needed. We only need the match id, session id, and the API key. Along with other details, the response includes a list of players and their ranks. For simplicity, we hardcoded the number of entries to retrieve as 32.

We wrap Submit Score and Get Leaderboard in AsyncTasks and call them in a nested fashion, just as we did with Authenticate and Enter Match. The nested calls look like this:

SubmitScoreTask(GameState.gameOnSession!!, score) {
    LoadLeaderboardTask(GameState.gameOnSession!!) { ld ->
        leaderboardData.clear()
        leaderboardData.addAll(ld)
        viewAdapter.notifyDataSetChanged()
    }.execute()
}.execute()

As with previous steps, the entire source for these changes is in the step-5 branch. There are more changes in this branch than others due to adding the RecyclerView and all of the accompanying UI elements.

Next - Wrap Up