From bc7d2c25f8ad8733ec33f837885a79d1e5feec0c Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 18 Nov 2021 18:46:45 +0000 Subject: [PATCH] Only update classes that have improved! --- .../framework/user-game-stats/classes.ts | 10 +++++----- .../framework/user-game-stats/update-ugs.ts | 13 ++++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/server/src/lib/score-import/framework/user-game-stats/classes.ts b/server/src/lib/score-import/framework/user-game-stats/classes.ts index 9ecdc3a2f..d3968b5ac 100644 --- a/server/src/lib/score-import/framework/user-game-stats/classes.ts +++ b/server/src/lib/score-import/framework/user-game-stats/classes.ts @@ -109,8 +109,8 @@ export async function UpdateUGSClasses( * Calculates the class "deltas" for this users classes. * This is for calculating scenarios where a users class has improved (i.e. they have gone from 9th dan to 10th dan). * - * Knowing this information allows us to attach it onto the import, and also emit things on redis - * s**This function emits webhook events and inserts classachieved documents into the DB!** + * Knowing this information allows us to attach it onto the import, and also emit things on webhooks. + * This function emits webhook events and inserts classachieved documents into the DB!** */ export async function ProcessClassDeltas( game: Game, @@ -122,7 +122,7 @@ export async function ProcessClassDeltas( ): Promise { const deltas: ClassDelta[] = []; - const updateOps = []; + const achievementOps = []; for (const s in classes) { const classSet = s as keyof GameClasses; @@ -160,7 +160,7 @@ export async function ProcessClassDeltas( EmitWebhookEvent({ type: "class-update/v1", content: { userID, ...delta } }); - updateOps.push({ + achievementOps.push({ userID, classSet: delta.set, classOldValue: delta.old, @@ -177,7 +177,7 @@ export async function ProcessClassDeltas( } } - await db["class-achievements"].insert(updateOps); + await db["class-achievements"].insert(achievementOps); return deltas; } diff --git a/server/src/lib/score-import/framework/user-game-stats/update-ugs.ts b/server/src/lib/score-import/framework/user-game-stats/update-ugs.ts index 5a77aa3ae..90e3943ed 100644 --- a/server/src/lib/score-import/framework/user-game-stats/update-ugs.ts +++ b/server/src/lib/score-import/framework/user-game-stats/update-ugs.ts @@ -2,13 +2,13 @@ // Some games have dedicated methods to calculate statistics like these, other games do not. // That's about all there is to it! -import { Game, Playtypes, integer, UserGameStats, ClassDelta } from "tachi-common"; +import { Game, Playtypes, gameClasses, integer, UserGameStats, ClassDelta } from "tachi-common"; import db from "external/mongo/db"; import { CreateGameSettings } from "lib/game-settings/create-game-settings"; import { KtLogger } from "lib/logger/logger"; import { ProcessClassDeltas, UpdateUGSClasses } from "./classes"; import { CalculateRatings } from "./rating"; -import { ClassHandler } from "./types"; +import { ClassHandler, ScoreClasses } from "./types"; export async function UpdateUsersGamePlaytypeStats( game: Game, @@ -41,6 +41,13 @@ export async function UpdateUsersGamePlaytypeStats( if (userGameStats) { logger.debug(`Updated player gamestats for ${game} (${playtype})`); + + const updateClasses: Record = {}; + + for (const delta of deltas) { + updateClasses[`classes.${delta.set}`] = delta.new; + } + await db["game-stats"].update( { game, @@ -50,7 +57,7 @@ export async function UpdateUsersGamePlaytypeStats( { $set: { ratings, - classes, + ...updateClasses, }, } );