firstbyte 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +94 -2
  2. package/bin/firstbyte.js +32 -0
  3. package/blocks/achievements/Achievements/Achievements.js +102 -0
  4. package/blocks/catalog/Kinds/Kinds.js +18 -0
  5. package/blocks/catalog/Listing/Listing.js +178 -0
  6. package/blocks/catalog/Needs/Needs.js +68 -0
  7. package/blocks/check/Checker/Checker.js +63 -0
  8. package/blocks/check/FileChecks/FileChecks.js +228 -0
  9. package/blocks/check/PlayChecks/PlayChecks.js +289 -0
  10. package/blocks/check/PlayChecks/harness.html +21 -0
  11. package/blocks/check/PlayChecks/png.js +70 -0
  12. package/blocks/check/PlayChecks/serve.js +50 -0
  13. package/blocks/check/ReportText/ReportText.js +57 -0
  14. package/blocks/dev/Levels/Levels.js +47 -0
  15. package/blocks/load/Build/Build.js +45 -0
  16. package/blocks/load/Served/Served.js +27 -0
  17. package/blocks/load/Zip/Zip.js +141 -0
  18. package/blocks/metrics/Rollup/Rollup.js +79 -0
  19. package/blocks/rules/Brief/Brief.js +168 -0
  20. package/blocks/rules/Rulebook/Rulebook.js +214 -0
  21. package/blocks/rules/Starter/Starter.js +22 -0
  22. package/blocks/rules/Starter/game/README.txt +11 -0
  23. package/blocks/rules/Starter/game/achievements.json +6 -0
  24. package/blocks/rules/Starter/game/cover.jpg +0 -0
  25. package/blocks/rules/Starter/game/game.js +147 -0
  26. package/blocks/rules/Starter/game/game.json +9 -0
  27. package/blocks/rules/Starter/game/index.html +14 -0
  28. package/blocks/rules/Starter/game/style.css +3 -0
  29. package/blocks/sdk/Host/Host.js +123 -0
  30. package/blocks/sdk/Sdk/Sdk.js +157 -0
  31. package/index.js +7 -0
  32. package/package.json +52 -4
  33. package/site/data/demo-sessions.json +1 -0
  34. package/site/data/games.json +9 -0
