diff --git a/index.ts b/index.ts index f2abf0a..9f9ffd3 100644 --- a/index.ts +++ b/index.ts @@ -24,9 +24,16 @@ const fileType = await select({ value: 'special_item_group', label: 'special_item_group.txt', }, + { + value: 'blend', + label: 'blend.txt', + }, ], }); +const getProto = () => + csvToJson.fieldDelimiter('\t').csvStringToJson(itemProto); + if (isCancel(fileType)) { cancel('Operation cancelled'); process.exit(0); @@ -36,9 +43,9 @@ await match(fileType) .with('special_item_group', async () => { const s = spinner(); - const proto = csvToJson.fieldDelimiter('\t').csvStringToJson(itemProto); + const proto = getProto(); - s.start('Transforming'); + s.start('Transforming special_item_group.txt'); const input = Bun.file('./input/special_item_group.txt'); if (!input.exists()) { @@ -138,6 +145,58 @@ await match(fileType) ), ); }) + .with('blend', async () => { + const s = spinner(); + + s.start('Transforming blend.txt'); + + const input = Bun.file('./input/blend.txt'); + if (!input.exists()) { + throw new Error('The file input/special_item_group.txt was not found'); + } + + const blend = await input.text(); + + const REGEX = + /section\s+[\s\S]*?item_vnum\s+(\d+)[\s\S]*?apply_type\s+(\S+)[\s\S]*?apply_value\s+([\d\s]+)[\s\S]*?apply_duration\s+([\d\s]+)[\s\S]*?end/gim; + + const matches = Array.from(blend.matchAll(REGEX)).map( + ([_, vnum, apply_type, values, durations]) => { + const validDurations = durations.trim().split(/\t/); + + return { + vnum: parseInt(vnum, 10), + apply_type, + apply_data: values + .trim() + .split(/\t/) + .map((value, index) => ({ + value: parseInt(value, 10), + duration: parseInt(validDurations[index], 10), + })), + }; + }, + ); + + s.stop('blend.txt was transformed'); + + const out = Bun.file('./output/blend.json'); + if (await out.exists()) { + await out.delete(); + } + + out.write( + JSON.stringify( + { + $schema: + 'https://raw.githubusercontent.com/WildEgo/m2-json-schemas/refs/heads/main/blend.json', + rows: matches, + }, + null, + 2, + ), + ); + }) .otherwise(() => { outro(color.bgRed('Invalid file type')); process.exit(1); diff --git a/schemas/blend.json b/schemas/blend.json new file mode 100644 index 0000000..f1de183 --- /dev/null +++ b/schemas/blend.json @@ -0,0 +1,45 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://raw.githubusercontent.com/WildEgo/m2-json-schemas/refs/heads/main/blend.json", + "title": "Blend configuration", + "type": "object", + "properties": { + "$schema": { + "type": "string" + }, + "rows": { + "type": "array", + "items": { + "type": "object", + "properties": { + "vnum": { + "type": "integer" + }, + "apply_type": { + "type": "string" + }, + "apply_data": { + "type": "array", + "items": { + "type": "object", + "properties": { + "value": { + "type": "integer" + }, + "duration": { + "type": "integer" + } + }, + "required": ["value", "duration"], + "additionalProperties": false + } + } + }, + "required": ["vnum", "apply_type", "apply_data"], + "additionalProperties": false + } + } + }, + "required": ["rows"], + "additionalProperties": false +} diff --git a/schemas/give_basic_weapon.json b/schemas/give_basic_weapon.json index f26210f..5a65025 100644 --- a/schemas/give_basic_weapon.json +++ b/schemas/give_basic_weapon.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "http://localhost:8080/$schemas/give_basic_weapon.json", + "$id": "https://raw.githubusercontent.com/WildEgo/m2-json-schemas/refs/heads/main/give_basic_weapon.json", + "title": "Starting items configuration", "type": "object", "properties": { "$schema": { diff --git a/schemas/special_item_group.json b/schemas/special_item_group.json index 1afbcad..5e355ce 100644 --- a/schemas/special_item_group.json +++ b/schemas/special_item_group.json @@ -1,6 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "http://localhost:8080/$schemas/special_item_group.json", + "$id": "https://raw.githubusercontent.com/WildEgo/m2-json-schemas/refs/heads/main/special_item_group.json", + "title": "Special item group configuration", "type": "object", "properties": { "$schema": {