mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
draft: 从fps文件导入题目
This commit is contained in:
1 parent
59f95734b8
commit
ce69235207
7 files changed
+149
-9
No files matched your search
@@ -0,0 +1,95 @@
|
|||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
import path from 'path';
|
||||||
|
import os from 'os';
|
||||||
|
import fs from 'fs-extra';
|
||||||
|
import TurndownService from 'turndown';
|
||||||
|
import AdmZip from 'adm-zip';
|
||||||
|
import xml2js from 'xml2js';
|
||||||
|
import { LocalProblemConfig } from 'hydrooj';
|
||||||
|
import {
|
||||||
|
Route, Handler, param, Types,
|
||||||
|
} from 'hydrooj/dist/service/server';
|
||||||
|
import { PERM } from 'hydrooj/dist/model/builtin';
|
||||||
|
|
||||||
|
const turndownService = new TurndownService();
|
||||||
|
|
||||||
|
class FpsProblemImportHandler extends Handler {
|
||||||
|
async get() {
|
||||||
|
this.response.template = 'problem_import_fps.html';
|
||||||
|
}
|
||||||
|
|
||||||
|
@param('input', Types.String)
|
||||||
|
async post(domainId: string, input: string) {
|
||||||
|
const result = await xml2js.parseStringPromise(input);
|
||||||
|
const problem = global.Hydro.model.problem;
|
||||||
|
for (const p of result.fps.item) {
|
||||||
|
const testdata = new AdmZip();
|
||||||
|
const content = [];
|
||||||
|
if (p.description) {
|
||||||
|
content.push(
|
||||||
|
this.translate('problem.import.problem_description'),
|
||||||
|
p.description.map((i) => turndownService.turndown(i)).join('\n'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (p.input) {
|
||||||
|
content.push(
|
||||||
|
this.translate('problem.import.input_format'),
|
||||||
|
p.input.map((i) => turndownService.turndown(i)).join('\n'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (p.output) {
|
||||||
|
content.push(
|
||||||
|
this.translate('problem.import.output_format'),
|
||||||
|
p.output.map((i) => turndownService.turndown(i)).join('\n'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (p.sample_input) {
|
||||||
|
content.push(
|
||||||
|
this.translate('problem.import.sample_input'),
|
||||||
|
// eslint-disable-next-line prefer-template
|
||||||
|
p.sample_input.map((i: string) => ('```\n' + i + '\n```')).join('\n'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (p.sample_output) {
|
||||||
|
content.push(
|
||||||
|
this.translate('problem.import.sample_output'),
|
||||||
|
// eslint-disable-next-line prefer-template
|
||||||
|
p.sample_output.map((i: string) => ('```\n' + i + '\n```')).join('\n'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (p.hint) {
|
||||||
|
content.push(
|
||||||
|
this.translate('problem.import.hint'),
|
||||||
|
p.hint.map((i) => turndownService.turndown(i)).join('\n'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const config: LocalProblemConfig = {
|
||||||
|
time: p.time_limit[0]._ + p.time_limit[0].$.unit,
|
||||||
|
memory: p.memory_limit[0]._ + p.memory_limit[0].$.unit,
|
||||||
|
};
|
||||||
|
const pid = await problem.add(domainId, null, p.title.join(' '), content.join('\n'), this.user._id, p.source, []);
|
||||||
|
testdata.addFile('config.yaml', Buffer.from(`time:${config.time}\nmemory:${config.memory}`));
|
||||||
|
for (let i = 0; i < p.test_input.length; i++) {
|
||||||
|
testdata.addFile(`${i + 1}.in`, Buffer.from(p.test_input[i]));
|
||||||
|
testdata.addFile(`${i + 1}.out`, Buffer.from(p.test_output[i]));
|
||||||
|
}
|
||||||
|
const file = path.resolve(os.tmpdir(), 'hydro', `${Math.random()}.zip`);
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
testdata.writeZip(file, (err) => {
|
||||||
|
if (err) reject(err);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await problem.setTestdata(domainId, pid, file);
|
||||||
|
await fs.unlink(file);
|
||||||
|
}
|
||||||
|
this.response.body = { count: result.fps.item.length };
|
||||||
|
this.response.redirect = this.url('problem_main');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function apply() {
|
||||||
|
Route('problem_import_fps', '/problem/import/fps', FpsProblemImportHandler, PERM.PERM_CREATE_PROBLEM);
|
||||||
|
}
|
||||||
|
|
||||||
|
global.Hydro.handler.fpsImport = apply;
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "@hydrooj/fps-importer",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Import FPS problems",
|
||||||
|
"main": "package.json",
|
||||||
|
"repository": "https://github.com/hydro-dev/Hydro.git",
|
||||||
|
"author": "undefined <masnn0@outlook.com>",
|
||||||
|
"license": "AGPL-3.0-only",
|
||||||
|
"dependencies": {
|
||||||
|
"adm-zip": "^0.4.16",
|
||||||
|
"fs-extra": "^9.0.1",
|
||||||
|
"hydrooj": "^2.12.22",
|
||||||
|
"turndown": "^6.0.0",
|
||||||
|
"xml2js": "^0.4.23"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/turndown": "^5.0.0",
|
||||||
|
"@types/xml2js": "^0.4.5"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"lint": "eslint . --ext ts --fix"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2019",
|
||||||
|
"module": "commonjs",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"declaration": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"composite": true,
|
||||||
|
"strictBindCallApply": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"outDir": ".",
|
||||||
|
"rootDir": "."
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"*.ts"
|
||||||
|
],
|
||||||
|
"exclude": []
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
"shell-quote": "^1.7.2",
|
"shell-quote": "^1.7.2",
|
||||||
"systeminformation": "^4.26.10"
|
"systeminformation": "^4.26.10"
|
||||||
},
|
},
|
||||||
"license": "GPL-3.0-only",
|
"license": "AGPL-3.0-only",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bson": "^4.0.2",
|
"@types/bson": "^4.0.2",
|
||||||
"@types/fs-extra": "^9.0.1",
|
"@types/fs-extra": "^9.0.1",
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.12.22",
|
"version": "2.12.23",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "dist/loader.js",
|
"main": "dist/loader.js",
|
||||||
"typings": "dist/loader.d.ts",
|
"typings": "dist/loader.d.ts",
|
||||||
"repository": "https://github.com/hydro-dev/Hydro.git",
|
"repository": "https://github.com/hydro-dev/Hydro.git",
|
||||||
"author": "masnn",
|
"author": "masnn",
|
||||||
"license": "GPL-3.0-only",
|
"license": "AGPL-3.0-only",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10.10"
|
"node": ">=10.10"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -270,8 +270,8 @@ class ProblemPretestHandler extends ProblemDetailHandler {
|
|||||||
lang, code, true,
|
lang, code, true,
|
||||||
{
|
{
|
||||||
input,
|
input,
|
||||||
time: 1000,
|
time: '1s',
|
||||||
memory: 256,
|
memory: '256m',
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const rdoc = await record.get(domainId, rid);
|
const rdoc = await record.get(domainId, rid);
|
||||||
|
|||||||
@@ -101,8 +101,10 @@ export interface SubtaskConfig {
|
|||||||
export interface LocalProblemConfig {
|
export interface LocalProblemConfig {
|
||||||
type?: LocalProblemType,
|
type?: LocalProblemType,
|
||||||
score?: number,
|
score?: number,
|
||||||
time?: number,
|
/** ms */
|
||||||
memory?: number,
|
time?: string,
|
||||||
|
/** Megabytes */
|
||||||
|
memory?: string,
|
||||||
checker_type?: string,
|
checker_type?: string,
|
||||||
checker?: string,
|
checker?: string,
|
||||||
interactor?: string,
|
interactor?: string,
|
||||||
@@ -167,8 +169,8 @@ export interface TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface PretestConfig {
|
export interface PretestConfig {
|
||||||
time?: number,
|
time?: string,
|
||||||
memory?: number,
|
memory?: string,
|
||||||
input: string,
|
input: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user