From 88613c8a05a65de9997d76b0263cd5c813947253 Mon Sep 17 00:00:00 2001 From: undefined Date: Thu, 10 Nov 2022 13:33:49 +0800 Subject: [PATCH] fps-import: support fps version 1.4 --- packages/fps-importer/index.ts | 36 +++++++++++++++------------- packages/fps-importer/locale/zh.yaml | 1 - packages/utils/lib/utils.ts | 6 ++--- 3 files changed, 23 insertions(+), 20 deletions(-) delete mode 100644 packages/fps-importer/locale/zh.yaml diff --git a/packages/fps-importer/index.ts b/packages/fps-importer/index.ts index 3958b9d64..3f7792117 100644 --- a/packages/fps-importer/index.ts +++ b/packages/fps-importer/index.ts @@ -61,29 +61,30 @@ class FpsProblemImportHandler extends Handler { const title = decodeHTML(p.title.join(' ')); const tags = _.filter(p.source, (i: string) => i.trim()); const pid = await ProblemModel.add(domainId, null, title, buildContent(content, 'html'), this.user._id, tags); - await ProblemModel.addTestdata(domainId, pid, 'config.yaml', Buffer.from(`time: ${config.time}\nmemory: ${config.memory}`)); + const tasks = [ + ProblemModel.edit(domainId, pid, { html: true }), + ProblemModel.addTestdata(domainId, pid, 'config.yaml', Buffer.from(`time: ${config.time}\nmemory: ${config.memory}`)), + ]; + const addTestdata = (node: any, id: string, ext: string) => { + let c = node; + if (node.$?.name) { + id = node.$.name; + c = node._; + } + tasks.push(ProblemModel.addTestdata(domainId, pid, `${id}.${ext}`, Buffer.from(c))); + }; if (p.test_output) { for (let i = 0; i < p.test_input.length; i++) { - const input = typeof p.test_input[i]?._ === 'string' - ? p.test_input[i]?._ - : typeof p.test_input[i] === 'string' - ? p.test_input[i] - : ''; - const output = typeof p.test_output[i]?._ === 'string' - ? p.test_output[i]?._ - : typeof p.test_output[i] === 'string' - ? p.test_output[i] - : ''; - await ProblemModel.addTestdata(domainId, pid, `${i + 1}.in`, Buffer.from(input)); - await ProblemModel.addTestdata(domainId, pid, `${i + 1}.out`, Buffer.from(output)); + addTestdata(p.test_input[i], `${i + 1}`, 'in'); + addTestdata(p.test_output[i], `${i + 1}`, 'out'); } } else if (p.test_input) { for (let i = 0; i < p.test_input.length / 2; i++) { - await ProblemModel.addTestdata(domainId, pid, `${i + 1}.in`, Buffer.from(p.test_input[2 * i])); - await ProblemModel.addTestdata(domainId, pid, `${i + 1}.out`, Buffer.from(p.test_input[2 * i + 1])); + addTestdata(p.test_input[2 * i], `${i + 1}`, 'in'); + addTestdata(p.test_input[2 * i + 1], `${i + 1}`, 'out'); } } - await ProblemModel.edit(domainId, pid, { html: true }); + await Promise.all(tasks); if (p.solution) { let s = ''; for (const sol of p.solution) { @@ -131,4 +132,7 @@ class FpsProblemImportHandler extends Handler { export async function apply(ctx: Context) { ctx.Route('problem_import_fps', '/problem/import/fps', FpsProblemImportHandler, PERM.PERM_CREATE_PROBLEM); ctx.inject('ProblemAdd', 'problem_import_fps', { icon: 'copy', text: 'From FPS File' }); + ctx.i18n.load('zh', { + 'From FPS File': '从 FPS 文件导入', + }); } diff --git a/packages/fps-importer/locale/zh.yaml b/packages/fps-importer/locale/zh.yaml deleted file mode 100644 index 0b375dc4b..000000000 --- a/packages/fps-importer/locale/zh.yaml +++ /dev/null @@ -1 +0,0 @@ -From FPS File: 从 FPS 文件导入 \ No newline at end of file diff --git a/packages/utils/lib/utils.ts b/packages/utils/lib/utils.ts index 8f399d49a..1fe2a3356 100644 --- a/packages/utils/lib/utils.ts +++ b/packages/utils/lib/utils.ts @@ -64,13 +64,13 @@ String.prototype.format = function formatStr(...args) { return result; }; -export function isClass(obj: any, strict = false) { +export function isClass(obj: any, strict = false): obj is new (...args: any) => any { if (typeof obj !== 'function') return false; - const str = obj.toString(); if (obj.prototype === undefined) return false; if (obj.prototype.constructor !== obj) return false; - if (str.slice(0, 5) === 'class') return true; if (Object.getOwnPropertyNames(obj.prototype).length >= 2) return true; + const str = obj.toString(); + if (str.slice(0, 5) === 'class') return true; if (/^function\s+\(|^function\s+anonymous\(/.test(str)) return false; if (strict && /^function\s+[A-Z]/.test(str)) return true; if (/\b\(this\b|\bthis[.[]\b/.test(str)) {