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.
- package/README.md +94 -2
- package/bin/firstbyte.js +32 -0
- package/blocks/achievements/Achievements/Achievements.js +102 -0
- package/blocks/catalog/Kinds/Kinds.js +18 -0
- package/blocks/catalog/Listing/Listing.js +178 -0
- package/blocks/catalog/Needs/Needs.js +68 -0
- package/blocks/check/Checker/Checker.js +63 -0
- package/blocks/check/FileChecks/FileChecks.js +228 -0
- package/blocks/check/PlayChecks/PlayChecks.js +289 -0
- package/blocks/check/PlayChecks/harness.html +21 -0
- package/blocks/check/PlayChecks/png.js +70 -0
- package/blocks/check/PlayChecks/serve.js +50 -0
- package/blocks/check/ReportText/ReportText.js +57 -0
- package/blocks/dev/Levels/Levels.js +47 -0
- package/blocks/load/Build/Build.js +45 -0
- package/blocks/load/Served/Served.js +27 -0
- package/blocks/load/Zip/Zip.js +141 -0
- package/blocks/metrics/Rollup/Rollup.js +79 -0
- package/blocks/rules/Brief/Brief.js +168 -0
- package/blocks/rules/Rulebook/Rulebook.js +214 -0
- package/blocks/rules/Starter/Starter.js +22 -0
- package/blocks/rules/Starter/game/README.txt +11 -0
- package/blocks/rules/Starter/game/achievements.json +6 -0
- package/blocks/rules/Starter/game/cover.jpg +0 -0
- package/blocks/rules/Starter/game/game.js +147 -0
- package/blocks/rules/Starter/game/game.json +9 -0
- package/blocks/rules/Starter/game/index.html +14 -0
- package/blocks/rules/Starter/game/style.css +3 -0
- package/blocks/sdk/Host/Host.js +123 -0
- package/blocks/sdk/Sdk/Sdk.js +157 -0
- package/index.js +7 -0
- package/package.json +52 -4
- package/site/data/demo-sessions.json +1 -0
- package/site/data/games.json +9 -0
package/README.md
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
#
|
|
1
|
+
# firstbyte
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The checker for [First Byte](https://firstbyte.games), a site that shows a few new browser games each
|
|
4
|
+
week. It plays your game and tells you exactly what to fix before you send it.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
npx firstbyte check my-game.zip
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
`my-game.zip` can also be your build folder. Every rule gets PASS or FAIL with its fix, and the last
|
|
11
|
+
line says what to do next. Needs Node 22 or newer.
|
|
12
|
+
|
|
13
|
+
## Playing the game
|
|
14
|
+
|
|
15
|
+
Half the rules are checked by playing the game in a headless browser. That needs Playwright, installed
|
|
16
|
+
once in your game's folder:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm i -D playwright@1.63.0
|
|
20
|
+
npx playwright install chromium
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Without it the files are still checked, and the report says the play checks were skipped.
|
|
24
|
+
|
|
25
|
+
Nothing to install at all: drop your zip at [firstbyte.games/send](https://firstbyte.games/send)
|
|
26
|
+
and press **Just check it**. The same checks run there, and nothing is sent to our team until you say so.
|
|
27
|
+
|
|
28
|
+
## Commands
|
|
29
|
+
|
|
30
|
+
| Command | What it does |
|
|
31
|
+
|---|---|
|
|
32
|
+
| `npx firstbyte check <zip or folder>` | Checks a game against every rule |
|
|
33
|
+
| `npx firstbyte check <zip or folder> --json` | The same report as JSON, for your AI to read |
|
|
34
|
+
| `npx firstbyte check <zip or folder> --no-play` | Checks the files only |
|
|
35
|
+
| `npx firstbyte brief` | Prints the rules, written for an AI |
|
|
36
|
+
|
|
37
|
+
The command exits with 0 when nothing fails and 1 when something does.
|
|
38
|
+
|
|
39
|
+
## Building with an AI
|
|
40
|
+
|
|
41
|
+
Paste the brief into it first: `npx firstbyte brief`, or [firstbyte.games/ai.txt](https://firstbyte.games/ai.txt).
|
|
42
|
+
It has every rule by id, the SDK's calls, and a whole starter game that already passes. Then let the AI
|
|
43
|
+
run `npx firstbyte check . --json` and fix what it reports.
|
|
44
|
+
|
|
45
|
+
## Start from a game that passes
|
|
46
|
+
|
|
47
|
+
[firstbyte.games/starter.zip](https://firstbyte.games/starter.zip): a tiny, complete game. Its comments
|
|
48
|
+
name the rule each line is there for.
|
|
49
|
+
|
|
50
|
+
## Your game's page, in the zip
|
|
51
|
+
|
|
52
|
+
Two optional files next to `index.html` fill in the boxes when you send the game, so nothing is typed twice:
|
|
53
|
+
|
|
54
|
+
```json
|
|
55
|
+
{ "name": "Moon Hop",
|
|
56
|
+
"hook": "Hop between moons that shrink.",
|
|
57
|
+
"controls": [ { "keys": ["arrows", "wasd"], "does": "Move" }, { "keys": ["space"], "does": "Jump" } ],
|
|
58
|
+
"kinds": ["quick-runs"] }
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
That is `game.json`: any of the name, one line on what you do, how to play, and the kinds. `cover.jpg`
|
|
62
|
+
beside it is the picture players see: a 16:9 JPEG, at least 640 wide, under 1 MB. The checker checks
|
|
63
|
+
both (rule F8) and says exactly what to fix, and the brief lists every key and kind you can use. The
|
|
64
|
+
starter game has both, as a working example.
|
|
65
|
+
|
|
66
|
+
## The SDK (optional)
|
|
67
|
+
|
|
68
|
+
```html
|
|
69
|
+
<script src="https://firstbyte.games/sdk.js"></script>
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
FirstByte.ready(); /* once, when the first frame is on screen */
|
|
74
|
+
FirstByte.playing(true); /* a round starts; playing(false) when it ends */
|
|
75
|
+
await FirstByte.breakTime(); /* an ad, only at a break */
|
|
76
|
+
FirstByte.save({ level: 4 }); /* progress; FirstByte.load() reads it back */
|
|
77
|
+
FirstByte.on('mute', quiet); /* the site's mute button */
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Every call also works when the game runs on its own, so a game needs no special case. Without the SDK
|
|
81
|
+
a game is still welcome: it earns no ad share and keeps no saves with us.
|
|
82
|
+
|
|
83
|
+
## From code
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
import { check, formatReport } from 'firstbyte';
|
|
87
|
+
|
|
88
|
+
const report = await check('my-game.zip');
|
|
89
|
+
console.log(formatReport(report));
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Sending your game
|
|
93
|
+
|
|
94
|
+
[firstbyte.games/send](https://firstbyte.games/send): sign in with your email, drop the zip,
|
|
95
|
+
name it, and say in one sentence what's new about it. Our team plays it and answers within a week.
|
package/bin/firstbyte.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { check } from '../blocks/check/Checker/Checker.js';
|
|
3
|
+
import { formatReport } from '../blocks/check/ReportText/ReportText.js';
|
|
4
|
+
import { brief } from '../blocks/rules/Brief/Brief.js';
|
|
5
|
+
|
|
6
|
+
const HELP = `First Byte
|
|
7
|
+
|
|
8
|
+
firstbyte check <build.zip | build-folder> check a game against every rule
|
|
9
|
+
--json print the report as JSON (for your AI)
|
|
10
|
+
--no-play only check the files, do not play the game
|
|
11
|
+
firstbyte brief print the rules, written for an AI
|
|
12
|
+
firstbyte help this list
|
|
13
|
+
`;
|
|
14
|
+
|
|
15
|
+
const [cmd, ...args] = process.argv.slice(2);
|
|
16
|
+
|
|
17
|
+
if (cmd === 'check') {
|
|
18
|
+
const path = args.find((a) => !a.startsWith('--'));
|
|
19
|
+
if (!path) {
|
|
20
|
+
process.stderr.write('Say which build to check: firstbyte check my-game.zip\n');
|
|
21
|
+
process.exit(2);
|
|
22
|
+
}
|
|
23
|
+
const rep = await check(path, { play: !args.includes('--no-play') });
|
|
24
|
+
if (args.includes('--json')) process.stdout.write(JSON.stringify(rep, null, 2) + '\n');
|
|
25
|
+
else process.stdout.write(formatReport(rep, { color: process.stdout.isTTY }));
|
|
26
|
+
process.exit(rep.ok ? 0 : 1);
|
|
27
|
+
} else if (cmd === 'brief') {
|
|
28
|
+
process.stdout.write(brief());
|
|
29
|
+
} else {
|
|
30
|
+
process.stdout.write(HELP);
|
|
31
|
+
process.exit(cmd && cmd !== 'help' && cmd !== '--help' ? 2 : 0);
|
|
32
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A game's achievements: the list it ships as achievements.json, checked in plain words; the unlocks
|
|
3
|
+
* this browser has; how rare each one is, in whole percents. For fun only: the game reports them, so
|
|
4
|
+
* nothing that counts ever reads them. Plain ESM with no Node imports.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const FILE = 'achievements.json';
|
|
8
|
+
export const LIMITS = { most: 30, idChars: 32, nameChars: 32, aboutChars: 80, minPlayers: 50 };
|
|
9
|
+
export const ICONS = ['star', 'crown', 'bolt', 'heart', 'flag', 'coin', 'gem', 'key', 'shield', 'sword', 'skull', 'clock', 'trophy', 'rocket', 'leaf', 'eye'];
|
|
10
|
+
|
|
11
|
+
const FIELDS = ['id', 'name', 'about', 'icon', 'secret'];
|
|
12
|
+
const ID = /^[a-z][a-z0-9-]*$/;
|
|
13
|
+
const LINK = /https?:|www\.|\b[a-z0-9-]+\.(com|net|org|io|gg|games|app|dev|co|xyz|me|ly|tv)\b/i;
|
|
14
|
+
const EXAMPLE = '{ "achievements": [ { "id": "first-win", "name": "First win", "about": "Win a round." } ] }';
|
|
15
|
+
|
|
16
|
+
export const isId = (s) => typeof s === 'string' && s.length <= LIMITS.idChars && ID.test(s);
|
|
17
|
+
|
|
18
|
+
/* A small, stable hash, so an id always gets the same icon. */
|
|
19
|
+
function hash(s) {
|
|
20
|
+
let h = 2166136261;
|
|
21
|
+
for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619);
|
|
22
|
+
return h >>> 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const slug = (s) => String(s).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^[^a-z]+|-+$/g, '').slice(0, LIMITS.idChars) || 'first-win';
|
|
26
|
+
const letters = (s) => [...s].length;
|
|
27
|
+
|
|
28
|
+
/* The problems with one piece of text: missing, too long, markup, a link. */
|
|
29
|
+
function textProblems(v, what, most) {
|
|
30
|
+
if (typeof v !== 'string' || !v.trim()) return [`it needs ${what === 'name' ? 'a name' : 'an about line'}.`];
|
|
31
|
+
const out = [];
|
|
32
|
+
if (letters(v) > most) out.push(`the ${what === 'name' ? 'name' : 'about line'} is ${letters(v)} letters. Keep it to ${most}.`);
|
|
33
|
+
if (/[<>]/.test(v)) out.push(`the ${what === 'name' ? 'name' : 'about line'} has < or >. Use plain words.`);
|
|
34
|
+
if (LINK.test(v)) out.push(`the ${what === 'name' ? 'name' : 'about line'} has a link. Leave links out.`);
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function readList(text) {
|
|
39
|
+
let data;
|
|
40
|
+
try { data = JSON.parse(text); } catch (e) {
|
|
41
|
+
return { list: null, problems: [`${FILE} isn't valid JSON: ${String(e.message).replace(/\.$/, '')}. Fix the file, or leave it out.`] };
|
|
42
|
+
}
|
|
43
|
+
const all = data && typeof data === 'object' && !Array.isArray(data) ? data.achievements : undefined;
|
|
44
|
+
if (!Array.isArray(all)) return { list: null, problems: [`${FILE} needs a list called "achievements": ${EXAMPLE}.`] };
|
|
45
|
+
if (!all.length) return { list: null, problems: ['The list is empty. Add an achievement, or leave the file out.'] };
|
|
46
|
+
if (all.length > LIMITS.most) return { list: null, problems: [`There are ${all.length} achievements. Keep it to ${LIMITS.most}.`] };
|
|
47
|
+
|
|
48
|
+
const problems = [];
|
|
49
|
+
const seen = new Set();
|
|
50
|
+
const list = all.map((a, i) => {
|
|
51
|
+
if (!a || typeof a !== 'object' || Array.isArray(a)) {
|
|
52
|
+
problems.push(`Achievement ${i + 1} isn't an achievement like { "id": "first-win", "name": "First win", "about": "Win a round." }.`);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const at = `Achievement ${i + 1}${isId(a.id) ? ` (${a.id})` : ''}: `;
|
|
56
|
+
const add = (p) => problems.push(at + p);
|
|
57
|
+
if (!isId(a.id)) add(`the id ${JSON.stringify(a.id ?? '')} must be lowercase letters, digits and hyphens, starting with a letter, up to ${LIMITS.idChars}. Try "${slug(a.id ?? a.name ?? '')}".`);
|
|
58
|
+
else if (seen.has(a.id)) add('the id is used twice. Give each its own.');
|
|
59
|
+
seen.add(a.id);
|
|
60
|
+
for (const k of Object.keys(a)) if (!FIELDS.includes(k)) add(`"${k}" isn't a field. Use id, name, about, icon and secret.`);
|
|
61
|
+
textProblems(a.name, 'name', LIMITS.nameChars).forEach(add);
|
|
62
|
+
textProblems(a.about, 'about', LIMITS.aboutChars).forEach(add);
|
|
63
|
+
if ('icon' in a && !ICONS.includes(a.icon)) add(`${JSON.stringify(a.icon)} isn't an icon. Use one of: ${ICONS.join(', ')}.`);
|
|
64
|
+
if ('secret' in a && a.secret !== true) add('"secret" can only be true. Leave it out otherwise.');
|
|
65
|
+
const out = { id: a.id, name: a.name, about: a.about };
|
|
66
|
+
if ('icon' in a) out.icon = a.icon;
|
|
67
|
+
if ('secret' in a) out.secret = a.secret;
|
|
68
|
+
return out;
|
|
69
|
+
});
|
|
70
|
+
return problems.length ? { list: null, problems } : { list, problems: [] };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const iconOf = (a) => (ICONS.includes(a?.icon) ? a.icon : ICONS[hash(String(a?.id ?? '')) % ICONS.length]);
|
|
74
|
+
|
|
75
|
+
export function addUnlock(have, id, ids, at) {
|
|
76
|
+
const base = Array.isArray(have) ? have.filter((u) => u && isId(u.id)) : [];
|
|
77
|
+
if (!ids.includes(id)) return { have: base, isNew: false, refused: 'unknown' };
|
|
78
|
+
if (base.some((u) => u.id === id)) return { have: base, isNew: false };
|
|
79
|
+
return { have: [...base, { id, at }], isNew: true };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function rarity(counts, list) {
|
|
83
|
+
const players = Number(counts?.players);
|
|
84
|
+
const enough = Number.isFinite(players) && players >= LIMITS.minPlayers;
|
|
85
|
+
return Object.fromEntries(list.map((a) => {
|
|
86
|
+
if (!enough) return [a.id, null];
|
|
87
|
+
const raw = Number(counts.holders?.[a.id]);
|
|
88
|
+
const held = Number.isFinite(raw) ? Math.min(Math.max(0, Math.floor(raw)), players) : 0;
|
|
89
|
+
return [a.id, held ? Math.max(1, Math.min(100, Math.round((held / players) * 100))) : 0];
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function forPlayer(list, have, shares = {}) {
|
|
94
|
+
const got = new Map((Array.isArray(have) ? have : []).map((u) => [u.id, u]));
|
|
95
|
+
return list.map((a) => {
|
|
96
|
+
const u = got.get(a.id);
|
|
97
|
+
const share = shares[a.id] ?? null;
|
|
98
|
+
if (a.secret && !u) return { id: a.id, name: 'Secret', about: 'Keep playing to find it.', icon: 'eye', secret: true, unlocked: false, share };
|
|
99
|
+
const shown = { id: a.id, name: a.name, about: a.about, icon: iconOf(a), secret: !!a.secret, unlocked: !!u, share };
|
|
100
|
+
return u ? { ...shown, at: u.at } : shown;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The kinds of game First Byte shows. This list is the only thing a person maintains, and only when a
|
|
3
|
+
* whole new kind of game appears. What we need each week is worked out from it (see needs.js).
|
|
4
|
+
* Ids are never renamed: games in the catalog point at them.
|
|
5
|
+
* Plain ESM with no Node imports: the site runs this same file in the browser.
|
|
6
|
+
*/
|
|
7
|
+
export const CATEGORIES = [
|
|
8
|
+
{ id: 'two-player', name: '2 players, 1 keyboard', icon: 'two' },
|
|
9
|
+
{ id: 'rhythm', name: 'Rhythm', icon: 'music' },
|
|
10
|
+
{ id: 'story', name: 'Short stories', icon: 'book' },
|
|
11
|
+
{ id: 'puzzle', name: 'Puzzles', icon: 'puzzle' },
|
|
12
|
+
{ id: 'quick-runs', name: 'Quick runs', icon: 'repeat' },
|
|
13
|
+
{ id: 'action', name: 'Action', icon: 'tense' },
|
|
14
|
+
{ id: 'physics', name: 'Smash and physics', icon: 'smashy' },
|
|
15
|
+
{ id: 'builder', name: 'Builders', icon: 'cozy' },
|
|
16
|
+
{ id: 'racing', name: 'Racing and sports', icon: 'pad' },
|
|
17
|
+
{ id: 'merge-idle', name: 'Merge and idle', icon: 'layers' },
|
|
18
|
+
];
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/* A game's listing and cover, checked before it goes live. Plain ESM with no Node imports. */
|
|
2
|
+
import { CATEGORIES } from '../Kinds/Kinds.js';
|
|
3
|
+
|
|
4
|
+
export const MOODS = ['Cozy', 'Smashy', 'Tense', 'Tidy'];
|
|
5
|
+
export const LIMITS = { id: 40, name: 40, hook: 90, why: 240, developer: 60, kinds: 3, coverBytes: 1024 * 1024, coverWidth: 640, controls: 4, keys: 3, does: 16 };
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* How a game is played, as pictures: the keys, mouse moves and touches staff can pick for a control.
|
|
9
|
+
* caps: drawn as keycaps (site/js/controls-ui.js); icon: drawn as a mouse or a hand. label: the words,
|
|
10
|
+
* for search engines and screen readers (controlWords).
|
|
11
|
+
*/
|
|
12
|
+
export const CONTROL_KEYS = {
|
|
13
|
+
arrows: { label: 'Arrow keys', caps: ['←', '↑', '↓', '→'] },
|
|
14
|
+
'left-right': { label: 'Left and right', caps: ['←', '→'] },
|
|
15
|
+
'up-down': { label: 'Up and down', caps: ['↑', '↓'] },
|
|
16
|
+
wasd: { label: 'W A S D', caps: ['W', 'A', 'S', 'D'] },
|
|
17
|
+
space: { label: 'Space', caps: ['Space'] },
|
|
18
|
+
enter: { label: 'Enter', caps: ['Enter'] },
|
|
19
|
+
shift: { label: 'Shift', caps: ['Shift'] },
|
|
20
|
+
z: { label: 'Z', caps: ['Z'] },
|
|
21
|
+
x: { label: 'X', caps: ['X'] },
|
|
22
|
+
mouse: { label: 'Move the mouse', icon: 'mouse' },
|
|
23
|
+
click: { label: 'Click', icon: 'mouse' },
|
|
24
|
+
'right-click': { label: 'Right-click', icon: 'mouse' },
|
|
25
|
+
drag: { label: 'Drag', icon: 'mouse' },
|
|
26
|
+
scroll: { label: 'Scroll', icon: 'mouse' },
|
|
27
|
+
tap: { label: 'Tap', icon: 'hand' },
|
|
28
|
+
swipe: { label: 'Swipe', icon: 'hand' },
|
|
29
|
+
hold: { label: 'Hold', icon: 'hand' },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/* The controls as plain words: "Arrow keys: move. Space or click: jump." */
|
|
33
|
+
export function controlWords(controls) {
|
|
34
|
+
return (Array.isArray(controls) ? controls : [])
|
|
35
|
+
.map((c) => `${c.keys.map((k, i) => (i ? CONTROL_KEYS[k].label.toLowerCase() : CONTROL_KEYS[k].label)).join(' or ')}: ${c.does.toLowerCase()}.`)
|
|
36
|
+
.join(' ');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/* Controls are optional; each problem names its row. */
|
|
40
|
+
function checkControls(input, problems) {
|
|
41
|
+
if (input === undefined || input === null) return [];
|
|
42
|
+
if (!Array.isArray(input) || input.length > LIMITS.controls) { problems.push(`Controls: at most ${LIMITS.controls}.`); return []; }
|
|
43
|
+
return input.map((c, i) => {
|
|
44
|
+
const row = `Controls, row ${i + 1}:`;
|
|
45
|
+
const keys = Array.isArray(c?.keys) ? c.keys.map((k) => String(k)) : [];
|
|
46
|
+
const does = tidy(c?.does);
|
|
47
|
+
if (!keys.length || keys.length > LIMITS.keys) problems.push(`${row} pick 1 to ${LIMITS.keys} keys.`);
|
|
48
|
+
const unknown = keys.find((k) => !Object.hasOwn(CONTROL_KEYS, k));
|
|
49
|
+
if (unknown) problems.push(`${row} "${unknown}" is not a key we show. Use one of the keys in the list.`);
|
|
50
|
+
else if (new Set(keys).size !== keys.length) problems.push(`${row} each key once.`);
|
|
51
|
+
if (!does) problems.push(`${row} say what it does, in a word or two.`);
|
|
52
|
+
else if (does.length > LIMITS.does) problems.push(`${row} what it does, in at most ${LIMITS.does} letters.`);
|
|
53
|
+
else if (!/^[A-Za-z][A-Za-z '-]*$/.test(does)) problems.push(`${row} plain words only.`);
|
|
54
|
+
return { keys, does };
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/* Addresses the site or the games domain uses, or that would mislead. */
|
|
58
|
+
export const RESERVED = ['www', 'api', 'app', 'admin', 'staff', 'mail', 'cdn', 'static', 'assets', 'covers', 'games', 'game', 'play',
|
|
59
|
+
'week', 'developers', 'signin', 'mine', 'submissions', 'stats', 'sdk', 'help', 'support', 'status', 'firstbyte', 'first-byte', 'login', 'account'];
|
|
60
|
+
|
|
61
|
+
const ID = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
|
|
62
|
+
const TEXT = { name: 'Name', hook: 'Hook', why: 'Why we picked it', developer: 'Developer' };
|
|
63
|
+
const tidy = (s) => (typeof s === 'string' ? s.replace(/\s+/g, ' ').trim() : '');
|
|
64
|
+
|
|
65
|
+
/* One line of text: filled in, within its limit, plain words, no web addresses. Answers it tidied. */
|
|
66
|
+
function checkText(label, value, max, problems) {
|
|
67
|
+
const text = tidy(value);
|
|
68
|
+
if (!text) problems.push(`${label}: fill it in.`);
|
|
69
|
+
else if (text.length > max) problems.push(`${label}: at most ${max} characters (it has ${text.length}).`);
|
|
70
|
+
else if (/[<>]/.test(text)) problems.push(`${label}: plain words only, no < or >.`);
|
|
71
|
+
else if (/https?:\/\/|www\.|\b[a-z0-9-]+\.(com|net|org|io|gg|games|app|dev)\b/i.test(text)) problems.push(`${label}: no web addresses.`);
|
|
72
|
+
return text;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* 1 to 3 kinds we know, each once. */
|
|
76
|
+
function checkKinds(input, kinds, problems) {
|
|
77
|
+
const out = Array.isArray(input) ? input.map(tidy) : [];
|
|
78
|
+
const known = new Set(kinds.map((k) => k.id));
|
|
79
|
+
if (!out.length) problems.push('Kinds: pick at least one.');
|
|
80
|
+
else if (out.length > LIMITS.kinds) problems.push(`Kinds: at most ${LIMITS.kinds}.`);
|
|
81
|
+
const unknown = out.filter((k) => !known.has(k));
|
|
82
|
+
if (unknown.length) problems.push(`Kinds: unknown ${unknown.map((k) => `"${k}"`).join(', ')}. The kinds are: ${kinds.map((k) => k.id).join(', ')}.`);
|
|
83
|
+
if (new Set(out).size !== out.length) problems.push('Kinds: each kind once.');
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/*
|
|
88
|
+
* The part of a listing its developer gives: the name, one line on what you do, how to play and the
|
|
89
|
+
* kinds. All optional: what is given is checked by the same rules as the listing, and what is left
|
|
90
|
+
* out our team fills in. It comes from the boxes on the site, or from game.json in the zip
|
|
91
|
+
* (readGameFile), which fills those boxes. The cover is cover.jpg beside it (checkCover).
|
|
92
|
+
*/
|
|
93
|
+
export const GAME_FILE = 'game.json';
|
|
94
|
+
export const COVER_FILE = 'cover.jpg';
|
|
95
|
+
const DETAILS = { name: 'Name', hook: 'Hook' };
|
|
96
|
+
/* Things a listing has that are not the developer's to set, and who sets them. */
|
|
97
|
+
const OURS = {
|
|
98
|
+
id: "Our team picks the game's address.", mood: 'Our team picks the mood.', week: 'Our team picks the week.',
|
|
99
|
+
why: 'Our team writes why we picked it.', developer: 'Your developer name comes from your account.', cover: `The cover is the file ${COVER_FILE}, next to index.html.`,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export function checkDetails(input, { kinds = CATEGORIES } = {}) {
|
|
103
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) return { details: null, problems: ['It must be one { } holding any of: name, hook, controls, kinds.'] };
|
|
104
|
+
const problems = [];
|
|
105
|
+
const out = {};
|
|
106
|
+
for (const key of Object.keys(input)) {
|
|
107
|
+
if (!['name', 'hook', 'controls', 'kinds'].includes(key)) problems.push(`"${key}" is not something a game sets. ${OURS[key] || 'It can have: name, hook, controls, kinds.'}`);
|
|
108
|
+
}
|
|
109
|
+
for (const [key, label] of Object.entries(DETAILS)) if (input[key] !== undefined) out[key] = checkText(label, input[key], LIMITS[key], problems);
|
|
110
|
+
if (input.kinds !== undefined) out.kinds = checkKinds(input.kinds, kinds, problems);
|
|
111
|
+
if (input.controls !== undefined) out.controls = checkControls(input.controls, problems);
|
|
112
|
+
return problems.length ? { details: null, problems } : { details: out, problems };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* game.json as text. A file that isn't JSON says so in plain words, with where it went wrong. */
|
|
116
|
+
export function readGameFile(text, opts) {
|
|
117
|
+
let data;
|
|
118
|
+
try { data = JSON.parse(text); } catch (e) { return { details: null, problems: [`It is not valid JSON: ${e.message}`] }; }
|
|
119
|
+
return checkDetails(data, opts);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function checkListing(input, { kinds = CATEGORIES } = {}) {
|
|
123
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) return { listing: null, problems: ['Fill in the listing.'] };
|
|
124
|
+
const problems = [];
|
|
125
|
+
const out = {};
|
|
126
|
+
|
|
127
|
+
out.id = tidy(input.id).toLowerCase();
|
|
128
|
+
if (!ID.test(out.id) || out.id.length > LIMITS.id) {
|
|
129
|
+
problems.push(`Address: use lowercase letters, numbers and single dashes, starting with a letter, up to ${LIMITS.id} characters (like "keep-cracker").`);
|
|
130
|
+
} else if (out.id.startsWith('review-')) {
|
|
131
|
+
problems.push('Address: "review-" addresses are kept for builds being reviewed. Pick another.');
|
|
132
|
+
} else if (RESERVED.includes(out.id)) {
|
|
133
|
+
problems.push(`Address: "${out.id}" is kept for the site. Pick another.`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
for (const [key, label] of Object.entries(TEXT)) out[key] = checkText(label, input[key], LIMITS[key], problems);
|
|
137
|
+
|
|
138
|
+
out.mood = tidy(input.mood);
|
|
139
|
+
if (!MOODS.includes(out.mood)) problems.push(`Mood: pick one of ${MOODS.join(', ')}.`);
|
|
140
|
+
|
|
141
|
+
out.kinds = checkKinds(input.kinds, kinds, problems);
|
|
142
|
+
|
|
143
|
+
out.week = typeof input.week === 'string' && /^\d+$/.test(input.week.trim()) ? Number(input.week) : input.week;
|
|
144
|
+
if (!Number.isInteger(out.week) || out.week < 1) problems.push('Week: a whole number from 1.');
|
|
145
|
+
|
|
146
|
+
out.controls = checkControls(input.controls, problems);
|
|
147
|
+
|
|
148
|
+
return problems.length ? { listing: null, problems } : { listing: out, problems };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* The width and height in a JPEG's start-of-frame marker. */
|
|
152
|
+
function jpegSize(b) {
|
|
153
|
+
if (!(b.length > 4 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff)) return null;
|
|
154
|
+
let i = 2;
|
|
155
|
+
while (i + 9 < b.length) {
|
|
156
|
+
if (b[i] !== 0xff) { i++; continue; }
|
|
157
|
+
const m = b[i + 1];
|
|
158
|
+
if (m === 0xd8 || m === 0x01 || (m >= 0xd0 && m <= 0xd7) || m === 0xff) { i += m === 0xff ? 1 : 2; continue; }
|
|
159
|
+
const len = (b[i + 2] << 8) | b[i + 3];
|
|
160
|
+
if (m >= 0xc0 && m <= 0xcf && m !== 0xc4 && m !== 0xc8 && m !== 0xcc) {
|
|
161
|
+
return { height: (b[i + 5] << 8) | b[i + 6], width: (b[i + 7] << 8) | b[i + 8] };
|
|
162
|
+
}
|
|
163
|
+
if (len < 2) return null;
|
|
164
|
+
i += 2 + len;
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function checkCover(bytes) {
|
|
170
|
+
const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(0);
|
|
171
|
+
const size = jpegSize(b);
|
|
172
|
+
if (!size) return { width: null, height: null, problems: ['Cover: it must be a JPEG picture (.jpg).'] };
|
|
173
|
+
const problems = [];
|
|
174
|
+
if (b.length > LIMITS.coverBytes) problems.push(`Cover: at most 1 MB (it is ${(b.length / 1024 / 1024).toFixed(1)} MB). Save it smaller.`);
|
|
175
|
+
if (size.width < LIMITS.coverWidth) problems.push(`Cover: at least ${LIMITS.coverWidth} pixels wide (it is ${size.width}).`);
|
|
176
|
+
if (Math.abs(size.width / size.height - 16 / 9) > (16 / 9) * 0.02) problems.push(`Cover: 16:9, like 1280 × 720 (it is ${size.width} × ${size.height}).`);
|
|
177
|
+
return { ...size, problems };
|
|
178
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* What we need, worked out from the catalog, so nobody writes the list by hand.
|
|
3
|
+
*
|
|
4
|
+
* For each kind of game: how many recent games it has, against how many it should have.
|
|
5
|
+
* recent = games from the last `weeks` weeks of the catalog
|
|
6
|
+
* share = every kind starts equal; with play data, a kind whose games are played more than
|
|
7
|
+
* average gets a bigger share (players want more of it)
|
|
8
|
+
* target = the kind's share of the recent games, never fewer than `least`
|
|
9
|
+
* ratio = count / target
|
|
10
|
+
* level = 'Most wanted' (none), 'Wanted' (under target), 'Enough', or 'Full' (twice the target)
|
|
11
|
+
*
|
|
12
|
+
* Plain ESM with no Node imports: the site runs this same file in the browser.
|
|
13
|
+
*/
|
|
14
|
+
import { CATEGORIES } from '../Kinds/Kinds.js';
|
|
15
|
+
|
|
16
|
+
export const LEVELS = ['Most wanted', 'Wanted', 'Enough', 'Full'];
|
|
17
|
+
|
|
18
|
+
export function needs(games, opts = {}) {
|
|
19
|
+
const categories = opts.categories || CATEGORIES;
|
|
20
|
+
const weeks = opts.weeks ?? 8;
|
|
21
|
+
const least = opts.least ?? 2;
|
|
22
|
+
const minutes = opts.minutes || null;
|
|
23
|
+
|
|
24
|
+
const latest = Math.max(0, ...games.map((g) => g.week || 0));
|
|
25
|
+
const recent = games.filter((g) => (g.week || 0) > latest - weeks);
|
|
26
|
+
const inKind = (c) => recent.filter((g) => (g.kinds || []).includes(c.id));
|
|
27
|
+
|
|
28
|
+
const weight = weights(categories, inKind, minutes);
|
|
29
|
+
const totalWeight = categories.reduce((s, c) => s + weight.get(c.id), 0);
|
|
30
|
+
|
|
31
|
+
const rows = categories.map((c, order) => {
|
|
32
|
+
const count = inKind(c).length;
|
|
33
|
+
const target = Math.max(least, Math.round(recent.length * weight.get(c.id) / totalWeight));
|
|
34
|
+
const ratio = count / target;
|
|
35
|
+
const level = count === 0 ? LEVELS[0] : ratio < 1 ? LEVELS[1] : ratio >= 2 ? LEVELS[3] : LEVELS[2];
|
|
36
|
+
return { id: c.id, name: c.name, icon: c.icon, level, count, target, ratio: round(ratio), weight: round(weight.get(c.id)), order };
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
rows.sort((a, b) => a.ratio - b.ratio || b.weight - a.weight || a.order - b.order);
|
|
40
|
+
return rows.map(({ order, ...row }) => row);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* With play minutes per game id, a kind played more than average weighs more (at most 3 times). */
|
|
44
|
+
function weights(categories, inKind, minutes) {
|
|
45
|
+
const w = new Map(categories.map((c) => [c.id, 1]));
|
|
46
|
+
if (!minutes) return w;
|
|
47
|
+
const perGame = (c) => {
|
|
48
|
+
const games = inKind(c);
|
|
49
|
+
return games.length ? games.reduce((s, g) => s + (minutes[g.id] || 0), 0) / games.length : null;
|
|
50
|
+
};
|
|
51
|
+
const known = categories.map(perGame).filter((m) => m !== null);
|
|
52
|
+
const average = known.length ? known.reduce((s, m) => s + m, 0) / known.length : 0;
|
|
53
|
+
if (!average) return w;
|
|
54
|
+
for (const c of categories) {
|
|
55
|
+
const m = perGame(c);
|
|
56
|
+
if (m !== null) w.set(c.id, Math.min(3, Math.max(0.5, m / average)));
|
|
57
|
+
}
|
|
58
|
+
return w;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* The few to show: everything we lack first, then at most one we have plenty of. */
|
|
62
|
+
export function shortlist(rows, n = 6) {
|
|
63
|
+
const lacking = rows.filter((r) => r.level === LEVELS[0] || r.level === LEVELS[1]);
|
|
64
|
+
const full = rows.filter((r) => r.level === LEVELS[3]).slice(-1);
|
|
65
|
+
return [...lacking.slice(0, n - full.length), ...full];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const round = (x) => Math.round(x * 100) / 100;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/* Runs every automatic rule on a build and returns one report. */
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
|
+
import { loadGame, findRoot } from '../../load/Build/Build.js';
|
|
4
|
+
import { checkFiles } from '../FileChecks/FileChecks.js';
|
|
5
|
+
import { checkPlay, skipPlay } from '../PlayChecks/PlayChecks.js';
|
|
6
|
+
import { RULES } from '../../rules/Rulebook/Rulebook.js';
|
|
7
|
+
import { ZipError } from '../../load/Zip/Zip.js';
|
|
8
|
+
|
|
9
|
+
export async function check(path, opts = {}) {
|
|
10
|
+
let game;
|
|
11
|
+
try {
|
|
12
|
+
game = loadGame(path);
|
|
13
|
+
} catch (e) {
|
|
14
|
+
if (e.code === 'ENOENT') return broken(path, `There is nothing at "${path}".`);
|
|
15
|
+
if (e instanceof ZipError) return broken(path, e.message);
|
|
16
|
+
throw e;
|
|
17
|
+
}
|
|
18
|
+
return checkGame(game, opts);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function checkGame(game, opts = {}) {
|
|
22
|
+
const root = findRoot(game.files);
|
|
23
|
+
const results = checkFiles(game.files, root);
|
|
24
|
+
if (opts.play === false) results.push(...skipPlay('Skipped (--no-play).'));
|
|
25
|
+
else if (root === null) results.push(...skipPlay('Fix F1 first: the checker cannot find index.html to play.'));
|
|
26
|
+
else results.push(...await checkPlay(game.files, root, opts));
|
|
27
|
+
return report(game.path || 'upload.zip', game.kind, results);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function broken(path, why) {
|
|
31
|
+
return { name: basename(path), kind: null, ok: false, error: why, results: [], counts: { fail: 1 } };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function report(path, kind, results) {
|
|
35
|
+
const byId = new Map(results.map((r) => [r.id, r]));
|
|
36
|
+
const full = RULES.map((rule) => {
|
|
37
|
+
const r = byId.get(rule.id) || (rule.check === 'person' ? { status: 'person' } : { status: 'skip', detail: ['Not checked.'] });
|
|
38
|
+
return { id: rule.id, key: rule.key, check: rule.check, title: rule.title, status: r.status, detail: r.detail || [], fix: r.status === 'fail' || r.status === 'warn' ? rule.fix : undefined };
|
|
39
|
+
});
|
|
40
|
+
link(full);
|
|
41
|
+
const counts = {};
|
|
42
|
+
for (const r of full) counts[r.status] = (counts[r.status] || 0) + 1;
|
|
43
|
+
return { name: basename(path), kind, ok: !counts.fail, results: full, counts };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* One mistake often shows up as several failures: a script the game could not load (P1) also leaves
|
|
48
|
+
* the screen blank (P2), deaf to keys (P4) and silent (P5). Each such failure gets `after`, the rule
|
|
49
|
+
* to fix first, and its fix says so, so a developer starts in the right place. "Probably", because the
|
|
50
|
+
* checker has not proved it: the rule's own fix stays, for when the failure outlives the first one.
|
|
51
|
+
*/
|
|
52
|
+
const AFTER_LOAD = ['P2', 'P4', 'P5', 'P8'];
|
|
53
|
+
function link(full) {
|
|
54
|
+
const by = new Map(full.map((r) => [r.id, r]));
|
|
55
|
+
const failed = (id) => by.get(id)?.status === 'fail';
|
|
56
|
+
if (failed('P1')) {
|
|
57
|
+
for (const id of AFTER_LOAD) if (failed(id)) by.get(id).after = 'P1';
|
|
58
|
+
/* Every file it missed was asked for with a path starting "/": that is F2's mistake, seen again. */
|
|
59
|
+
const p1 = by.get('P1');
|
|
60
|
+
if (failed('F2') && p1.detail.length && p1.detail.every((d) => d.startsWith('Missing file:') && d.includes('starts with "/"'))) p1.after = 'F2';
|
|
61
|
+
}
|
|
62
|
+
for (const r of full) if (r.after) r.fix = `Probably because of ${r.after}: fix that first, then check again. If it is still here: ${r.fix}`;
|
|
63
|
+
}
|