From 4c38940ff56568dfed451a6a18b2b9adc12d75d1 Mon Sep 17 00:00:00 2001 From: zkldi Date: Wed, 9 Jun 2021 15:25:02 +0100 Subject: [PATCH] add CreateGoalID --- server/src/lib/achievables/goals.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/server/src/lib/achievables/goals.ts b/server/src/lib/achievables/goals.ts index 90db764a7..1629722dd 100644 --- a/server/src/lib/achievables/goals.ts +++ b/server/src/lib/achievables/goals.ts @@ -4,6 +4,7 @@ import db from "../../external/mongo/db"; import { KtLogger } from "../logger/logger"; import { GetFolderChartIDs } from "../../utils/folder"; import { FilterQuery } from "mongodb"; +import fjsh from "fast-json-stable-hash"; export interface EvaluatedGoalReturn { achieved: boolean; @@ -13,6 +14,21 @@ export interface EvaluatedGoalReturn { outOfHuman: string; } +/** + * Creates a goalID from a goals charts and criteria. + * + * This uses FJSH to stable-stringify the charts and criteria, + * then hashes that string under sha256. + * + * @note We could do better here, by converting criteria + * to 'similar' criteria - like 100% resolving to 1million score + * but that proves very complex to implement when it comes + * to multiple games. + */ +export function CreateGoalID(charts: GoalDocument["charts"], criteria: GoalDocument["criteria"]) { + return fjsh.hash({ charts, criteria }, "sha256"); +} + export async function EvaluateGoalForUser( goal: GoalDocument, userID: integer,