@@ -0,0 +1,157 @@
1
+ /*
2
+ * First Byte SDK. Load it with
3
+ * <script src="https://firstbyte.games/sdk.js"></script>
4
+ * and call window.FirstByte. Every call also works when the game runs on its own
5
+ * (no First Byte page around it), so a game never needs a special case.
6
+ *
7
+ * It talks to the page around it with postMessage: { firstbyte: 1, type, data, id }.
8
+ * The other side of that conversation is sdk/host.js.
9
+ */
10
+ (function (w) {
11
+ 'use strict';
12
+ if (w.FirstByte) return;
13
+
14
+ var VERSION = '0.1.0';
15
+ var REPLY_MS = 3000;
16
+ var SAVE_BYTES = 1024 * 1024;
17
+ var ACHIEVEMENT_ID = /^[a-z][a-z0-9-]{0,31}$/;
18
+ var host = w.parent && w.parent !== w ? w.parent : null;
19
+ var key = 'firstbyte:' + (w.location ? w.location.pathname : 'game');
20
+ var pending = {};
21
+ var nextId = 1;
22
+ var listeners = {};
23
+ var state = { ready: false, playing: false, muted: false };
24
+
25
+ function warn(msg) { try { console.warn('[FirstByte] ' + msg); } catch (e) {} }
26
+ function send(type, data) { if (host) host.postMessage({ firstbyte: 1, type: type, data: data }, '*'); }
27
+
28
+ function ask(type, data, alone) {
29
+ return new Promise(function (done) {
30
+ if (!host) return done(alone());
31
+ var id = nextId++;
32
+ var timer = setTimeout(function () { delete pending[id]; done(alone()); }, REPLY_MS);
33
+ pending[id] = function (m) { clearTimeout(timer); done(m.ok ? m.data : alone()); };
34
+ host.postMessage({ firstbyte: 1, type: type, data: data, id: id }, '*');
35
+ });
36
+ }
37
+
38
+ function emit(name) {
39
+ var list = (listeners[name] || []).slice();
40
+ for (var i = 0; i < list.length; i++) {
41
+ try { list[i](); } catch (e) { try { console.error(e); } catch (e2) {} }
42
+ }
43
+ }
44
+
45
+ w.addEventListener('message', function (e) {
46
+ var m = e.data;
47
+ if (!m || m.firstbyte !== 1 || !host || e.source !== host) return;
48
+ if (m.type === 'reply' && pending[m.id]) {
49
+ var f = pending[m.id];
50
+ delete pending[m.id];
51
+ f(m);
52
+ } else if (m.type === 'event') {
53
+ if (m.name === 'mute') state.muted = true;
54
+ if (m.name === 'unmute') state.muted = false;
55
+ emit(m.name);
56
+ }
57
+ });
58
+
59
+ function store(get, value) {
60
+ try {
61
+ if (get) return w.localStorage.getItem(key);
62
+ w.localStorage.setItem(key, value);
63
+ } catch (e) {}
64
+ return get ? null : true;
65
+ }
66
+
67
+ /* On its own, unlocks are kept in this browser; any well-formed id unlocks, since only the page knows the list. */
68
+ function unlockAlone(id) {
69
+ var kept = [];
70
+ try { kept = JSON.parse(w.localStorage.getItem(key + ':achievements')) || []; } catch (e) { kept = []; }
71
+ if (!Array.isArray(kept)) kept = [];
72
+ for (var i = 0; i < kept.length; i++) if (kept[i] && kept[i].id === id) return { unlocked: true, new: false };
73
+ kept.push({ id: id, at: Date.now() });
74
+ try { w.localStorage.setItem(key + ':achievements', JSON.stringify(kept)); } catch (e) {}
75
+ return { unlocked: true, new: true };
76
+ }
77
+
78
+ var api = {
79
+ version: VERSION,
80
+
81
+ /* Once, when the first frame is on screen. */
82
+ ready: function () {
83
+ if (state.ready) return;
84
+ state.ready = true;
85
+ send('ready');
86
+ },
87
+
88
+ /* true when a round starts, false when it ends (game over, between levels, a menu). */
89
+ playing: function (on) {
90
+ on = !!on;
91
+ if (on === state.playing) return;
92
+ state.playing = on;
93
+ send('playing', { on: on });
94
+ },
95
+
96
+ /* An ad, only at a break. Resolves { shown: true|false }. Never rejects. */
97
+ breakTime: function () {
98
+ if (state.playing) {
99
+ warn('breakTime() was called while playing, so no ad was shown. Call FirstByte.playing(false) first.');
100
+ send('break-refused', { reason: 'playing' });
101
+ return Promise.resolve({ shown: false, reason: 'playing' });
102
+ }
103
+ return ask('break', null, function () { return { shown: false, reason: 'no-site' }; });
104
+ },
105
+
106
+ /* Progress: anything JSON can store, up to 1 MB. */
107
+ save: function (data) {
108
+ var text;
109
+ try { text = JSON.stringify(data); } catch (e) { text = undefined; }
110
+ if (text === undefined) return Promise.reject(new Error('FirstByte.save() takes data that JSON can store.'));
111
+ if (text.length > SAVE_BYTES) return Promise.reject(new Error('FirstByte.save() is limited to 1 MB.'));
112
+ return ask('save', text, function () { return store(false, text); }).then(function () {});
113
+ },
114
+
115
+ /* The saved progress, or null. */
116
+ load: function () {
117
+ return ask('load', null, function () { return store(true); }).then(function (text) {
118
+ if (text == null) return null;
119
+ try { return JSON.parse(text); } catch (e) { return null; }
120
+ });
121
+ },
122
+
123
+ /* An optional score board. */
124
+ score: function (n) {
125
+ if (typeof n !== 'number' || !isFinite(n)) { warn('score() takes a number.'); return; }
126
+ send('score', { value: n });
127
+ },
128
+
129
+ /* An achievement earned, by its id in achievements.json. Only the first time is new. Never rejects. */
130
+ achieve: function (id) {
131
+ if (typeof id !== 'string' || !ACHIEVEMENT_ID.test(id)) {
132
+ warn('achieve() takes an id from achievements.json: lowercase letters, digits and hyphens, starting with a letter, like "first-win".');
133
+ return Promise.resolve({ unlocked: false, new: false, reason: 'bad-id' });
134
+ }
135
+ return ask('achieve', { id: id }, function () { return unlockAlone(id); });
136
+ },
137
+
138
+ /* 'mute', 'unmute', 'pause' (before an ad), 'resume'. Returns a function that stops listening. */
139
+ on: function (name, fn) {
140
+ if (typeof fn !== 'function') { warn('on() takes a name and a function.'); return function () {}; }
141
+ (listeners[name] = listeners[name] || []).push(fn);
142
+ send('listen', { event: name });
143
+ return function () {
144
+ var list = listeners[name] || [];
145
+ var i = list.indexOf(fn);
146
+ if (i >= 0) list.splice(i, 1);
147
+ };
148
+ },
149
+
150
+ get muted() { return state.muted; },
151
+ get isPlaying() { return state.playing; },
152
+ get onSite() { return !!host; },
153
+ };
154
+
155
+ w.FirstByte = api;
156
+ send('hello', { version: VERSION });
157
+ })(typeof window !== 'undefined' ? window : globalThis);
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ /* The public API: what a script or another tool imports from 'firstbyte'. Every export is a block's entry. */
2
+ export { check, report } from './blocks/check/Checker/Checker.js';
3
+ export { formatReport, summary } from './blocks/check/ReportText/ReportText.js';
4
+ export { brief } from './blocks/rules/Brief/Brief.js';
5
+ export { RULES, LIMITS, ALLOWED, ruleById } from './blocks/rules/Rulebook/Rulebook.js';
6
+ export { CATEGORIES } from './blocks/catalog/Kinds/Kinds.js';
7
+ export { needs, shortlist } from './blocks/catalog/Needs/Needs.js';
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "firstbyte",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "First Byte: the game SDK, the checker that tells a developer exactly what to fix, and the site.",
5
+ "type": "module",
6
+ "license": "UNLICENSED",
7
+ "engines": {
8
+ "node": ">=22"
9
+ },
10
+ "bin": {
11
+ "firstbyte": "bin/firstbyte.js"
12
+ },
13
+ "files": [
14
+ "bin/",
15
+ "index.js",
16
+ "blocks/achievements/Achievements/",
17
+ "blocks/catalog/Kinds/",
18
+ "blocks/catalog/Listing/",
19
+ "blocks/catalog/Needs/",
20
+ "blocks/check/",
21
+ "blocks/dev/Levels/",
22
+ "blocks/load/",
23
+ "blocks/metrics/Rollup/",
24
+ "blocks/rules/",
25
+ "blocks/sdk/",
26
+ "site/data/games.json",
27
+ "site/data/demo-sessions.json",
28
+ "!**/*.test.js",
29
+ "!**/*.spec.md"
30
+ ],
31
+ "exports": {
32
+ ".": "./index.js",
33
+ "./sdk": "./blocks/sdk/Sdk/Sdk.js",
34
+ "./host": "./blocks/sdk/Host/Host.js"
35
+ },
36
+ "scripts": {
37
+ "check": "node node_modules/blockkit/bin/blockkit.js check",
38
+ "build": "node scripts/build-site.js",
39
+ "test": "node --test --test-concurrency=1 \"blocks/**/*.test.js\" \"test/**/*.test.js\"",
40
+ "verify": "npm run check && node scripts/build-site.js --check && npm test",
41
+ "site": "node scripts/serve-site.js"
42
+ },
43
+ "peerDependencies": {
44
+ "playwright": "1.63.0"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "playwright": {
48
+ "optional": true
49
+ }
50
+ },
51
+ "devDependencies": {
52
+ "blockkit": "github:jacksonkvandyke/Blockkit#7eb509eb2213fd73575dd22816550ab26d72304b"
53
+ }
54
+ }