-
Notifications
You must be signed in to change notification settings - Fork 123
/
mygamegenerator.js
226 lines (202 loc) · 7.07 KB
/
mygamegenerator.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
if (typeof process !== "undefined" && typeof require !== "undefined") {
fs = require("fs");
vm = require("vm");
vm.runInThisContext(fs.readFileSync("headless.js"), "headless.js");
args = process.argv;
args.shift();
args.shift();
gameDir = args[0] || "mygame";
beta = args[1];
try {
console.log(generateMygame(gameDir, beta, fs));
} catch (e) {
console.error(e.message);
process.exit(1);
}
}
function generateMygame(gameDir = "mygame", beta, fs) {
var output = "";
function parseSceneList(lines, lineNum) {
var nextIndent = null;
var scenes = [];
var purchases = {};
var line;
while (typeof (line = lines[++lineNum]) != "undefined") {
if (!line.trim()) continue;
var indent = /^(\s*)/.exec(line)[1].length;
if (nextIndent === null || nextIndent === undefined) {
// initialize nextIndent with whatever indentation the line turns out to be
// ...unless it's not indented at all
if (indent === 0) throw new Error("invalid scene_list indent, expected at least one row");
this.indent = nextIndent = indent;
}
if (indent === 0) break;
if (indent != this.indent) {
// all scenes are supposed to be at the same indentation level
throw new Error("invalid scene_list indent, expected " + this.indent + ", was " + indent);
}
line = line.trim();
var purchaseMatch = /^\$(\w*)\s+(.*)/.exec(line);
if (purchaseMatch) {
line = purchaseMatch[2];
var product = purchaseMatch[1].trim() || "adfree";
purchases[line] = product;
}
if (!scenes.length && "startup" != line) scenes.push("startup");
scenes.push(line);
}
return { scenes: scenes, purchases: purchases, lineNum: lineNum - 1 };
}
function parseAchievement(data, lines, lineNum) {
var nextIndent = null;
var parsed = /(\S+)\s+(\S+)\s+(\S+)\s+(.*)/.exec(data);
var achievementName = parsed[1] = parsed[1].toLowerCase();
var visibility = parsed[2];
var visible = (visibility != "hidden");
parsed[2] = visible;
var pointString = parsed[3];
parsed[3] = pointString * 1;
var title = parsed[4];
var line = lines[++lineNum];
var preEarnedDescription = line.trim();
parsed[6] = preEarnedDescription;
var postEarnedDescription = null;
while (typeof (line = lines[++lineNum]) != "undefined") {
if (line.trim()) break;
}
if (/^\s/.test(line)) {
postEarnedDescription = line.trim();
} else {
// No indent means the next line is not a post-earned description
lineNum--;
}
if (postEarnedDescription === null) postEarnedDescription = preEarnedDescription;
parsed[5] = postEarnedDescription;
parsed.shift();
achievements.push(parsed);
return lineNum;
}
function parseCheckPurchase(data) {
var products = data.split(" ");
for (var i = 0; i < products.length; i++) {
var product = products[i];
if (!productMap[product]) {
purchases["fake:" + product] = product;
}
}
}
function parseCreateValue(value) {
if (/^true$/i.test(value)) value = "true";
if (/^false$/i.test(value)) value = "false";
if (/^".*"$/.test(value)) value = value.slice(1, -1).replace(/\\(.)/g, "$1");
return value;
}
function parseCreateArray(line) {
var result = /^(\w+)\s+(.*)/.exec(line);
var variable = result[1].toLowerCase();
var values = result[2].split(/\s+/);
var length = Number(values.shift());
if (values.length === 1) {
var value = parseCreateValue(values[0]);
for (var i = 0; i < length; i++) {
stats[variable + "_" + (i + 1)] = value;
}
} else {
for (var i = 0; i < length; i++) {
var value = parseCreateValue(values[i]);
stats[variable + "_" + (i + 1)] = value;
}
}
}
var lines = slurpFileLines("web/" + gameDir + "/scenes/startup.txt");
var stats = {}, purchases = {}, productMap = {};
var scenes = ["startup"];
var create = /^\*create +(\w+) +(.*)/;
var result, variable, value;
var achievements = [];
var ignoredInitialCommands = { "comment": 1, "author": 1, "ifid": 1 };
for (var i = 0; i < lines.length; i++) {
var line = ("" + lines[i]).trim();
if (!line) { continue; }
var result = /^\s*\*(\w+)(.*)/.exec(line);
if (!result) break;
var command = result[1].toLowerCase();
var data = result[2].trim();
if (ignoredInitialCommands[command]) { continue; }
else if (command == "create") {
var result = /^(\w*)(.*)/.exec(data);
variable = result[1];
value = parseCreateValue(result[2].trim());
stats[variable.toLowerCase()] = value;
} else if (command == "create_array") {
parseCreateArray(data);
} else if (command == "scene_list") {
result = parseSceneList(lines, i);
scenes = result.scenes;
purchases = result.purchases;
i = result.lineNum;
} else if (command == "title") {
stats.choice_title = data;
} else if (command == "achievement") {
i = parseAchievement(data, lines, i);
} else if (command == "product") {
// ignore products for now; we compute them from check_purchases
// *product this only has an effect on quicktest
} else if (command === "bug") {
if (beta === '"beta"' && data === "choice_beta") {
continue;
}
console.error("startup.txt contains *bug");
process.exit(1);
} else {
break;
}
}
for (var scene in purchases) {
productMap[purchases[scene]] = scene;
}
if (fs) {
var sceneDir = fs.readdirSync("web/" + gameDir + "/scenes");
for (i = 0; i < sceneDir.length; i++) {
var lines = slurpFileLines("web/" + gameDir + "/scenes/" + sceneDir[i]);
for (var j = 0; j < lines.length; j++) {
var line = ("" + lines[j]).trim();
if (!line) { continue; }
var result = /^\s*\*(\w+)(.*)/.exec(line);
if (!result) continue;
var command = result[1].toLowerCase();
var data = result[2].trim();
if (command == "check_purchase") {
parseCheckPurchase(data);
} else if (command == "delay_ending") {
purchases["fake:skiponce"] = "skiponce";
}
}
}
}
output += ("\ufeffnav = new SceneNavigator(");
function logJson(x) {
var json = JSON.stringify(x, null, " ");
json = json.replace(/[\u007f-\uffff]/g, function (c) {
return '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4);
});
output += (json);
}
logJson(scenes);
output += (");\nstats = ");
logJson(stats);
output += (";\npurchases = ");
logJson(purchases);
output += (";\nachievements = ");
logJson(achievements);
output += (";\n");
if (beta === '"beta"' || beta === '"beta-iap"') {
output += ("beta = " + beta + ";\n");
const betaPassword = slurpFile("beta-password.txt").trim();
output += (`betaPassword = "${btoa(`beta:${betaPassword}`)}";`)
}
output += ("nav.setStartingStatsClone(stats);");
output += ("if (achievements.length) {\n nav.loadAchievements(achievements);\n}");
output += ("\nif (nav.loadProducts) nav.loadProducts([], purchases);\n");
return output;
}