From 741bb441cd971c38899286e25550a648f960cfcd Mon Sep 17 00:00:00 2001 From: andycandy-de <8217449+andycandy-de@users.noreply.github.com> Date: Mon, 17 May 2021 12:07:52 +0200 Subject: [PATCH] Improvements + corrected line number for boss rooms --- package.json | 3 +-- src/ValidateDungeons.ts | 16 ++++------------ src/ValidateJson.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index b276f6def..a281fe857 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,7 @@ "main": "index.js", "scripts": { "watch": "nodemon --watch src src/ValidateJson.ts", - "start": "ts-node src/ValidateJson.ts", - "validate-dungeons": "ts-node src/ValidateDungeons.ts" + "start": "ts-node src/ValidateJson.ts" }, "repository": { "type": "git", diff --git a/src/ValidateDungeons.ts b/src/ValidateDungeons.ts index 1f98b1c98..1bc98f010 100644 --- a/src/ValidateDungeons.ts +++ b/src/ValidateDungeons.ts @@ -1,4 +1,3 @@ -import { FileHelper } from "./FileHelper"; import * as PF from 'pathfinding' export class DungeonValidator { @@ -18,12 +17,11 @@ export class DungeonValidator { for (const dungeon of dungeons) { try { - const key = this.validateGenKey(dungeon); + const key = this.validateAndGenerateKey(dungeon); if (dungeonMap.has(key)) { errors.push({ type: type, roomNumber: index, - message: `Duplicate found! room number ${index} is a duplicate of room number ${dungeonMap.get(key)}!`, - lineNumber: this.getLineNumberByIndex(index), dungeon + message: `Duplicate found! room number ${index} is a duplicate of room number ${dungeonMap.get(key)}!`, dungeon: dungeon }); } else { @@ -32,8 +30,7 @@ export class DungeonValidator { } catch (e) { errors.push({ - type: type, roomNumber: index, message: e.message, - lineNumber: this.getLineNumberByIndex(index), dungeon + type: type, roomNumber: index, message: e.message, dungeon: dungeon }); } @@ -42,10 +39,7 @@ export class DungeonValidator { return errors; } - private getLineNumberByIndex(index: number) { - return (index * 19) + 4 - } - private validateGenKey(dungeon: Dungeon): string { + private validateAndGenerateKey(dungeon: Dungeon): string { if (!dungeon || !dungeon.tiles) { throw new Error(`Dungeon is not valid! Cannot find tiles!`); @@ -60,7 +54,6 @@ export class DungeonValidator { layout.push([]); } for (let i = 0; i < dungeon.tiles.length; ++i) { - const column = []; if (dungeon.tiles[i] !== ' ' && dungeon.tiles[i] !== '#') { throw new Error(`Tiles has a unkown character! possible character: " ", "#"`); } @@ -164,6 +157,5 @@ export interface ErrorRoom { type: string, roomNumber: number, message: string, - lineNumber: number, dungeon: Dungeon } \ No newline at end of file diff --git a/src/ValidateJson.ts b/src/ValidateJson.ts index a8ad9bbfa..6f3dec86e 100644 --- a/src/ValidateJson.ts +++ b/src/ValidateJson.ts @@ -32,5 +32,33 @@ if (errors.length == 0) { console.log('Dungeons validation complete.') } else { + addLineNumbersToErrors(errors, maskStr); + console.error('Found invalid dungeon rooms'); + errors.forEach((e) => { + console.error(e); + }) throw new Error(`Dungeons validation not successful! errors: ${JSON.stringify(errors)}`); +} + +function addLineNumbersToErrors(errorRooms: ErrorRoom[], maskStr: string) { + const splitLines = maskStr.split(/\r?\n/); + const preparedSplitted = splitLines.map((s, i) => { + const searchRegExp = /"tiles"\s*:/g; + const replaceWith = `"lineNumber": ${i + 1},"tiles":`; + const replaced = s.replace(searchRegExp, replaceWith); + return replaced; + }); + const prepared = preparedSplitted.join('\n'); + const dungeonsWithLineNumber: { rooms: DungeonWithLineNumber[], bossRooms: DungeonWithLineNumber[] } = JSON.parse(prepared); + + errorRooms.forEach((e) => { + e['lineNumber'] = getLineNumber(e, dungeonsWithLineNumber); + }); +} +function getLineNumber(errorRoom: ErrorRoom, dungeonsWithLineNumber: { rooms: DungeonWithLineNumber[], bossRooms: DungeonWithLineNumber[] }): number { + const dungeonWithLineNumber: DungeonWithLineNumber[] = dungeonsWithLineNumber[errorRoom.type]; + return dungeonWithLineNumber[errorRoom.roomNumber].lineNumber; +} +interface DungeonWithLineNumber extends Dungeon { + lineNumber: number } \ No newline at end of file