From 38fc47ff2a64f86d625ce0ab52bd45f8584f03b0 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 18 Mar 2025 15:20:38 -0400 Subject: [PATCH] fixing how the actions are triggered and accessing the metric from the config --- .../config/item-created-count-config.ts | 3 +- .../vault-item-created-count-validator.ts | 48 +++++++++++++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/libs/common/src/tools/achievements/validators/config/item-created-count-config.ts b/libs/common/src/tools/achievements/validators/config/item-created-count-config.ts index 7fe9e37e407..16d45521c6c 100644 --- a/libs/common/src/tools/achievements/validators/config/item-created-count-config.ts +++ b/libs/common/src/tools/achievements/validators/config/item-created-count-config.ts @@ -140,7 +140,8 @@ export class ItemCreatedCountConfig implements Achievement { name: name, validator: Type.Threshold, active: { - metric: `item-${cipherType ? `${CipherType[cipherType]}-` : ""}quantity` as MetricId, + metric: + `item-${cipherType ? `${CipherType[cipherType].toLowerCase()}-` : ""}quantity` as MetricId, low: threshold - 1, high: threshold, }, diff --git a/libs/common/src/tools/achievements/validators/vault-item-created-count-validator.ts b/libs/common/src/tools/achievements/validators/vault-item-created-count-validator.ts index 213b20f08b1..7eddf29e29c 100644 --- a/libs/common/src/tools/achievements/validators/vault-item-created-count-validator.ts +++ b/libs/common/src/tools/achievements/validators/vault-item-created-count-validator.ts @@ -1,7 +1,11 @@ import { CipherType } from "../../../vault/enums"; -import { EventFormat } from "../../log/ecs-format"; import { earnedEvent, progressEvent } from "../achievement-events"; -import { AchievementProgressEvent, AchievementValidator, MetricId } from "../types"; +import { + AchievementProgressEvent, + AchievementValidator, + MetricId, + UserActionEvent, +} from "../types"; import { ItemCreatedCountConfig } from "./config/item-created-count-config"; @@ -15,8 +19,15 @@ import { ItemCreatedCountConfig } from "./config/item-created-count-config"; export class VaultItemCreatedCountValidator implements AchievementValidator { private itemCreatedProgress: MetricId; constructor(private config: ItemCreatedCountConfig) { - this.itemCreatedProgress = - `item-${config.cipherType ? `${CipherType[config.cipherType]}-` : ""}quantity` as MetricId; + // All of the configs for created items must have a metric. + // This checks the types to allow us to assign the metric. + if (config.active === "until-earned") { + throw new Error( + `${config.achievement}: invalid configuration; 'active' must contain a metric`, + ); + } + + this.itemCreatedProgress = config.active.metric; } base: AchievementValidator; @@ -36,14 +47,11 @@ export class VaultItemCreatedCountValidator implements AchievementValidator { return this.config.hidden; } - trigger(item: EventFormat) { - return ( - item.action === - `vault-${this.config.cipherType ? `${CipherType[this.config.cipherType]}-` : ""}item-added` - ); + trigger(item: UserActionEvent) { + return item.action === "vault-item-added" && this.validateItemType(item); } - measure(_item: EventFormat, progress: Map) { + measure(_item: UserActionEvent, progress: Map) { const value = 1 + (progress.get(this.itemCreatedProgress) ?? 0); return [progressEvent(this.itemCreatedProgress, value)]; } @@ -52,4 +60,24 @@ export class VaultItemCreatedCountValidator implements AchievementValidator { const value = progress.get(this.itemCreatedProgress) ?? 0; return value >= this.config.threshold ? [earnedEvent(this.achievement)] : []; } + + /** + * Will check the vault item types. The UserAction is lower case and the + * cipher type is capitalized/camel case. Making it all lowercase will make + * an even comparison. We are coupled to the CipherType enum unless any better + * suggestions. + * @param item The event data checking the trigger + * @returns true or false if the cipherType matches + */ + private validateItemType(item: UserActionEvent): boolean { + // If the config's cipher type is not present no need to check + // for the type. + if (!this.config.cipherType) { + return true; + } + + const lowerConfigType = CipherType[this.config.cipherType].toLowerCase(); + const lowerItemType = item.labels?.["vault-item-type"].toString().toLowerCase(); + return lowerItemType === lowerConfigType; + } }