-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
605 lines (583 loc) · 16.9 KB
/
Copy pathindex.html
File metadata and controls
605 lines (583 loc) · 16.9 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
<!doctype html>
<!-- ============================================
ACES ARCADE — the engine, in a browser.
There is NO build step and NO bundle. This
page fetches the very same CommonJS modules
that `node test.js` runs (core.js,
calculator.js, store.js, nav.js, ledger.js,
platform.js, parse.js, version.js ...) and
wires them with a tiny require() shim below.
The site serves its own source: view-source
is the real program.
Isomorphism, demonstrated:
- same decide/evolve/react as the CLI
- the 'speak' EFFECT runs on the browser's
TTS (Text To Speech) processor here — and
is GATED by the nav archetype's folded
sound setting.
- NAVIGATION IS STATE: the tab strip is the
nav archetype; the route table is a
parser-printer (parse.js). decide()
validates a destination by PARSING it;
react() PRINTS state into location.hash.
Deep links replay the same road inward.
- REPLAY drops state and refolds the log,
live, because state is only a cache.
============================================= -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1">
<title>ACES Arcade</title>
<link rel="icon" href="data:,">
<style>
:root {
--ink: #33ff66; --dim: #1a8c3c;
--bg: #050b07; --panel: #08130c;
--hot: #aaffc3;
}
* { box-sizing: border-box; }
body {
margin: 0; background: var(--bg);
color: var(--ink);
font-family: "SFMono-Regular", Menlo,
Consolas, monospace;
font-size: 15px; line-height: 1.35;
}
body.glyph-sm { font-size: 12px; }
body.glyph-lg { font-size: 19px; }
/* CRT scanlines */
body::after {
content: ""; position: fixed; inset: 0;
pointer-events: none;
background:
repeating-linear-gradient(0deg,
rgba(0,0,0,.16) 0 1px,
transparent 1px 3px);
}
header {
padding: 14px 18px 6px;
display: flex; gap: 12px;
align-items: baseline; flex-wrap: wrap;
}
h1 { margin: 0; font-size: 20px;
letter-spacing: 3px;
text-shadow: 0 0 8px var(--dim); }
.chip { color: var(--hot);
border: 1px solid var(--dim);
padding: 1px 8px; border-radius: 10px;
font-size: 12px; }
nav { padding: 6px 18px; display: flex;
gap: 8px; flex-wrap: wrap; }
nav button, .pad button, .act {
background: var(--panel);
color: var(--ink);
border: 1px solid var(--dim);
border-radius: 6px;
font: inherit; padding: 8px 14px;
min-height: 44px; cursor: pointer;
}
nav button[aria-selected="true"] {
color: var(--bg); background: var(--ink);
font-weight: 700;
}
main {
display: grid; gap: 16px;
grid-template-columns: minmax(0,1fr)
minmax(0,1fr);
padding: 12px 18px 40px;
}
@media (max-width: 760px) {
main { grid-template-columns: 1fr; }
}
section, aside {
background: var(--panel);
border: 1px solid var(--dim);
border-radius: 8px; padding: 14px;
min-width: 0;
}
pre {
margin: 0 0 12px; overflow-x: auto;
text-shadow: 0 0 6px rgba(51,255,102,.35);
}
.pad { display: grid; gap: 8px;
grid-template-columns: repeat(4, 1fr);
max-width: 340px; }
.pad button:active,
.act:active { background: var(--ink);
color: var(--bg); }
.acts { display: flex; gap: 8px;
flex-wrap: wrap; margin-top: 10px; }
aside h2, section h2 {
margin: 0 0 10px; font-size: 13px;
color: var(--hot); letter-spacing: 2px; }
#tape { max-height: 420px; overflow-y: auto;
font-size: 12px; color: var(--dim); }
#tape .v { color: var(--ink); }
#toast { position: fixed; left: 50%;
bottom: 24px; transform: translateX(-50%);
background: var(--ink); color: var(--bg);
padding: 10px 16px; border-radius: 8px;
opacity: 0; transition: opacity .3s;
max-width: 90vw; }
footer { padding: 0 18px 30px;
color: var(--dim); font-size: 12px; }
a { color: var(--hot); }
</style>
</head>
<body>
<header>
<h1>ACES ARCADE</h1>
<span class="chip" id="ver">v…</span>
<span class="chip" id="planet">…</span>
<span class="chip" id="voice">voice …</span>
</header>
<nav id="tabs" role="tablist"></nav>
<main>
<section>
<pre id="screen">booting…</pre>
<div id="controls"></div>
</section>
<aside>
<h2>EVENT TAPE — the log is truth</h2>
<div class="acts">
<button class="act" id="replay">
⟲ drop state + replay log</button>
<button class="act" id="back">
← back (a fact too)</button>
</div>
<pre id="tape"></pre>
</aside>
</main>
<footer>
Every keypress becomes a fact; state is a
fold of the tape; the address bar is printed
by the same parser that validates it.
Source (unbundled, this site serves it):
<a href="core.js">core.js</a> ·
<a href="parse.js">parse.js</a> ·
<a href="nav.js">nav.js</a> ·
<a href="ledger.js">ledger.js</a> ·
<a href="calculator.js">calculator.js</a> ·
<a href="store.js">store.js</a> ·
<a href="https://github.com/technoplato/NoIssue">repo</a>
</footer>
<div id="toast" role="status"></div>
<script>
'use strict';
/* -- a require() the size of a haiku ---------
* Fetch every module's source, then evaluate
* lazily, memoized, exactly like node would.
*/
const SRC = {}, CACHE = {};
function req(path) {
const n = path.replace(/^\.\//, '');
if (CACHE[n]) return CACHE[n].exports;
const m = CACHE[n] = { exports: {} };
new Function(
'module', 'exports', 'require',
SRC[n])(m, m.exports, req);
return m.exports;
}
const $ = id => document.getElementById(id);
function toast(text) {
const t = $('toast');
t.textContent = text;
t.style.opacity = 1;
clearTimeout(toast.tid);
toast.tid = setTimeout(
() => t.style.opacity = 0, 2600);
}
async function boot() {
const mods = ['core', 'calculator', 'ui',
'store', 'store-ui', 'platform',
'version', 'view', 'parse', 'nav',
'ledger', 'llm', 'assistant'];
// standalone builds (deploy/build-artifact
// .js) inline the sources + package.json;
// the hosted site fetches its own files.
const inline = window.__ACES_SRC__;
let pkg;
if (inline) {
for (const n of mods)
SRC[n] = inline[n];
pkg = inline['package.json'];
} else {
await Promise.all(mods.map(async n => {
const r = await fetch(n + '.js');
SRC[n] = await r.text();
}));
pkg = await (
await fetch('package.json')).json();
}
const { createRuntime } = req('core');
const { versioned, defineSchema } =
req('version');
const platform = req('platform');
const navMod = req('nav');
const schema =
defineSchema({ version: pkg.version });
$('ver').textContent =
'v' + pkg.version + ' · schema ' +
schema.tag;
$('planet').textContent =
platform.detect() + ' → ' +
platform.recommend().use;
/* -- effect PROCESSORS for THIS planet ------
* The archetypes ask; the browser answers.
* speak is gated by the nav archetype's
* folded sound setting — a setting is
* state, so muting is replayable history.
*/
const procs = {
speak: async fx => {
const s = nav.getState().settings;
if (s.sound &&
'speechSynthesis' in window)
speechSynthesis.speak(
new SpeechSynthesisUtterance(
fx.text));
return [];
},
notify: async fx => {
toast(fx.text);
return [];
},
// nav's react() PRINTS the route here:
hash: fx => {
history.replaceState(null, '',
'#' + fx.text);
return [];
},
};
const nav = createRuntime(
versioned(navMod.machine, schema),
{ processors: procs, nodeId: 'web' });
const calc = createRuntime(
versioned(req('calculator').machine,
schema),
{ processors: procs, nodeId: 'web' });
const store = createRuntime(
versioned(req('store').machine, schema),
{ processors: procs, nodeId: 'web' });
const ledger = createRuntime(
versioned(req('ledger')
.createLedger('michael'), schema),
{ processors: procs, nodeId: 'web' });
// the Shopify emoji buyer's seed, replayed
// into the browser ledger as a fact:
await ledger.dispatch({
type: 'RecordPurchase',
account: 'emoji-buyer', cents: 1428,
source: 'shopify',
ref: 'shopify:order-1001' });
// -- the event tape -----------------------
const tape = $('tape');
function tapeLine(who, e) {
const meta = { ...e };
delete meta._seq; delete meta._at;
delete meta._id; delete meta._origin;
const div =
document.createElement('div');
div.innerHTML =
'[' + who + '] <span class="v">' +
(e.v || '·') + '</span> ';
div.append(JSON.stringify(meta));
tape.prepend(div);
while (tape.childNodes.length > 200)
tape.lastChild.remove();
}
const wire = (rt, who) =>
rt.subscribe(m => {
if (m.t === 'event')
tapeLine(who, m.e);
render();
});
wire(calc, 'calc');
wire(store, 'store');
wire(ledger, 'ledger');
wire(nav, 'nav');
// -- screens ------------------------------
const KEYS = [
['7', '8', '9', '/'],
['4', '5', '6', '*'],
['1', '2', '3', '-'],
['0', '.', '=', '+'],
['(', ')', '^', 'C'],
['<'],
];
const SKUS = ['APL', 'MLK', 'EGG', 'BRD'];
const key = k =>
calc.dispatch({ type: 'KEY', key: k });
const el = t =>
document.createElement(t);
const btn = (label, fn, cls) => {
const b = el('button');
if (cls) b.className = cls;
b.textContent = label;
b.onclick = fn;
return b;
};
const actRow = (...bs) => {
const d = el('div');
d.className = 'acts';
bs.forEach(b => d.append(b));
return d;
};
let seededRefs = 1; // demo purchase ids
const SCREENS = {
calc: {
screen: () => calc.render(),
controls() {
const pad = el('div');
pad.className = 'pad';
for (const row of KEYS)
for (const k of row)
pad.append(btn(
k === '<' ? '< back' : k,
() => key(k)));
return pad;
},
},
store: {
screen: () => store.render(),
controls() {
const w = el('div');
w.append(
actRow(...SKUS.map(s =>
btn('+ ' + s, () =>
store.dispatch({
type: 'AddToCart',
sku: s }), 'act'))),
actRow(...SKUS.map(s =>
btn('− ' + s, () =>
store.dispatch({
type: 'RemoveFromCart',
sku: s }), 'act'))),
actRow(btn('CHECKOUT ✓', () =>
store.dispatch(
{ type: 'Checkout' }),
'act')));
return w;
},
},
ledger: {
screen: () => ledger.render(),
controls() {
const w = el('div');
w.append(
actRow(
btn('simulate $5 purchase',
() => ledger.dispatch({
type: 'RecordPurchase',
account: 'player-' +
(1 + seededRefs % 3),
cents: 500,
source: 'stripe-demo',
ref: 'demo:' +
(++seededRefs),
}), 'act'),
btn('tip genesis 100', () =>
ledger.dispatch({
type: 'Transfer',
from: 'emoji-buyer',
to: 'michael',
amount: 100 }), 'act')),
actRow(
btn('try to enrich genesis',
() => ledger.dispatch({
type: 'Transfer',
from: 'emoji-buyer',
to: 'michael',
amount: 142000 }), 'act'),
btn('burn 50 (play game)',
() => ledger.dispatch({
type: 'Burn',
account: 'emoji-buyer',
amount: 50,
why: 'game' }), 'act')));
const p = el('p');
p.style.color = 'var(--dim)';
p.append('Rejections land on the ' +
'tape as facts. Genesis (*) can ' +
'never out-hold a player.');
w.append(p);
return w;
},
},
platform: {
screen: () =>
platform.report() + '\n\n' +
'recommend() → ' + JSON.stringify(
platform.recommend(), null, 2) +
'\n\nthe closed enum (' +
platform.leaves().length +
' leaves):\n ' +
platform.leaves().join('\n '),
controls: () => el('div'),
},
settings: {
screen: () => nav.render(),
controls() {
const set = (k, v) => () =>
nav.dispatch({ type: 'SET',
key: k, value: v });
const w = el('div');
w.append(
actRow(
btn('glyph sm',
set('glyph', 'sm'), 'act'),
btn('glyph md',
set('glyph', 'md'), 'act'),
btn('glyph lg',
set('glyph', 'lg'), 'act')),
actRow(
btn('sound on',
set('sound', true), 'act'),
btn('sound off',
set('sound', false), 'act'),
btn('bogus setting (watch it ' +
'refuse)', set('crt', 'on'),
'act')));
return w;
},
},
};
// -- tab strip = the nav archetype --------
const goto = to =>
nav.dispatch({ type: 'NAVIGATE', to });
const navBar = $('tabs');
for (const name of navMod.SCREENS) {
const b = btn(name.toUpperCase(),
() => goto(name));
b.dataset.tab = name;
navBar.append(b);
}
function mount() {
const active = nav.getState().screen;
for (const b of navBar.children)
b.setAttribute('aria-selected',
b.dataset.tab === active);
$('controls').replaceChildren(
SCREENS[active].controls());
}
let lastScreen = '';
function render() {
const st = nav.getState();
if (st.screen !== lastScreen) {
lastScreen = st.screen;
mount();
}
document.body.className =
'glyph-' + st.settings.glyph;
$('screen').textContent =
SCREENS[st.screen].screen();
}
// deep links + manual hash edits: the SAME
// route spec parses them; junk becomes a
// Rejected fact on the tape, not a crash.
const fromHash = () => {
const h = location.hash
.replace(/^#\/?/, '');
if (h) goto(h);
};
addEventListener('hashchange', fromHash);
// -- keyboard drives the calculator -------
addEventListener('keydown', ev => {
if (nav.getState().screen !== 'calc')
return;
const map = {
Enter: '=', Backspace: '<',
Escape: 'C', c: 'C',
};
if (map[ev.key]) {
key(map[ev.key]); return;
}
if ('0123456789.+-*/^()='
.includes(ev.key) && ev.key)
key(ev.key);
});
// -- replay: state is only a cache --------
$('replay').onclick = () => {
const n = calc.getLog().length +
store.getLog().length +
ledger.getLog().length +
nav.getLog().length;
calc.replay(calc.getLog());
store.replay(store.getLog());
ledger.replay(ledger.getLog());
nav.replay(nav.getLog());
lastScreen = '';
render();
toast('rebuilt state from ' + n +
' facts — same answer, always');
};
$('back').onclick = () =>
nav.dispatch({ type: 'BACK' });
/* -- the voice assistant ------------------
* Senses (llm.js) are the dependency that
* causes the side effects: on-device
* Gemini Nano (Chrome Prompt API) + Web
* Speech here; a CLI swaps the bag and the
* archetype never notices. No LLM on this
* planet? OfferDeclined lands on the tape
* as a fact and nobody is nagged.
*/
const A = req('assistant');
const senses =
req('llm').browserSenses(window);
const RTS = { calc, store, nav, ledger };
const world = {
offerText: 'Talk to the arcade with ' +
'your voice? Uses the on-device ' +
'model; nothing leaves the page.',
describe: () => [
'You drive an event-sourced arcade.',
'Answer ONLY strict JSON:',
'{"target":t,"actions":[...]}',
'targets and their actions:',
'calc: {"type":"KEY","key":k} where',
' k in 0-9 . + - * / ^ ( ) = C <',
' display now: ' +
calc.getState().display,
'store: {"type":"AddToCart","sku":s}|',
' {"type":"RemoveFromCart","sku":s}|',
' {"type":"Checkout"} sku in APL MLK',
' EGG BRD',
'nav: {"type":"NAVIGATE","to":x} x in '
+ navMod.SCREENS.join(' '),
'ledger: {"type":"Transfer","from":a,',
' "to":b,"amount":n} accounts: ' +
Object.keys(ledger.getState()
.accounts).join(' '),
].join('\n'),
perform: async plan => {
const rt = RTS[plan.target];
for (const act of plan.actions)
await rt.dispatch(act);
},
};
const assistant = createRuntime(
versioned(A.createAssistant(
{ targets: Object.keys(RTS) }),
schema),
{ processors: A.processorsFor(
senses, world),
nodeId: 'web' });
assistant.subscribe(m => {
if (m.t === 'event')
tapeLine('voice', m.e);
$('voice').textContent = 'voice ' +
assistant.getState().phase;
});
mount(); render(); fromHash();
assistant.dispatch({ type: 'MOUNT' });
}
boot().catch(e => {
$('screen').textContent =
'BOOT FAILURE\n' + e.stack;
});
</script>
</body>
</html>