playertwo-sdk 0.2.9 → 0.2.11
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 +45 -5
- package/index.d.ts +124 -2
- package/package.json +2 -2
- package/room.d.ts +5 -0
- package/room.mjs +58 -0
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# playertwo-sdk
|
|
2
2
|
|
|
3
3
|
The game SDK for [PlayerTwo](https://playertwo.lol), with TypeScript types. Trophies, XP and levels, stats,
|
|
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`
|
|
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
|
|
@@ -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 */
|
|
@@ -75,9 +75,13 @@ with code `not-on-player-two`; with `{ required: true }` the page says "Play thi
|
|
|
75
75
|
| Cloud saves (8 slots, 1 MB each) | `p2.saves.list()`, `.get(slot)`, `.put(slot, data, { version })`, `.delete(slot)` | [call-saves](https://playertwo.lol/docs/sdk#call-saves) |
|
|
76
76
|
| Add-ons (sold on the game's page) | `p2.addons.list()`, `p2.addons.has(key)` | [addons](https://playertwo.lol/docs/sdk#addons) |
|
|
77
77
|
| In-game purchases | `p2.store.list()`, `.owned()`, `.buy(key)`, `.pay(chargeId)` | [purchases](https://playertwo.lol/docs/sdk#purchases) |
|
|
78
|
+
| Donations to you | `p2.donate.available()`, `.open()`, `.button({ corner, parent })` | [donate](https://playertwo.lol/docs/sdk#donate) |
|
|
78
79
|
| Presence | `p2.presence.set(line, { join })` | [call-presence](https://playertwo.lol/docs/sdk#call-presence) |
|
|
79
80
|
| Identity and invites | `p2.identity()`, `p2.invite(params)`, `p2.params` | [call-identity](https://playertwo.lol/docs/sdk#call-identity) |
|
|
80
81
|
| Loading and play events | `p2.loading.done()`, `p2.gameplay.start()`, `.stop()` | [call-gameplay](https://playertwo.lol/docs/sdk#call-gameplay) |
|
|
82
|
+
| Your game's own events, in your studio's analytics | `p2.track(name, data)` | [call-track](https://playertwo.lol/docs/sdk#call-track) |
|
|
83
|
+
| Its errors, caught by themselves | `p2.errors.report(error, where)`, `p2.errors.off()` | [call-crashes](https://playertwo.lol/docs/sdk#call-crashes) |
|
|
84
|
+
| Feeds: outside data PlayerTwo fetches for your game | `p2.feeds.get(name)` | [feeds](https://playertwo.lol/docs/sdk#feeds) |
|
|
81
85
|
| The overlay, and closing | `p2.on("overlay", fn)`, `p2.on("flush", fn)` | [call-overlay](https://playertwo.lol/docs/sdk#call-overlay) |
|
|
82
86
|
| On air in the Plaza | `p2.onAir` | [call-air](https://playertwo.lol/docs/sdk#call-air) |
|
|
83
87
|
| Test mode and demos | `p2.test`, `p2.demo` | [test](https://playertwo.lol/docs/sdk#test) |
|
|
@@ -97,6 +101,7 @@ Multiplayer, through `p2.rooms`:
|
|
|
97
101
|
| Corrections and lag-compensated hits | `onSync`, `room.correct`, `room.past` (room.js); `sync.seen(id)` | [rooms-corrections](https://playertwo.lol/docs/sdk#rooms-corrections) |
|
|
98
102
|
| Server-moved objects | `room.objects()` (page), `room.objects.set` (room.js) | [rooms-objects](https://playertwo.lol/docs/sdk#rooms-objects) |
|
|
99
103
|
| Physics engines in WebAssembly | a `room/` folder | [rooms-engines](https://playertwo.lol/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.lol/docs/sdk#rooms-feeds) |
|
|
100
105
|
| Teams and party matches | `{ teams, together }`, `room.team(id, n)` | [rooms-teams](https://playertwo.lol/docs/sdk#rooms-teams) |
|
|
101
106
|
| Matching by skill (Elo by mode) | `{ skill: true }`, `room.result(places)` | [rooms-matching](https://playertwo.lol/docs/sdk#rooms-matching) |
|
|
102
107
|
| Ranked boards and server-given trophies | `room.score`, `room.trophy` (room.js) | [rooms-ranked](https://playertwo.lol/docs/sdk#rooms-ranked) |
|
|
@@ -112,7 +117,7 @@ https://playertwo.lol/docs/sdk#rooms-limits.
|
|
|
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,11 +151,36 @@ 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.lol/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: { ... } })
|
|
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
|
|
154
184
|
room on PlayerTwo. Docs: https://playertwo.lol/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
|
|
@@ -169,6 +199,16 @@ as its sales are: PlayerTwo keeps 20%. Every purchase gives your game a signed r
|
|
|
169
199
|
keys at https://playertwo.lol/.well-known/p2-keys.json) and your studio's webhooks a `purchase` event. Docs:
|
|
170
200
|
https://playertwo.lol/docs/sdk#purchases.
|
|
171
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.lol/docs/sdk#donate.
|
|
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
|
package/index.d.ts
CHANGED
|
@@ -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
|
|
|
@@ -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,6 +894,9 @@ 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 {
|
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.lol): trophies, leaderboards, stats, cloud saves, in-game purchases, voice chat and multiplayer rooms with server-side room code.",
|
|
3
|
+
"version": "0.2.11",
|
|
4
|
+
"description": "SDK for HTML5 and browser games on PlayerTwo (playertwo.lol): 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",
|
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
|
+
}
|