feat: update player auth, fix bugs
This commit is contained in:
@@ -468,6 +468,7 @@ function rpcGetLeaderboardStats(ctx, logger, nk, payload) {
|
||||
username: record.username,
|
||||
display_name: record.displayName || record.username,
|
||||
avatar_url: metadata.avatar_url || "",
|
||||
loadout_character: metadata.loadout_character || "Copper",
|
||||
high_score: record.score || 0,
|
||||
games_played: metadata.games_played || 0,
|
||||
games_won: metadata.games_won || 0
|
||||
@@ -492,7 +493,8 @@ function rpcSubmitScore(ctx, logger, nk, payload) {
|
||||
var metadata = {
|
||||
games_played: request.games_played || 0,
|
||||
games_won: request.games_won || 0,
|
||||
avatar_url: account.user.avatarUrl || request.avatar_url || ""
|
||||
avatar_url: account.user.avatarUrl || request.avatar_url || "",
|
||||
loadout_character: request.loadout_character || "Copper"
|
||||
};
|
||||
nk.leaderboardRecordWrite(
|
||||
"global_high_score",
|
||||
@@ -523,7 +525,6 @@ function rpcSyncLeaderboard(ctx, logger, nk, payload) {
|
||||
var userId = obj.userId;
|
||||
var value;
|
||||
try {
|
||||
// obj.value may already be an object or a string depending on Nakama version
|
||||
value = (typeof obj.value === "string") ? JSON.parse(obj.value) : obj.value;
|
||||
} catch (e) { continue; }
|
||||
if (!value) continue;
|
||||
@@ -533,13 +534,43 @@ function rpcSyncLeaderboard(ctx, logger, nk, payload) {
|
||||
high_score: value.high_score || 0,
|
||||
games_played: value.games_played || 0,
|
||||
games_won: value.games_won || 0,
|
||||
avatar_url: value.avatar_url || ""
|
||||
avatar_url: value.avatar_url || "",
|
||||
loadout_character: value.loadout_character || ""
|
||||
};
|
||||
} else {
|
||||
userGroup[userId].high_score = Math.max(userGroup[userId].high_score, value.high_score || 0);
|
||||
userGroup[userId].games_played = Math.max(userGroup[userId].games_played, value.games_played || 0);
|
||||
userGroup[userId].games_won = Math.max(userGroup[userId].games_won, value.games_won || 0);
|
||||
}
|
||||
|
||||
// Prioritize avatar and character from game_stats or if current is empty
|
||||
if (obj.key === "game_stats" || !userGroup[userId].avatar_url) {
|
||||
if (value.avatar_url) userGroup[userId].avatar_url = value.avatar_url;
|
||||
if (value.loadout_character) userGroup[userId].loadout_character = value.loadout_character;
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Read profiles collection to get loadout and avatars!
|
||||
var profileResult = nk.storageList(null, "profiles", 100, "");
|
||||
var profileObjects = profileResult.objects || [];
|
||||
for (var i = 0; i < profileObjects.length; i++) {
|
||||
var obj = profileObjects[i];
|
||||
var userId = obj.userId;
|
||||
if (obj.key !== "profile") continue;
|
||||
var value;
|
||||
try {
|
||||
value = (typeof obj.value === "string") ? JSON.parse(obj.value) : obj.value;
|
||||
} catch (e) { continue; }
|
||||
|
||||
if (!userGroup[userId]) {
|
||||
userGroup[userId] = {
|
||||
high_score: 0, games_played: 0, games_won: 0,
|
||||
avatar_url: "", loadout_character: ""
|
||||
};
|
||||
}
|
||||
// If the profile has avatar or loadout, merge them. Natively preferred over empty
|
||||
if (value.avatar_url && !userGroup[userId].avatar_url) userGroup[userId].avatar_url = value.avatar_url;
|
||||
if (value.loadout_character && !userGroup[userId].loadout_character) userGroup[userId].loadout_character = value.loadout_character;
|
||||
}
|
||||
|
||||
var count = 0;
|
||||
@@ -551,7 +582,8 @@ function rpcSyncLeaderboard(ctx, logger, nk, payload) {
|
||||
var meta = {
|
||||
games_played: stats.games_played || 0,
|
||||
games_won: stats.games_won || 0,
|
||||
avatar_url: stats.avatar_url || account.user.avatarUrl || ""
|
||||
avatar_url: stats.avatar_url || account.user.avatarUrl || "res://assets/graphics/character_selection/sc_characters/sc_copper.png",
|
||||
loadout_character: stats.loadout_character || "Copper"
|
||||
};
|
||||
nk.leaderboardRecordWrite("global_high_score", uid, account.user.username, stats.high_score, 0, meta);
|
||||
count++;
|
||||
@@ -724,10 +756,11 @@ function rpcAdminUpdateStats(ctx, logger, nk, payload) {
|
||||
var metadata = {
|
||||
games_played: stats.games_played || 0,
|
||||
games_won: stats.games_won || 0,
|
||||
avatar_url: account.user.avatarUrl || ""
|
||||
avatar_url: account.user.avatarUrl || "",
|
||||
loadout_character: stats.loadout_character || "Copper"
|
||||
};
|
||||
|
||||
nk.leaderboardRecordWrite("global_high_score", targetUserId, account.user.username, score, subscore, JSON.stringify(metadata));
|
||||
nk.leaderboardRecordWrite("global_high_score", targetUserId, account.user.username, score, subscore, metadata);
|
||||
|
||||
logger.info("Stats updated for user " + targetUserId + " by admin " + ctx.userId + " (game_stats + Native)");
|
||||
return JSON.stringify({ success: true });
|
||||
@@ -792,7 +825,8 @@ function rpcAdminSyncLeaderboard(ctx, logger, nk, payload) {
|
||||
high_score: value.high_score || 0,
|
||||
games_played: value.games_played || 0,
|
||||
games_won: value.games_won || 0,
|
||||
avatar_url: ""
|
||||
avatar_url: "",
|
||||
loadout_character: value.loadout_character || "Copper"
|
||||
};
|
||||
} else {
|
||||
// Merge logic: sum counts, max high score
|
||||
@@ -801,11 +835,11 @@ function rpcAdminSyncLeaderboard(ctx, logger, nk, payload) {
|
||||
userGroup[userId].games_won += (value.games_won || 0);
|
||||
}
|
||||
|
||||
// Prioritize avatar from game_stats
|
||||
// Prioritize avatar and character from game_stats
|
||||
if (key === "game_stats" || !userGroup[userId].avatar_url) {
|
||||
try {
|
||||
// Try to get avatar from storage value if present, else fallback to account later
|
||||
if (value.avatar_url) userGroup[userId].avatar_url = value.avatar_url;
|
||||
if (value.loadout_character) userGroup[userId].loadout_character = value.loadout_character;
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
@@ -820,7 +854,8 @@ function rpcAdminSyncLeaderboard(ctx, logger, nk, payload) {
|
||||
var metadata = {
|
||||
games_played: stats.games_played,
|
||||
games_won: stats.games_won,
|
||||
avatar_url: stats.avatar_url || account.user.avatarUrl || ""
|
||||
avatar_url: stats.avatar_url || account.user.avatarUrl || "",
|
||||
loadout_character: stats.loadout_character || "Copper"
|
||||
};
|
||||
|
||||
nk.leaderboardRecordWrite("global_high_score", userId, account.user.username, stats.high_score, 0, metadata);
|
||||
|
||||
Reference in New Issue
Block a user