mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
vjudge: handle provider registration
This commit is contained in:
1 parent
bbbb3cc84a
commit
b51620d634
3 files changed
+41
-21
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/vjudge",
|
"name": "@hydrooj/vjudge",
|
||||||
"version": "1.6.3",
|
"version": "1.6.4",
|
||||||
"description": "Submit problems to remote oj",
|
"description": "Submit problems to remote oj",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "https://github.com/hydro-dev/Hydro.git",
|
"repository": "https://github.com/hydro-dev/Hydro.git",
|
||||||
|
|||||||
@@ -2,18 +2,17 @@
|
|||||||
import os from 'os';
|
import os from 'os';
|
||||||
import {
|
import {
|
||||||
Context, db, DomainModel, JudgeHandler, Logger,
|
Context, db, DomainModel, JudgeHandler, Logger,
|
||||||
ProblemModel, RecordModel, sleep, STATUS, TaskModel, Time,
|
ProblemModel, RecordModel, Service, sleep, STATUS, TaskModel, Time,
|
||||||
} from 'hydrooj';
|
} from 'hydrooj';
|
||||||
import { BasicProvider, IBasicProvider, RemoteAccount } from './interface';
|
import { BasicProvider, IBasicProvider, RemoteAccount } from './interface';
|
||||||
import { getDifficulty } from './providers/codeforces';
|
import { getDifficulty } from './providers/codeforces';
|
||||||
import providers from './providers/index';
|
import providers from './providers/index';
|
||||||
|
|
||||||
const coll = db.collection('vjudge');
|
const coll = db.collection('vjudge');
|
||||||
const Pool = {};
|
|
||||||
const logger = new Logger('vjudge');
|
const logger = new Logger('vjudge');
|
||||||
const syncing = {};
|
const syncing = {};
|
||||||
|
|
||||||
class Service {
|
class AccountService {
|
||||||
api: IBasicProvider;
|
api: IBasicProvider;
|
||||||
problemLists: Set<string>;
|
problemLists: Set<string>;
|
||||||
listUpdated = false;
|
listUpdated = false;
|
||||||
@@ -127,6 +126,9 @@ declare module 'hydrooj' {
|
|||||||
interface Model {
|
interface Model {
|
||||||
vjudge: VJudgeModel;
|
vjudge: VJudgeModel;
|
||||||
}
|
}
|
||||||
|
interface Context {
|
||||||
|
vjudge: VJudgeService;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VJudgeModel {
|
class VJudgeModel {
|
||||||
@@ -139,16 +141,41 @@ class VJudgeModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class VJudgeService extends Service {
|
||||||
|
constructor(ctx) {
|
||||||
|
super(ctx, 'vjudge', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
accounts: RemoteAccount[];
|
||||||
|
private providers: Record<string, any> = {};
|
||||||
|
private pool: Record<string, any> = {};
|
||||||
|
async start() {
|
||||||
|
this.accounts = await coll.find().toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
addProvider(type: string, provider: BasicProvider) {
|
||||||
|
if (this.providers[type]) throw new Error(`duplicate provider ${type}`);
|
||||||
|
this.providers[type] = provider;
|
||||||
|
for (const account of this.accounts.filter((a) => a.type === type)) {
|
||||||
|
if (account.enableOn && !account.enableOn.includes(os.hostname())) continue;
|
||||||
|
this.pool[`${account.type}/${account.handle}`] = new AccountService(provider, account);
|
||||||
|
}
|
||||||
|
this.caller?.on('dispose', () => {
|
||||||
|
// TODO dispose session
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
global.Hydro.model.vjudge = VJudgeModel;
|
global.Hydro.model.vjudge = VJudgeModel;
|
||||||
|
|
||||||
export function apply(ctx: Context) {
|
Context.service('vjudge', VJudgeService);
|
||||||
|
export const name = 'vjudge';
|
||||||
|
export async function apply(ctx: Context) {
|
||||||
if (process.env.NODE_APP_INSTANCE !== '0') return;
|
if (process.env.NODE_APP_INSTANCE !== '0') return;
|
||||||
ctx.on('ready', async () => {
|
const vjudge = new VJudgeService(ctx);
|
||||||
const accounts = await coll.find().toArray();
|
await vjudge.start();
|
||||||
for (const account of accounts) {
|
for (const [k, v] of Object.entries(providers)) {
|
||||||
if (!providers[account.type]) continue;
|
vjudge.addProvider(k, v);
|
||||||
if (account.enableOn && !account.enableOn.includes(os.hostname())) continue;
|
}
|
||||||
Pool[`${account.type}/${account.handle}`] = new Service(providers[account.type], account);
|
ctx.vjudge = vjudge;
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
@@ -5,14 +5,7 @@ import poj from './poj';
|
|||||||
import spoj from './spoj';
|
import spoj from './spoj';
|
||||||
import uoj from './uoj';
|
import uoj from './uoj';
|
||||||
|
|
||||||
declare module 'hydrooj' {
|
|
||||||
interface HydroGlobal {
|
|
||||||
vjudge: typeof vjudge;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const vjudge: Record<string, any> = {
|
const vjudge: Record<string, any> = {
|
||||||
codeforces, csgoj, luogu, poj, spoj, uoj,
|
codeforces, csgoj, luogu, poj, spoj, uoj,
|
||||||
};
|
};
|
||||||
global.Hydro.vjudge = vjudge;
|
export default vjudge;
|
||||||
export = vjudge;
|
|
||||||
Reference in new issue
Block a user