Skip to content

Commit

Permalink
godot 3.3 -- add ability to get player hiscore and rank
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozzadar committed Jun 9, 2021
1 parent de8ebc2 commit 93669a7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
9 changes: 0 additions & 9 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions app/src/main/java/io/cgisca/godot/gpgs/PlayGameServicesGodot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class PlayGameServicesGodot(godot: Godot) : GodotPlugin(godot), AchievementsList
SignalInfo("_on_achievement_steps_setting_failed", String::class.java)
val SIGNAL_ACHIEVEMENT_INFO_LOAD = SignalInfo("_on_achievement_info_loaded", String::class.java)
val SIGNAL_ACHIEVEMENT_INFO_LOAD_FAILED = SignalInfo("_on_achievement_info_load_failed", String::class.java)
val SIGNAL_LEADERBOARD_SCORE_RETRIEVED = SignalInfo("_on_leaderboard_score_retrieved", String::class.java)
val SIGNAL_LEADERBOARD_SCORE_RETRIEVED_FAILED = SignalInfo("_on_leaderboard_score_retrieve_failed")
val SIGNAL_LEADERBOARD_SCORE_SUBMITTED = SignalInfo("_on_leaderboard_score_submitted", String::class.java)
val SIGNAL_LEADERBOARD_SCORE_SUBMITTED_FAILED =
SignalInfo("_on_leaderboard_score_submitting_failed", String::class.java)
Expand Down Expand Up @@ -103,6 +105,7 @@ class PlayGameServicesGodot(godot: Godot) : GodotPlugin(godot), AchievementsList
"incrementAchievement",
"setAchievementSteps",
"loadAchievementInfo",
"retrieveLeaderboardScore",
"showLeaderBoard",
"showAllLeaderBoards",
"submitLeaderBoardScore",
Expand Down Expand Up @@ -133,6 +136,8 @@ class PlayGameServicesGodot(godot: Godot) : GodotPlugin(godot), AchievementsList
SIGNAL_ACHIEVEMENT_STEPS_SET_FAILED,
SIGNAL_ACHIEVEMENT_INFO_LOAD,
SIGNAL_ACHIEVEMENT_INFO_LOAD_FAILED,
SIGNAL_LEADERBOARD_SCORE_RETRIEVED,
SIGNAL_LEADERBOARD_SCORE_RETRIEVED_FAILED,
SIGNAL_LEADERBOARD_SCORE_SUBMITTED,
SIGNAL_LEADERBOARD_SCORE_SUBMITTED_FAILED,
SIGNAL_EVENT_SUBMITTED,
Expand Down Expand Up @@ -281,6 +286,13 @@ class PlayGameServicesGodot(godot: Godot) : GodotPlugin(godot), AchievementsList
}
}

fun retrieveLeaderboardScore(leaderBoardId: String, span: Int, leaderboardCollection: String) {
runOnUiThread {

leaderboardsController.retrieveLeaderboardScore(leaderBoardId, span, leaderboardCollection)
}
}

fun submitLeaderBoardScore(leaderBoardId: String, score: Int) {
runOnUiThread {
leaderboardsController.submitScore(leaderBoardId, score)
Expand Down Expand Up @@ -395,6 +407,14 @@ class PlayGameServicesGodot(godot: Godot) : GodotPlugin(godot), AchievementsList
emitSignal(SIGNAL_EVENTS_LOADED_FAILED.name)
}

override fun onCurrentPlayerLeaderBoardScoreLoadingFailed() {
emitSignal(SIGNAL_LEADERBOARD_SCORE_RETRIEVED_FAILED.name)
}

override fun onCurrentPlayerLeaderBoardScoreLoaded(scoreJson: String) {
emitSignal(SIGNAL_LEADERBOARD_SCORE_RETRIEVED.name, scoreJson)
}

override fun onLeaderBoardScoreSubmitted(leaderboardId: String) {
emitSignal(SIGNAL_LEADERBOARD_SCORE_SUBMITTED.name, leaderboardId)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.cgisca.godot.gpgs.leaderboards

interface LeaderBoardsListener {
fun onCurrentPlayerLeaderBoardScoreLoadingFailed()
fun onCurrentPlayerLeaderBoardScoreLoaded(scoreJson: String)
fun onLeaderBoardScoreSubmitted(leaderboardId: String)
fun onLeaderBoardScoreSubmittingFailed(leaderboardId: String)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.cgisca.godot.gpgs.leaderboards

import android.app.Activity
import android.util.Log
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.games.Games
import com.google.android.gms.games.leaderboard.LeaderboardVariant.COLLECTION_FRIENDS
import com.google.android.gms.games.leaderboard.LeaderboardVariant.COLLECTION_PUBLIC
import com.google.gson.Gson
import io.cgisca.godot.gpgs.ConnectionController
import io.cgisca.godot.gpgs.model.LeaderboardScore
import java.util.Locale

class LeaderboardsController(
private val activity: Activity,
Expand All @@ -15,6 +21,51 @@ class LeaderboardsController(
const val RC_LEADERBOARD_UI = 9004
}

fun retrieveLeaderboardScore(leaderboardId: String, span: Int, leaderboardCollection: String) {
val googleSignInAccount = GoogleSignIn.getLastSignedInAccount(activity)

var collection = COLLECTION_PUBLIC

if (leaderboardCollection.toLowerCase(Locale.ROOT).contains("friends")) {
collection = COLLECTION_FRIENDS
}

if (connectionController.isConnected().first && googleSignInAccount != null) {
Log.i("godot", "-------------------\n\n THE LEADERBOARD:\n ${leaderboardId}\n\n")

Games.getLeaderboardsClient(activity, googleSignInAccount)
.loadPlayerCenteredScores(leaderboardId, span, collection, 1)
.addOnSuccessListener { lbScores ->
// val scores = lbScores.get();
// Log.i("godot", "-------------------\n\n THE RESULT:\n ${scores.scores[0].rank}\n\n")

val leaderboardScore = LeaderboardScore (
-1, 0, "Unknown"
)

if (lbScores != null) {
val scores = lbScores.get();

if (scores != null && scores.scores.count != 0) {
leaderboardScore.rank = scores.scores[0].rank
leaderboardScore.score = scores.scores[0].rawScore
leaderboardScore.scoreHolder = scores.scores[0].scoreHolderDisplayName
}
}

leaderBoardsListener.onCurrentPlayerLeaderBoardScoreLoaded(Gson().toJson(leaderboardScore))
}
.addOnFailureListener {reason ->
Log.i("godot", "-------------------\n\n FAILURE REASON:\n ${reason}\n\n")
Log.i("godot", "-------------------\n\n FAILURE REASON:\n ${reason.message}\n\n")

leaderBoardsListener.onCurrentPlayerLeaderBoardScoreLoadingFailed()
}

}

}

fun submitScore(leaderboardId: String, score: Int) {
val googleSignInAccount = GoogleSignIn.getLastSignedInAccount(activity)
if (connectionController.isConnected().first && googleSignInAccount != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.cgisca.godot.gpgs.model

import com.google.gson.annotations.SerializedName

data class LeaderboardScore(
@SerializedName("rank") var rank: Long,
@SerializedName("score") var score: Long,
@SerializedName("scoreHolder") var scoreHolder: String,
)
2 changes: 1 addition & 1 deletion godot-lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configurations.maybeCreate("default")
artifacts.add("default", file('godot-lib.3.3.stable.release.aar'))
artifacts.add("default", file('godot-li.3.2.stable.release.aar'))

0 comments on commit 93669a7

Please sign in to comment.