Skip to content

Commit

Permalink
Update achievements documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Iakobs committed Dec 11, 2023
1 parent 48d5726 commit 6346e4c
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 52 deletions.
4 changes: 2 additions & 2 deletions plugin/demo/MainMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ extends Control

func _ready() -> void:
if GodotPlayGameServices.android_plugin:
SignInClient.is_user_authenticated.connect(func(is_authenticated: bool):
SignInClient.user_authenticated.connect(func(is_authenticated: bool):
print("Is user authenticated?: %s" % is_authenticated)
)
button.pressed.connect(func():
GodotPlayGameServices.android_plugin.showAllLeaderboards()
GodotPlayGameServices.android_plugin.showAchievements()
)
12 changes: 6 additions & 6 deletions plugin/export_scripts_template/autoloads/sign_in_client.gd
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
extends Node

signal is_user_authenticated
signal is_user_signed_in
signal user_authenticated
signal user_signed_in

func _ready() -> void:
connect_signals()

func connect_signals() -> void:
GodotPlayGameServices.android_plugin.isUserAuthenticated.connect(func(is_authenticated: bool):
is_user_authenticated.emit(is_authenticated)
GodotPlayGameServices.android_plugin.userAuthenticated.connect(func(is_authenticated: bool):
user_authenticated.emit(is_authenticated)
)
GodotPlayGameServices.android_plugin.isUserSignedIn.connect(func(is_signed_in: bool):
is_user_signed_in.emit(is_signed_in)
GodotPlayGameServices.android_plugin.userSignedIn.connect(func(is_signed_in: bool):
user_signed_in.emit(is_signed_in)
)

func is_authenticated() -> void:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ class GodotAndroidPlugin(godot: Godot) : GodotPlugin(godot) {
* Use this method to check if the user is already authenticated. If the user is authenticated,
* a popup will be shown on screen.
*
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.isUserAuthenticated] signal.
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.userAuthenticated] signal.
*/
@UsedByGodot
fun isAuthenticated() = signInProxy.isAuthenticated()

/**
* Use this method to provide a manual way to the user for signing in.
*
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.isUserSignedIn] signal.
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.userSignedIn] signal.
*/
@UsedByGodot
fun signIn() = signInProxy.signIn()
Expand All @@ -74,26 +74,54 @@ class GodotAndroidPlugin(godot: Godot) : GodotPlugin(godot) {
* Use this method to increment a given achievement in the given amount. For normal achievements,
* use the [unlockAchievement] method instead.
*
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.isAchievementUnlocked] signal.
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.achievementUnlocked] signal.
*
* @param achievementId The achievement Id.
* @param achievementId The achievement id.
* @param amount The number of steps to increment by. Must be greater than 0.
*/
@UsedByGodot
fun incrementAchievement(achievementId: String, amount: Int) =
achievementsProxy.incrementAchievement(achievementId, amount)

/**
* Call this method and subscribe to the emitted signal to receive the list of the game
* achievements in JSON format. The JSON received from the [com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.achievementsLoaded]
* signal contains a representation of the [com.google.android.gms.games.achievement.Achievement](https://developers.google.com/android/reference/com/google/android/gms/games/achievement/Achievement) class.
*
* @param forceReload If true, this call will clear any locally cached data and attempt to fetch
* the latest data from the server.
*/
@UsedByGodot
fun loadAchievements(forceReload: Boolean) =
achievementsProxy.loadAchievements(forceReload)

/**
* Use this method to reveal a hidden achievement to the current signed player. If the achievement
* is already unlocked, this method will have no effect.
*
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.achievementRevealed] signal.
*
* @param achievementId The achievement id.
*/
@UsedByGodot
fun revealAchievement(achievementId: String) =
achievementsProxy.revealAchievement(achievementId)

/**
* Use this method to open a new window with the achievements of the game, and the progress of
* the player to unlock those achievements.
*/
@UsedByGodot
fun showAchievements() = achievementsProxy.showAchievements()

/**
* Immediately unlocks the given achievement for the signed in player. If the achievement is
* secret, it will be revealed to the player.
*
* The method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.achievementUnlocked] signal.
*
* @param achievementId The achievement id.
*/
@UsedByGodot
fun unlockAchievement(achievementId: String) =
achievementsProxy.unlockAchievement(achievementId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import com.google.android.gms.games.AchievementsClient
import com.google.android.gms.games.PlayGames
import com.google.gson.Gson
import com.jacobibanez.plugin.android.godotplaygameservices.BuildConfig
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.isAchievementUnlocked
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.loadAchievementsFailure
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.loadAchievementsSuccess
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.revealAchievementFailure
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.revealAchievementSuccess
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.unlockAchievementFailure
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.unlockAchievementSuccess
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.achievementUnlocked
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.achievementsLoaded
import com.jacobibanez.plugin.android.godotplaygameservices.signals.AchievementsSignals.achievementRevealed
import org.godotengine.godot.Dictionary
import org.godotengine.godot.Godot
import org.godotengine.godot.plugin.GodotPlugin.emitSignal
Expand All @@ -34,14 +30,14 @@ class AchievementsProxy(
tag,
"Achievement $achievementId incremented successfully. Unlocked? ${task.result}"
)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, isAchievementUnlocked, task.result)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementUnlocked, task.result)
} else {
Log.e(
tag,
"Achievement $achievementId not incremented. Cause: ${task.exception}",
task.exception
)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, isAchievementUnlocked, false)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementUnlocked, false)
}
}
}
Expand All @@ -62,10 +58,11 @@ class AchievementsProxy(
emptyList()
}

emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, loadAchievementsSuccess, Gson().toJson(achievements))
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementsLoaded, Gson().toJson(achievements))
} else {
Log.e(tag, "Failed to load achievements. Cause: ${task.exception}", task.exception)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, loadAchievementsFailure)
val emptyResponse: List<Dictionary> = listOf(Dictionary())
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementsLoaded, Gson().toJson(emptyResponse))
}
}
}
Expand All @@ -75,14 +72,14 @@ class AchievementsProxy(
achievementsClient.revealImmediate(achievementId).addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(tag, "Achievement $achievementId revealed")
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, revealAchievementSuccess)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementRevealed, true)
} else {
Log.e(
tag,
"Achievement $achievementId not revealed. Cause: ${task.exception}",
task.exception
)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, revealAchievementFailure)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementRevealed, false)
}
}
}
Expand All @@ -102,14 +99,14 @@ class AchievementsProxy(
achievementsClient.unlockImmediate(achievementId).addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(tag, "Achievement with id $achievementId unlocked")
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, unlockAchievementSuccess)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementUnlocked, true)
} else {
Log.e(
tag,
"Error unlocking achievement $achievementId. Cause: ${task.exception}",
task.exception
)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, unlockAchievementFailure)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, achievementUnlocked, false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import org.godotengine.godot.plugin.SignalInfo

