diff --git a/.gitignore b/.gitignore index 9c5b57e15..f0b58870d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,10 @@ dump /cypress/screenshots .DS_Store -.excalidraw \ No newline at end of file +.excalidraw + +/scripts/output/* +!/scripts/output/.gitkeep + +/scripts/dicts/*.json +/scripts/dicts/langs/*.json \ No newline at end of file diff --git a/package.json b/package.json index 7153cd138..67535369b 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "migrate:cypress": "cross-env DB_PATH=db-cypress.sqlite3 ley", "migrate:reset": "node scripts/delete-db-files.mjs && npm run migrate && npm run seed", "add-badge": "node --experimental-specifier-resolution=node --loader ts-node/esm -r tsconfig-paths/register scripts/add-badge.ts", + "create-weapon-json": "node --experimental-specifier-resolution=node --loader ts-node/esm -r tsconfig-paths/register scripts/create-weapon-json.ts", "lint:ts": "eslint . --ext .ts,.tsx", "lint:styles": "stylelint \"app/styles/**/*.css\"", "prettier:check": "prettier --check .", diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 000000000..ffee64a3a --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,8 @@ +This folder has scripts that can be used run time for admin only actions or to prepare some data. + +#### `create-weapon-json.ts` + +Before using this script you need to copy some files from another repository. + +1. Move .json files matching wanted languages from https://github.com/Leanny/leanny.github.io/tree/master/splat3/data/language to /scripts/dicts/langs +2. Move WeaponInfoMain.json from https://github.com/Leanny/leanny.github.io/blob/master/splat3/data/mush/099/WeaponInfoMain.json to /scripts/dicts diff --git a/scripts/create-weapon-json.ts b/scripts/create-weapon-json.ts new file mode 100644 index 000000000..7deb25d53 --- /dev/null +++ b/scripts/create-weapon-json.ts @@ -0,0 +1,63 @@ +import weapons from "./dicts/WeaponInfoMain.json"; +import fs from "node:fs"; +import path from "node:path"; +import invariant from "tiny-invariant"; + +const INTERNAL_NAMES_TO_IGNORE: readonly string[] = ["Free"] as const; +const LANG_DICTS_PATH = path.join(__dirname, "dicts", "langs"); +const OUTPUT_PATH = path.join(__dirname, "output", "weapons.json"); + +async function loadLangDicts() { + const result: Array< + [langCode: string, translations: Record] + > = []; + + const files = await fs.promises.readdir(LANG_DICTS_PATH); + for (const file of files) { + const translations = JSON.parse( + fs.readFileSync(path.join(LANG_DICTS_PATH, file), "utf8") + ); + + result.push([file.replace(".json", ""), translations]); + } + + return result; +} + +async function main() { + const result: Array<{ + id: number; + internalName: string; + translations: Array<{ language: string; name: string }>; + }> = []; + const langDicts = await loadLangDicts(); + + for (const weapon of weapons) { + if ( + weapon.Type === "Coop" || + INTERNAL_NAMES_TO_IGNORE.includes(weapon.__RowId) + ) { + continue; + } + + result.push({ + id: weapon.Id, + internalName: weapon.__RowId, + translations: langDicts.map(([langCode, translations]) => { + const name = translations[weapon.__RowId]; + invariant(name); + + return { + language: langCode, + name, + }; + }), + }); + } + + result.sort((a, b) => a.id - b.id); + + fs.writeFileSync(OUTPUT_PATH, JSON.stringify(result, null, 2)); +} + +void main(); diff --git a/scripts/dicts/.gitkeep b/scripts/dicts/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/scripts/dicts/langs/.gitkeep b/scripts/dicts/langs/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/scripts/output/.gitkeep b/scripts/output/.gitkeep new file mode 100644 index 000000000..e69de29bb