From 8afb5152315a4a78c69587242b7dced3b68a6440 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Sat, 19 Apr 2025 09:04:22 -0700 Subject: [PATCH] fix(stats): captures not being tracked for a new enemy (#1728) Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1728 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- src/services/statsService.ts | 47 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/services/statsService.ts b/src/services/statsService.ts index 626bbe27..a074b637 100644 --- a/src/services/statsService.ts +++ b/src/services/statsService.ts @@ -1,6 +1,5 @@ import { Stats, TStatsDatabaseDocument } from "@/src/models/statsModel"; import { - IEnemy, IStatsAdd, IStatsMax, IStatsSet, @@ -137,34 +136,34 @@ export const updateStats = async (accountOwnerId: string, payload: IStatsUpdate) case "HEADSHOT": case "KILL_ASSIST": { playerStats.Enemies ??= []; - const enemyStatKey = { - KILL_ENEMY: "kills", - EXECUTE_ENEMY: "executions", - HEADSHOT: "headshots", - KILL_ASSIST: "assists" - }[category] as "kills" | "executions" | "headshots" | "assists"; + const enemyStatKey = ( + { + KILL_ENEMY: "kills", + EXECUTE_ENEMY: "executions", + HEADSHOT: "headshots", + KILL_ASSIST: "assists" + } as const + )[category]; for (const [type, count] of Object.entries(data as IUploadEntry)) { - const enemy = playerStats.Enemies.find(element => element.type === type); - if (enemy) { - if (category === "KILL_ENEMY") { - enemy.kills ??= 0; - const captureCount = (actionData as IStatsAdd)["CAPTURE_ENEMY"]?.[type]; - if (captureCount) { - enemy.kills += Math.max(count - captureCount, 0); - enemy.captures ??= 0; - enemy.captures += captureCount; - } else { - enemy.kills += count; - } + let enemy = playerStats.Enemies.find(element => element.type === type); + if (!enemy) { + enemy = { type: type }; + playerStats.Enemies.push(enemy); + } + if (category === "KILL_ENEMY") { + enemy.kills ??= 0; + const captureCount = (actionData as IStatsAdd)["CAPTURE_ENEMY"]?.[type]; + if (captureCount) { + enemy.kills += Math.max(count - captureCount, 0); + enemy.captures ??= 0; + enemy.captures += captureCount; } else { - enemy[enemyStatKey] ??= 0; - enemy[enemyStatKey] += count; + enemy.kills += count; } } else { - const newEnemy: IEnemy = { type: type }; - newEnemy[enemyStatKey] = count; - playerStats.Enemies.push(newEnemy); + enemy[enemyStatKey] ??= 0; + enemy[enemyStatKey] += count; } } break;