forked from dfabulich/choicescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomtest.html
212 lines (199 loc) · 9.13 KB
/
randomtest.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
<!DOCTYPE html>
<html>
<head>
<title>Randomtest</title>
</head>
<body>
<p>Iterations: <input type="number" id="iterations" value="10000"></p>
<p>Seed: <input type="number" id="seed" value="0"></p>
<p><input type="checkbox" id="avoidUsedOptions" checked> Avoid used options (less random; finds bugs faster)</p>
<p><input type="checkbox" id="showChoices" checked> Show choices selected during game</p>
<p><input type="checkbox" id="showText"> Show full text during game</p>
<p><input type="checkbox" id="highlightGenderPronouns"> Highlight gender pronouns</p>
<p><input type="checkbox" id="showCoverage"> Show line coverage statistics after test</p>
<p><input type="checkbox" id="autoScroll"> Automatically scroll to the bottom</p>
<p style="display:none"><input type="checkbox" id="recordBalance"> Record data for auto() balancing</p>
<p id="uploadPrompt" style="display:none"><input type="file" id="uploadDirectory" webkitdirectory> "Upload" choicescript folder (including randomtest.html)</p>
<button id="start">Start Randomtest</button>
<script>
// CSIDE Window
var scope = window.opener ? window.opener.parent : parent.window;
var thisProject = scope.cside.getActiveProject();
if ("file:" === window.location.protocol) {
document.getElementById("uploadPrompt").style.display = "";
}
var uncheckWithBalance = ["avoidUsedOptions", "highlightGenderPronouns", "showChoices", "showCoverage", "showText"];
document.getElementById("recordBalance").addEventListener("click", function() {
if (document.getElementById("recordBalance").checked) {
for (var i = 0; i < uncheckWithBalance.length; i++) {
document.getElementById(uncheckWithBalance[i]).checked = false;
}
}
});
var uncheckBalance = function() {
document.getElementById("recordBalance").checked = false;
}
for (var i = 0; i < uncheckWithBalance.length; i++) {
document.getElementById(uncheckWithBalance[i]).addEventListener("click", uncheckBalance);
}
var worker;
var messages = [];
var highlightGenderPronouns;
setInterval(function() {
if (messages.length) {
var frag = document.createDocumentFragment();
var length = messages.length;
for (var i = 0; i < length; i++) {
printBody(messages[i], frag);
frag.appendChild(document.createElement("br"));
};
document.body.appendChild(frag);
messages = [];
if (queryArgs.autoScroll) {
var br = document.createElement("br");
document.body.appendChild(br);
br.scrollIntoView();
}
}
}, 100);
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>');
if (highlightGenderPronouns) {
msg = msg.replace(/\b(he|him|his|she|her|hers)\b/gi, function(match, pronoun, index, all) {
var variable = all.charCodeAt(index-1) == 8203;
var color = variable ? "blue" : "red";
return "<span style='background-color: yellow; color: "+color+"'>"+pronoun+"</span>";
});
}
var frag = document.createDocumentFragment();
temp = document.createElement('div');
temp.innerHTML = msg;
while (temp.firstChild) {
frag.appendChild(temp.firstChild);
}
parent.appendChild(frag);
}
var queryArgList = (""+window.location.search).substr(1).split(/&/);
var queryArgs = {};
for (var i = queryArgList.length - 1; i >= 0; i--) {
var parts = queryArgList[i].split(/=/);
if ("false" === parts[1]) parts[1] = false;
queryArgs[parts[0]] = parts[1];
};
if (queryArgs.iterations) document.getElementById("iterations").value = queryArgs.iterations;
if (queryArgs.seed) document.getElementById("seed").value = queryArgs.seed;
if (typeof queryArgs.avoidUsedOptions != "undefined") document.getElementById("avoidUsedOptions").checked = !!queryArgs.avoidUsedOptions;
if (queryArgs.showText) document.getElementById("showText").checked = queryArgs.showText;
if (queryArgs.highlightGenderPronouns) document.getElementById("highlightGenderPronouns").checked = queryArgs.highlightGenderPronouns;
if (queryArgs.showCoverage) document.getElementById("showCoverage").checked = queryArgs.showCoverage;
if (typeof queryArgs.showChoices != "undefined") document.getElementById("showChoices").checked = !!queryArgs.showChoices;
if (queryArgs.autoScroll) document.getElementById("autoScroll").checked = queryArgs.autoScroll;
if (queryArgs.recordBalance) document.getElementById("recordBalance").checked = queryArgs.recordBalance;
var clicked = false;
document.getElementById("start").onclick = function() {
if (document.getElementById("autoScroll").checked) queryArgs.autoScroll = true;
if (clicked) return;
clicked = true;
iterations = document.getElementById("iterations").value;
randomSeed = document.getElementById("seed").value*1;
var avoidUsedOptions = document.getElementById("avoidUsedOptions").checked;
var showText = document.getElementById("showText").checked;
var showCoverage = document.getElementById("showCoverage").checked;
var showChoices = document.getElementById("showChoices").checked;
highlightGenderPronouns = document.getElementById("highlightGenderPronouns").checked;
var recordBalance = document.getElementById("recordBalance").checked;
document.body.innerHTML = "";
var isFile = "file:" === window.location.protocol;
if (isFile) {
var numFiles = uploadDirectory.files.length;
var startupCandidates = [];
var randomtestJsCandidates = [];
var relativePathMap = {};
for (var i = 0; i < numFiles; i++) {
var file = uploadDirectory.files[i];
relativePathMap[file.webkitRelativePath] = file;
if (file.name === "startup.txt") {
startupCandidates.push(file);
}
if (file.name === "randomtest.js") {
randomtestJsCandidates.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 randomtest 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("randomtest.js", randomtestJsCandidates);
evaluateCandidates("startup.txt", startupCandidates);
var startup = startupCandidates[0];
var randomtestJs = randomtestJsCandidates[0];
var rootDirTest = new RegExp("^" + startup.webkitRelativePath.replace(/\/startup.txt$/, "/[^/]+$"));
var sceneFiles = [];
for (var i = 0; i < numFiles; i++) {
var file = uploadDirectory.files[i];
if (rootDirTest.test(file.webkitRelativePath)) {
sceneFiles.push(file);
}
}
var sceneContent = {};
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];
sceneContent[file.name] = results[i];
}
var randomtestRoot = randomtestJs.webkitRelativePath.replace(/\/randomtest.js$/, "/");
var requiredFiles = ["web/scene.js", "web/navigator.js", "web/util.js", "web/mygame/mygame.js", "seedrandom.js", "randomtest.js"];
Promise.all(requiredFiles.map(function (file) {
return new Response(relativePathMap[randomtestRoot+file]).text()
})).then(function (results) {
worker = new Worker(URL.createObjectURL(new Blob([results.join('\n;\n')], {type: 'text/javascript'})));
worker.onmessage = function(x) {
var msg = x.data.msg;
messages.push(msg);
console.log(msg);
}
worker.postMessage({iterations:iterations, randomSeed:randomSeed, showText:showText, showCoverage:showCoverage, highlightGenderPronouns:highlightGenderPronouns, showChoices:showChoices, avoidUsedOptions:avoidUsedOptions, recordBalance:recordBalance, sceneContent:sceneContent, projectPath: thisProject.getPath() });
});
});
} else {
document.body.innerHTML = "";
var worker = new Worker("randomtest.js");
worker.onmessage = function(x) {
var msg = x.data.msg;
messages.push(msg);
console.log(msg);
}
worker.postMessage({iterations:iterations, randomSeed:randomSeed, showText:showText, showCoverage:showCoverage, highlightGenderPronouns:highlightGenderPronouns, showChoices:showChoices, avoidUsedOptions:avoidUsedOptions, recordBalance:recordBalance, projectPath: thisProject.getPath() });
}
}
</script>
</body>
</html>