add some type magic

This commit is contained in:
undefined committed 2021-03-08 14:32:11 +08:00
1 parent 2344a41816
commit a4d7dc4b04
6 files changed
+23 -23

No files matched your search

+1 -1
View File
@@ -62,7 +62,7 @@ for (const package of packages) {
if (!fs.statSync(path.resolve(basedir, 'src', file)).isFile()) continue; if (!fs.statSync(path.resolve(basedir, 'src', file)).isFile()) continue;
const name = file.split('.')[0]; const name = file.split('.')[0];
if (['handler', 'service', 'lib', 'model', 'script'].includes(name)) { if (['handler', 'service', 'lib', 'model', 'script'].includes(name)) {
fs.writeFileSync(path.resolve(basedir, name + '.js'), `module.exports = require('./dist/${name}');\n`) fs.writeFileSync(path.resolve(basedir, name + '.js'), `module.exports = require('./dist/${name}');\n`);
} }
} }
} }
+6 -6
View File
@@ -269,9 +269,9 @@ export interface TrainingNode {
pids: number[], pids: number[],
} }
export interface Tdoc extends Document { export interface Tdoc<docType = document['TYPE_CONTEST'] | document['TYPE_HOMEWORK'] | document['TYPE_TRAINING']> extends Document {
docId: ObjectID, docId: ObjectID,
docType: document['TYPE_CONTEST'] | document['TYPE_HOMEWORK'] | document['TYPE_TRAINING'], docType: docType & number,
beginAt: Date, beginAt: Date,
endAt: Date, endAt: Date,
attend: number, attend: number,
@@ -399,12 +399,12 @@ export interface ContestRule {
TEXT: string, TEXT: string,
check: (args: any) => any, check: (args: any) => any,
statusSort: any, statusSort: any,
showScoreboard: (tdoc: Tdoc, now: Date) => boolean, showScoreboard: (tdoc: Tdoc<30 | 60>, now: Date) => boolean,
showRecord: (tdoc: Tdoc, now: Date) => boolean, showRecord: (tdoc: Tdoc<30 | 60>, now: Date) => boolean,
stat: (tdoc: Tdoc, journal: any[]) => ContestStat, stat: (tdoc: Tdoc<30 | 60>, journal: any[]) => ContestStat,
scoreboard: ( scoreboard: (
isExport: boolean, _: (s: string) => string, isExport: boolean, _: (s: string) => string,
tdoc: Tdoc, rankedTsdocs: any[], udict: Udict, pdict: Pdict tdoc: Tdoc<30 | 60>, rankedTsdocs: any[], udict: Udict, pdict: Pdict
) => ScoreboardRow[], ) => ScoreboardRow[],
rank: (tsdocs: any[]) => any[], rank: (tsdocs: any[]) => any[],
} }
+11 -10
View File
@@ -385,9 +385,10 @@ export async function edit(
return await document.set(domainId, type, tid, $set); return await document.set(domainId, type, tid, $set);
} }
export async function get( export async function get(domainId: string, tid: ObjectID, type: -1): Promise<Tdoc<30 | 60>>;
domainId: string, tid: ObjectID, type: Type | -1 = document.TYPE_CONTEST, export async function get<T extends Type>(domainId: string, tid: ObjectID, type: T): Promise<Tdoc<T>>;
) { export async function get(domainId: string, tid: ObjectID): Promise<Tdoc<30>>;
export async function get(domainId: string, tid: ObjectID, type: Type | -1 = document.TYPE_CONTEST) {
let tdoc: Tdoc; let tdoc: Tdoc;
if (type === -1) { if (type === -1) {
tdoc = await document.get(domainId, document.TYPE_CONTEST, tid); tdoc = await document.get(domainId, document.TYPE_CONTEST, tid);
@@ -413,14 +414,14 @@ export async function updateStatus(
export function getStatus( export function getStatus(
domainId: string, tid: ObjectID, uid: number, domainId: string, tid: ObjectID, uid: number,
type = document.TYPE_CONTEST, type: 30 | 60 = document.TYPE_CONTEST,
) { ) {
return document.getStatus(domainId, type, tid, uid); return document.getStatus(domainId, type, tid, uid);
} }
export async function getListStatus( export async function getListStatus(
domainId: string, uid: number, tids: ObjectID[], domainId: string, uid: number, tids: ObjectID[],
type = document.TYPE_CONTEST, type: 30 | 60 = document.TYPE_CONTEST,
) { ) {
const r = {}; const r = {};
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
@@ -441,7 +442,7 @@ export async function attend(
return {}; return {};
} }
export function getMultiStatus(domainId: string, query: any, docType = document.TYPE_CONTEST) { export function getMultiStatus(domainId: string, query: any, docType: 30 | 60 = document.TYPE_CONTEST) {
return document.getMultiStatus(domainId, docType, query); return document.getMultiStatus(domainId, docType, query);
} }
@@ -480,13 +481,13 @@ export const ContestHandlerMixin = (c) => class extends c {
return this.user.hasPerm(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD); return this.user.hasPerm(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
} }
canShowRecord(tdoc: Tdoc, allowPermOverride = true) { canShowRecord(tdoc: Tdoc<30 | 60>, allowPermOverride = true) {
if (RULES[tdoc.rule].showRecord(tdoc, new Date())) return true; if (RULES[tdoc.rule].showRecord(tdoc, new Date())) return true;
if (allowPermOverride && this.canViewHiddenScoreboard()) return true; if (allowPermOverride && this.canViewHiddenScoreboard()) return true;
return false; return false;
} }
canShowScoreboard(tdoc: Tdoc, allowPermOverride = true) { canShowScoreboard(tdoc: Tdoc<30 | 60>, allowPermOverride = true) {
if (RULES[tdoc.rule].showScoreboard(tdoc, new Date())) return true; if (RULES[tdoc.rule].showScoreboard(tdoc, new Date())) return true;
if (allowPermOverride && this.canViewHiddenScoreboard()) return true; if (allowPermOverride && this.canViewHiddenScoreboard()) return true;
return false; return false;
@@ -495,7 +496,7 @@ export const ContestHandlerMixin = (c) => class extends c {
async getScoreboard( async getScoreboard(
domainId: string, tid: ObjectID, domainId: string, tid: ObjectID,
isExport = false, docType: 30 | 60 = document.TYPE_CONTEST, isExport = false, docType: 30 | 60 = document.TYPE_CONTEST,
): Promise<[Tdoc, ScoreboardRow[], Udict]> { ): Promise<[Tdoc<30 | 60>, ScoreboardRow[], Udict]> {
const { const {
tdoc, udict, pdict, rankedTsdocs, tdoc, udict, pdict, rankedTsdocs,
} = await this.getRawStatus(domainId, tid, docType); } = await this.getRawStatus(domainId, tid, docType);
@@ -549,7 +550,7 @@ export async function getAndListStatus(
domainId: string, tid: ObjectID, docType: Type | -1 = document.TYPE_CONTEST, domainId: string, tid: ObjectID, docType: Type | -1 = document.TYPE_CONTEST,
): Promise<[Tdoc, any[]]> { ): Promise<[Tdoc, any[]]> {
// TODO(iceboy): projection, pagination. // TODO(iceboy): projection, pagination.
const tdoc = await get(domainId, tid, docType); const tdoc = await get(domainId, tid, docType as any);
const tsdocs = await document.getMultiStatus(domainId, docType, { docId: tid }) const tsdocs = await document.getMultiStatus(domainId, docType, { docId: tid })
.sort(RULES[tdoc.rule].statusSort).toArray(); .sort(RULES[tdoc.rule].statusSort).toArray();
return [tdoc, tsdocs]; return [tdoc, tsdocs];
+4 -4
View File
@@ -50,13 +50,13 @@ async function runProblem(...arg: any[]) {
} }
} }
async function runContest(tdoc: Tdoc, udict: ND, report: Function): Promise<void>; async function runContest(tdoc: Tdoc<30 | 60>, udict: ND, report: Function): Promise<void>;
async function runContest( async function runContest(
domainId: string, tid: ObjectID, udict: ND, report: Function domainId: string, tid: ObjectID, udict: ND, report: Function
): Promise<void>; ): Promise<void>;
async function runContest(...arg: any[]) { async function runContest(...arg: any[]) {
const start = new Date().getTime(); const start = new Date().getTime();
const tdoc: Tdoc = (typeof arg[0] === 'string') const tdoc: Tdoc<30 | 60> = (typeof arg[0] === 'string')
? await contest.get(arg[0], arg[1], -1) ? await contest.get(arg[0], arg[1], -1)
: arg[0]; : arg[0];
const udict: ND = (typeof arg[0] === 'string') ? arg[2] : arg[1]; const udict: ND = (typeof arg[0] === 'string') ? arg[2] : arg[1];
@@ -127,8 +127,8 @@ async function runInDomain(domainId: string, isSub: boolean, report: Function) {
for (const dudoc of dudocs) { for (const dudoc of dudocs) {
deltaudict[dudoc.uid] = dudoc.rpdelta || 0; deltaudict[dudoc.uid] = dudoc.rpdelta || 0;
} }
const contests = await contest.getMulti(domainId, { rated: true }) const contests: Tdoc<30 | 60>[] = await contest.getMulti(domainId, { rated: true })
.sort('endAt', -1).toArray(); .sort('endAt', -1).toArray() as any;
await report({ message: `Found ${contests.length} contests in ${domainId}` }); await report({ message: `Found ${contests.length} contests in ${domainId}` });
for (const i in contests) { for (const i in contests) {
const tdoc = contests[i]; const tdoc = contests[i];
+1 -1
View File
@@ -11,7 +11,7 @@ const coll = db.collection('status');
const logger = new Logger('monitor'); const logger = new Logger('monitor');
function crypt(str: string) { function crypt(str: string) {
const cipher = crypto.createCipheriv('des-ecb', 'hydro-oj', ''); const cipher = crypto.createCipheriv('des-ecb', 'hydro-oj', ''); // lgtm [js/hardcoded-credentials]
let encrypted = cipher.update(str, 'utf8', 'hex'); let encrypted = cipher.update(str, 'utf8', 'hex');
encrypted += cipher.final('hex'); encrypted += cipher.final('hex');
return encrypted; return encrypted;
-1
View File
@@ -86,7 +86,6 @@ const scripts: UpgradeScript[] = [
logger.error('Cannot upgrade. Please change storage config.'); logger.error('Cannot upgrade. Please change storage config.');
return false; return false;
} }
const coll: Collection<Ufdoc> = db.collection('file');
logger.info('Changing storage engine. This may take a long time.'); logger.info('Changing storage engine. This may take a long time.');
// Problem file and User file // Problem file and User file
let savedProgress = system.get('upgrade.file.progress.domain'); let savedProgress = system.get('upgrade.file.progress.domain');