Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements + corrected line number for boss rooms #782

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 4 additions & 12 deletions src/ValidateDungeons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FileHelper } from "./FileHelper";
import * as PF from 'pathfinding'

export class DungeonValidator {
Expand All @@ -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 {
Expand All @@ -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
});
}

Expand All @@ -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!`);
Expand All @@ -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: " ", "#"`);
}
Expand Down Expand Up @@ -164,6 +157,5 @@ export interface ErrorRoom {
type: string,
roomNumber: number,
message: string,
lineNumber: number,
dungeon: Dungeon
}
28 changes: 28 additions & 0 deletions src/ValidateJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}