forked from DexterHuang/CyberCodeOnline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructurMaskUtils.ts
More file actions
63 lines (58 loc) · 2.72 KB
/
StructurMaskUtils.ts
File metadata and controls
63 lines (58 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
export namespace StructurMaskUtils {
export function chunkString(str: string, length: number) {
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}
export function printRoomsToLineNumberInfo(structureMaskLineNumbers: StructureMaskLineNumbers) {
structureMaskLineNumbers.rooms.map((l, i) => ` rooms -> roomNumber: ${i} -> lineNumber: ${l}`).forEach(s => console.log(s));
structureMaskLineNumbers.bossRooms.map((l, i) => ` bossRooms -> roomNumber: ${i} -> lineNumber: ${l}`).forEach(s => console.log(s));
}
export function normalizeStructureMask(maskString: string): string {
const json = JSON.parse(maskString);
const jsonString = JSON.stringify(json, null, ' ');
const splitLines = jsonString.split(/\r?\n/);
let newMaskString = '';
const arrayContent: string[] = [];
for (const line of splitLines) {
if (/^(( *" ")|( *"#")),?$/.test(line)) {
arrayContent.push(line);
}
else {
if (arrayContent.length > 0) {
let arrayString = '';
const indent = arrayContent[0].match(/^ */)[0];
const partSize = arrayContent[0].trim().length + 1;
const singleLine = arrayContent.map(c => c.trim()).join(' ');
chunkString(singleLine, partSize * 15).forEach((s) => {
arrayString += `${indent}${s.trim()}\n`
})
newMaskString += arrayString;
arrayContent.length = 0;
}
newMaskString += `${line}\n`;
}
}
return newMaskString.slice(0, -1);
}
export function getStructureMaskLineNumbers(maskString: string): StructureMaskLineNumbers {
const splitLines = maskString.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 json: { rooms: { lineNumber: number }[], bossRooms: { lineNumber: number }[] } = JSON.parse(prepared);
const structureMaskLineNumbers: StructureMaskLineNumbers = {
rooms: [],
bossRooms: []
};
json.rooms.forEach(e => structureMaskLineNumbers.rooms.push(e.lineNumber));
json.bossRooms.forEach(e => structureMaskLineNumbers.bossRooms.push(e.lineNumber));
return structureMaskLineNumbers;
}
}
export interface StructureMaskLineNumbers {
rooms: number[]
bossRooms: number[]
}