Merge pull request #558 from TNG-dev:zkldi/issue-556

Add update-score and delete-score functionality
This commit is contained in:
zkldi
2021-12-08 20:54:04 +00:00
committed by GitHub
12 changed files with 276 additions and 21471 deletions
@@ -1,5 +0,0 @@
get the data by going to https://iidx.insane.pe.kr/!/sp10/ and typing copy(tabledata_orig).
parsers here handle the rest.
the sp8h, 9 and 10 tierlists *suck*, so, we're ignoring them.
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -1,117 +0,0 @@
/* eslint-disable no-await-in-loop */
import db from "external/mongo/db";
import fs from "fs";
import CreateLogCtx from "lib/logger/logger";
import path from "path";
import { Difficulties } from "tachi-common";
import { FindChartWithPTDF } from "utils/queries/charts";
import { FindSongOnTitle } from "utils/queries/songs";
const logger = CreateLogCtx(__filename);
async function parseKr(
file: string,
mode: "NC" | "HC",
catVals: { text: string; value: number; idv: boolean }[]
) {
const krdata = JSON.parse(fs.readFileSync(path.join(__dirname, "./iidx-pe-kr", file), "utf-8"));
for (let i = 0; i < krdata.categories.length; i++) {
const data = krdata.categories[i];
if (data.sortindex < 0) {
continue;
}
const cv = catVals[i];
if (!cv) {
throw new Error(`cv krdata mismatch ${krdata.categories.length} ${catVals.length}?`);
}
for (const item of data.items) {
const song = await FindSongOnTitle("iidx", item.data.title.replace(/†$/u, "").trim());
if (!song) {
logger.warn(`Could not find song with title ${item.data.title}`);
continue;
}
const t = item.data.type;
let diff: Difficulties["iidx:SP"];
if (t === "A") {
diff = "ANOTHER";
} else if (t === "L") {
diff = "LEGGENDARIA";
} else if (t === "H") {
diff = "HYPER";
} else if (t === "N") {
diff = "NORMAL";
} else {
logger.warn(`${song.title} Unknown difficulty ${t}.`);
continue;
}
const chart = await FindChartWithPTDF("iidx", song.id, "SP", diff);
if (!chart) {
logger.warn(`${song.title} ${diff} - Couldn't find chart?`);
continue;
}
// Update this part of the tierlist, we've got everything.
await db.charts.iidx.update(
{
chartID: chart.chartID,
},
{
$set: {
[`tierlistInfo.kt-${mode}`]: {
text: cv.text,
value: cv.value,
individualDifference: cv.idv,
},
},
}
);
logger.verbose(`Updated ${chart.chartID} to value ${cv.value}.`);
}
}
logger.info(`Finished parsing ${file}`);
}
function h(text: string, value: number, idv = false) {
return { text, value, idv };
}
parseKr("sp11N.json", "NC", [
h("11S+", 12.2),
h("11S", 12),
h("11A+", 11.9),
h("11A", 11.8),
h("11B", 11.6),
h("11C", 11.4),
h("11D", 11.2),
h("11E", 11.0),
h("11F", 10.8),
]);
parseKr("sp11H.json", "HC", [
h("11S+", 12.2),
h("11S+", 12.2, true), // 個人差
h("11S", 12),
h("11S", 12, true), // 個人差
h("11A+", 11.9),
h("11A+", 11.9, true), // 個人差
h("11A", 11.8),
h("11A", 11.0, true), // 個人差
h("11B+", 11.7),
h("11B", 11.6),
h("11C", 11.4),
h("11D", 11.2),
h("11E", 11.0),
h("11F", 10.8),
]);
+1 -1
View File
@@ -195,7 +195,7 @@ export const rootLogger = winston.createLogger({
__Worker: !!process.env.IS_WORKER,
__ReplicaID: Environment.replicaIdentity,
},
});
}) as KtLogger;
if (!!ServerConfig.LOGGER_CONFIG.SEQ_API_KEY !== !!Environment.seqUrl) {
rootLogger.warn(
@@ -0,0 +1,124 @@
import t from "tap";
import { ImportDocument, ScoreDocument, SessionDocument } from "tachi-common";
import deepmerge from "deepmerge";
import { TestingIIDXSPScore } from "test-utils/test-data";
import db from "external/mongo/db";
import UpdateScore from "./update-score";
import { CreateScoreID } from "lib/score-import/framework/score-importing/score-id";
import ResetDBState from "test-utils/resets";
const mockImportDocument: ImportDocument = {
userID: 1,
userIntent: false,
classDeltas: [],
createdSessions: [],
errors: [],
goalInfo: [],
idStrings: [],
importID: "mockImportID",
importType: "file/batch-manual",
milestoneInfo: [],
scoreIDs: ["scoreid_1", "scoreid_2"],
timeFinished: 1000,
timeStarted: 0,
game: "iidx",
playtypes: ["SP"],
};
const mockSessionDocument: SessionDocument = {
userID: 1,
calculatedData: {},
desc: "",
game: "iidx",
playtype: "SP",
highlight: false,
importType: "file/batch-manual",
name: "",
scoreInfo: [
{
isNewScore: true,
scoreID: "scoreid_1",
},
{
isNewScore: true,
scoreID: "scoreid_2",
},
],
sessionID: "mockSessionID",
timeEnded: 1000,
timeInserted: 0,
timeStarted: 0,
views: 0,
};
t.test("#UpdateScore", (t) => {
t.beforeEach(ResetDBState);
t.test("Should update a score and everything pertaining to it", async (t) => {
TestingIIDXSPScore.scoreID = "scoreid_1";
const score = deepmerge<ScoreDocument>(TestingIIDXSPScore, {
scoreData: { score: 1020 },
} as any);
const newScoreID = CreateScoreID(score.userID, score, score.chartID);
await db.imports.insert(mockImportDocument);
await db.sessions.insert(mockSessionDocument);
await db.scores.insert(TestingIIDXSPScore);
// This function doesn't return anything, instead,
// we need to check external state.
await UpdateScore(TestingIIDXSPScore, score);
const dbScore = await db.scores.findOne({
scoreID: "scoreid_1",
});
t.equal(dbScore, null, "Should have updated the scoreID from the database.");
const dbNewScore = await db.scores.findOne({
scoreID: newScoreID,
});
t.hasStrict(
dbNewScore?.scoreData,
score.scoreData,
"The new score inserted into the database should have the new scoreData."
);
const dbImport = await db.imports.findOne({
importID: "mockImportID",
});
t.strictSame(
dbImport?.scoreIDs,
[newScoreID, "scoreid_2"],
"Should update scoreid_1 to the new hash."
);
const dbSession = await db.sessions.findOne({
sessionID: "mockSessionID",
});
t.strictSame(
dbSession?.scoreInfo,
[
{
isNewScore: true,
scoreID: newScoreID,
},
{
isNewScore: true,
scoreID: "scoreid_2",
},
],
"Should update scoreid_1 to the new hash."
);
t.end();
});
t.end();
});
@@ -0,0 +1,149 @@
/* eslint-disable no-await-in-loop */
import db from "external/mongo/db";
import { KtLogger, rootLogger } from "lib/logger/logger";
import { CreateCalculatedData } from "lib/score-import/framework/calculated-data/calculated-data";
import { CreateScoreID } from "lib/score-import/framework/score-importing/score-id";
import { ScoreDocument } from "tachi-common";
import { UpdateAllPBs } from "utils/calculations/recalc-scores";
import { FormatUserDoc, GetUserWithID } from "utils/user";
/**
* Updates a score from oldScore to newScore, applying all necessary state
* changes on the way.
*
* @note You don't need to recalc the scoreID for newScore, it's done for you.
*/
export default async function UpdateScore(oldScore: ScoreDocument, newScore: ScoreDocument) {
const userID = oldScore.userID;
const user = await GetUserWithID(userID);
if (!user) {
rootLogger.severe(
`User ${userID} does not exist, yet a score update was called for them? Panicking.`
);
throw new Error(
`User ${userID} does not exist, yet a score update was called for them? Panicking.`
);
}
const chartID = newScore.chartID;
const chart = await db.charts[oldScore.game].findOne({
chartID,
});
if (!chart) {
rootLogger.severe(
`Chart ${chartID} does not exist, yet a score update was called for it? Panicking.`
);
throw new Error(
`Chart ${chartID} does not exist, yet a score update was called for it? Panicking.`
);
}
const logger = rootLogger.child({
context: ["Update Score", oldScore.scoreID, newScore.scoreID, FormatUserDoc(user)],
oldScore,
newScore,
}) as KtLogger;
logger.info("Received Update Score request.");
const oldScoreID = oldScore.scoreID;
const newScoreID = CreateScoreID(newScore.userID, newScore, newScore.chartID);
// We need to change *so* many references to score IDs, and recalculate *so*
// much stored state. Obviously, changing a scoreID is an exceptional circumstance
// brought on by a bug.
// So hopefully, we wont have to use this much.
newScore.scoreID = newScoreID;
newScore.calculatedData = await CreateCalculatedData(
newScore,
chart,
newScore.scoreData.esd,
logger
);
await db.scores.update(
{
scoreID: oldScoreID,
},
{ $set: newScore }
);
const sessions = await db.sessions.find({
"scoreInfo.scoreID": oldScoreID,
});
logger.info(`Updating ${sessions.length} sessions.`);
// For every session that interacts with this score ID (there should only ever be one)
for (const session of sessions) {
// Go over all the scoreInfo and alter the ones that involve this scoreID.
for (const scoreInfo of session.scoreInfo) {
// If this scoreInfo needs to be changed
if (scoreInfo.scoreID === oldScoreID) {
scoreInfo.scoreID = newScoreID;
// If there's any cached grade/lamp diffs, we need to update them.
if (!scoreInfo.isNewScore) {
const gradeDiff = oldScore.scoreData.gradeIndex - newScore.scoreData.gradeIndex;
const lampDiff = oldScore.scoreData.lampIndex - newScore.scoreData.lampIndex;
const percentDiff = oldScore.scoreData.percent - newScore.scoreData.percent;
const scoreDiff = oldScore.scoreData.score - newScore.scoreData.score;
scoreInfo.gradeDelta += gradeDiff;
scoreInfo.lampDelta += lampDiff;
scoreInfo.scoreDelta += scoreDiff;
scoreInfo.percentDelta += percentDiff;
}
}
}
await db.sessions.update(
{
sessionID: session.sessionID,
},
{
$set: { scoreInfo: session.scoreInfo },
}
);
}
logger.info(`Updating PBs.`);
// Update the PBs to reference properly.
// We run updateAllPbs on just the modified chart -- the reason
// for this is to update ranking info incase that might fall out of
// sync as a result.
await UpdateAllPBs(undefined, {
chartID: newScore.chartID,
});
await UpdateAllPBs(undefined, {
chartID: oldScore.chartID,
});
const imports = await db.imports.find({
scoreIDs: oldScoreID,
});
logger.info(`Updating ${imports.length} imports.`);
for (const importDoc of imports) {
await db.imports.update(
{
importID: importDoc.importID,
},
{
$set: {
scoreIDs: importDoc.scoreIDs.map((e) => (e === oldScoreID ? newScoreID : e)),
},
}
);
}
logger.info(`Done updating score.`);
}
@@ -10,7 +10,7 @@ import { ServerConfig, TachiConfig } from "lib/setup/config";
import { Game, UserAuthLevels } from "tachi-common";
import db from "external/mongo/db";
import { DeleteScore } from "lib/delete-scores/delete-scores";
import { DeleteScore } from "lib/score-mutation/delete-scores";
import { UpdateAllPBs } from "utils/calculations/recalc-scores";
import DestroyUserGamePlaytypeData from "utils/reset-state/destroy-ugpt";
@@ -1,7 +1,7 @@
import { Router } from "express";
import db from "external/mongo/db";
import { SYMBOL_TachiData } from "lib/constants/tachi";
import { DeleteScore } from "lib/delete-scores/delete-scores";
import { DeleteScore } from "lib/score-mutation/delete-scores";
import CreateLogCtx from "lib/logger/logger";
import p from "prudence";
import { RequirePermissions } from "server/middleware/auth";
-2
View File
@@ -58,8 +58,6 @@ export async function ResolveFolderToCharts(
} else if (folder.type === "charts") {
const folderDataTransposed = TransposeFolderData(folder.data);
logger.warn("a", folderDataTransposed);
const fx = deepmerge.all([filter, { playtype: folder.playtype }, folderDataTransposed]);
charts = await db.charts[folder.game].find(fx);