forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprigfuzzy.js
266 lines (226 loc) · 6.55 KB
/
sprigfuzzy.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
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
import { baseEngine } from "../src/lib/engine/1-base/index.ts";
import { iterateReader } from "https://deno.land/[email protected]/streams/conversion.ts";
import * as pathUtils from "https://deno.land/std/path/mod.ts";
async function spadeRun(path) {
const H = Deno.env.get("HOME");
const p = Deno.run({
cmd: [H + "/spade/pc_build/spade", H + "/sprig/games/" + path],
stdout: "piped",
stdin: "piped"
});
/* feels like this is necessary, to get 'er warmed up */
await new Promise(res => setTimeout(res, 100));
async function *mapReader() {
const decoder = new TextDecoder();
for await (const out of iterateReader(p.stdout))
yield decoder.decode(out).trim();
}
const encoder = new TextEncoder();
return {
out: mapReader(),
simKey: key => p.stdin.write(encoder.encode(key + '\n')),
cleanup: () => p.close(),
};
}
// for (const slug of gameSlugs)
// await testScript(slug);
let brokenGames = [];
const SKIP = ["mandelbrot.js"];
const ONLY = [];
async function main() {
brokenGames = [];
const tasks = [];
for await (const dirEntry of Deno.readDir('./games')) {
const name = dirEntry.name;
if (ONLY.length > 0) {
if (ONLY.some(x => x === name)) {
console.log("running", name);
await testScriptJS(name);
}
continue;
}
const isJS = name.slice(-3) === ".js";
if (!isJS || SKIP.some(x => x === name)) continue;
console.log("running", name);
tasks.push((async () => {
const i = setInterval(() => console.log(name + ' is still running'), 10000);
await testScriptJS(name);
clearInterval(i);
})());
}
await Promise.all(tasks);
console.log("broken games:", brokenGames);
for (const { error, name } of brokenGames)
console.log(` --- ${name} ---\n` + error);
console.log("number of broken games:", brokenGames.length);
return brokenGames;
}
main();
async function testScript(name) {
const script = await Deno.readTextFile(`./games/${name}`);
const { api, cleanup, simulateKey } = simEngine();
/* generate simulated inputs */
const choose = arr => arr[Math.floor(Math.random() * arr.length)];
const shakespeareMonKeys = [...Array(1000)].map(_ => choose("wasdjilk".split('')));
const spade = await spadeRun(name);
try {
const compareMaps = async () => {
const spadeMap = (await spade.out.next()).value;
const sprigMap = gridToString(api);
if (spadeMap != sprigMap) {
const text = `maps different!\nsprig map:\n${sprigMap}\nspade map:\n${spadeMap}`;
throw new Error(text);
}
}
console.log(`running ${name}!`);
const fn = new Function(...Object.keys(api), script);
fn(...Object.values(api)); /* init */
for (const key of shakespeareMonKeys) {
await compareMaps();
simulateKey(key);
await spade.simKey(key);
// if (log) console.log(`<<< pressing ${key} >>>`);
// if (log) console.log(gridToString(api));
}
} catch(e) {
console.log(`ERROR WHILE RUNNING "${name}"`);
brokenGames.push({
name,
error: e
})
}
finally {
cleanup();
spade.cleanup();
}
}
async function testScriptJS(name) {
const script = await Deno.readTextFile(`./games/${name}`);
const { api, cleanup, simulateKey } = simEngine();
/* generate simulated inputs */
const choose = arr => arr[Math.floor(Math.random() * arr.length)];
const shakespeareMonKeys = [...Array(1000)].map(_ => choose("wasdjilk".split('')));
try {
console.log(`running ${name}!`);
const fn = new Function(...Object.keys(api), script);
fn(...Object.values(api)); /* init */
for (const key of shakespeareMonKeys) {
simulateKey(key);
}
} catch(e) {
console.log(`ERROR WHILE RUNNING "${name}"`);
brokenGames.push({
name,
error: e
});
} finally {
cleanup();
}
}
function gridToString(api) {
const w = api.width()
const h = api.height()
const max_z = Math.max(...api.getGrid().map(x => x.length));
return new Array(h).fill(0).map((_, y) => (
new Array(max_z).fill(0).map((_, z) => (
new Array(w).fill(0).map((_, x) => {
const tile = api.getTile(x, y).reverse()[z];
return (tile) ? tile.type : '.';
}).join('')
)).join('|')
)).join('\n');
}
function simEngine() {
let { api, state } = baseEngine();
let intervals = [];
let timeouts = [];
timeouts.forEach(clearTimeout);
timeouts = [];
api.setTimeout = () => {};
// api.setTimeout = (fn, n) => {
// const t = setTimeout(fn, n);
// timeouts.push(t);
// return t;
// }
intervals.forEach(clearInterval);
intervals = [];
api.setInterval = () => {};
// api.setInterval = (fn, n) => {
// const i = setInterval(fn, n);
// intervals.push(i);
// return i;
// };
let tileInputs = {
w: [],
s: [],
a: [],
d: [],
i: [],
j: [],
k: [],
l: [],
};
let afterInputs = [];
function onInput(type, fn) {
if (!(type in tileInputs)) throw new Error(
`Unknown input key, "${type}": expected one of ${VALID_INPUTS.join(', ')}`
)
tileInputs[type].push(fn);
};
function afterInput(fn) {
afterInputs.push(fn);
};
let jsr = 0x5EED;
const random = () => {
jsr^=(jsr<<17);
jsr^=(jsr>>13);
jsr^=(jsr<<5);
return (jsr>>>0)/4294967295;
};
api = {
/* you literally shouldn't use this */
getState: () => { throw new Error(" BAD! NO! ") },
/* not implementing these */
playTune: () => {},
setBackground: (type) => {},
/* will simulate input into these */
onInput, afterInput,
/* actually kind of need this one */
setLegend: (...bitmaps) => {
bitmaps.forEach(([ key, value ]) => {
if (key.length !== 1) throw new Error(`Bitmaps must have one character names.`);
})
state.legend = bitmaps;
},
...api,
/* gotta do watchu gotta do */
console: { log: () => {} },
Math: new Proxy({}, {
get(target, prop, receiver) {
if (prop == "random") return random;
return Reflect.get(Math, prop, Math);
}
}),
};
return {
api,
cleanup: () => {
timeouts.forEach(clearTimeout);
intervals.forEach(clearInterval);
intervals = [];
timeouts = [];
},
simulateKey: key => {
const VALID_INPUTS = ["w", "a", "s", "d", "i", "j", "k", "l"];
if (!VALID_INPUTS.includes(key)) return;
for (const valid_key of VALID_INPUTS)
if (key == valid_key)
tileInputs[key].forEach(fn => fn());
afterInputs.forEach(f => f());
state.sprites.forEach(s => {
s.dx = 0;
s.dy = 0;
})
}
}
}