diff --git a/common/js/common-core/score-core.d.ts b/common/js/common-core/score-core.d.ts new file mode 100644 index 000000000..53b7460fd --- /dev/null +++ b/common/js/common-core/score-core.d.ts @@ -0,0 +1,3 @@ +import { ScoreDocument } from ".."; +import { ICollection } from "monk"; +export declare function AutoCoerce(collection: ICollection, scores: ScoreDocument[]): Promise; diff --git a/common/js/common-core/score-core.js b/common/js/common-core/score-core.js new file mode 100644 index 000000000..5b392ae44 --- /dev/null +++ b/common/js/common-core/score-core.js @@ -0,0 +1,55 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.AutoCoerce = void 0; +function AutoCoerce(collection, scores) { + return __awaiter(this, void 0, void 0, function* () { + let notPBsArr = []; + for (const s of scores) { + if (!s.isLampPB) { + notPBsArr.push({ + userID: s.userID, + chartID: s.chartID, + isLampPB: true, + }); + } + } + if (notPBsArr.length === 0) { + return scores; + } + let lampPBsArr = yield collection.find({ + $or: notPBsArr, + }); + let lampPBs = new Map(); + for (const score of lampPBsArr) { + lampPBs.set(`${score.userID}-${score.chartID}`, score); + } + for (const score of scores) { + if (!score.isLampPB) { + let lampPB = lampPBs.get(`${score.userID}-${score.chartID}`); + if (lampPB) { + score.scoreData.lamp = lampPB.scoreData.lamp; + score.scoreData.lampIndex = lampPB.scoreData.lampIndex; + score.calculatedData.lampRating = lampPB.calculatedData.lampRating; + // not too happy about this, this is a sign + // of something not properly being generalised. + if (score.game === "bms") { + score.calculatedData.rating = lampPB.calculatedData.rating; + } + score.isLampPB = true; + } + } + } + return scores; + }); +} +exports.AutoCoerce = AutoCoerce; +//# sourceMappingURL=score-core.js.map \ No newline at end of file diff --git a/common/js/common-core/score-core.js.map b/common/js/common-core/score-core.js.map new file mode 100644 index 000000000..3c5f31c22 --- /dev/null +++ b/common/js/common-core/score-core.js.map @@ -0,0 +1 @@ +{"version":3,"file":"score-core.js","sourceRoot":"","sources":["../../src/common-core/score-core.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,SAAsB,UAAU,CAC5B,UAAsC,EACtC,MAAuB;;QAEvB,IAAI,SAAS,GAAG,EAAE,CAAC;QAEnB,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE;YACpB,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE;gBACb,SAAS,CAAC,IAAI,CAAC;oBACX,MAAM,EAAE,CAAC,CAAC,MAAM;oBAChB,OAAO,EAAE,CAAC,CAAC,OAAO;oBAClB,QAAQ,EAAE,IAAI;iBACjB,CAAC,CAAC;aACN;SACJ;QAED,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;YACxB,OAAO,MAAM,CAAC;SACjB;QAED,IAAI,UAAU,GAAG,MAAM,UAAU,CAAC,IAAI,CAAC;YACnC,GAAG,EAAE,SAAS;SACjB,CAAC,CAAC;QAEH,IAAI,OAAO,GAA+B,IAAI,GAAG,EAAE,CAAC;QACpD,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE;YAC5B,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,CAAC,CAAC;SAC1D;QAED,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;YACxB,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE;gBACjB,IAAI,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;gBAE7D,IAAI,MAAM,EAAE;oBACR,KAAK,CAAC,SAAS,CAAC,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC;oBAC7C,KAAK,CAAC,SAAS,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC;oBACvD,KAAK,CAAC,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC;oBAEnE,2CAA2C;oBAC3C,+CAA+C;oBAC/C,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK,EAAE;wBACtB,KAAK,CAAC,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC;qBAC9D;oBACD,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC;iBACzB;aACJ;SACJ;QAED,OAAO,MAAM,CAAC;IAClB,CAAC;CAAA;AAjDD,gCAiDC"} \ No newline at end of file diff --git a/common/js/index.d.ts b/common/js/index.d.ts index 5d60d2631..5207ec1b1 100644 --- a/common/js/index.d.ts +++ b/common/js/index.d.ts @@ -2,3 +2,4 @@ export * from "./types"; export * as config from "./config"; export * as gameClasses from "./game-classes"; export * as ESDCore from "./common-core/esd-core"; +export * as ScoreCore from "./common-core/score-core"; diff --git a/common/js/index.js b/common/js/index.js index 72e69fe51..c92fa1134 100644 --- a/common/js/index.js +++ b/common/js/index.js @@ -22,9 +22,10 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.ESDCore = exports.gameClasses = exports.config = void 0; +exports.ScoreCore = exports.ESDCore = exports.gameClasses = exports.config = void 0; __exportStar(require("./types"), exports); exports.config = __importStar(require("./config")); exports.gameClasses = __importStar(require("./game-classes")); exports.ESDCore = __importStar(require("./common-core/esd-core")); +exports.ScoreCore = __importStar(require("./common-core/score-core")); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/common/js/index.js.map b/common/js/index.js.map index 19b3b3d63..dbb126e57 100644 --- a/common/js/index.js.map +++ b/common/js/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAwB;AAExB,mDAAmC;AACnC,8DAA8C;AAC9C,kEAAkD"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAwB;AAExB,mDAAmC;AACnC,8DAA8C;AAC9C,kEAAkD;AAClD,sEAAsD"} \ No newline at end of file diff --git a/common/src/common-core/score-core.ts b/common/src/common-core/score-core.ts new file mode 100644 index 000000000..465d20ff3 --- /dev/null +++ b/common/src/common-core/score-core.ts @@ -0,0 +1,53 @@ +import { ScoreDocument } from ".."; +import { ICollection } from "monk"; + +export async function AutoCoerce( + collection: ICollection, + scores: ScoreDocument[] +): Promise { + let notPBsArr = []; + + for (const s of scores) { + if (!s.isLampPB) { + notPBsArr.push({ + userID: s.userID, + chartID: s.chartID, + isLampPB: true, + }); + } + } + + if (notPBsArr.length === 0) { + return scores; + } + + let lampPBsArr = await collection.find({ + $or: notPBsArr, + }); + + let lampPBs: Map = new Map(); + for (const score of lampPBsArr) { + lampPBs.set(`${score.userID}-${score.chartID}`, score); + } + + for (const score of scores) { + if (!score.isLampPB) { + let lampPB = lampPBs.get(`${score.userID}-${score.chartID}`); + + if (lampPB) { + score.scoreData.lamp = lampPB.scoreData.lamp; + score.scoreData.lampIndex = lampPB.scoreData.lampIndex; + score.calculatedData.lampRating = lampPB.calculatedData.lampRating; + + // not too happy about this, this is a sign + // of something not properly being generalised. + if (score.game === "bms") { + score.calculatedData.rating = lampPB.calculatedData.rating; + } + score.isLampPB = true; + } + } + } + + return scores; +} diff --git a/common/src/index.ts b/common/src/index.ts index b9a082b50..c47b0173a 100644 --- a/common/src/index.ts +++ b/common/src/index.ts @@ -3,3 +3,4 @@ export * from "./types"; export * as config from "./config"; export * as gameClasses from "./game-classes"; export * as ESDCore from "./common-core/esd-core"; +export * as ScoreCore from "./common-core/score-core";