Only update classes that have improved!

This commit is contained in:
zkldi
2021-11-18 18:46:45 +00:00
parent b0a044970a
commit bc7d2c25f8
2 changed files with 15 additions and 8 deletions
@@ -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<ClassDelta[]> {
const deltas: ClassDelta[] = [];
const updateOps = [];
const achievementOps = [];
for (const s in classes) {
const classSet = s as keyof GameClasses<IDStrings>;
@@ -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;
}
@@ -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<string, number> = {};
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,
},
}
);