@homie-rocks/studio 0.2.0
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/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +40 -0
- package/bin/homie-studio.mjs +166 -0
- package/lib/build.mjs +86 -0
- package/lib/check.mjs +123 -0
- package/lib/cloudflare.mjs +155 -0
- package/lib/directory.mjs +19 -0
- package/lib/scaffold.mjs +254 -0
- package/lib/studio.mjs +102 -0
- package/lib/version.mjs +17 -0
- package/netplay/NETPLAY.md +575 -0
- package/netplay/netplay.ts +1309 -0
- package/package.json +44 -0
- package/starters/gem-rush/game.json +9 -0
- package/starters/gem-rush/index.html +17 -0
- package/starters/gem-rush/src/main.ts +719 -0
- package/worker/index.mjs +349 -0
- package/worker/pages.mjs +205 -0
- package/worker/room.mjs +659 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @homie-rocks/studio
|
|
2
|
+
|
|
3
|
+
A game studio on its own Cloudflare. One repository (`AGENTS.md`, `games/`, `music/`,
|
|
4
|
+
`videos/`, `posts/`, `site/`), multiplayer games on the Homie netplay contract (every
|
|
5
|
+
browser renders, strangers meet in public rooms, bots fill seats, rounds restart), and a
|
|
6
|
+
site Worker with Table/Lobby Durable Objects, D1 and R2 that the studio deploys to its
|
|
7
|
+
own Cloudflare account with Wrangler. The homie.rocks directory lists the games.
|
|
8
|
+
|
|
9
|
+
Most people never run this by hand: the Homie plugin for Claude Code and Codex does,
|
|
10
|
+
and the person approves Cloudflare once in their browser.
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npx -y --package=https://homie.rocks/npm/homie-studio-0.2.0.tgz homie-studio new ./night-owls --name "Night Owls"
|
|
14
|
+
cd night-owls && npm install
|
|
15
|
+
npx homie-studio game new crown-thief --from gem-rush --name "Crown Thief"
|
|
16
|
+
npx homie-studio dev # the whole site locally
|
|
17
|
+
npx homie-studio check crown-thief --url http://127.0.0.1:8787
|
|
18
|
+
npx homie-studio deploy # the studio's own Cloudflare
|
|
19
|
+
npx homie-studio publish # the homie.rocks directory
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
| Path | What it is |
|
|
23
|
+
| --- | --- |
|
|
24
|
+
| `bin/homie-studio.mjs` | The CLI. |
|
|
25
|
+
| `lib/scaffold.mjs` | `new`: the studio monorepo, only into a new or empty folder, every file listed. |
|
|
26
|
+
| `lib/build.mjs` | `build`: games bundled with esbuild into `site/dist`, `games.json`, each game's shared `source.json`. |
|
|
27
|
+
| `lib/cloudflare.mjs` | `deploy`: Wrangler, D1, R2, migrations, the directory claim; refuses resources it did not create. |
|
|
28
|
+
| `lib/check.mjs` | `check`: two fresh Chrome processes (computer + phone) must share a room and finish a round. |
|
|
29
|
+
| `worker/index.mjs` | The site Worker and the `Table` (netplay relay, `room.mjs`) and `Lobby` Durable Objects. |
|
|
30
|
+
| `netplay/` | The netplay contract (`NETPLAY.md`) and its game helper (`@homie-rocks/studio/netplay`). |
|
|
31
|
+
| `starters/gem-rush/` | The reference multiplayer starter. |
|
|
32
|
+
|
|
33
|
+
Versions are immutable: a published version never changes, so a studio that pinned one
|
|
34
|
+
never changes by surprise. A change ships as a new `version`. Until the package is on the
|
|
35
|
+
npm registry, each version's tarball is served at
|
|
36
|
+
`https://homie.rocks/npm/homie-studio-<version>.tgz`.
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
Apache-2.0. See `LICENSE` and `NOTICE`.
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* homie-studio — make a game studio, make its games, put its site on the
|
|
4
|
+
* studio's own Cloudflare, and list its games in the homie.rocks directory.
|
|
5
|
+
*
|
|
6
|
+
* homie-studio new <folder> --name "<Studio Name>" [--homie <directory url>] [--no-install]
|
|
7
|
+
* homie-studio starters
|
|
8
|
+
* homie-studio game new <id> [--from gem-rush] [--name "<Game Name>"]
|
|
9
|
+
* homie-studio game remix <source.json url> --id <new id>
|
|
10
|
+
* homie-studio games
|
|
11
|
+
* homie-studio build [<id>]
|
|
12
|
+
* homie-studio dev [--port 8787]
|
|
13
|
+
* homie-studio check <id> [--url <site>] [--shots <dir>]
|
|
14
|
+
* homie-studio deploy
|
|
15
|
+
* homie-studio publish
|
|
16
|
+
* homie-studio media put <file> [--as <key>]
|
|
17
|
+
* homie-studio status
|
|
18
|
+
*
|
|
19
|
+
* Every command prints a few lines for a person; --json prints the result.
|
|
20
|
+
*/
|
|
21
|
+
import { spawn } from 'node:child_process';
|
|
22
|
+
import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
23
|
+
import { basename, join, relative, resolve } from 'node:path';
|
|
24
|
+
import { build } from '../lib/build.mjs';
|
|
25
|
+
import { check } from '../lib/check.mjs';
|
|
26
|
+
import { deploy, whoami, wranglerBin } from '../lib/cloudflare.mjs';
|
|
27
|
+
import { publish } from '../lib/directory.mjs';
|
|
28
|
+
import { newStudio } from '../lib/scaffold.mjs';
|
|
29
|
+
import { listGames, newGame, readStudio, remixGame, requireStudio, starters, writeStudio } from '../lib/studio.mjs';
|
|
30
|
+
import { STUDIO_VERSION } from '../lib/version.mjs';
|
|
31
|
+
|
|
32
|
+
const argv = process.argv.slice(2);
|
|
33
|
+
const flags = new Map();
|
|
34
|
+
const positional = [];
|
|
35
|
+
for (let i = 0; i < argv.length; i++) {
|
|
36
|
+
const a = argv[i];
|
|
37
|
+
if (a.startsWith('--')) {
|
|
38
|
+
const [k, v] = a.slice(2).split(/=(.*)/s, 2);
|
|
39
|
+
if (v !== undefined) flags.set(k, v);
|
|
40
|
+
else if (argv[i + 1] !== undefined && !argv[i + 1].startsWith('--') && !['json', 'yes', 'detach', 'no-install'].includes(k)) flags.set(k, argv[++i]);
|
|
41
|
+
else flags.set(k, true);
|
|
42
|
+
} else positional.push(a);
|
|
43
|
+
}
|
|
44
|
+
const asJson = flags.has('json');
|
|
45
|
+
const log = asJson ? () => {} : (line) => process.stderr.write(`${line}\n`);
|
|
46
|
+
|
|
47
|
+
function print(result) {
|
|
48
|
+
if (asJson) { process.stdout.write(`${JSON.stringify(result, null, 2)}\n`); return; }
|
|
49
|
+
if (result.ok === false) { process.stdout.write(`homie-studio: ${result.why ?? 'failed'}\n`); return; }
|
|
50
|
+
const lines = [];
|
|
51
|
+
switch (result.command) {
|
|
52
|
+
case 'new':
|
|
53
|
+
lines.push(`${result.name} is a studio now: ${result.dir}`, '', 'Wrote:', ...result.wrote.map((f) => ` ${f}`), '', `Dependencies: ${result.installed}`, '', 'Next:', ...result.next.map((n) => ` ${n}`));
|
|
54
|
+
break;
|
|
55
|
+
case 'game remix':
|
|
56
|
+
lines.push(`games/${result.id} is a remix of ${result.from} (${result.files.length} files). Make it yours in games/${result.id}/src/, then: npx homie-studio dev`);
|
|
57
|
+
break;
|
|
58
|
+
case 'game new':
|
|
59
|
+
lines.push(`games/${result.id} is a new game from the ${result.from} starter. Change it in games/${result.id}/src/, then: npx homie-studio dev`);
|
|
60
|
+
break;
|
|
61
|
+
case 'build':
|
|
62
|
+
lines.push(`Built ${result.games.map((g) => `${g.id} (${Math.round(g.bytes / 1024)} KB)`).join(', ') || 'nothing'} into ${relative(process.cwd(), result.dist) || result.dist}`);
|
|
63
|
+
break;
|
|
64
|
+
case 'deploy':
|
|
65
|
+
lines.push(`Live: ${result.url}`, ...result.games.map((g) => ` ${g.id}: ${g.play}`), '', `Cloudflare: Worker ${result.worker}, D1 ${result.d1}${result.r2 ? `, R2 ${result.r2}` : ', no R2 (not enabled on this account)'}`,
|
|
66
|
+
result.claim ? 'Directory claim stored: list the games with the Homie MCP tool studio_publish, or: npx homie-studio publish' : 'No directory claim yet (the directory did not answer); publish will try again.');
|
|
67
|
+
break;
|
|
68
|
+
case 'publish':
|
|
69
|
+
lines.push(`Listed in the directory: ${result.studioPage ?? result.directory}`, ...(result.games ?? []).map((g) => ` ${g.name}: ${g.play}`));
|
|
70
|
+
break;
|
|
71
|
+
case 'check':
|
|
72
|
+
lines.push(`PASS: two fresh browsers in room ${result.room} finished round ${result.round.n} (${result.round.humans} humans, ${result.round.bots} bots) in ${Math.round(result.totalMs / 1000)} s.`,
|
|
73
|
+
...result.seats.map((s) => ` ${s.browser}: seat ${s.seat} (${s.role}), seated in ${(s.seatedMs / 1000).toFixed(1)} s`));
|
|
74
|
+
break;
|
|
75
|
+
default:
|
|
76
|
+
lines.push(JSON.stringify(result, null, 2));
|
|
77
|
+
}
|
|
78
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function main() {
|
|
82
|
+
const [cmd, sub] = positional;
|
|
83
|
+
if (!cmd || cmd === 'help' || flags.has('help')) {
|
|
84
|
+
const text = readFileSync(new URL(import.meta.url), 'utf8').split('*/')[0].split('\n').slice(1).map((l) => l.replace(/^ \* ?/, '')).join('\n');
|
|
85
|
+
process.stdout.write(`homie-studio ${STUDIO_VERSION}\n${text}\n`);
|
|
86
|
+
return { ok: true, command: 'help' };
|
|
87
|
+
}
|
|
88
|
+
if (cmd === 'version' || flags.has('version')) return { ok: true, command: 'version', version: STUDIO_VERSION };
|
|
89
|
+
if (cmd === 'new') return newStudio(positional[1], { name: flags.get('name'), homie: flags.get('homie'), slug: flags.get('slug'), install: !flags.has('no-install') });
|
|
90
|
+
if (cmd === 'starters') return { ok: true, command: 'starters', starters: starters() };
|
|
91
|
+
|
|
92
|
+
const root = requireStudio();
|
|
93
|
+
if (cmd === 'game' && sub === 'new') return newGame(root, positional[2], { from: flags.get('from') ?? 'gem-rush', name: flags.get('name') });
|
|
94
|
+
if (cmd === 'game' && sub === 'remix') return remixGame(root, positional[2], flags.get('id'), { name: flags.get('name') });
|
|
95
|
+
if (cmd === 'games') return { ok: true, command: 'games', games: listGames(root).map(({ dir, ...g }) => ({ ...g, dir: relative(root, dir) })) };
|
|
96
|
+
if (cmd === 'build') return build(root, { only: positional[1] ?? null, log });
|
|
97
|
+
if (cmd === 'status') {
|
|
98
|
+
const studio = readStudio(root);
|
|
99
|
+
return { ok: true, command: 'status', root, studio, games: listGames(root).map((g) => g.id), cloudflareSignedIn: Boolean(wranglerBin(root) && whoami(root)) };
|
|
100
|
+
}
|
|
101
|
+
if (cmd === 'dev') return dev(root);
|
|
102
|
+
if (cmd === 'check') {
|
|
103
|
+
const game = positional[1] ?? listGames(root)[0]?.id;
|
|
104
|
+
const url = flags.get('url') ?? readStudio(root).cloudflare?.url;
|
|
105
|
+
if (!url) return { ok: false, command: 'check', why: 'give --url (the local dev address or the live site)' };
|
|
106
|
+
return check({ url, game, shots: flags.get('shots') ? resolve(flags.get('shots')) : null, log });
|
|
107
|
+
}
|
|
108
|
+
if (cmd === 'deploy') return deploy(root, { log, homie: flags.get('homie') });
|
|
109
|
+
if (cmd === 'publish') return publish(root, { homie: flags.get('homie'), site: flags.get('site') });
|
|
110
|
+
if (cmd === 'media' && sub === 'put') return mediaPut(root, positional[2], flags.get('as'));
|
|
111
|
+
return { ok: false, command: cmd, why: `unknown command "${[cmd, sub].filter(Boolean).join(' ')}" (homie-studio help)` };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The whole site locally: pages, rooms (room.mjs in a local Durable Object), D1. */
|
|
115
|
+
async function dev(root) {
|
|
116
|
+
const port = String(flags.get('port') ?? 8787);
|
|
117
|
+
const b = await build(root, { log });
|
|
118
|
+
if (!b.catalogue.length) return { ok: false, command: 'dev', why: 'no games yet: npx homie-studio game new <id> --from gem-rush' };
|
|
119
|
+
const bin = wranglerBin(root);
|
|
120
|
+
if (!bin) return { ok: false, command: 'dev', why: 'run npm install in the studio first' };
|
|
121
|
+
const studio = readStudio(root);
|
|
122
|
+
const env = { ...process.env, WRANGLER_SEND_METRICS: 'false', CI: '1' };
|
|
123
|
+
await new Promise((done) => {
|
|
124
|
+
const m = spawn(bin, ['d1', 'migrations', 'apply', studio.cloudflare.d1, '--local'], { cwd: join(root, 'site'), env, stdio: ['ignore', 'ignore', 'inherit'] });
|
|
125
|
+
m.on('close', done);
|
|
126
|
+
});
|
|
127
|
+
log(`Local site: http://127.0.0.1:${port}/ (each game: http://127.0.0.1:${port}/<id>/play — open it in two browsers)`);
|
|
128
|
+
const child = spawn(bin, ['dev', '--local', '--ip', '127.0.0.1', '--port', port], { cwd: join(root, 'site'), env, stdio: 'inherit' });
|
|
129
|
+
await new Promise((done) => child.on('close', done));
|
|
130
|
+
return { ok: true, command: 'dev', stopped: true };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** A big file into the studio's R2, and its line in music/ or videos/ manifest.json. */
|
|
134
|
+
function mediaPut(root, file, as) {
|
|
135
|
+
if (!file || !existsSync(file) || !statSync(file).isFile()) return { ok: false, command: 'media put', why: 'usage: homie-studio media put <file> [--as <key>]' };
|
|
136
|
+
const studio = readStudio(root);
|
|
137
|
+
const r2 = studio.cloudflare?.r2;
|
|
138
|
+
if (!r2 || !(studio.cloudflare?.created ?? []).includes(`r2:${r2}`)) return { ok: false, command: 'media put', why: 'this studio has no R2 bucket yet (deploy first; R2 needs to be enabled on the Cloudflare account)' };
|
|
139
|
+
const rel = relative(root, resolve(file));
|
|
140
|
+
const folder = rel.startsWith('videos/') ? 'videos' : 'music';
|
|
141
|
+
const key = as ?? `${folder}/${basename(file)}`;
|
|
142
|
+
const bin = wranglerBin(root);
|
|
143
|
+
return new Promise((done) => {
|
|
144
|
+
const p = spawn(bin, ['r2', 'object', 'put', `${r2}/${key}`, '--file', resolve(file), '--remote'], { cwd: join(root, 'site'), env: { ...process.env, CI: '1' }, stdio: ['ignore', 'ignore', 'inherit'] });
|
|
145
|
+
p.on('close', (code) => {
|
|
146
|
+
if (code !== 0) return done({ ok: false, command: 'media put', why: 'wrangler r2 object put failed' });
|
|
147
|
+
const manifest = join(root, folder, 'manifest.json');
|
|
148
|
+
const m = existsSync(manifest) ? JSON.parse(readFileSync(manifest, 'utf8')) : { items: [] };
|
|
149
|
+
m.items = [...(m.items ?? []).filter((i) => i.key !== key), { key, file: rel, bytes: statSync(file).size, url: studio.cloudflare?.url ? `${studio.cloudflare.url}/media/${key}` : null, at: new Date().toISOString() }];
|
|
150
|
+
writeFileSync(manifest, `${JSON.stringify(m, null, 2)}\n`);
|
|
151
|
+
writeStudio(root, studio);
|
|
152
|
+
done({ ok: true, command: 'media put', key, manifest: relative(root, manifest) });
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
const result = await main();
|
|
159
|
+
if (result && result.command !== 'help') print(result);
|
|
160
|
+
if (result?.ok === false) process.exitCode = 1;
|
|
161
|
+
// Chrome's pipes can outlive browser.close(); a finished check must not hang its caller.
|
|
162
|
+
if (result?.command === 'check') process.exit(process.exitCode ?? 0);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
print({ ok: false, why: error instanceof Error ? error.message : String(error) });
|
|
165
|
+
process.exitCode = 1;
|
|
166
|
+
}
|
package/lib/build.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `homie-studio build` — every game in games/ bundled into site/dist, plus the
|
|
3
|
+
* catalogue the Worker serves (games.json).
|
|
4
|
+
*
|
|
5
|
+
* site/dist/games/<id>/index.html the game's page (the Worker adds HOMIE_NET)
|
|
6
|
+
* site/dist/games/<id>/assets/main.js its bundle (esbuild; @homie-rocks/studio/netplay inlined)
|
|
7
|
+
* site/dist/games/<id>/... everything in games/<id>/public/
|
|
8
|
+
* site/dist/games.json { studio, games[] } from studio.json and game.json files
|
|
9
|
+
*/
|
|
10
|
+
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { createRequire } from 'node:module';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { listGames, readStudio } from './studio.mjs';
|
|
14
|
+
import { STUDIO_VERSION } from './version.mjs';
|
|
15
|
+
|
|
16
|
+
const SOURCE_SKIP = new Set(['node_modules', 'dist', '.git', '.wrangler']);
|
|
17
|
+
const TEXT = /\.(ts|tsx|js|mjs|jsx|json|html|css|md|txt|svg|glsl|wgsl|frag|vert)$/i;
|
|
18
|
+
/** Text files of a game folder, capped (2 MB total, 512 KB each): what `homie-studio game remix` takes. */
|
|
19
|
+
export function sourceOf(dir, id) {
|
|
20
|
+
const files = {};
|
|
21
|
+
let total = 0;
|
|
22
|
+
const walk = (rel) => {
|
|
23
|
+
for (const entry of readdirSync(join(dir, rel), { withFileTypes: true })) {
|
|
24
|
+
if (entry.name.startsWith('.') || SOURCE_SKIP.has(entry.name)) continue;
|
|
25
|
+
const path = rel ? `${rel}/${entry.name}` : entry.name;
|
|
26
|
+
if (entry.isDirectory()) walk(path);
|
|
27
|
+
else if (TEXT.test(entry.name)) {
|
|
28
|
+
const text = readFileSync(join(dir, path), 'utf8');
|
|
29
|
+
if (text.length > 512 * 1024 || total + text.length > 2 * 1024 * 1024) continue;
|
|
30
|
+
files[path] = text;
|
|
31
|
+
total += text.length;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
walk('');
|
|
36
|
+
return { v: 1, kind: 'homie-game-source', id, files };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function build(root, { only = null, log = () => {} } = {}) {
|
|
40
|
+
const require = createRequire(join(root, 'package.json'));
|
|
41
|
+
let esbuild;
|
|
42
|
+
try { esbuild = require('esbuild'); } catch { esbuild = await import('esbuild'); }
|
|
43
|
+
const studio = readStudio(root);
|
|
44
|
+
const dist = join(root, 'site', 'dist');
|
|
45
|
+
const games = listGames(root).filter((g) => !only || g.id === only);
|
|
46
|
+
if (only && !games.length) throw new Error(`no game "${only}" in games/`);
|
|
47
|
+
if (!only) rmSync(dist, { recursive: true, force: true });
|
|
48
|
+
mkdirSync(dist, { recursive: true });
|
|
49
|
+
const built = [];
|
|
50
|
+
for (const g of games) {
|
|
51
|
+
const out = join(dist, 'games', g.id);
|
|
52
|
+
rmSync(out, { recursive: true, force: true });
|
|
53
|
+
mkdirSync(join(out, 'assets'), { recursive: true });
|
|
54
|
+
const entry = join(g.dir, g.entry ?? 'src/main.ts');
|
|
55
|
+
if (!existsSync(entry)) throw new Error(`games/${g.id}: entry ${g.entry ?? 'src/main.ts'} not found`);
|
|
56
|
+
const started = Date.now();
|
|
57
|
+
const result = await esbuild.build({
|
|
58
|
+
entryPoints: [entry], bundle: true, format: 'esm', target: 'es2022', minify: true, sourcemap: false,
|
|
59
|
+
outfile: join(out, 'assets', 'main.js'), absWorkingDir: root, logLevel: 'silent', metafile: true,
|
|
60
|
+
loader: { '.png': 'file', '.jpg': 'file', '.webp': 'file', '.mp3': 'file', '.ogg': 'file', '.wav': 'file', '.glb': 'file', '.svg': 'file' },
|
|
61
|
+
assetNames: '[name]-[hash]',
|
|
62
|
+
}).catch((error) => {
|
|
63
|
+
const first = error.errors?.[0];
|
|
64
|
+
throw new Error(`games/${g.id} did not build: ${first ? `${first.text}${first.location ? ` (${first.location.file}:${first.location.line})` : ''}` : error.message}`);
|
|
65
|
+
});
|
|
66
|
+
const html = join(g.dir, 'index.html');
|
|
67
|
+
if (!existsSync(html)) throw new Error(`games/${g.id}/index.html is missing`);
|
|
68
|
+
writeFileSync(join(out, 'index.html'), readFileSync(html, 'utf8'));
|
|
69
|
+
if (existsSync(join(g.dir, 'public'))) cpSync(join(g.dir, 'public'), out, { recursive: true });
|
|
70
|
+
// The game's own source, for other studios to remix (game.json "share": { "source": false } keeps it private).
|
|
71
|
+
if (g.share?.source !== false) writeFileSync(join(out, 'source.json'), `${JSON.stringify(sourceOf(g.dir, g.id))}\n`);
|
|
72
|
+
const bytes = statSync(join(out, 'assets', 'main.js')).size;
|
|
73
|
+
built.push({ id: g.id, name: g.name, bytes, ms: Date.now() - started, warnings: result.warnings.length });
|
|
74
|
+
log(`built ${g.id} (${Math.round(bytes / 1024)} KB)`);
|
|
75
|
+
}
|
|
76
|
+
const all = listGames(root);
|
|
77
|
+
const catalogue = {
|
|
78
|
+
studio: { name: studio.name, slug: studio.slug, version: STUDIO_VERSION },
|
|
79
|
+
games: all.filter((g) => existsSync(join(dist, 'games', g.id, 'index.html'))).map((g) => ({
|
|
80
|
+
id: g.id, name: g.name ?? g.id, blurb: g.blurb ?? '', players: g.players ?? { min: 1, max: 8 },
|
|
81
|
+
roundSeconds: g.roundSeconds ?? null, movement: g.netplay?.movement ?? null, cover: g.cover ?? null,
|
|
82
|
+
})),
|
|
83
|
+
};
|
|
84
|
+
writeFileSync(join(dist, 'games.json'), `${JSON.stringify(catalogue, null, 2)}\n`);
|
|
85
|
+
return { ok: true, command: 'build', dist, games: built, catalogue: catalogue.games.map((g) => g.id) };
|
|
86
|
+
}
|
package/lib/check.mjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `homie-studio check <game> --url <site>` — the promise, measured: two fresh
|
|
3
|
+
* browsers (separate Chrome processes and profiles, one a computer and one a
|
|
4
|
+
* phone) press Play, land in the SAME public room, play, and both see the round
|
|
5
|
+
* finish. Exit 0 only when all of that happened. Uses this Mac's Chrome through
|
|
6
|
+
* puppeteer-core, on the GPU (a software renderer fakes a stuck game).
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
|
|
9
|
+
import { tmpdir } from 'node:os';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
|
|
12
|
+
const CHROMES = [
|
|
13
|
+
process.env.CHROME_PATH,
|
|
14
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
15
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
16
|
+
'/usr/bin/google-chrome', '/usr/bin/chromium', '/usr/bin/chromium-browser',
|
|
17
|
+
].filter(Boolean);
|
|
18
|
+
|
|
19
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
20
|
+
|
|
21
|
+
export async function check({ url, game, roundTimeoutMs = 150_000, shots = null, userAgentTag = 'homie-studio-check', log = () => {} }) {
|
|
22
|
+
if (!url || !game) throw new Error('usage: homie-studio check <game> --url <site url>');
|
|
23
|
+
const chrome = CHROMES.find((p) => existsSync(p));
|
|
24
|
+
if (!chrome) return { ok: false, command: 'check', why: 'no Chrome found (set CHROME_PATH)' };
|
|
25
|
+
let puppeteer;
|
|
26
|
+
try { puppeteer = (await import('puppeteer-core')).default; } catch { return { ok: false, command: 'check', why: 'puppeteer-core is not installed (it comes with @homie-rocks/studio; run npm install)' }; }
|
|
27
|
+
const base = String(url).replace(/\/+$/, '');
|
|
28
|
+
const play = `${base}/${game}/play`;
|
|
29
|
+
if (shots) mkdirSync(shots, { recursive: true });
|
|
30
|
+
const profiles = [];
|
|
31
|
+
const browsers = [];
|
|
32
|
+
const started = Date.now();
|
|
33
|
+
const kinds = [
|
|
34
|
+
{ name: 'computer', viewport: { width: 1280, height: 800 } },
|
|
35
|
+
{ name: 'phone', viewport: { width: 390, height: 844, isMobile: true, hasTouch: true, deviceScaleFactor: 2 } },
|
|
36
|
+
];
|
|
37
|
+
const players = [];
|
|
38
|
+
try {
|
|
39
|
+
for (const kind of kinds) {
|
|
40
|
+
const profile = mkdtempSync(join(tmpdir(), 'homie-studio-check-'));
|
|
41
|
+
profiles.push(profile);
|
|
42
|
+
const browser = await puppeteer.launch({
|
|
43
|
+
executablePath: chrome, headless: true, userDataDir: profile,
|
|
44
|
+
args: ['--use-angle=metal', '--enable-gpu-rasterization', '--ignore-gpu-blocklist', '--autoplay-policy=no-user-gesture-required', '--no-first-run', '--no-default-browser-check'],
|
|
45
|
+
});
|
|
46
|
+
browsers.push(browser);
|
|
47
|
+
const page = await browser.newPage();
|
|
48
|
+
await page.setViewport(kind.viewport);
|
|
49
|
+
const ua = await browser.userAgent();
|
|
50
|
+
await page.setUserAgent(`${ua} ${userAgentTag}`);
|
|
51
|
+
const t0 = Date.now();
|
|
52
|
+
await page.goto(play, { waitUntil: 'domcontentloaded', timeout: 60_000 });
|
|
53
|
+
players.push({ kind: kind.name, page, t0 });
|
|
54
|
+
log(`${kind.name} opened ${play}`);
|
|
55
|
+
}
|
|
56
|
+
// Seated: the shell knows its room and the game reported a seat.
|
|
57
|
+
const seated = await Promise.all(players.map(async (p) => {
|
|
58
|
+
const deadline = Date.now() + 45_000;
|
|
59
|
+
while (Date.now() < deadline) {
|
|
60
|
+
const s = await p.page.evaluate(() => { const s = window.__shell; return s ? { room: s.room, seat: s.seat, role: s.stats?.role ?? null, players: s.facts?.counts?.players ?? null } : null; }).catch(() => null);
|
|
61
|
+
if (s?.room && s.seat !== null && s.seat !== undefined && s.role) { p.seatedAt = Date.now(); p.seat = s.seat; return { ...s, ms: Date.now() - p.t0 }; }
|
|
62
|
+
await sleep(250);
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}));
|
|
66
|
+
if (seated.some((s) => !s)) return { ok: false, command: 'check', why: 'a browser never got a seat within 45 s', seated };
|
|
67
|
+
const sameRoom = seated[0].room === seated[1].room;
|
|
68
|
+
log(`seated: ${seated.map((s, i) => `${kinds[i].name} room ${s.room} seat ${s.seat} ${s.role}`).join('; ')}`);
|
|
69
|
+
if (!sameRoom) return { ok: false, command: 'check', why: `the two browsers landed in different rooms (${seated[0].room}, ${seated[1].room})`, seated };
|
|
70
|
+
// Play: the computer holds keys, the phone drags from the lower left.
|
|
71
|
+
const playing = (async () => {
|
|
72
|
+
const keys = ['KeyD', 'KeyS', 'KeyA', 'KeyW'];
|
|
73
|
+
for (let i = 0; Date.now() - started < roundTimeoutMs && i < 400; i++) {
|
|
74
|
+
const k = keys[i % keys.length];
|
|
75
|
+
await players[0].page.keyboard.down(k).catch(() => {});
|
|
76
|
+
await sleep(600);
|
|
77
|
+
await players[0].page.keyboard.up(k).catch(() => {});
|
|
78
|
+
const vp = kinds[1].viewport;
|
|
79
|
+
await players[1].page.touchscreen.touchStart(vp.width * 0.2, vp.height * 0.8).catch(() => {});
|
|
80
|
+
await players[1].page.touchscreen.touchMove(vp.width * 0.2 + (i % 2 ? 60 : -60), vp.height * 0.8 - 40).catch(() => {});
|
|
81
|
+
await sleep(300);
|
|
82
|
+
await players[1].page.touchscreen.touchEnd().catch(() => {});
|
|
83
|
+
if (players.every((p) => p.over)) break;
|
|
84
|
+
}
|
|
85
|
+
})();
|
|
86
|
+
if (shots) for (const [i, p] of players.entries()) await p.page.screenshot({ path: join(shots, `${kinds[i].name}-playing.png`) }).catch(() => {});
|
|
87
|
+
// Only a round that finishes AFTER both browsers were seated, with BOTH of them in its results, counts: an
|
|
88
|
+
// idle room remembers its last round for a while, and that stale result proves nothing.
|
|
89
|
+
const bothSeated = Math.max(...players.map((p) => p.seatedAt));
|
|
90
|
+
const seats = players.map((p) => p.seat);
|
|
91
|
+
const over = await Promise.all(players.map(async (p) => {
|
|
92
|
+
const deadline = Date.now() + roundTimeoutMs;
|
|
93
|
+
while (Date.now() < deadline) {
|
|
94
|
+
const r = await p.page.evaluate((after, want) => {
|
|
95
|
+
const s = window.__shell;
|
|
96
|
+
const hit = (s?.results ?? []).find((x) => x.at > after && want.every((seat) => x.results.some((row) => row.seat === seat)));
|
|
97
|
+
return hit ? { n: hit.n, endsAt: hit.endsAt, results: hit.results, room: s.room } : null;
|
|
98
|
+
}, bothSeated, seats).catch(() => null);
|
|
99
|
+
if (r) { p.over = true; return { ...r, ms: Date.now() - p.t0 }; }
|
|
100
|
+
await sleep(500);
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}));
|
|
104
|
+
await playing.catch(() => {});
|
|
105
|
+
if (shots) for (const [i, p] of players.entries()) await p.page.screenshot({ path: join(shots, `${kinds[i].name}-round-over.png`) }).catch(() => {});
|
|
106
|
+
if (over.some((o) => !o)) return { ok: false, command: 'check', why: 'a browser did not see a round finish in time', seated, over };
|
|
107
|
+
const humans = over[0].results.filter((r) => !r.bot).length;
|
|
108
|
+
const sameRound = over[0].n === over[1].n && over[0].endsAt === over[1].endsAt && over[0].room === over[1].room;
|
|
109
|
+
return {
|
|
110
|
+
ok: sameRoom && sameRound && humans >= 2,
|
|
111
|
+
command: 'check',
|
|
112
|
+
play,
|
|
113
|
+
room: seated[0].room,
|
|
114
|
+
seats: seated.map((s, i) => ({ browser: kinds[i].name, seat: s.seat, role: s.role, seatedMs: s.ms })),
|
|
115
|
+
round: { n: over[0].n, humans, bots: over[0].results.length - humans, results: over[0].results, overMs: over.map((o) => o.ms) },
|
|
116
|
+
why: !sameRound ? 'the two browsers saw different rounds finish' : humans >= 2 ? undefined : `the round's results list ${humans} human(s); both browsers should be in it`,
|
|
117
|
+
totalMs: Date.now() - started,
|
|
118
|
+
};
|
|
119
|
+
} finally {
|
|
120
|
+
for (const b of browsers) await b.close().catch(() => {});
|
|
121
|
+
for (const p of profiles) rmSync(p, { recursive: true, force: true });
|
|
122
|
+
}
|
|
123
|
+
}
|