mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-08 14:58:12 +03:00
42 lines
1004 B
TypeScript
42 lines
1004 B
TypeScript
import db from "external/mongo/db";
|
|
import { ONE_HOUR } from "lib/constants/time";
|
|
import NodeCache from "node-cache";
|
|
import { Game, gameClasses, IDStrings, integer, Playtypes } from "tachi-common";
|
|
|
|
const classDistCache = new NodeCache();
|
|
|
|
export async function GetClassDistribution(
|
|
game: Game,
|
|
playtype: Playtypes[Game],
|
|
className: gameClasses.GameClassSets[IDStrings]
|
|
) {
|
|
const cacheKey = `${game}:${playtype}:${className}`;
|
|
const cache = classDistCache.get(cacheKey);
|
|
|
|
if (!cache) {
|
|
const distribution = (await db["game-stats"].aggregate([
|
|
{
|
|
$match: {
|
|
game,
|
|
playtype,
|
|
},
|
|
},
|
|
{
|
|
$group: {
|
|
_id: `$classes.${className}`,
|
|
count: { $sum: 1 },
|
|
},
|
|
},
|
|
])) as { _id: string; count: integer }[];
|
|
|
|
// Converts {_id: "kaiden", count: 3} to {"kaiden": 3}, more or less.
|
|
const convert = Object.fromEntries(distribution.map((e) => [e._id, e.count]));
|
|
|
|
classDistCache.set(cacheKey, convert, ONE_HOUR);
|
|
|
|
return convert;
|
|
} else {
|
|
return cache;
|
|
}
|
|
}
|