mirror of
https://github.com/zkldi/Tachi.git
synced 2026-09-29 18:37:58 +03:00
de-utils a lot of stuff
This commit is contained in:
@@ -6,10 +6,47 @@ import {
|
||||
GoalImportInfo,
|
||||
UserMilestoneDocument,
|
||||
MilestoneImportInfo,
|
||||
MilestoneDocument,
|
||||
} from "kamaitachi-common";
|
||||
import db from "../../../external/mongo/db";
|
||||
import { ProcessMilestoneFromGII } from "../../../utils/milestone";
|
||||
import { BulkWriteUpdateOneOperation } from "mongodb";
|
||||
import { CalculateMilestoneOutOf, GetGoalIDsFromMilestone } from "../../../utils/milestone";
|
||||
|
||||
/**
|
||||
* Processes and updates a user's milestones from their Goal Import Info (i.e. what is returned
|
||||
* about goals from imports)
|
||||
*/
|
||||
export function ProcessMilestoneFromGII(
|
||||
milestone: MilestoneDocument,
|
||||
gii: Map<string, GoalImportInfo["new"]>
|
||||
) {
|
||||
const goalIDs = GetGoalIDsFromMilestone(milestone);
|
||||
|
||||
let progress = 0;
|
||||
|
||||
for (const goalID of goalIDs) {
|
||||
const userInfo = gii.get(goalID);
|
||||
|
||||
if (!userInfo) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!userInfo.achieved) {
|
||||
continue;
|
||||
}
|
||||
|
||||
progress++;
|
||||
}
|
||||
|
||||
const outOf = CalculateMilestoneOutOf(milestone, goalIDs);
|
||||
|
||||
// milestone achieved!
|
||||
if (progress >= outOf) {
|
||||
return { achieved: true, progress };
|
||||
}
|
||||
|
||||
return { achieved: false, progress };
|
||||
}
|
||||
|
||||
export async function UpdateUsersMilestones(
|
||||
importGoalInfo: GoalImportInfo[],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { USCClientScore } from "../../../../server/router/ir/usc/common";
|
||||
import { USCClientScore } from "../../../../server/router/ir/usc/usc";
|
||||
import { FindSongOnID } from "../../../../utils/database-lookup/song";
|
||||
import { KtLogger } from "../../../../utils/types";
|
||||
import { InternalFailure, InvalidScoreFailure } from "../../../framework/common/converter-failures";
|
||||
|
||||
@@ -3,7 +3,7 @@ import { ChartDocument } from "kamaitachi-common";
|
||||
import p, { PrudenceSchema } from "prudence";
|
||||
import { InvalidScoreFailure } from "../../../framework/common/converter-failures";
|
||||
import { FormatPrError } from "../../../../utils/prudence";
|
||||
import { USCClientScore } from "../../../../server/router/ir/usc/common";
|
||||
import { USCClientScore } from "../../../../server/router/ir/usc/usc";
|
||||
import { IRUSCContext } from "./types";
|
||||
import { ConverterIRUSC } from "./converter";
|
||||
import { ParserFunctionReturnsSync } from "../../common/types";
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { RequestHandler } from "express";
|
||||
import multer, { MulterError } from "multer";
|
||||
import CreateLogCtx from "./logger";
|
||||
import CreateLogCtx from "../../utils/logger";
|
||||
|
||||
const defaultLogger = CreateLogCtx(__filename);
|
||||
|
||||
@@ -7,12 +7,12 @@ import {
|
||||
ValidateCaptcha,
|
||||
} from "./auth";
|
||||
import t from "tap";
|
||||
import db, { CloseMongoConnection } from "../external/mongo/db";
|
||||
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
|
||||
import { PrivateUserDocument } from "kamaitachi-common";
|
||||
import { prAssert } from "../test-utils/asserts";
|
||||
import { prAssert } from "../../../../test-utils/asserts";
|
||||
import Prudence from "prudence";
|
||||
import ResetDBState from "../test-utils/reset-db-state";
|
||||
import { MockFetch } from "../test-utils/mock-fetch";
|
||||
import ResetDBState from "../../../../test-utils/reset-db-state";
|
||||
import { MockFetch } from "../../../../test-utils/mock-fetch";
|
||||
|
||||
t.test("#CreateAPIKey", (t) => {
|
||||
t.match(
|
||||
@@ -6,13 +6,13 @@ import {
|
||||
PublicAPIKeyDocument,
|
||||
PublicUserDocument,
|
||||
} from "kamaitachi-common";
|
||||
import db from "../external/mongo/db";
|
||||
import { GetNextCounterValue } from "./db";
|
||||
import { InsertResult } from "monk";
|
||||
import CreateLogCtx from "./logger";
|
||||
import { FormatUserDoc } from "./user";
|
||||
import nodeFetch from "./fetch";
|
||||
import { CAPTCHA_SECRET_KEY } from "../secrets";
|
||||
import { CAPTCHA_SECRET_KEY } from "../../../../example-secrets";
|
||||
import db from "../../../../external/mongo/db";
|
||||
import { GetNextCounterValue } from "../../../../utils/db";
|
||||
import CreateLogCtx from "../../../../utils/logger";
|
||||
import { FormatUserDoc } from "../../../../utils/user";
|
||||
import nodeFetch from "../../../../utils/fetch";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
ReinstateInvite,
|
||||
ValidatePassword,
|
||||
ValidateCaptcha,
|
||||
} from "../../../../utils/auth";
|
||||
} from "./auth";
|
||||
import {
|
||||
FormatUserDoc,
|
||||
GetUserCaseInsensitive,
|
||||
|
||||
@@ -9,7 +9,7 @@ import { RequireLoggedIn } from "../../../middleware/require-logged-in";
|
||||
import ScoreImportFatalError from "../../../../score-import/framework/score-importing/score-import-error";
|
||||
import { KtLogger } from "../../../../utils/types";
|
||||
import { ExpressWrappedScoreImportMain } from "../../../../score-import/framework/express-wrapper";
|
||||
import { CreateMulterSingleUploadMiddleware } from "../../../../utils/multer";
|
||||
import { CreateMulterSingleUploadMiddleware } from "../../../middleware/multer-upload";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
|
||||
@@ -10,13 +10,13 @@ import {
|
||||
} from "kamaitachi-common";
|
||||
import { AssertStrAsPositiveNonZeroInt } from "../../../../score-import/framework/common/string-asserts";
|
||||
import CreateLogCtx from "../../../../utils/logger";
|
||||
import { CreatePOSTScoresResponseBody, KtchiScoreToServerScore } from "./common";
|
||||
import { CreatePOSTScoresResponseBody, KtchiScoreToServerScore } from "./usc";
|
||||
import { ExpressWrappedScoreImportMain } from "../../../../score-import/framework/express-wrapper";
|
||||
import { GetUserWithID } from "../../../../utils/user";
|
||||
import { KtLogger } from "../../../../utils/types";
|
||||
import { ParseIRUSC } from "../../../../score-import/import-types/ir/usc/parser";
|
||||
import { USCIR_MAX_LEADERBOARD_N } from "../../../../constants/usc-ir";
|
||||
import { CreateMulterSingleUploadMiddleware } from "../../../../utils/multer";
|
||||
import { CreateMulterSingleUploadMiddleware } from "../../../middleware/multer-upload";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
import t from "tap";
|
||||
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
|
||||
import ResetDBState from "../../../../test-utils/reset-db-state";
|
||||
import { CreatePOSTScoresResponseBody, KtchiScoreToServerScore } from "./common";
|
||||
import { CreatePOSTScoresResponseBody, KtchiScoreToServerScore } from "./usc";
|
||||
import { ChartDocument, PBScoreDocument, ScoreDocument } from "kamaitachi-common";
|
||||
import deepmerge from "deepmerge";
|
||||
|
||||
@@ -1,46 +1,10 @@
|
||||
import { MilestoneDocument, integer, GoalImportInfo } from "kamaitachi-common";
|
||||
|
||||
/**
|
||||
* Processes and updates a user's milestones from their Goal Import Info (i.e. what is returned
|
||||
* about goals from imports)
|
||||
*/
|
||||
export function ProcessMilestoneFromGII(
|
||||
milestone: MilestoneDocument,
|
||||
gii: Map<string, GoalImportInfo["new"]>
|
||||
) {
|
||||
const goalIDs = GetGoalIDsFromMilestone(milestone);
|
||||
|
||||
let progress = 0;
|
||||
|
||||
for (const goalID of goalIDs) {
|
||||
const userInfo = gii.get(goalID);
|
||||
|
||||
if (!userInfo) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!userInfo.achieved) {
|
||||
continue;
|
||||
}
|
||||
|
||||
progress++;
|
||||
}
|
||||
|
||||
const outOf = CalculateMilestoneOutOf(milestone, goalIDs);
|
||||
|
||||
// milestone achieved!
|
||||
if (progress >= outOf) {
|
||||
return { achieved: true, progress };
|
||||
}
|
||||
|
||||
return { achieved: false, progress };
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the goalID documents in a single array from the
|
||||
* nested structure of milestones.
|
||||
*/
|
||||
function GetGoalIDsFromMilestone(milestone: MilestoneDocument) {
|
||||
export function GetGoalIDsFromMilestone(milestone: MilestoneDocument) {
|
||||
// this sucks - maybe a nicer way to do this, because nested
|
||||
// maps are just ugly
|
||||
return milestone.milestoneData.map((e) => e.goals.map((e) => e.goalID)).flat(1);
|
||||
@@ -50,7 +14,7 @@ function GetGoalIDsFromMilestone(milestone: MilestoneDocument) {
|
||||
* Work out how many goals need to be achieved for this
|
||||
* milestone to be considered completed.
|
||||
*/
|
||||
function CalculateMilestoneOutOf(milestone: MilestoneDocument, goalIDs: string[]): integer {
|
||||
export function CalculateMilestoneOutOf(milestone: MilestoneDocument, goalIDs: string[]): integer {
|
||||
if (milestone.criteria.type === "all") {
|
||||
return goalIDs.length;
|
||||
} else if (milestone.criteria.type === "abs") {
|
||||
|
||||
Reference in New Issue
Block a user