-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinput-parser.js
More file actions
157 lines (134 loc) · 4.15 KB
/
input-parser.js
File metadata and controls
157 lines (134 loc) · 4.15 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const ViewParser = require("./parsers/view-parser.js");
const TokenParser = require("./parsers/token.js");
const IconParser = require("./parsers/icon.js");
const OverlayParser = require("./parsers/overlay.js");
const BackgroundParser = require("./parsers/background.js");
const LineParser = require("./parsers/line-parser.js");
const Icon = require("./icon.js");
const EffectParser = require("./parsers/effect-parser.js");
const FogParser = require("./parsers/fog-parser.js");
const ConfigParser = require('./parsers/config.js');
module.exports = class InputParser {
constructor() {
this.lines = [];
this.tokens = [];
this.effects = [];
this.fog = [];
this.icons = [];
this.overlays = [];
this.tokenImages = {};
this.configParser = new ConfigParser();
this.backgroundParser = new BackgroundParser();
this.viewParser = new ViewParser();
this.tokenParser = new TokenParser();
this.iconParser = new IconParser();
this.overlayParser = new OverlayParser();
this.lineParser = new LineParser();
this.effectParser = new EffectParser();
this.fogParser = new FogParser();
}
async parse(options, pathname = "", query = {}) {
if (query.load) {
const config = await this.configParser.parse(query);
if (query.bg) {
config.background = query.bg;
}
await this.parseConfig(config, options);
} else {
if (query.bg) {
options.background.image = await this.backgroundParser.parse(query);
}
}
let parts = [];
// trim off leading /
if (pathname[0] === "/") parts = pathname.substr(1);
// trim of trailing /
if (pathname[pathname.length - 1] === "/")
pathname.substr(0, pathname.length - 1);
parts = decodeURIComponent(pathname).split("/");
for (let part of parts) {
part = part.trim();
if (part[0] === '/') part = part.substr(1);
if (part[part.length-1] === '/') part = part.substr(0, part.length - 1);
let parsed = this.viewParser.parse(part);
if (parsed) {
options.view = parsed;
continue;
}
parsed = await this.tokenParser.parse(part, this.tokenImages);
if (parsed) {
this.tokens.push(parsed);
continue;
}
parsed = this.overlayParser.parse(part);
if (parsed) {
this.overlays.push(parsed);
continue;
}
parsed = this.iconParser.parse(part);
if (parsed) {
this.icons.push({ x: parsed.x, y: parsed.y, item: new Icon(parsed) });
continue;
}
parsed = this.lineParser.parse(part);
if (parsed) {
this.lines = this.lines.concat(parsed);
continue;
}
parsed = this.effectParser.parse(part);
if (parsed) {
this.effects.push(parsed);
continue;
}
parsed = this.fogParser.parse(part);
if (parsed) {
this.fog.push(parsed);
continue;
}
// Extend by adding more parsers here
parsed = options.parseOptions(part);
}
}
async parseConfig(c, options) {
if (c.background) {
options.background.image = await this.backgroundParser.parse({ bg: c.background });
}
if (c.tokenImages) {
this.tokenImages = c.tokenImages;
}
if (c.view) {
const parsed = this.viewParser.parse(c.view);
if (parsed) options.view = parsed;
}
if (c.tokens) {
for (const token of c.tokens) {
const parsed = await this.tokenParser.parse(token, this.tokenImages);
if (parsed) this.tokens.push(parsed);
}
}
if (c.objects) {
for (const overlay of c.overlays) {
const parsed = this.overlayParser.parse(overlay);
if (parsed) this.overlays.push(parsed);
}
}
if (c.walls) {
for (const wall of c.walls) {
const parsed = this.lineParser.parse(wall);
if (parsed) this.lines = this.lines.concat(parsed);
}
}
if (c.overlays) {
for (const overlay of c.overlays) {
const parsed = this.effectParser.parse(overlay);
if (parsed) {
this.effects.push(parsed);
continue;
}
}
}
if (c.options) {
options.parseOptions(c.options);
}
}
};