Developer Console

Leaderboards and Tournaments Tutorial - Enter Tournament or Match

Return to Tutorial Overview


This step creates the code for a player to join a competition. Because a player can submit multiple scores to a competition, there are two API methods. When the player first enters the tournament, call Enter Tournament. The response to this call includes a match id. Successive entries to the same competition use this match id and call Enter Match. To decide which method to use, we store the tournament id after entering the tournament. If the stored tournament id exists, we can call Enter Match:

if (!hasTournamentId()) {
    val tournamentResponse = gameOnApi.enterTournamentUsingPOST(
            tournamentId,
            EnterTournamentRequest(playerAttributes = mapOf("a" to "b")),
            sessionId,
            apiKey)

    saveTournamentId(tournamentResponse.tournamentId)
    saveMatchId(tournamentResponse.matchId)
} else {
    gameOnSession.tournamentId = loadTournamentId()
    gameOnSession.matchId = loadMatchId()

    val response = gameOnApi.enterMatchUsingPOST(
            gameOnSession.matchId!!,
            gameOnSession.sessionId!!,
            gameOnSession.apiKey)

    require(response.attemptsRemaining >= 0, { "No more matches remaining." })
}

We also check the response from Enter Match to ensure that we have remaining attempts available.

Putting all this together, we can now register, authenticate, and then join a competition, all initiated from the button on the splash screen:

competeButton.setOnClickListener {
    prefs.edit().putString("playerName", playerName.text.toString()).apply()
    GameState.session = startGame(3)

    AuthenticateGameOnTask(gameOnSession!!, prefs) {
        EnterMatchTask(gameOnSession!!, prefs) {
            if (gameOnSession!!.sessionId != null && gameOnSession!!.matchId != null) {
                GameState.session = startGame(3)
                startActivity(Intent(this, QuizActivity::class.java))
            } else {
                Toast.makeText(this, "Failed to connect to Leaderboards and Tournaments.", Toast.LENGTH_LONG).show()
            }
        }.execute()
    }.execute()
}

The full source code for this functionality is available in the step-4 branch.

In the next step, we will submit a score and get the leaderboard after the player completes the quiz.

Next - Submit Score and Get Leaderboard