diff --git a/packages/vjudge/src/interface.ts b/packages/vjudge/src/interface.ts index b891fff77..48288a437 100644 --- a/packages/vjudge/src/interface.ts +++ b/packages/vjudge/src/interface.ts @@ -27,6 +27,7 @@ export interface IBasicProvider { files: Record; tag: string[]; content: string; + difficulty?: number; }>; listProblem(page: number, resync: boolean): Promise; submitProblem(id: string, lang: string, code: string, info: any, next: any, end: any): Promise; diff --git a/packages/vjudge/src/model.ts b/packages/vjudge/src/model.ts index eb723b0c5..f7af2ea97 100644 --- a/packages/vjudge/src/model.ts +++ b/packages/vjudge/src/model.ts @@ -9,6 +9,7 @@ import TaskModel from 'hydrooj/src/model/task'; import * as bus from 'hydrooj/src/service/bus'; import db from 'hydrooj/src/service/db'; import { BasicProvider, IBasicProvider, RemoteAccount } from './interface'; +import { getDifficulty } from './providers/codeforces'; import providers from './providers/index'; const coll = db.collection('vjudge'); @@ -53,6 +54,7 @@ class Service { const res = await this.api.getProblem(pid); if (!res) continue; const id = await ProblemModel.add(domainId, pid, res.title, res.content, 1, res.tag, false); + if (res.difficulty) await ProblemModel.edit(domainId, id, { difficulty: res.difficulty }); for (const key in res.files) { await ProblemModel.addAdditionalFile(domainId, id, key, res.files[key]); } @@ -104,4 +106,22 @@ async function loadAccounts() { } } +declare module 'hydrooj/src/interface' { + interface Model { + vjudge: VJudgeModel; + } +} + +class VJudgeModel { + static async fixCodeforcesDifficulty(domainId = 'codeforces') { + const pdocs = await ProblemModel.getMulti(domainId, {}).toArray(); + for (const pdoc of pdocs) { + await ProblemModel.edit(domainId, pdoc.docId, { difficulty: getDifficulty(pdoc.tag) }); + } + return true; + } +} + +global.Hydro.model.vjudge = VJudgeModel; + bus.on('app/started', loadAccounts); diff --git a/packages/vjudge/src/providers/codeforces.ts b/packages/vjudge/src/providers/codeforces.ts index 06e0ec934..30372c5d6 100644 --- a/packages/vjudge/src/providers/codeforces.ts +++ b/packages/vjudge/src/providers/codeforces.ts @@ -27,6 +27,25 @@ puppeteer.use(StealthPlugin()).use(PortalPlugin({ }, })); +export function getDifficulty(tags: string[]) { + const d = tags.find((i) => /^\*\d+$/.test(i))?.split('*')[1]; + if (!(d && +d)) return null; + const map = [ + [500, 1], + [800, 2], + [1200, 3], + [1500, 4], + [1800, 5], + [2000, 6], + [2200, 7], + [2400, 8], + [2600, 9], + [2800, 10], + ]; + for (const [i, j] of map) if (+d < i) return j; + return 10; +} + export default class CodeforcesProvider implements IBasicProvider { constructor(public account: RemoteAccount, private save: (data: any) => Promise) { if (account.cookie) this.cookie = account.cookie; @@ -269,6 +288,7 @@ export default class CodeforcesProvider implements IBasicProvider { }, files, tag, + difficulty: getDifficulty(tag), content: buildContent([ { type: 'Text', sectionTitle: 'Description', subType: 'markdown', text: description,