playertwo-sdk 0.2.9 → 0.2.12
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 +103 -63
- package/index.d.ts +129 -7
- package/index.mjs +5 -6
- package/package.json +4 -4
- package/room.d.ts +5 -0
- package/room.mjs +58 -0
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# playertwo-sdk
|
|
2
2
|
|
|
3
|
-
The game SDK for [PlayerTwo](https://playertwo.
|
|
4
|
-
leaderboards, cloud saves, presence, invites, add-ons, in-game purchases, the player's avatar, travel
|
|
5
|
-
and multiplayer rooms with no server of your own (voice chat, worlds, replays and your own server code in `room.js`
|
|
3
|
+
The game SDK for [PlayerTwo](https://playertwo.plus), with TypeScript types. Trophies, XP and levels, stats,
|
|
4
|
+
leaderboards, cloud saves, presence, invites, add-ons, in-game purchases, donations to you, the player's avatar, travel
|
|
5
|
+
between games, and multiplayer rooms with no server of your own (voice chat, worlds, replays and your own server code in `room.js`
|
|
6
6
|
included).
|
|
7
7
|
|
|
8
8
|
PlayerTwo is a game store and a launchpad. Games are sold (or free) to play in the browser or download, and raises
|
|
9
9
|
let players back a game while it is being made, paid to its developer stage by stage as they post proof. A web game
|
|
10
10
|
hosted on PlayerTwo gets its own address on `playertwo.cc` and every service below through one account per player.
|
|
11
11
|
|
|
12
|
-
- Docs: https://playertwo.
|
|
13
|
-
- Everything a game gets, with a link to its part of the docs: https://playertwo.
|
|
12
|
+
- Docs: https://playertwo.plus/docs/sdk
|
|
13
|
+
- Everything a game gets, with a link to its part of the docs: https://playertwo.plus/llms.txt
|
|
14
14
|
- Source of these packages, examples and Markdown docs: https://github.com/TheDudeCommits/playertwo-sdk
|
|
15
15
|
|
|
16
16
|
## Install
|
|
@@ -22,7 +22,7 @@ npm install playertwo-sdk
|
|
|
22
22
|
Or no install at all: the SDK itself is one script.
|
|
23
23
|
|
|
24
24
|
```html
|
|
25
|
-
<script src="https://playertwo.
|
|
25
|
+
<script src="https://playertwo.plus/sdk/v1.js"></script>
|
|
26
26
|
<script>
|
|
27
27
|
PlayerTwo.init().then((p2) => p2.trophies.unlock("first-win")).catch(() => {
|
|
28
28
|
/* not on PlayerTwo (e.code "not-on-player-two"): the game plays on without services */
|
|
@@ -31,7 +31,7 @@ Or no install at all: the SDK itself is one script.
|
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
On PlayerTwo the script is already in your game's page. `init()` from the npm package uses that copy, and loads
|
|
34
|
-
`https://playertwo.
|
|
34
|
+
`https://playertwo.plus/sdk/v1.js` only when the page doesn't have it (on localhost, say).
|
|
35
35
|
|
|
36
36
|
## Quick start
|
|
37
37
|
|
|
@@ -61,58 +61,63 @@ sync.on("leave", (id) => removeMesh(id));
|
|
|
61
61
|
```
|
|
62
62
|
|
|
63
63
|
Every call returns a promise. A refusal rejects with an `Error` that has a `code` and a `status` (the codes:
|
|
64
|
-
https://playertwo.
|
|
64
|
+
https://playertwo.plus/docs/sdk#call-errors). Off PlayerTwo and off localhost, `init()` rejects after 1.5 seconds
|
|
65
65
|
with code `not-on-player-two`; with `{ required: true }` the page says "Play this on PlayerTwo" and stops.
|
|
66
66
|
|
|
67
67
|
## What it gives your game
|
|
68
68
|
|
|
69
69
|
| Feature | Calls | Docs |
|
|
70
70
|
| --- | --- | --- |
|
|
71
|
-
| Who is playing | `p2.player`, `p2.game`, `p2.me()` | [call-me](https://playertwo.
|
|
72
|
-
| Trophies, XP and levels | `p2.trophies.list()`, `p2.trophies.unlock(key)` | [call-trophies](https://playertwo.
|
|
73
|
-
| Leaderboards | `p2.scores.submit(board, value)`, `p2.scores.list(board, { around, friends, limit })` | [call-scores](https://playertwo.
|
|
74
|
-
| Stats | `p2.stats.get()`, `.get(key)`, `.set(key, n)`, `.add(key, n?)` | [call-stats](https://playertwo.
|
|
75
|
-
| Cloud saves (8 slots, 1 MB each) | `p2.saves.list()`, `.get(slot)`, `.put(slot, data, { version })`, `.delete(slot)` | [call-saves](https://playertwo.
|
|
76
|
-
| Add-ons (sold on the game's page) | `p2.addons.list()`, `p2.addons.has(key)` | [addons](https://playertwo.
|
|
77
|
-
| In-game purchases | `p2.store.list()`, `.owned()`, `.buy(key)`, `.pay(chargeId)` | [purchases](https://playertwo.
|
|
78
|
-
|
|
|
79
|
-
|
|
|
80
|
-
|
|
|
81
|
-
|
|
|
82
|
-
|
|
|
83
|
-
|
|
|
84
|
-
|
|
|
85
|
-
|
|
|
71
|
+
| Who is playing | `p2.player`, `p2.game`, `p2.me()` | [call-me](https://playertwo.plus/docs/sdk#call-me) |
|
|
72
|
+
| Trophies, XP and levels | `p2.trophies.list()`, `p2.trophies.unlock(key)` | [call-trophies](https://playertwo.plus/docs/sdk#call-trophies) |
|
|
73
|
+
| Leaderboards | `p2.scores.submit(board, value)`, `p2.scores.list(board, { around, friends, limit })` | [call-scores](https://playertwo.plus/docs/sdk#call-scores) |
|
|
74
|
+
| Stats | `p2.stats.get()`, `.get(key)`, `.set(key, n)`, `.add(key, n?)` | [call-stats](https://playertwo.plus/docs/sdk#call-stats) |
|
|
75
|
+
| Cloud saves (8 slots, 1 MB each) | `p2.saves.list()`, `.get(slot)`, `.put(slot, data, { version })`, `.delete(slot)` | [call-saves](https://playertwo.plus/docs/sdk#call-saves) |
|
|
76
|
+
| Add-ons (sold on the game's page) | `p2.addons.list()`, `p2.addons.has(key)` | [addons](https://playertwo.plus/docs/sdk#addons) |
|
|
77
|
+
| In-game purchases | `p2.store.list()`, `.owned()`, `.buy(key)`, `.pay(chargeId)` | [purchases](https://playertwo.plus/docs/sdk#purchases) |
|
|
78
|
+
| Donations to you | `p2.donate.available()`, `.open()`, `.button({ corner, parent })` | [donate](https://playertwo.plus/docs/sdk#donate) |
|
|
79
|
+
| Presence | `p2.presence.set(line, { join })` | [call-presence](https://playertwo.plus/docs/sdk#call-presence) |
|
|
80
|
+
| Identity and invites | `p2.identity()`, `p2.invite(params)`, `p2.params` | [call-identity](https://playertwo.plus/docs/sdk#call-identity) |
|
|
81
|
+
| Loading and play events | `p2.loading.done()`, `p2.gameplay.start()`, `.stop()` | [call-gameplay](https://playertwo.plus/docs/sdk#call-gameplay) |
|
|
82
|
+
| Your game's own events, in your studio's analytics | `p2.track(name, data)` | [call-track](https://playertwo.plus/docs/sdk#call-track) |
|
|
83
|
+
| Its errors, caught by themselves | `p2.errors.report(error, where)`, `p2.errors.off()` | [call-crashes](https://playertwo.plus/docs/sdk#call-crashes) |
|
|
84
|
+
| Feeds: outside data PlayerTwo fetches for your game | `p2.feeds.get(name)` | [feeds](https://playertwo.plus/docs/sdk#feeds) |
|
|
85
|
+
| The overlay, and closing | `p2.on("overlay", fn)`, `p2.on("flush", fn)` | [call-overlay](https://playertwo.plus/docs/sdk#call-overlay) |
|
|
86
|
+
| On air in the Plaza | `p2.onAir` | [call-air](https://playertwo.plus/docs/sdk#call-air) |
|
|
87
|
+
| Test mode and demos | `p2.test`, `p2.demo` | [test](https://playertwo.plus/docs/sdk#test) |
|
|
88
|
+
| The player's avatar, 2D and 3D | `p2.avatar()`, `p2.items.list()` | [travel#avatar](https://playertwo.plus/docs/sdk/travel#avatar) |
|
|
89
|
+
| Travel with the party | `p2.travel.to(slug, { params, place })`, `.home()`, `.links()` | [travel#travel](https://playertwo.plus/docs/sdk/travel#travel) |
|
|
86
90
|
|
|
87
91
|
Multiplayer, through `p2.rooms`:
|
|
88
92
|
|
|
89
93
|
| Feature | Calls | Docs |
|
|
90
94
|
| --- | --- | --- |
|
|
91
|
-
| Matches, private codes, parties | `p2.rooms.join({ mode, size, teams, skill, together, code, room })`, `.create()`, `.party()` | [rooms-join](https://playertwo.
|
|
92
|
-
| Messages and shared state | `room.send(type, data, { to, fast })`, `room.on("message:<type>")`, `room.set`, `room.get`, `room.state` | [rooms-talk](https://playertwo.
|
|
93
|
-
| Smooth movement | `room.sync(object, options)` | [rooms-sync](https://playertwo.
|
|
94
|
-
| Ping, stats, the room's clock | `room.ping()`, `room.stats()`, `room.now()` | [rooms-fast](https://playertwo.
|
|
95
|
-
| Your server code (`room.js`) | see below | [rooms-scripted](https://playertwo.
|
|
96
|
-
| Input-run rooms (client prediction, corrected by the server's state) | `room.inputs({ state, input, step })` | [rooms-inputs](https://playertwo.
|
|
97
|
-
| Corrections and lag-compensated hits | `onSync`, `room.correct`, `room.past` (room.js); `sync.seen(id)` | [rooms-corrections](https://playertwo.
|
|
98
|
-
| Server-moved objects | `room.objects()` (page), `room.objects.set` (room.js) | [rooms-objects](https://playertwo.
|
|
99
|
-
| Physics engines in WebAssembly | a `room/` folder | [rooms-engines](https://playertwo.
|
|
100
|
-
|
|
|
101
|
-
|
|
|
102
|
-
|
|
|
103
|
-
|
|
|
104
|
-
|
|
|
105
|
-
|
|
|
106
|
-
|
|
|
95
|
+
| Matches, private codes, parties | `p2.rooms.join({ mode, size, teams, skill, together, code, room })`, `.create()`, `.party()` | [rooms-join](https://playertwo.plus/docs/sdk#rooms-join) |
|
|
96
|
+
| Messages and shared state | `room.send(type, data, { to, fast })`, `room.on("message:<type>")`, `room.set`, `room.get`, `room.state` | [rooms-talk](https://playertwo.plus/docs/sdk#rooms-talk) |
|
|
97
|
+
| Smooth movement | `room.sync(object, options)` | [rooms-sync](https://playertwo.plus/docs/sdk#rooms-sync) |
|
|
98
|
+
| Ping, stats, the room's clock | `room.ping()`, `room.stats()`, `room.now()` | [rooms-fast](https://playertwo.plus/docs/sdk#rooms-fast) |
|
|
99
|
+
| Your server code (`room.js`) | see below | [rooms-scripted](https://playertwo.plus/docs/sdk#rooms-scripted) |
|
|
100
|
+
| Input-run rooms (client prediction, corrected by the server's state) | `room.inputs({ state, input, step })` | [rooms-inputs](https://playertwo.plus/docs/sdk#rooms-inputs) |
|
|
101
|
+
| Corrections and lag-compensated hits | `onSync`, `room.correct`, `room.past` (room.js); `sync.seen(id)` | [rooms-corrections](https://playertwo.plus/docs/sdk#rooms-corrections) |
|
|
102
|
+
| Server-moved objects | `room.objects()` (page), `room.objects.set` (room.js) | [rooms-objects](https://playertwo.plus/docs/sdk#rooms-objects) |
|
|
103
|
+
| Physics engines in WebAssembly | a `room/` folder | [rooms-engines](https://playertwo.plus/docs/sdk#rooms-engines) |
|
|
104
|
+
| Outside data, checked (a feed's signed items, relayed by a page) | `checkFeedItem(key, item, sig)` (room.js) | [rooms-feeds](https://playertwo.plus/docs/sdk#rooms-feeds) |
|
|
105
|
+
| Teams and party matches | `{ teams, together }`, `room.team(id, n)` | [rooms-teams](https://playertwo.plus/docs/sdk#rooms-teams) |
|
|
106
|
+
| Matching by skill (Elo by mode) | `{ skill: true }`, `room.result(places)` | [rooms-matching](https://playertwo.plus/docs/sdk#rooms-matching) |
|
|
107
|
+
| Ranked boards and server-given trophies | `room.score`, `room.trophy` (room.js) | [rooms-ranked](https://playertwo.plus/docs/sdk#rooms-ranked) |
|
|
108
|
+
| Watching and replays | `p2.rooms.live()`, `.watch({ room })`, `.replays()`, `.replay(id)`, `room.record()` | [rooms-watch](https://playertwo.plus/docs/sdk#rooms-watch) |
|
|
109
|
+
| Worlds of up to 256 an area | `p2.rooms.world(name, { area })` | [rooms-worlds](https://playertwo.plus/docs/sdk#rooms-worlds) |
|
|
110
|
+
| Voice chat (24 a room) | `room.voice.join()`, `.leave()`, `.mute()`, `.volume(id, v)`, `.speaking(id)` | [rooms-voice](https://playertwo.plus/docs/sdk#rooms-voice) |
|
|
111
|
+
| A bad connection, on purpose | `p2.rooms.simulate({ lag, jitter, loss })` or `?p2-net=150,30,0.02` | [rooms-simulate](https://playertwo.plus/docs/sdk#rooms-simulate) |
|
|
107
112
|
|
|
108
113
|
Rooms are 1 to 64 players (256 in an area of a world) and are for games hosted on PlayerTwo (a build you push, or
|
|
109
114
|
your own site served through PlayerTwo). A release never mixes versions in a room. All limits:
|
|
110
|
-
https://playertwo.
|
|
115
|
+
https://playertwo.plus/docs/sdk#rooms-limits.
|
|
111
116
|
|
|
112
117
|
## A room's server: `playertwo-sdk/room`
|
|
113
118
|
|
|
114
119
|
Put a `room.js` (one ES module, up to 1 MB) at the root of the build you push, and every room of that build runs it on
|
|
115
|
-
PlayerTwo's servers: a sandbox with no network,
|
|
120
|
+
PlayerTwo's servers: a sandbox with no network, 250 ms of CPU a call (over 60 ms is told to the studio as slow), 1 to 60 ticks a second. In a scripted room your
|
|
116
121
|
pages' messages go to your code, and only your code writes the shared state, so a player can't fake a hit or a score.
|
|
117
122
|
PlayerTwo takes the file out of your build and never serves it to players.
|
|
118
123
|
|
|
@@ -146,15 +151,40 @@ export const { tickRate, onCreate, onJoin, onLeave, onMessage, onSync, onTick }
|
|
|
146
151
|
Hooks: `onCreate`, `onStart`, `onJoin`, `onLeave`, `onMessage`, `onSync`, `onTick`. Everything `room` has:
|
|
147
152
|
[`room.d.ts`](./room.d.ts).
|
|
148
153
|
|
|
154
|
+
### Outside data in a room
|
|
155
|
+
|
|
156
|
+
Room code has no network. A feed (set up by your agent: `PUT /api/v1/agent/games/<slug>/feeds/<name>`) is an https
|
|
157
|
+
address PlayerTwo fetches for your game every 60 to 3,600 seconds while it is being opened; with `items` and `signed`,
|
|
158
|
+
each item of its list comes with `sig`, the hex HMAC-SHA256 of `JSON.stringify(item)` keyed with the feed's key (its 64
|
|
159
|
+
hex characters, as text, shown once when it is made). A page reads it and relays each item; the room checks it with the
|
|
160
|
+
key built into `room.js`, so a player can't make one up:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
// a page
|
|
164
|
+
const feed = await p2.feeds.get("x-posts"); // { at, items: [{ item, sig }] }
|
|
165
|
+
for (const x of feed.items) room.send("x-post", x);
|
|
166
|
+
|
|
167
|
+
// room.js
|
|
168
|
+
import { checkFeedItem } from "playertwo-sdk/room"; // synchronous, plain JavaScript (no WebCrypto needed)
|
|
169
|
+
const FEED_KEY = "9f1c..."; // the feed's key, 64 hex characters
|
|
170
|
+
export function onMessage(room, player, type, data) {
|
|
171
|
+
if (type === "x-post" && checkFeedItem(FEED_KEY, data?.item, data?.sig)) use(room, data.item);
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
A feed is public (anyone can read `GET /api/v1/games/<slug>/feeds/<name>`); its secret, put into a header's
|
|
176
|
+
`{{secret}}` as it is fetched, never leaves PlayerTwo. A game nobody opened in the last 24 hours has its feeds left
|
|
177
|
+
alone until someone does. Every rule: https://playertwo.plus/docs/sdk#feeds-setup.
|
|
178
|
+
|
|
149
179
|
## Building without PlayerTwo
|
|
150
180
|
|
|
151
181
|
On localhost, a stand-in answers every call from memory, and two tabs are two players. If your dev server serves
|
|
152
182
|
`/room.js`, the first tab runs it as the room's server. Give the stand-in your trophies, add-ons, products and params
|
|
153
|
-
with `init({ local: { ... } })
|
|
154
|
-
room on PlayerTwo. Docs: https://playertwo.
|
|
183
|
+
with `init({ local: { ... } })` (and what `p2.feeds.get` answers, `local.feeds`); `local: true` (or `{ force: true }`) uses it wherever the game runs. There, `p2.track` and errors are said in the console. Voice needs a
|
|
184
|
+
room on PlayerTwo. Docs: https://playertwo.plus/docs/sdk#start-local.
|
|
155
185
|
|
|
156
186
|
On PlayerTwo you, your team and your playtesters play in test mode (`p2.test` is true): every call works, nothing is
|
|
157
|
-
paid, and what you earn is kept apart. Docs: https://playertwo.
|
|
187
|
+
paid, and what you earn is kept apart. Docs: https://playertwo.plus/docs/sdk#test.
|
|
158
188
|
|
|
159
189
|
Using the script tag and only want the types of the global `PlayerTwo`? Add `"types": ["playertwo-sdk"]` to your
|
|
160
190
|
`tsconfig.json`, or `/// <reference types="playertwo-sdk" />` at the top of a file.
|
|
@@ -166,55 +196,65 @@ makes at its own prices with your game key (`p2.store.pay(chargeId)`). PlayerTwo
|
|
|
166
196
|
and the payment from their PlayerTwo balance (USDC on Base). You get 80% of every in-game purchase, PlayerTwo keeps
|
|
167
197
|
20% (less your backers' share if your game raised with a share of its sales). Add-ons sold on the game's page are split
|
|
168
198
|
as its sales are: PlayerTwo keeps 20%. Every purchase gives your game a signed receipt (a JWT typed `p2-buy`, RS256,
|
|
169
|
-
keys at https://playertwo.
|
|
170
|
-
https://playertwo.
|
|
199
|
+
keys at https://playertwo.plus/.well-known/p2-keys.json) and your studio's webhooks a `purchase` event. Docs:
|
|
200
|
+
https://playertwo.plus/docs/sdk#purchases.
|
|
201
|
+
|
|
202
|
+
## Donations
|
|
203
|
+
|
|
204
|
+
Players can donate to your studio for your game, even while it's free. Turn donations on in your studio (Sell,
|
|
205
|
+
Donations) and pick their one place: your game's PlayerTwo page (PlayerTwo's card, in your words, colour and picture),
|
|
206
|
+
or inside the game. Inside the game, your own button calls `p2.donate.open()` to open PlayerTwo's donation sheet, or
|
|
207
|
+
`p2.donate.button()` puts PlayerTwo's pixel-heart "Support the dev" button in a corner (or inside an element of yours);
|
|
208
|
+
`p2.donate.available()` says whether donations are on inside the game. The entire donation, minus the transaction fees,
|
|
209
|
+
goes to your studio: PlayerTwo takes no cut. A donation buys nothing in the game, and the game never learns who gave
|
|
210
|
+
or how much, so it never belongs in your shop. Docs: https://playertwo.plus/docs/sdk#donate.
|
|
171
211
|
|
|
172
212
|
## Your game's own server
|
|
173
213
|
|
|
174
214
|
A game key (`p2g_...`, made in your studio) lets your server check a session or a signed identity from
|
|
175
215
|
`p2.identity()`, give server-only trophies, post to trusted leaderboards, read add-ons, make and check charges, use up
|
|
176
|
-
consumables and report matches, under `https://playertwo.
|
|
177
|
-
https://playertwo.
|
|
216
|
+
consumables and report matches, under `https://playertwo.plus/api/v1/sdk/server/...`. Docs:
|
|
217
|
+
https://playertwo.plus/docs/sdk#server and https://playertwo.plus/docs/sdk/connect (a game played on its own site).
|
|
178
218
|
|
|
179
219
|
## Putting it on PlayerTwo
|
|
180
220
|
|
|
181
221
|
A whole game to start from: `npm create playertwo-game@latest my-game` (a small multiplayer Three.js game with rooms
|
|
182
222
|
and `room.js`, see [create-playertwo-game](https://www.npmjs.com/package/create-playertwo-game)).
|
|
183
223
|
|
|
184
|
-
1. Submit the game at https://playertwo.
|
|
224
|
+
1. Submit the game at https://playertwo.plus/submit (that gives it its address, `your-game`).
|
|
185
225
|
2. Make an agent key in your studio, under Agents.
|
|
186
226
|
3. Push the build:
|
|
187
227
|
|
|
188
228
|
```
|
|
189
|
-
curl -fsSLO https://playertwo.
|
|
229
|
+
curl -fsSLO https://playertwo.plus/cli/p2.mjs
|
|
190
230
|
P2_AGENT_KEY=p2_agent_... node p2.mjs push dist --game your-game --version 1.0.0
|
|
191
231
|
```
|
|
192
232
|
|
|
193
233
|
It plays at `https://your-game.playertwo.cc/` in PlayerTwo's player, with no code changes. A pushed folder may be up to
|
|
194
|
-
5 GB and 20,000 files; only the files PlayerTwo doesn't have yet are sent. Docs: https://playertwo.
|
|
234
|
+
5 GB and 20,000 files; only the files PlayerTwo doesn't have yet are sent. Docs: https://playertwo.plus/docs/hosting.
|
|
195
235
|
|
|
196
236
|
## For AI agents: MCP and the agent API
|
|
197
237
|
|
|
198
|
-
PlayerTwo runs an MCP server (Streamable HTTP) at `https://playertwo.
|
|
238
|
+
PlayerTwo runs an MCP server (Streamable HTTP) at `https://playertwo.plus/api/mcp`:
|
|
199
239
|
|
|
200
240
|
```
|
|
201
|
-
claude mcp add --transport http player-two https://playertwo.
|
|
202
|
-
claude mcp add --transport http player-two https://playertwo.
|
|
241
|
+
claude mcp add --transport http player-two https://playertwo.plus/api/mcp
|
|
242
|
+
claude mcp add --transport http player-two https://playertwo.plus/api/mcp --header "Authorization: Bearer p2_agent_..."
|
|
203
243
|
```
|
|
204
244
|
|
|
205
245
|
The first line is enough for the public tools, which need no key: `get_submission_guide` and `check_game`, and
|
|
206
246
|
`search_games`, `list_games`, `get_game_facts`, `get_sdk_docs` and `where_to_publish`. Tools that change a game
|
|
207
247
|
(submit it, edit its page, upload media and web builds, define trophies, leaderboards and products, roll back builds,
|
|
208
|
-
post updates, read its rooms) need the player's agent key, made in their studio on playertwo.
|
|
248
|
+
post updates, read its rooms) need the player's agent key, made in their studio on playertwo.plus under Agents, or no
|
|
209
249
|
key at all in a client that signs in (Claude's and ChatGPT's connectors, Claude Code, Cursor): the first time it calls
|
|
210
|
-
one, the player is asked to sign in to PlayerTwo and allow it (OAuth 2.1 with PKCE). Its
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
- The agent API: `GET https://playertwo.
|
|
214
|
-
`POST https://playertwo.
|
|
215
|
-
- OpenAPI 3.1 spec: https://playertwo.
|
|
216
|
-
- For language models: https://playertwo.
|
|
217
|
-
- Docs: https://playertwo.
|
|
250
|
+
one, the player is asked to sign in to PlayerTwo and allow it (OAuth 2.1 with PKCE). Its server card is at
|
|
251
|
+
https://playertwo.plus/.well-known/mcp.json.
|
|
252
|
+
|
|
253
|
+
- The agent API: `GET https://playertwo.plus/api/v1/agent/guide` explains every field; submit with
|
|
254
|
+
`POST https://playertwo.plus/api/v1/agent/games` `{ "draft": { ... }, "publish": true }`.
|
|
255
|
+
- OpenAPI 3.1 spec: https://playertwo.plus/openapi.json
|
|
256
|
+
- For language models: https://playertwo.plus/llms.txt
|
|
257
|
+
- Docs: https://playertwo.plus/docs#agents
|
|
218
258
|
|
|
219
259
|
## License
|
|
220
260
|
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Types for PlayerTwo's game SDK, https://playertwo.
|
|
3
|
-
* keep these in step with its part 4, "the helpers the game uses", and part 4b, "rooms"). Docs: https://playertwo.
|
|
2
|
+
* Types for PlayerTwo's game SDK, https://playertwo.plus/sdk/v1.js (its source: public/sdk/v1.js in PlayerTwo's code;
|
|
3
|
+
* keep these in step with its part 4, "the helpers the game uses", and part 4b, "rooms"). Docs: https://playertwo.plus/docs/sdk
|
|
4
4
|
*
|
|
5
5
|
* import { init } from "playertwo-sdk"; // or the global PlayerTwo, from the script tag
|
|
6
6
|
* const p2 = await init();
|
|
@@ -47,8 +47,12 @@ export interface LocalOptions {
|
|
|
47
47
|
trophies?: { key: string; name?: string; how?: string; tier?: number; hidden?: boolean }[];
|
|
48
48
|
addons?: { key: string; name?: string; about?: string; price?: number; owned?: boolean }[];
|
|
49
49
|
params?: Record<string, string>;
|
|
50
|
+
/** The stand-in's feeds, by name: what p2.feeds.get answers there (unsigned: sig null). */
|
|
51
|
+
feeds?: Record<string, { body?: Json } | { items: Json[] }>;
|
|
50
52
|
/** Use the stand-in wherever the game runs. */
|
|
51
53
|
force?: boolean;
|
|
54
|
+
/** This browser's tabs of the game as one PlayerTwo party (as ?p2-party does): true joins it, "lead" takes its lead. */
|
|
55
|
+
party?: boolean | "lead";
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
export interface InitOptions {
|
|
@@ -204,6 +208,9 @@ export interface Product {
|
|
|
204
208
|
kind: ProductKind;
|
|
205
209
|
name: string;
|
|
206
210
|
about: string;
|
|
211
|
+
/** Its picture, when its studio gave it one: an https address in PlayerTwo's file store. PlayerTwo's card shows it
|
|
212
|
+
beside the name; use it in your own shop too. */
|
|
213
|
+
picture?: string;
|
|
207
214
|
/** In US dollars. */
|
|
208
215
|
price: number;
|
|
209
216
|
/** A consumable's: how many one purchase gives. */
|
|
@@ -248,6 +255,9 @@ export type Paid =
|
|
|
248
255
|
}
|
|
249
256
|
| { ok: false; reason: "cancelled" };
|
|
250
257
|
|
|
258
|
+
/** What p2.signUp() answers. */
|
|
259
|
+
export type SignUpAnswer = { ok: true; card?: true; local?: true } | { ok: false; reason: "cancelled" };
|
|
260
|
+
|
|
251
261
|
export interface Store {
|
|
252
262
|
/** Each product on sale, with what the player has of it. */
|
|
253
263
|
list(): Promise<Product[]>;
|
|
@@ -265,6 +275,24 @@ export interface Store {
|
|
|
265
275
|
pay(charge: string): Promise<Paid>;
|
|
266
276
|
}
|
|
267
277
|
|
|
278
|
+
/** What p2.donate.open() answers: the sheet was shown (never how much, nor whether anything was given), or why not. */
|
|
279
|
+
export type DonateShown = { shown: true; local?: true } | { shown: false; reason: "off" | "placement" | "too-soon" };
|
|
280
|
+
|
|
281
|
+
/** Donations to the game's developer (p2.donate; docs: /docs/sdk#donate), when its studio put them inside the game (its
|
|
282
|
+
one place: the game or its PlayerTwo page). The entire donation, minus the transaction fees, goes to the studio; PlayerTwo takes
|
|
283
|
+
nothing. A donation buys nothing in the game, and the game never learns of one. */
|
|
284
|
+
export interface Donate {
|
|
285
|
+
/** Whether the game takes donations inside itself now. */
|
|
286
|
+
available(): Promise<boolean>;
|
|
287
|
+
/** Opens PlayerTwo's donation sheet over the game (from the player's press on your own button). At most once every
|
|
288
|
+
two minutes. Rejects with code "no-donate" outside PlayerTwo's player on the web, "busy" while another of its
|
|
289
|
+
sheets is open. */
|
|
290
|
+
open(): Promise<DonateShown>;
|
|
291
|
+
/** PlayerTwo's own Donate button (a pixel heart, "Support the dev"), for a game that draws none: in a corner of the
|
|
292
|
+
page, or inside an element of yours. Shown only while the game takes donations inside itself; null otherwise. */
|
|
293
|
+
button(options?: { corner?: "top-right" | "top-left" | "bottom-right" | "bottom-left"; parent?: HTMLElement }): Promise<HTMLButtonElement | null>;
|
|
294
|
+
}
|
|
295
|
+
|
|
268
296
|
export interface Stats {
|
|
269
297
|
/** Every stat. */
|
|
270
298
|
get(): Promise<{ stats: Record<string, number> }>;
|
|
@@ -336,8 +364,25 @@ export interface JoinOptions extends MatchOptions {
|
|
|
336
364
|
between far-apart players by itself once it knows their round trips, and goes back if that wasn't faster for the
|
|
337
365
|
furthest one: each page goes there as it would come back ("reconnecting", "reconnected", "moved"). */
|
|
338
366
|
between?: boolean;
|
|
367
|
+
/** Your own version of what your pages say to each other (letters, digits, `.`, `_`, `-`; up to 32). A match meets
|
|
368
|
+
pages of any build whose rooms run the same room code (rooms from r27); naming a `net` keeps apart builds whose
|
|
369
|
+
pages differ while the code doesn't, and lets a relay room's builds (no room.js) meet across builds. */
|
|
370
|
+
net?: string;
|
|
371
|
+
/** Milliseconds (1 000 to 600 000): the room says "alone" once if nobody else has come by then, so the game can
|
|
372
|
+
offer its bots, another mode or playing offline. */
|
|
373
|
+
alone?: number;
|
|
339
374
|
}
|
|
340
375
|
|
|
376
|
+
/** A player's PlayerTwo party for your game (p2.party.get). */
|
|
377
|
+
export interface PartyInfo {
|
|
378
|
+
id: string;
|
|
379
|
+
me: { id: string; leader: boolean };
|
|
380
|
+
leader: { id: string; handle: string; name: string } | null;
|
|
381
|
+
/** Every member (the leader first): `here`, playing your game now. */
|
|
382
|
+
members: { id: string; handle: string; name: string; leader: boolean; here: boolean }[];
|
|
383
|
+
}
|
|
384
|
+
/** The party's leader in your game, and what their game joins friends with now (p2.party.lead, "lead"). */
|
|
385
|
+
export interface PartyLead { leader: { handle: string; name: string }; join: Record<string, string> | null; at: number }
|
|
341
386
|
/** One of the game's public rooms open now (p2.rooms.live), to watch. */
|
|
342
387
|
export interface LiveRoom { id: string; mode: string; size: number; teams?: number; players: number; watchers: number; skill: number | null; closed: boolean }
|
|
343
388
|
/** One of the game's replays (p2.rooms.replays). */
|
|
@@ -369,8 +414,10 @@ export interface Rooms {
|
|
|
369
414
|
party(options?: MatchOptions): Promise<Room>;
|
|
370
415
|
/** A world the game keeps for good ("main" when unnamed), or an area of it: one room each, never closed, its shared
|
|
371
416
|
state (and a scripted world's room.data and room.store) kept while no one is in it. It plays the live build: a page
|
|
372
|
-
on an older one hears "close" with "update" (reload it).
|
|
373
|
-
|
|
417
|
+
on an older one hears "close" with "update" (reload it). With `region`, the area is kept in that region's one best
|
|
418
|
+
place for all its players (the data centre nearest the region's middle), never near whoever opened it first; an
|
|
419
|
+
area already open moves there the next time it is empty, with everything it keeps. */
|
|
420
|
+
world(name?: string, options?: { area?: string; region?: "na" | "europe" | "asia" | "oceania" }): Promise<Room>;
|
|
374
421
|
/** Watch a room (its id, from live() or a friend): you hear and draw everything its players do, with no seat, and can
|
|
375
422
|
say nothing (room.watching). */
|
|
376
423
|
watch(options: { room: string }): Promise<Room>;
|
|
@@ -556,6 +603,11 @@ export interface RoomStats {
|
|
|
556
603
|
peers: Record<string, { ping: number; jitter: number }>;
|
|
557
604
|
/** The worst of `peers`, or null. */
|
|
558
605
|
lanePing: number | null;
|
|
606
|
+
/** (2026-11-21) How many times in the last 30 s a steady stream from the room (messages under 120 ms apart) was held
|
|
607
|
+
up for over 400 ms while the page was free: TCP resending what a bad line lost. */
|
|
608
|
+
stalls: number;
|
|
609
|
+
/** The longest of those (ms), or 0. */
|
|
610
|
+
stall: number;
|
|
559
611
|
}
|
|
560
612
|
|
|
561
613
|
export interface Room {
|
|
@@ -583,6 +635,9 @@ export interface Room {
|
|
|
583
635
|
readonly world: boolean;
|
|
584
636
|
/** Whether you're watching it (p2.rooms.watch, or a replay): you hear everything and can say nothing. */
|
|
585
637
|
readonly watching: boolean;
|
|
638
|
+
/** Messages a second you may send it, bursts of twice that: your game's rate (60 unless its developer raised it in
|
|
639
|
+
the studio, up to 240). Past it a message is refused with too-fast. Pace inputs to it. */
|
|
640
|
+
readonly rate: number;
|
|
586
641
|
/** A replay's controls (a room from p2.rooms.replay). */
|
|
587
642
|
readonly replay?: ReplayControls;
|
|
588
643
|
/** Voice chat. */
|
|
@@ -634,6 +689,8 @@ export interface Room {
|
|
|
634
689
|
on(type: "speaking", fn: (id: string, speaking: boolean) => void): () => void;
|
|
635
690
|
/** You're out: "left", "closed", "lost", "replaced", "stopped", "time", "empty", ... */
|
|
636
691
|
on(type: "close", fn: (why: string) => void): () => void;
|
|
692
|
+
/** Nobody else came within rooms.join's `alone` ms (once). */
|
|
693
|
+
on(type: "alone", fn: (info: { ms: number }) => void): () => void;
|
|
637
694
|
on(type: "error", fn: (error: P2Error) => void): () => void;
|
|
638
695
|
}
|
|
639
696
|
|
|
@@ -705,7 +762,7 @@ export interface Door {
|
|
|
705
762
|
/** Travel to other games on PlayerTwo, with the player's party. In PlayerTwo's player on the web; elsewhere each
|
|
706
763
|
rejects with code "no-travel". */
|
|
707
764
|
export interface Travel {
|
|
708
|
-
/** To another game, by its address (playertwo.
|
|
765
|
+
/** To another game, by its address (playertwo.plus/game/<slug>): it is given p2.params (yours, then `from`, this
|
|
709
766
|
game's address, and `place`). Resolves as the player goes; this page is closed after. */
|
|
710
767
|
to(slug: string, options?: { params?: Record<string, string>; place?: string }): Promise<{ to: { slug: string; title: string }; params: Record<string, string> }>;
|
|
711
768
|
/** Back to the Plaza, where every game on PlayerTwo has its screen. */
|
|
@@ -718,6 +775,41 @@ export interface Travel {
|
|
|
718
775
|
/* ---------- the SDK ---------- */
|
|
719
776
|
|
|
720
777
|
/** What PlayerTwo.init() answers: who is playing, and every call. */
|
|
778
|
+
/** p2.track's data: up to 12 keys (a letter, then up to 31 letters, digits, _ and -), each text (cut to 120
|
|
779
|
+
characters), a finite number or true/false; anything else is left out. */
|
|
780
|
+
export type TrackData = Record<string, string | number | boolean>;
|
|
781
|
+
|
|
782
|
+
/** Your game's errors, in your studio's analytics and for your agent (GET /api/v1/agent/games/<slug>/events). The
|
|
783
|
+
page's uncaught errors and unhandled rejections are caught by themselves from the moment the SDK loads. Ten a page
|
|
784
|
+
load, each message and place once. */
|
|
785
|
+
export interface Errors {
|
|
786
|
+
/** Stop catching them for this page (report still sends). */
|
|
787
|
+
off(): void;
|
|
788
|
+
/** Report one by hand: an Error (or its message), and where in your game, in your own words (up to 40 characters). */
|
|
789
|
+
report(error: unknown, where?: string): void;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/** A feed's last good answer, as PlayerTwo fetched it: when (null before its first good fetch), its HTTP status, and its
|
|
793
|
+
JSON as it came (or its text). */
|
|
794
|
+
export interface FeedAnswer<T = Json> {
|
|
795
|
+
at: number | null;
|
|
796
|
+
status: number | null;
|
|
797
|
+
body: T | string | null;
|
|
798
|
+
}
|
|
799
|
+
/** A feed of items (its `items` names a list in the answer): each item, and its signature, the hex HMAC-SHA256 of
|
|
800
|
+
JSON.stringify(item) keyed with the feed's key (its 64 hex characters as text); null for a feed that isn't signed.
|
|
801
|
+
Relay an item and its sig to your room, and check it there with the key in your room.js. */
|
|
802
|
+
export interface FeedItems<T = Json> {
|
|
803
|
+
at: number | null;
|
|
804
|
+
items: { item: T; sig: string | null }[];
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/** Outside data PlayerTwo fetches for your game on a clock (set up by your agent: PUT /api/v1/agent/games/<slug>/feeds/<name>). */
|
|
808
|
+
export interface Feeds {
|
|
809
|
+
/** The feed's last good answer. Rejects with code unknown-feed (404), or no-feeds in the PlayerTwo app for now. */
|
|
810
|
+
get<T = Json>(name: string): Promise<FeedAnswer<T> | FeedItems<T>>;
|
|
811
|
+
}
|
|
812
|
+
|
|
721
813
|
export interface P2 {
|
|
722
814
|
env: Env;
|
|
723
815
|
player: Player | null;
|
|
@@ -726,6 +818,15 @@ export interface P2 {
|
|
|
726
818
|
test: boolean;
|
|
727
819
|
/** True in a demo: demos earn no trophies. */
|
|
728
820
|
demo: boolean;
|
|
821
|
+
/** True for a guest: playing without a PlayerTwo account (in Instagram's browser, or a card on X). Nothing is sold to
|
|
822
|
+
a guest (the store's calls reject "guest"): ask them to sign up with signUp(). */
|
|
823
|
+
guest: boolean;
|
|
824
|
+
/** Asks a guest to sign up to PlayerTwo, on PlayerTwo's own card, with your reason (`why`, up to 80 characters: "To
|
|
825
|
+
buy the Gold Pack"). In PlayerTwo's player: { ok: true } as they press Sign up (your last saves are asked for,
|
|
826
|
+
the game closes, and it opens again for them signed in, their play theirs), or { ok: false, reason: "cancelled" }.
|
|
827
|
+
In a card on X: the card's own Continue on PlayerTwo comes up, { ok: true, card: true }. Rejects with code
|
|
828
|
+
"not-guest" (they have an account), "busy", "no-sign-up" or "offline". */
|
|
829
|
+
signUp(options?: { why?: string }): Promise<SignUpAnswer>;
|
|
729
830
|
/** The invite params the player came with (a join link, or a friend's Join), or null. */
|
|
730
831
|
params: Record<string, string> | null;
|
|
731
832
|
/** Any call by its name (the helpers are these). */
|
|
@@ -740,13 +841,31 @@ export interface P2 {
|
|
|
740
841
|
/** What the game sells inside itself: consumables, items and skins, mods, packs made in its studio, or charges its
|
|
741
842
|
own server makes at its own prices. */
|
|
742
843
|
store: Store;
|
|
844
|
+
/** Donations to the game's developer, when its studio put them inside the game. */
|
|
845
|
+
donate: Donate;
|
|
743
846
|
stats: Stats;
|
|
744
847
|
scores: Scores;
|
|
745
848
|
saves: Saves;
|
|
746
849
|
/** The line friends see, and with { join } what they join the player with. */
|
|
747
850
|
presence: { set(status: string | null, options?: { join?: Record<string, string> }): Promise<unknown> };
|
|
851
|
+
/** The player's PlayerTwo party, to play together: the leader decides (docs: /docs/sdk/travel#party-play). */
|
|
852
|
+
party: {
|
|
853
|
+
/** The party for your game, or null when the player isn't in one (or is a guest). Ids are each one's for your game. */
|
|
854
|
+
get(): Promise<PartyInfo | null>;
|
|
855
|
+
/** The leader in your game now and what their game joins friends with (p2.presence.set's join), or null. */
|
|
856
|
+
lead(): PartyLead | null;
|
|
857
|
+
/** Told as the leader's pick changes (null: the leader left your game). Says how to stop listening. */
|
|
858
|
+
on(type: "lead", fn: (lead: PartyLead | null) => void): () => void;
|
|
859
|
+
};
|
|
748
860
|
loading: { done(): Promise<unknown> };
|
|
749
861
|
gameplay: { start(): Promise<unknown>; stop(): Promise<unknown> };
|
|
862
|
+
/** A moment your game names itself (a lowercase letter, then up to 31 lowercase letters, digits and _), with data:
|
|
863
|
+
kept in your studio's analytics. Fire and forget: never throws, never waits; 200 a page load. */
|
|
864
|
+
track(name: string, data?: TrackData): void;
|
|
865
|
+
/** Your game's errors: caught by themselves, or reported by hand. */
|
|
866
|
+
errors: Errors;
|
|
867
|
+
/** Outside data PlayerTwo fetches for your game, each item signed for your room code to check. */
|
|
868
|
+
feeds: Feeds;
|
|
750
869
|
/** Multiplayer with no server of your own. */
|
|
751
870
|
rooms: Rooms;
|
|
752
871
|
/** Who the player is, to dress your game's character with: their critter as pixels and as a 3D model, and what they
|
|
@@ -775,16 +894,19 @@ export interface PlayerTwoGlobal {
|
|
|
775
894
|
init(options?: InitOptions): Promise<P2>;
|
|
776
895
|
/** Settles once init has been called and PlayerTwo found, or not. */
|
|
777
896
|
readonly ready: Promise<P2>;
|
|
897
|
+
/** On localhost, in a tab in the stand-in's party (?p2-party or local.party): "lead" takes the lead, "leave" leaves
|
|
898
|
+
the party, "join" comes back. Says the party as this tab now sees it. */
|
|
899
|
+
local?: { party(how: "lead" | "leave" | "join"): PartyInfo | null };
|
|
778
900
|
}
|
|
779
901
|
|
|
780
902
|
declare global {
|
|
781
|
-
/** PlayerTwo's SDK, from https://playertwo.
|
|
903
|
+
/** PlayerTwo's SDK, from https://playertwo.plus/sdk/v1.js (on PlayerTwo, already in the page). */
|
|
782
904
|
var PlayerTwo: PlayerTwoGlobal;
|
|
783
905
|
}
|
|
784
906
|
|
|
785
907
|
/** Where the SDK is loaded from when the page doesn't have it. */
|
|
786
908
|
export const SDK_URL: string;
|
|
787
|
-
/** Where it is looked for, in turn: SDK_URL (playertwo.
|
|
909
|
+
/** Where it is looked for, in turn: SDK_URL (playertwo.plus). */
|
|
788
910
|
export const SDK_URLS: readonly string[];
|
|
789
911
|
|
|
790
912
|
/** window.PlayerTwo, loading the script from `src`, else from each of SDK_URLS in turn, if the page doesn't have it. */
|
package/index.mjs
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
// playertwo-sdk: PlayerTwo's game SDK for code that is bundled (Vite, webpack, esbuild). The SDK itself is one
|
|
2
|
-
// script, https://playertwo.
|
|
2
|
+
// script, https://playertwo.plus/sdk/v1.js, which PlayerTwo's games host puts in a hosted game's page; this uses that
|
|
3
3
|
// copy when the page has it, and otherwise loads the script and waits for it. Types: index.d.ts. Docs:
|
|
4
|
-
// https://playertwo.
|
|
4
|
+
// https://playertwo.plus/docs/sdk
|
|
5
5
|
//
|
|
6
6
|
// import { init } from "playertwo-sdk";
|
|
7
7
|
// const p2 = await init(); // the same as PlayerTwo.init(), with the same options
|
|
8
8
|
|
|
9
|
-
export const SDK_URL = "https://playertwo.
|
|
10
|
-
/** Where the script is looked for, in turn:
|
|
11
|
-
|
|
12
|
-
export const SDK_URLS = [SDK_URL, "https://playertwo.plus/sdk/v1.js"];
|
|
9
|
+
export const SDK_URL = "https://playertwo.plus/sdk/v1.js";
|
|
10
|
+
/** Where the script is looked for, in turn: PlayerTwo's address, playertwo.plus. */
|
|
11
|
+
export const SDK_URLS = [SDK_URL];
|
|
13
12
|
|
|
14
13
|
let loading = null;
|
|
15
14
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playertwo-sdk",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "SDK for HTML5 and browser games on PlayerTwo (playertwo.
|
|
3
|
+
"version": "0.2.12",
|
|
4
|
+
"description": "SDK for HTML5 and browser games on PlayerTwo (playertwo.plus): trophies, leaderboards, stats, cloud saves, in-game purchases, donations to the developer, your own events and errors in your analytics, feeds of outside data, voice chat and multiplayer rooms with server-side room code.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"playertwo",
|
|
7
7
|
"player-two",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"typescript",
|
|
25
25
|
"threejs"
|
|
26
26
|
],
|
|
27
|
-
"homepage": "https://playertwo.
|
|
27
|
+
"homepage": "https://playertwo.plus/docs/sdk",
|
|
28
28
|
"bugs": {
|
|
29
29
|
"url": "https://github.com/TheDudeCommits/playertwo-sdk/issues"
|
|
30
30
|
},
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"directory": "packages/sdk"
|
|
35
35
|
},
|
|
36
36
|
"license": "MIT",
|
|
37
|
-
"author": "PlayerTwo <hello@playertwo.
|
|
37
|
+
"author": "PlayerTwo <hello@playertwo.plus>",
|
|
38
38
|
"type": "module",
|
|
39
39
|
"main": "./index.mjs",
|
|
40
40
|
"module": "./index.mjs",
|
package/room.d.ts
CHANGED
|
@@ -171,3 +171,8 @@ export interface RoomScript<D extends object = Record<string, any>> {
|
|
|
171
171
|
|
|
172
172
|
/** Types every hook at once: answers `script` as it is. */
|
|
173
173
|
export function defineRoom<D extends object = Record<string, any>>(script: RoomScript<D>): RoomScript<D>;
|
|
174
|
+
|
|
175
|
+
/** Whether a feed's item is as PlayerTwo fetched it (docs: /docs/sdk#rooms-feeds): `sig` is the hex HMAC-SHA256 of
|
|
176
|
+
JSON.stringify(item), keyed with the feed's key (its 64 hex characters, as text). Synchronous, plain JavaScript: put
|
|
177
|
+
the key in your room.js and check each `{ item, sig }` a page relays before you use it. */
|
|
178
|
+
export function checkFeedItem(key: string, item: unknown, sig: string): boolean;
|
package/room.mjs
CHANGED
|
@@ -4,3 +4,61 @@
|
|
|
4
4
|
export function defineRoom(script) {
|
|
5
5
|
return script;
|
|
6
6
|
}
|
|
7
|
+
|
|
8
|
+
/* ---------- a feed's signed items (docs: /docs/sdk#rooms-feeds) ---------- */
|
|
9
|
+
|
|
10
|
+
/* SHA-256 and HMAC in plain JavaScript, synchronous, so any hook can check an item at once (room code has no network,
|
|
11
|
+
and needn't count on WebCrypto, which is asynchronous where it is there at all) */
|
|
12
|
+
const K = new Uint32Array([
|
|
13
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
14
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
15
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
16
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
17
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
18
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
19
|
+
]);
|
|
20
|
+
function sha256(m) {
|
|
21
|
+
const n = ((m.length + 9 + 63) >> 6) << 6, b = new Uint8Array(n), w = new Uint32Array(64), h = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
|
|
22
|
+
b.set(m); b[m.length] = 0x80;
|
|
23
|
+
const bits = m.length * 8;
|
|
24
|
+
for (let i = 0; i < 4; i++) b[n - 1 - i] = (bits >>> (8 * i)) & 0xff;
|
|
25
|
+
b[n - 5] = Math.floor(bits / 2 ** 32) & 0xff;
|
|
26
|
+
for (let o = 0; o < n; o += 64) {
|
|
27
|
+
for (let i = 0; i < 16; i++) w[i] = (b[o + 4 * i] << 24) | (b[o + 4 * i + 1] << 16) | (b[o + 4 * i + 2] << 8) | b[o + 4 * i + 3];
|
|
28
|
+
for (let i = 16; i < 64; i++) {
|
|
29
|
+
const x = w[i - 15], y = w[i - 2];
|
|
30
|
+
w[i] = (w[i - 16] + (((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3)) + w[i - 7] + (((y >>> 17) | (y << 15)) ^ ((y >>> 19) | (y << 13)) ^ (y >>> 10))) | 0;
|
|
31
|
+
}
|
|
32
|
+
let [a, c, d, e, f, g, k, l] = h;
|
|
33
|
+
for (let i = 0; i < 64; i++) {
|
|
34
|
+
const t1 = (l + (((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7))) + ((f & g) ^ (~f & k)) + K[i] + w[i]) | 0;
|
|
35
|
+
const t2 = ((((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10))) + ((a & c) ^ (a & d) ^ (c & d))) | 0;
|
|
36
|
+
l = k; k = g; g = f; f = (e + t1) | 0; e = d; d = c; c = a; a = (t1 + t2) | 0;
|
|
37
|
+
}
|
|
38
|
+
h[0] += a; h[1] += c; h[2] += d; h[3] += e; h[4] += f; h[5] += g; h[6] += k; h[7] += l;
|
|
39
|
+
}
|
|
40
|
+
const out = new Uint8Array(32);
|
|
41
|
+
for (let i = 0; i < 8; i++) { out[4 * i] = h[i] >>> 24; out[4 * i + 1] = h[i] >>> 16; out[4 * i + 2] = h[i] >>> 8; out[4 * i + 3] = h[i]; }
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
/** The hex HMAC-SHA256 of `text`, keyed with `key`'s text (UTF-8), as a feed signs its items. */
|
|
45
|
+
function hmacHex(key, text) {
|
|
46
|
+
const enc = new TextEncoder();
|
|
47
|
+
let k = enc.encode(key);
|
|
48
|
+
if (k.length > 64) k = sha256(k);
|
|
49
|
+
const m = enc.encode(text), inner = new Uint8Array(64 + m.length), outer = new Uint8Array(96);
|
|
50
|
+
for (let i = 0; i < 64; i++) { inner[i] = (k[i] || 0) ^ 0x36; outer[i] = (k[i] || 0) ^ 0x5c; }
|
|
51
|
+
inner.set(m, 64);
|
|
52
|
+
outer.set(sha256(inner), 64);
|
|
53
|
+
return Array.from(sha256(outer), (x) => x.toString(16).padStart(2, "0")).join("");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Whether a feed's item is as PlayerTwo fetched it: `sig` is the hex HMAC-SHA256 of JSON.stringify(item), keyed with
|
|
57
|
+
the feed's key (its 64 hex characters, as text). Put the key in your room.js; a page relays `{ item, sig }`. */
|
|
58
|
+
export function checkFeedItem(key, item, sig) {
|
|
59
|
+
if (typeof key !== "string" || typeof sig !== "string" || sig.length !== 64) return false;
|
|
60
|
+
const want = hmacHex(key, JSON.stringify(item) ?? "null");
|
|
61
|
+
let d = 0;
|
|
62
|
+
for (let i = 0; i < 64; i++) d |= want.charCodeAt(i) ^ sig.charCodeAt(i);
|
|
63
|
+
return d === 0;
|
|
64
|
+
}
|