From 18d01840d06553d8008e9ce21d1e51ee192a8be8 Mon Sep 17 00:00:00 2001 From: JJ-8 <34778827+JJ-8@users.noreply.github.com> Date: Sun, 28 Aug 2022 13:57:09 +0200 Subject: [PATCH] Add ECSC task parser Parses the JSON output of the /stats.json endpoint of the ECSC gameboard. The output doesn't contain actual categories, so we use the 'contract' name as the category. --- front/src/ctfnote/parsers/ecsc.ts | 29 +++++++++++++++++++++++++++++ front/src/ctfnote/parsers/index.ts | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 front/src/ctfnote/parsers/ecsc.ts diff --git a/front/src/ctfnote/parsers/ecsc.ts b/front/src/ctfnote/parsers/ecsc.ts new file mode 100644 index 000000000..a487ce536 --- /dev/null +++ b/front/src/ctfnote/parsers/ecsc.ts @@ -0,0 +1,29 @@ +import { ParsedTask, Parser } from '.'; +import { parseJson, parseJsonStrict } from '../utils'; + +const ECSCParser: Parser = { + name: 'ECSC parser', + hint: 'paste ecsc /stats.json', + + parse(s: string): ParsedTask[] { + const tasks = []; + const data = parseJsonStrict<[{ task: string; contract: string }]>(s); + if (!Array.isArray(data)) { + return []; + } + + for (const task of data) { + if (!task.task || !task.contract) { + continue; + } + tasks.push({ title: task.task, category: task.contract }); + } + return tasks; + }, + isValid(s) { + const data = parseJson<[{ task: string; contract: string }]>(s); + return Array.isArray(data); + }, +}; + +export default ECSCParser; diff --git a/front/src/ctfnote/parsers/index.ts b/front/src/ctfnote/parsers/index.ts index f23d010bf..cf9181058 100644 --- a/front/src/ctfnote/parsers/index.ts +++ b/front/src/ctfnote/parsers/index.ts @@ -1,4 +1,5 @@ import CTFDParser from './ctfd'; +import ECSCParser from './ecsc'; import RawParser from './raw'; export type ParsedTask = { @@ -13,4 +14,4 @@ export type Parser = { parse(s: string): ParsedTask[]; }; -export default [RawParser, CTFDParser]; +export default [RawParser, CTFDParser, ECSCParser];