feat: edit some login indicator
This commit is contained in:
@@ -564,14 +564,14 @@ function rpcAdminUpdateStats(ctx, logger, nk, payload) {
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Update Storage (for legacy support/redundancy)
|
||||
// 1. Update Storage (priority: game_stats)
|
||||
nk.storageWrite([{
|
||||
collection: "stats",
|
||||
key: "stats",
|
||||
key: "game_stats",
|
||||
userId: targetUserId,
|
||||
value: JSON.stringify(stats),
|
||||
permissionRead: 1, // Public read
|
||||
permissionWrite: 0 // No one can write (except server)
|
||||
permissionWrite: 0
|
||||
}]);
|
||||
|
||||
// 2. Update Native Leaderboard
|
||||
@@ -586,7 +586,7 @@ function rpcAdminUpdateStats(ctx, logger, nk, payload) {
|
||||
|
||||
nk.leaderboardRecordWrite("global_high_score", targetUserId, account.user.username, score, subscore, JSON.stringify(metadata));
|
||||
|
||||
logger.info("Stats updated for user " + targetUserId + " by admin " + ctx.userId + " (Storage + Native)");
|
||||
logger.info("Stats updated for user " + targetUserId + " by admin " + ctx.userId + " (game_stats + Native)");
|
||||
return JSON.stringify({ success: true });
|
||||
} catch (e) {
|
||||
logger.error("Failed to update stats: " + e);
|
||||
@@ -605,12 +605,11 @@ function rpcAdminDeleteStats(ctx, logger, nk, payload) {
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Delete Storage
|
||||
nk.storageDelete([{
|
||||
collection: "stats",
|
||||
key: "stats",
|
||||
userId: targetUserId
|
||||
}]);
|
||||
// 1. Delete Storage (Both keys)
|
||||
nk.storageDelete([
|
||||
{ collection: "stats", key: "stats", userId: targetUserId },
|
||||
{ collection: "stats", key: "game_stats", userId: targetUserId }
|
||||
]);
|
||||
|
||||
// 2. Delete Native Leaderboard Record
|
||||
nk.leaderboardRecordDelete("global_high_score", targetUserId);
|
||||
@@ -629,25 +628,62 @@ function rpcAdminSyncLeaderboard(ctx, logger, nk, payload) {
|
||||
try {
|
||||
var result = nk.storageList(null, "stats", 100, "");
|
||||
var statsObjects = result.objects || [];
|
||||
var count = 0;
|
||||
var userGroup = {}; // [userId] = { highScore, gamesPlayed, gamesWon, avatar }
|
||||
|
||||
// Phase 1: Group and merge
|
||||
for (var i = 0; i < statsObjects.length; i++) {
|
||||
var obj = statsObjects[i];
|
||||
var userId = obj.userId;
|
||||
var stats = JSON.parse(obj.value);
|
||||
var key = obj.key; // "stats" or "game_stats"
|
||||
var value;
|
||||
|
||||
try {
|
||||
value = JSON.parse(obj.value || "{}");
|
||||
} catch (jsonErr) {
|
||||
logger.error("Skipping key " + key + " for user " + userId + " due to corrupt JSON");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!userGroup[userId]) {
|
||||
userGroup[userId] = {
|
||||
high_score: value.high_score || 0,
|
||||
games_played: value.games_played || 0,
|
||||
games_won: value.games_won || 0,
|
||||
avatar_url: ""
|
||||
};
|
||||
} else {
|
||||
// Merge logic: sum counts, max high score
|
||||
userGroup[userId].high_score = Math.max(userGroup[userId].high_score, value.high_score || 0);
|
||||
userGroup[userId].games_played += (value.games_played || 0);
|
||||
userGroup[userId].games_won += (value.games_won || 0);
|
||||
}
|
||||
|
||||
// Prioritize avatar 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;
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Write to native leaderboard
|
||||
var count = 0;
|
||||
for (var userId in userGroup) {
|
||||
try {
|
||||
var stats = userGroup[userId];
|
||||
var account = nk.accountGetId(userId);
|
||||
|
||||
var metadata = {
|
||||
games_played: stats.games_played || 0,
|
||||
games_won: stats.games_won || 0,
|
||||
avatar_url: account.user.avatarUrl || ""
|
||||
games_played: stats.games_played,
|
||||
games_won: stats.games_won,
|
||||
avatar_url: stats.avatar_url || account.user.avatarUrl || ""
|
||||
};
|
||||
|
||||
nk.leaderboardRecordWrite("global_high_score", userId, account.user.username, stats.high_score || 0, 0, JSON.stringify(metadata));
|
||||
nk.leaderboardRecordWrite("global_high_score", userId, account.user.username, stats.high_score, 0, JSON.stringify(metadata));
|
||||
count++;
|
||||
} catch (inner) {
|
||||
logger.error("Failed to sync record for " + userId + ": " + inner);
|
||||
logger.error("Failed to sync merged record for " + userId + ": " + inner);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user