feat: update player auth, fix bugs

This commit is contained in:
2026-04-09 00:36:23 +08:00
parent 7ffe7680ad
commit 222621139e
9 changed files with 118 additions and 36 deletions
+2 -2
View File
@@ -105,9 +105,9 @@ func _save_session(session: NakamaSession, mode: AuthMode) -> void:
func clear_session() -> void:
if FileAccess.file_exists(SESSION_FILE):
DirAccess.remove_absolute(ProjectSettings.globalize_path(SESSION_FILE))
DirAccess.remove_absolute(SESSION_FILE)
if FileAccess.file_exists(CREDENTIALS_FILE):
DirAccess.remove_absolute(ProjectSettings.globalize_path(CREDENTIALS_FILE))
DirAccess.remove_absolute(CREDENTIALS_FILE)
# =============================================================================
# Guest Authentication
+19 -7
View File
@@ -44,6 +44,11 @@ func _on_logged_out() -> void:
# =============================================================================
func load_profile() -> Dictionary:
# Reset state first to ensure no old account data carries over
profile = {}
stats = {}
is_profile_loaded = false
if not NakamaManager.session:
push_error("[UserProfileManager] No session available")
return {}
@@ -89,11 +94,14 @@ func load_profile() -> Dictionary:
# Auto-sync existing score to native Nakama leaderboard in background
if stats.get("high_score", 0) > 0 and NakamaManager.session and not AuthManager.is_guest:
_submit_to_leaderboard.call_deferred()
submit_to_leaderboard.call_deferred()
return profile
func load_stats() -> Dictionary:
# Reset stats first to ensure fresh data for new logins
stats = {}
if not NakamaManager.session:
return {}
@@ -104,12 +112,16 @@ func load_stats() -> Dictionary:
[NakamaStorageObjectId.new(STATS_COLLECTION, "game_stats", user_id)]
)
print("[UserProfileManager] Loading stats for user_id: ", user_id)
if not storage_result.is_exception() and storage_result.objects.size() > 0:
var stored_data = JSON.parse_string(storage_result.objects[0].value)
if stored_data:
stats = stored_data
print("[UserProfileManager] Stats loaded from Nakama: ", stats)
else:
# Initialize default stats
print("[UserProfileManager] No stats found in Nakama, creating defaults")
stats = {
"games_played": 0,
"games_won": 0,
@@ -191,7 +203,7 @@ func update_avatar(avatar_index: int) -> bool:
# Immediately update leaderboard with new avatar
if stats.get("high_score", 0) > 0:
_submit_to_leaderboard.call_deferred()
submit_to_leaderboard.call_deferred()
return success
@@ -277,20 +289,20 @@ func record_game_result(won: bool, score: int) -> void:
await update_stats(stats)
# Also submit to Nakama native leaderboard so global_high_score is populated
await _submit_to_leaderboard()
await submit_to_leaderboard()
func _submit_to_leaderboard() -> void:
func submit_to_leaderboard() -> void:
"""Submits the current high_score via server RPC (required for authoritative leaderboards)."""
if not NakamaManager.session:
return
if stats.get("high_score", 0) <= 0:
return
# We allow syncing even with 0 score, so the player appears on the leaderboard with their avatar/loadout
var payload = JSON.stringify({
"score": int(stats.get("high_score", 0)),
"games_played": int(stats.get("games_played", 0)),
"games_won": int(stats.get("games_won", 0)),
"avatar_url": profile.get("avatar_url", "")
"avatar_url": profile.get("avatar_url", ""),
"loadout_character": profile.get("loadout_character", "Copper")
})
var result = await NakamaManager.client.rpc_async(