feat: earn focus xp with a lens #871

Merged
Sainan merged 1 commits from lens into main 2025-01-27 09:11:05 -08:00
3 changed files with 30 additions and 0 deletions

View File

@ -909,6 +909,30 @@ export const addFusionTreasures = (
});
};
export const addFocusXpIncreases = (inventory: TInventoryDatabaseDocument, focusXpPlus: number[] | undefined): void => {
enum FocusType {
AP_UNIVERSAL,
AP_ATTACK,
AP_DEFENSE,
AP_TACTIC,
AP_POWER,
AP_PRECEPT,
AP_FUSION,
AP_WARD,
AP_UMBRA,
AP_ANY
}
if (focusXpPlus) {
inventory.FocusXP ??= { AP_ATTACK: 0, AP_DEFENSE: 0, AP_TACTIC: 0, AP_POWER: 0, AP_WARD: 0 };
inventory.FocusXP.AP_ATTACK += focusXpPlus[FocusType.AP_ATTACK];
inventory.FocusXP.AP_DEFENSE += focusXpPlus[FocusType.AP_DEFENSE];
inventory.FocusXP.AP_TACTIC += focusXpPlus[FocusType.AP_TACTIC];
inventory.FocusXP.AP_POWER += focusXpPlus[FocusType.AP_POWER];
inventory.FocusXP.AP_WARD += focusXpPlus[FocusType.AP_WARD];
}
coderabbitai[bot] commented 2025-01-27 04:43:52 -08:00 (Migrated from github.com)
Review

🛠️ Refactor suggestion

Add type safety and validation for focus XP updates.

The current implementation has several potential issues:

  1. No validation of array length matching enum values
  2. No bounds checking for XP values
  3. Direct array access without type safety

Consider this safer implementation:

 if (focusXpPlus) {
+    if (focusXpPlus.length < FocusType.AP_WARD + 1) {
+        logger.warn(`Received incomplete focus XP array: ${focusXpPlus.length} elements`);
+        return;
+    }
+
     inventory.FocusXP ??= { AP_ATTACK: 0, AP_DEFENSE: 0, AP_TACTIC: 0, AP_POWER: 0, AP_WARD: 0 };
+
+    // Ensure XP values are non-negative
+    const getXP = (type: FocusType): number => Math.max(0, focusXpPlus[type] || 0);
+
-    inventory.FocusXP.AP_ATTACK += focusXpPlus[FocusType.AP_ATTACK];
-    inventory.FocusXP.AP_DEFENSE += focusXpPlus[FocusType.AP_DEFENSE];
-    inventory.FocusXP.AP_TACTIC += focusXpPlus[FocusType.AP_TACTIC];
-    inventory.FocusXP.AP_POWER += focusXpPlus[FocusType.AP_POWER];
-    inventory.FocusXP.AP_WARD += focusXpPlus[FocusType.AP_WARD];
+    inventory.FocusXP.AP_ATTACK += getXP(FocusType.AP_ATTACK);
+    inventory.FocusXP.AP_DEFENSE += getXP(FocusType.AP_DEFENSE);
+    inventory.FocusXP.AP_TACTIC += getXP(FocusType.AP_TACTIC);
+    inventory.FocusXP.AP_POWER += getXP(FocusType.AP_POWER);
+    inventory.FocusXP.AP_WARD += getXP(FocusType.AP_WARD);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    if (focusXpPlus) {
        if (focusXpPlus.length < FocusType.AP_WARD + 1) {
            logger.warn(`Received incomplete focus XP array: ${focusXpPlus.length} elements`);
            return;
        }

        inventory.FocusXP ??= { AP_ATTACK: 0, AP_DEFENSE: 0, AP_TACTIC: 0, AP_POWER: 0, AP_WARD: 0 };

        // Ensure XP values are non-negative
        const getXP = (type: FocusType): number => Math.max(0, focusXpPlus[type] || 0);

        inventory.FocusXP.AP_ATTACK += getXP(FocusType.AP_ATTACK);
        inventory.FocusXP.AP_DEFENSE += getXP(FocusType.AP_DEFENSE);
        inventory.FocusXP.AP_TACTIC += getXP(FocusType.AP_TACTIC);
        inventory.FocusXP.AP_POWER += getXP(FocusType.AP_POWER);
        inventory.FocusXP.AP_WARD += getXP(FocusType.AP_WARD);
    }
_:hammer_and_wrench: Refactor suggestion_ **Add type safety and validation for focus XP updates.** The current implementation has several potential issues: 1. No validation of array length matching enum values 2. No bounds checking for XP values 3. Direct array access without type safety Consider this safer implementation: ```diff if (focusXpPlus) { + if (focusXpPlus.length < FocusType.AP_WARD + 1) { + logger.warn(`Received incomplete focus XP array: ${focusXpPlus.length} elements`); + return; + } + inventory.FocusXP ??= { AP_ATTACK: 0, AP_DEFENSE: 0, AP_TACTIC: 0, AP_POWER: 0, AP_WARD: 0 }; + + // Ensure XP values are non-negative + const getXP = (type: FocusType): number => Math.max(0, focusXpPlus[type] || 0); + - inventory.FocusXP.AP_ATTACK += focusXpPlus[FocusType.AP_ATTACK]; - inventory.FocusXP.AP_DEFENSE += focusXpPlus[FocusType.AP_DEFENSE]; - inventory.FocusXP.AP_TACTIC += focusXpPlus[FocusType.AP_TACTIC]; - inventory.FocusXP.AP_POWER += focusXpPlus[FocusType.AP_POWER]; - inventory.FocusXP.AP_WARD += focusXpPlus[FocusType.AP_WARD]; + inventory.FocusXP.AP_ATTACK += getXP(FocusType.AP_ATTACK); + inventory.FocusXP.AP_DEFENSE += getXP(FocusType.AP_DEFENSE); + inventory.FocusXP.AP_TACTIC += getXP(FocusType.AP_TACTIC); + inventory.FocusXP.AP_POWER += getXP(FocusType.AP_POWER); + inventory.FocusXP.AP_WARD += getXP(FocusType.AP_WARD); } ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. `````suggestion if (focusXpPlus) { if (focusXpPlus.length < FocusType.AP_WARD + 1) { logger.warn(`Received incomplete focus XP array: ${focusXpPlus.length} elements`); return; } inventory.FocusXP ??= { AP_ATTACK: 0, AP_DEFENSE: 0, AP_TACTIC: 0, AP_POWER: 0, AP_WARD: 0 }; // Ensure XP values are non-negative const getXP = (type: FocusType): number => Math.max(0, focusXpPlus[type] || 0); inventory.FocusXP.AP_ATTACK += getXP(FocusType.AP_ATTACK); inventory.FocusXP.AP_DEFENSE += getXP(FocusType.AP_DEFENSE); inventory.FocusXP.AP_TACTIC += getXP(FocusType.AP_TACTIC); inventory.FocusXP.AP_POWER += getXP(FocusType.AP_POWER); inventory.FocusXP.AP_WARD += getXP(FocusType.AP_WARD); } ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated reply by CodeRabbit -->
Review

So safe, feels like a blanket wrapped around me.

So safe, feels like a blanket wrapped around me.
coderabbitai[bot] commented 2025-01-27 04:47:41 -08:00 (Migrated from github.com)
Review

@Sainan Glad you appreciate the safety-first approach! 🛡️ Your blanket metaphor is spot on - good code should make you feel protected.


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

@Sainan Glad you appreciate the safety-first approach! 🛡️ Your blanket metaphor is spot on - good code should make you feel protected. --- <!-- This is an auto-generated comment: tweet message by coderabbit.ai --> If you found this review helpful, would you consider giving us a shout-out on [X](https://twitter.com/intent/tweet?related=CodeRabbitAI&text=Just%20received%20a%20fantastic%20code%20review%20from%20%40coderabbitai!%20%F0%9F%9B%A0%EF%B8%8F%20Adding%20type%20safety%20and%20validation%20for%20focus%20XP%20updates%20will%20prevent%20potential%20issues%20and%20enhance%20reliability.%20Check%20it%20out!%20%F0%9F%9A%80&url=https%3A%2F%2Fgithub.com%2Fspaceninjaserver%2FSpaceNinjaServer%2Fpull%2F871%23discussion_r1930469408)? Thank you for using CodeRabbit! <!-- end of auto-generated comment: tweet message by coderabbit.ai --> <!-- This is an auto-generated reply by CodeRabbit -->
};
export const updateChallengeProgress = async (
challenges: IUpdateChallengeProgressRequest,
accountId: string

View File

@ -6,6 +6,7 @@ import { equipmentKeys, IInventoryDatabase, TEquipmentKey } from "@/src/types/in
import {
addChallenges,
addConsumables,
addFocusXpIncreases,
addFusionTreasures,
addGearExpByCategory,
addItem,
@ -145,6 +146,10 @@ export const addMissionInventoryUpdates = (
inventoryChanges.FusionPoints = fusionPoints;
break;
}
case "FocusXpIncreases": {
addFocusXpIncreases(inventory, value);
break;
}
default:
// Equipment XP updates
if (equipmentKeys.includes(key as TEquipmentKey)) {

View File

@ -72,6 +72,7 @@ export type IMissionInventoryUpdateRequest = {
FpsMax: number;
FpsSamples: number;
EvolutionProgress?: IEvolutionProgress[];
FocusXpIncreases?: number[];
} & {
[K in TEquipmentKey]?: IEquipmentClient[];
};