From 2ffaf9201fd2b1139bbd4758ecf645dd95bbafb6 Mon Sep 17 00:00:00 2001 From: undefined Date: Fri, 16 Sep 2022 10:10:59 +0800 Subject: [PATCH] core: simplify contest rank function --- packages/hydrooj/src/lib/rank.ts | 44 +++------------------------ packages/hydrooj/src/model/contest.ts | 6 ++-- 2 files changed, 8 insertions(+), 42 deletions(-) diff --git a/packages/hydrooj/src/lib/rank.ts b/packages/hydrooj/src/lib/rank.ts index 60e8089c..c0ebbb41 100644 --- a/packages/hydrooj/src/lib/rank.ts +++ b/packages/hydrooj/src/lib/rank.ts @@ -1,20 +1,14 @@ import { Cursor } from 'mongodb'; import paginate from './paginate'; -type G = Promise<[[number, T], () => G] | []>; -interface ranked { - (cursor: Cursor, equ: (a: T, b: T) => boolean, page: number): Promise<[[number, T][], number]>; - iterate(cursor: Cursor, equ: (a: T, b: T) => boolean): G; - all(cursor: Cursor, equ: (a: T, b: T) => boolean): Promise<[[number, T][], 1]>; -} +type ranked = (cursor: Cursor, equ: ((a: T, b: T) => boolean), page?: number) => Promise<[[number, T][], number]>; -const ranked: ranked = async function rankedPage(cursor, equ = (a, b) => a === b, page) { - if (!page) return await ranked.all(cursor, equ); +const ranked: ranked = async (cursor, equ, page) => { let last = null; - let r = (page - 1) * 100; - let count = r; + let r = page ? (page - 1) * 100 : 0; + let count = page ? r : 0; const results = []; - const [docs, nPages] = await paginate(cursor, page, 100); + const [docs, nPages] = page ? await paginate(cursor, page, 100) : [await cursor.toArray(), 1]; for (const doc of docs) { count++; if (!last || !equ(last, doc)) r = count; @@ -23,34 +17,6 @@ const ranked: ranked = async function rankedPage(cursor, equ = (a, b) => a === b } return [results, nPages]; }; -ranked.iterate = async function rankedIterate(cursor: Cursor, equ = (a, b) => a === b) { - let last = null; - let r = 0; - let count = 0; - async function getNext(): G { - if (!await cursor.hasNext()) return []; - const doc = await cursor.next(); - count++; - if (!last || !equ(last, doc)) r = count; - last = doc; - return [[r, doc], getNext]; - } - return await getNext(); -}; -ranked.all = async function rankedAll(cursor, equ = (a, b) => a === b) { - let last = null; - let r = 0; - let count = 0; - const results = []; - const docs = await cursor.toArray(); - for (const doc of docs) { - count++; - if (!last || !equ(last, doc)) r = count; - last = doc; - results.push([r, doc]); - } - return [results, 1]; -}; global.Hydro.lib.rank = ranked; export = ranked; diff --git a/packages/hydrooj/src/model/contest.ts b/packages/hydrooj/src/model/contest.ts index 057685c8..2dc60076 100644 --- a/packages/hydrooj/src/model/contest.ts +++ b/packages/hydrooj/src/model/contest.ts @@ -189,7 +189,7 @@ const acm = buildContestRule({ return [rows, udict, nPages]; }, async ranked(tdoc, cursor) { - return await ranked.all(cursor, (a, b) => a.accept === b.accept && a.time === b.time); + return await ranked(cursor, (a, b) => a.accept === b.accept && a.time === b.time); }, }); @@ -310,7 +310,7 @@ const oi = buildContestRule({ return [rows, udict, nPages]; }, async ranked(tdoc, cursor) { - return await ranked.all(cursor, (a, b) => a.score === b.score); + return await ranked(cursor, (a, b) => a.score === b.score); }, }); @@ -467,7 +467,7 @@ const homework = buildContestRule({ return [rows, udict, nPages]; }, async ranked(tdoc, cursor) { - return await ranked.all(cursor, (a, b) => a.score === b.score); + return await ranked(cursor, (a, b) => a.score === b.score); }, });