add AutoCoerce from score-core.

This commit is contained in:
zkldi
2021-04-26 03:27:51 +01:00
parent f404d48457
commit 3c557fcc88
8 changed files with 117 additions and 2 deletions
+3
View File
@@ -0,0 +1,3 @@
import { ScoreDocument } from "..";
import { ICollection } from "monk";
export declare function AutoCoerce(collection: ICollection<ScoreDocument>, scores: ScoreDocument[]): Promise<ScoreDocument[]>;
+55
View File
@@ -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
+1
View File
@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AAG,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"}
+1
View File
@@ -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";
+2 -1
View File
@@ -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
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAwB;AAExB,mDAAmC;AACnC,8DAA8C;AAC9C,kEAAkD"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAwB;AAExB,mDAAmC;AACnC,8DAA8C;AAC9C,kEAAkD;AAClD,sEAAsD"}
+53
View File
@@ -0,0 +1,53 @@
import { ScoreDocument } from "..";
import { ICollection } from "monk";
export async function AutoCoerce(
collection: ICollection<ScoreDocument>,
scores: ScoreDocument[]
): Promise<ScoreDocument[]> {
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<string, ScoreDocument> = 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;
}
+1
View File
@@ -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";