/** @suppress */
fun getSignals(): MutableSet<SignalInfo> = mutableSetOf(
SignInSignals.isUserAuthenticated,
SignInSignals.isUserSignedIn,
SignInSignals.userAuthenticated,
SignInSignals.userSignedIn,

AchievementsSignals.isAchievementUnlocked,
AchievementsSignals.loadAchievementsSuccess,
AchievementsSignals.loadAchievementsFailure,
AchievementsSignals.revealAchievementSuccess,
AchievementsSignals.revealAchievementFailure,
AchievementsSignals.unlockAchievementSuccess,
AchievementsSignals.unlockAchievementFailure,
AchievementsSignals.achievementUnlocked,
AchievementsSignals.achievementsLoaded,
AchievementsSignals.achievementRevealed,

LeaderboardSignals.submitScoreSuccess,
LeaderboardSignals.submitScoreFailure,
Expand All @@ -31,33 +27,42 @@ object SignInSignals {
*
* @return `true` if the user is authenticated. `false` otherwise.
*/
val isUserAuthenticated = SignalInfo("isUserAuthenticated", Boolean::class.javaObjectType)
val userAuthenticated = SignalInfo("userAuthenticated", Boolean::class.javaObjectType)

/**
* This signal is emitted when calling the [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.signIn] method.
*
* @return `true` if the user is signed in. `false` otherwise.
*
*/
val isUserSignedIn = SignalInfo("isUserSignedIn", Boolean::class.javaObjectType)
val userSignedIn = SignalInfo("userSignedIn", Boolean::class.javaObjectType)
}

/**
* Signals emitted by Achievements methods
*/
object AchievementsSignals {
/**
* This signal is emitted when calling the [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.incrementAchievement] method.
* This signal is emitted when calling the [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.incrementAchievement]
* or [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.unlockAchievement] methods.
*
* @return `true` if the achievement is unlocked. `false` otherwise.
*/
val isAchievementUnlocked = SignalInfo("isAchievementUnlocked", Boolean::class.javaObjectType)
val loadAchievementsSuccess = SignalInfo("loadAchievementsSuccess", String::class.java)
val loadAchievementsFailure = SignalInfo("loadAchievementsFailure")
val revealAchievementSuccess = SignalInfo("revealAchievement")
val revealAchievementFailure = SignalInfo("revealAchievement")
val unlockAchievementSuccess = SignalInfo("unlockAchievementSuccess")
val unlockAchievementFailure = SignalInfo("unlockAchievementFailure")
val achievementUnlocked = SignalInfo("achievementUnlocked", Boolean::class.javaObjectType)

/**
* This signal is emitted when calling the [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.loadAchievements] method.
*
* @return A JSON with a list of [com.google.android.gms.games.achievement.Achievement](https://developers.google.com/android/reference/com/google/android/gms/games/achievement/Achievement).
*/
val achievementsLoaded = SignalInfo("achievementsLoaded", String::class.java)

/**
* This signal is emitted when calling the [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.revealAchievement] method.
*
* @return `true` if the achievement is revealed. `false` otherwise.
*/
val achievementRevealed = SignalInfo("achievementRevealed", Boolean::class.javaObjectType)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import android.util.Log
import com.google.android.gms.games.GamesSignInClient
import com.google.android.gms.games.PlayGames
import com.jacobibanez.plugin.android.godotplaygameservices.BuildConfig
import com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.isUserAuthenticated
import com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.isUserSignedIn
import com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.userAuthenticated
import com.jacobibanez.plugin.android.godotplaygameservices.signals.SignInSignals.userSignedIn
import org.godotengine.godot.Godot
import org.godotengine.godot.plugin.GodotPlugin.emitSignal

Expand All @@ -24,15 +24,15 @@ class SignInProxy(
emitSignal(
godot,
BuildConfig.GODOT_PLUGIN_NAME,
isUserAuthenticated,
userAuthenticated,
task.result.isAuthenticated
)
} else {
Log.e(tag, "User not authenticated. Cause: ${task.exception}", task.exception)
emitSignal(
godot,
BuildConfig.GODOT_PLUGIN_NAME,
isUserAuthenticated,
userAuthenticated,
false
)
}
Expand All @@ -44,10 +44,10 @@ class SignInProxy(
gamesSignInClient.signIn().addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(tag, "User signed in: ${task.result.isAuthenticated}")
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, isUserSignedIn, task.result.isAuthenticated)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, userSignedIn, task.result.isAuthenticated)
} else {
Log.e(tag, "User not signed in. Cause: ${task.exception}", task.exception)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, isUserSignedIn, false)
emitSignal(godot, BuildConfig.GODOT_PLUGIN_NAME, userSignedIn, false)
}
}
}
Expand Down

0 comments on commit 6346e4c

Please sign in to comment.