-
Notifications
You must be signed in to change notification settings - Fork 123
/
quicktest.html
471 lines (430 loc) · 15.3 KB
/
quicktest.html
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html>
<html>
<head>
<title>Quicktest</title>
<script src=web/scene.js></script>
<script src=web/navigator.js></script>
<script src=web/util.js></script>
<script src=editor/embeddable-autotester.js></script>
<script>
nav = new SceneNavigator(["startup"]);
nav.setStartingStatsClone({});
stats = {};
function doneLoading() {
var loadingDiv = document.getElementById("loading");
if (loadingDiv) loadingDiv.parentNode.removeChild(loadingDiv);
}
function changeTitle() {}
knownScenes = [];
knownImages = {};
sceneList = [];
warnings = [];
function addFile(name) {
for (var i = 0; i < knownScenes.length; i++) {
if (knownScenes[i] == name) return;
}
knownScenes.push(name);
}
addFile("startup.txt");
addFile("choicescript_stats.txt");
addFile("choicescript_screenshots.txt");
addFile("choicescript_upgrade.txt");
Scene.prototype.verifySceneFile = function webVerifySceneFile(name) {
addFile(name+".txt");
}
Scene.prototype.verifyImage = function webVerifyImage(fileName) {
if (knownImages[fileName]) return;
if ("file:" === window.location.protocol) {
for (var otherImage in knownImages) {
if (fileName.toLowerCase() === otherImage.toLowerCase()) {
throw new Error(this.lineMsg() + "Couldn't find "+fileName+" in the uploaded folder, but we did find "+otherImage+". File names must match exactly, including capitalization.");
}
}
throw new Error(this.lineMsg() + "File does not exist: " + fileName);
}
var xhr = new XMLHttpRequest();
var url = "web/mygame/" + fileName;
xhr.open("GET", url, false);
try {
xhr.send();
} catch (e) {
throw new Error(this.lineMsg() + "File does not exist: " + fileName);
}
if (xhr.status && xhr.status != 200) {
throw new Error(this.lineMsg() + "File does not exist: " + fileName);
}
knownImages[fileName] = 1;
}
Scene.prototype.feedback = function webFeedback() {};
Scene.prototype.testFinish = function commandLineTestFinish(buttonName) {
var nextSceneName;
for (var i = 0; i < sceneList.length; i++) {
if (sceneList[i] === this.name) {
nextSceneName = sceneList[i+1];
break;
}
}
if (!nextSceneName && !/^choicescript_/.test(this.name)) {
warnings.push(this.lineMsg() + "WARNING there is no next scene; *finish will end game. Use *ending instead.");
}
}
Scene.prototype.warning = function htmlWarning(message) {
warnings.push(this.lineMsg() + "WARNING " + message);
}
printFooter = function() {};
printShareLinks = function() {};
printLink = function() {};
printButton = function() {};
printImage = function() {};
showPassword = function() {};
achieve = function() {};
loginForm = function() {};
isRegistered = function() {return false;};
isRegisterAllowed = function() {return false;};
isRestorePurchasesSupported = function() {return false;};
isFullScreenAdvertisingSupported = function() {return false;};
areSaveSlotsSupported = function() {return false;};
printDiscount = function() {};
isFullScreenAdvertisingSupported = function() {return false;};
isAdvertisingSupported = function() {return false;};
isPrerelease = function() {return false;};
showFullScreenAdvertisementButton = function(message, callback) { callback(); }
function safeCall(obj, fn) {
if (obj) return fn.call(obj);
return fn.call();
}
function initStore() { return false; }
clearScreen = function clearScreen(code) {
code.call();
};
var oldLog = console.log;
saveCookie = function(callback) { if (callback) callback.call(); };
function printBody(msg, parent) {
if (msg === null || msg === undefined || msg === "") return;
if (!parent) parent = document.body;
if (msg == " ") {
// IE7 doesn't like innerHTML that's nothing but " "
parent.appendChild(document.createTextNode(" "));
return;
}
msg = (msg+"").replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/\[url\=(.*?)\]/g, '<a href="$1">')
.replace(/\[\/url\]/g, '</a>')
.replace(/\[n\/\]/g, '<br>')
.replace(/\[c\/\]/g, '')
.replace(/\[b\]/g, '<b>')
.replace(/\[\/b\]/g, '</b>')
.replace(/\[i\]/g, '<i>')
.replace(/\[\/i\]/g, '</i>');
var frag = document.createDocumentFragment();
temp = document.createElement('div');
temp.innerHTML = msg;
while (temp.firstChild) {
frag.appendChild(temp.firstChild);
}
parent.appendChild(frag);
}
function println() {}
console.log = function() {
var msg = arguments[0];
printBody(msg);
var br = document.createElement("br");
document.body.appendChild(br);
br.scrollIntoView();
oldLog.apply(this, arguments);
}
uncoveredScenes = [];
var success = true;
var gameTitle, authorIncluded;
var gotoSceneLabels = {};
function loadScenes(game, sceneNum) {
if (!game) game = {};
var fileName = knownScenes[sceneNum];
var sceneName = fileName.replace(/.txt$/, "");
var skip = false;
var loadFailed = false;
var sceneText;
if ("file:" === window.location.protocol) {
if (!window.uploadedFiles) {
document.body.innerHTML = "<p>Please \"upload\" your choicescript folder (including quicktest.html).</p>";
var input = document.createElement("input");
input.type = 'file';
input.webkitdirectory = true;
input.multiple = true;
input.addEventListener('change', function searchForStartup(e) {
var numFiles = input.files.length;
var startupCandidates = [];
var quicktestHtmlCandidates = [];
for (var i = 0; i < numFiles; i++) {
var file = input.files[i];
if (file.name === "startup.txt") {
startupCandidates.push(file);
}
if (file.name === "quicktest.html") {
quicktestHtmlCandidates.push(file);
}
}
function evaluateCandidates(fileName, candidates) {
if (!candidates.length) {
alert("We couldn't find "+fileName+" in the folder you chose. Please try again. (Note that quicktest requires access to the entire choicescript directory, not just the mygame folder");
clicked = false;
throw new Error();
}
if (candidates.length > 1) {
alert("There were multiple files called "+fileName+" in the folder you chose. Please try again.\n" +
candidates.map(function(file) {return "\u2022 " + file.webkitRelativePath}).join("\n"));
clicked = false;
throw new Error();
}
}
evaluateCandidates("quicktest.html", quicktestHtmlCandidates);
evaluateCandidates("startup.txt", startupCandidates);
var startup = startupCandidates[0];
var rootDirTest = new RegExp("^" + startup.webkitRelativePath.replace(/\/startup.txt$/, "/[^/]+$"));
var imageDir = startup.webkitRelativePath.replace(/\/[^/]+\/startup.txt$/, "/");
var imageDirTest = new RegExp("^" + imageDir);
var sceneFiles = [];
for (var i = 0; i < numFiles; i++) {
var file = input.files[i];
if (rootDirTest.test(file.webkitRelativePath)) {
sceneFiles.push(file);
} else if (imageDirTest.test(file.webkitRelativePath)) {
knownImages[file.name] = 1;
knownImages[file.webkitRelativePath.replace(imageDir, "")] = 1;
}
}
var _uploadedFiles = {};
document.body.innerHTML = "";
Promise.all([].map.call(sceneFiles, function(file) { return new Response(file).text();})).then(function(results) {
for (var i = 0; i < sceneFiles.length; i++) {
var file = sceneFiles[i];
_uploadedFiles[file.name] = results[i];
}
window.uploadedFiles = _uploadedFiles;
loadScenes(game, sceneNum);
});
});
document.body.appendChild(input);
return;
} else {
if (uploadedFiles[fileName]) {
sceneText = uploadedFiles[fileName];
} else {
loadFailed = true;
for (var otherFileName in uploadedFiles) {
if (fileName.toLowerCase() === otherFileName.toLowerCase()) {
console.log("Couldn't find "+fileName+" in the uploaded folder, but we did find "+otherFileName+". Scene file names must match exactly, including capitalization.");
success = false;
break;
}
}
}
}
} else {
var url = "web/mygame/scenes/" + fileName;
xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
try {
xhr.send();
} catch (x) {
if (/^choicescript_(stats|upgrade|screenshots)$/.test(sceneName)) {
skip = true;
} else {
if (window.location.protocol == "file:" && /Chrome/.test(navigator.userAgent)) {
doneLoading();
console.log("We're sorry, Google Chrome has blocked ChoiceScript from functioning. (\"file:\" URLs cannot "+
"load files in Chrome.) ChoiceScript works just fine in Chrome, but only on a published website like "+
"choiceofgames.com. For the time being, please try another browser like Mozilla Firefox.");
success = false;
return;
}
loadFailed = true;
}
}
if (xhr.status && xhr.status != 200) {
loadFailed = true;
}
if (!loadFailed) {
sceneText = xhr.responseText;
}
}
if (loadFailed) {
if (/^choicescript_(stats|upgrade|screenshots)$/.test(sceneName)) {
skip = true;
} else {
doneLoading();
console.log("QUICKTEST FAILED");
console.log("ERROR: couldn't open " + fileName);
success = false;
return;
}
}
if (!skip) {
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};
}
try {
game[fileName] = sceneText;
var sceneLines = sceneText.split("\n");
for (var j = 0; j < sceneLines.length; j++) {
var line = sceneLines[j];
var words;
var result = /^\s*\*(\w+)(.*)/.exec(line);
if (!result) continue;
var command = result[1].toLowerCase();
var data = trim(result[2]);
if (command == "goto_scene" || command == "gosub_scene") {
if (data === null) data = "";
if (/[\[\{]/.test(data)) {
// print warning?
} else {
var match = /(\S+)\s*(\S*)\s*(.*)/.exec(data);
if (match) {
if (match[3] !== "") {
// parameters; print warning?
} else {
addFile(match[1]+".txt");
if (match[2] !== "") {
if (!gotoSceneLabels[match[1]]) gotoSceneLabels[match[1]] = [];
gotoSceneLabels[match[1]].push({origin:sceneName, originLine:j, label:match[2]});
}
}
}
}
} else if (command == "save_game") {
if (data !== null) addFile(data+".txt");
} else if (command == "scene_list" && sceneNum === 0) {
var parsedSceneList = parseSceneList(sceneLines, j);
j = parsedSceneList.lineNum;
for (var k = 0; k < parsedSceneList.scenes.length; k++) {
addFile(parsedSceneList.scenes[k]+".txt");
sceneList.push(parsedSceneList.scenes[k]);
}
}
}
} catch (e) {
console.log("Error parsing "+sceneName+" line "+(j+1)+ "; we'll flag the error in detail later\n");
console.log(e);
}
}
if (++sceneNum < knownScenes.length) return setTimeout(function(){loadScenes(game, sceneNum)}, 0);
testScene(game, 0);
}
function testScene(game, sceneNum, progressCallback) {
var fileName = knownScenes[sceneNum];
var sceneName = fileName.replace(/.txt$/, "");
var sceneText = game[fileName];
var skip = false;
if (!sceneText) {
if (/^choicescript_(stats|upgrade|screenshots)\.txt$/.test(fileName)) {
skip = true;
} else {
doneLoading();
var errorMessage = "ERROR: couldn't open " + fileName;
if (progressCallback) progressCallback(sceneNum, knownScenes.length, errorMessage);
console.log("QUICKTEST FAILED");
console.log(errorMessage);
success = false;
return;
}
}
if (!skip) {
console.log(sceneName);
try {
var uncovered = autotester(sceneText, nav, sceneName, gotoSceneLabels[sceneName])[1];
if (uncovered) {
uncoveredScenes.push({name:sceneName, lines:uncovered});
}
if (sceneNum === 0) {
var match = /^\*title (.*)/m.exec(sceneText);
if (match) {
gameTitle = match[1];
}
authorIncluded = /^\*author /m.test(sceneText);
}
} catch (x) {
if (progressCallback) progressCallback(sceneNum, knownScenes.length, x);
console.log("QUICKTEST FAILED");
console.log(x);
console.error(x.stack);
success = false;
return;
}
}
++sceneNum;
if (progressCallback) progressCallback(sceneNum, knownScenes.length, !"error");
if (sceneNum < knownScenes.length) {
return setTimeout(function(){testScene(game, sceneNum, progressCallback)}, 0);
}
if (success) {
var allLinesTested = true;
for (var i = 0; i < uncoveredScenes.length; i++) {
allLinesTested = false;
var uncoveredScene = uncoveredScenes[i];
for (var j = 0; j < uncoveredScene.lines.length; j++) {
console.log("UNTESTED " + uncoveredScene.name + " " + uncoveredScene.lines[j]);
};
}
(function() {
if (nav.achievementList && nav.achievementList.length) {
for (var i = 0; i < nav.achievementList.length; i++) {
var name = nav.achievementList[i];
if (!nav.achieved[name]) {
console.log("UNUSED achievement: " + name);
}
}
}
})();
if (!allLinesTested) console.log("SOME LINES UNTESTED");
if (typeof gameTitle === "undefined") {
console.log("MISSING *TITLE COMMAND");
} else if (gameTitle.length > 30) {
console.log("TITLE TOO LONG (" + gameTitle.length + " out of 30 characters): " + gameTitle);
}
if (!authorIncluded) console.log("MISSING *AUTHOR COMMAND");
for (var i = 0; i < warnings.length; i++) {
console.log(warnings[i]);
}
console.log("QUICKTEST PASSED");
}
}
</script>
</head>
<body>
<div id="loading">Loading...</div>
<script>
if (!window.location.search) loadScenes(null, 0);
</script>
</body>
</html>