playertwo-sdk 0.2.6 → 0.2.9
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 +1 -1
- package/README.md +24 -24
- package/index.d.ts +36 -27
- package/index.mjs +5 -5
- package/package.json +3 -3
- package/room.d.ts +11 -4
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# playertwo-sdk
|
|
2
2
|
|
|
3
|
-
The game SDK for [
|
|
3
|
+
The game SDK for [PlayerTwo](https://playertwo.lol), with TypeScript types. Trophies, XP and levels, stats,
|
|
4
4
|
leaderboards, cloud saves, presence, invites, add-ons, in-game purchases, the player's avatar, travel between games,
|
|
5
5
|
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
|
-
hosted on
|
|
10
|
+
hosted on PlayerTwo gets its own address on `playertwo.cc` and every service below through one account per player.
|
|
11
11
|
|
|
12
12
|
- Docs: https://playertwo.lol/docs/sdk
|
|
13
13
|
- Everything a game gets, with a link to its part of the docs: https://playertwo.lol/llms.txt
|
|
@@ -25,12 +25,12 @@ Or no install at all: the SDK itself is one script.
|
|
|
25
25
|
<script src="https://playertwo.lol/sdk/v1.js"></script>
|
|
26
26
|
<script>
|
|
27
27
|
PlayerTwo.init().then((p2) => p2.trophies.unlock("first-win")).catch(() => {
|
|
28
|
-
/* not on
|
|
28
|
+
/* not on PlayerTwo (e.code "not-on-player-two"): the game plays on without services */
|
|
29
29
|
});
|
|
30
30
|
</script>
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
On
|
|
33
|
+
On PlayerTwo the script is already in your game's page. `init()` from the npm package uses that copy, and loads
|
|
34
34
|
`https://playertwo.lol/sdk/v1.js` only when the page doesn't have it (on localhost, say).
|
|
35
35
|
|
|
36
36
|
## Quick start
|
|
@@ -39,7 +39,7 @@ On Player Two the script is already in your game's page. `init()` from the npm p
|
|
|
39
39
|
import { init } from "playertwo-sdk";
|
|
40
40
|
|
|
41
41
|
const p2 = await init({
|
|
42
|
-
// localhost only: the stand-in's trophies (it refuses keys you don't list, as
|
|
42
|
+
// localhost only: the stand-in's trophies (it refuses keys you don't list, as PlayerTwo would)
|
|
43
43
|
local: { trophies: [{ key: "first-win", name: "First win", tier: 1 }] },
|
|
44
44
|
});
|
|
45
45
|
console.log(p2.env, p2.player?.name, p2.player?.level); // env: "p2" | "local" | "disabled"
|
|
@@ -61,8 +61,8 @@ 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.lol/docs/sdk#call-errors). Off
|
|
65
|
-
with code `not-on-player-two`; with `{ required: true }` the page says "Play this on
|
|
64
|
+
https://playertwo.lol/docs/sdk#call-errors). Off PlayerTwo and off localhost, `init()` rejects after 1.5 seconds
|
|
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
|
|
|
@@ -105,16 +105,16 @@ Multiplayer, through `p2.rooms`:
|
|
|
105
105
|
| Voice chat (24 a room) | `room.voice.join()`, `.leave()`, `.mute()`, `.volume(id, v)`, `.speaking(id)` | [rooms-voice](https://playertwo.lol/docs/sdk#rooms-voice) |
|
|
106
106
|
| A bad connection, on purpose | `p2.rooms.simulate({ lag, jitter, loss })` or `?p2-net=150,30,0.02` | [rooms-simulate](https://playertwo.lol/docs/sdk#rooms-simulate) |
|
|
107
107
|
|
|
108
|
-
Rooms are 1 to 64 players (256 in an area of a world) and are for games hosted on
|
|
109
|
-
your own site served through
|
|
108
|
+
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
|
+
your own site served through PlayerTwo). A release never mixes versions in a room. All limits:
|
|
110
110
|
https://playertwo.lol/docs/sdk#rooms-limits.
|
|
111
111
|
|
|
112
112
|
## A room's server: `playertwo-sdk/room`
|
|
113
113
|
|
|
114
114
|
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
|
-
|
|
115
|
+
PlayerTwo's servers: a sandbox with no network, 100 ms of CPU a call, 1 to 60 ticks a second. In a scripted room your
|
|
116
116
|
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
|
-
|
|
117
|
+
PlayerTwo takes the file out of your build and never serves it to players.
|
|
118
118
|
|
|
119
119
|
Plain JavaScript with JSDoc types (it is pushed as it is):
|
|
120
120
|
|
|
@@ -146,14 +146,14 @@ export const { tickRate, onCreate, onJoin, onLeave, onMessage, onSync, onTick }
|
|
|
146
146
|
Hooks: `onCreate`, `onStart`, `onJoin`, `onLeave`, `onMessage`, `onSync`, `onTick`. Everything `room` has:
|
|
147
147
|
[`room.d.ts`](./room.d.ts).
|
|
148
148
|
|
|
149
|
-
## Building without
|
|
149
|
+
## Building without PlayerTwo
|
|
150
150
|
|
|
151
151
|
On localhost, a stand-in answers every call from memory, and two tabs are two players. If your dev server serves
|
|
152
152
|
`/room.js`, the first tab runs it as the room's server. Give the stand-in your trophies, add-ons, products and params
|
|
153
153
|
with `init({ local: { ... } })`; `local: true` (or `{ force: true }`) uses it wherever the game runs. Voice needs a
|
|
154
|
-
room on
|
|
154
|
+
room on PlayerTwo. Docs: https://playertwo.lol/docs/sdk#start-local.
|
|
155
155
|
|
|
156
|
-
On
|
|
156
|
+
On PlayerTwo you, your team and your playtesters play in test mode (`p2.test` is true): every call works, nothing is
|
|
157
157
|
paid, and what you earn is kept apart. Docs: https://playertwo.lol/docs/sdk#test.
|
|
158
158
|
|
|
159
159
|
Using the script tag and only want the types of the global `PlayerTwo`? Add `"types": ["playertwo-sdk"]` to your
|
|
@@ -162,10 +162,10 @@ Using the script tag and only want the types of the global `PlayerTwo`? Add `"ty
|
|
|
162
162
|
## In-game purchases
|
|
163
163
|
|
|
164
164
|
Two ways, and a game can use both: products you make in your studio (`p2.store.buy(key)`), or charges your own server
|
|
165
|
-
makes at its own prices with your game key (`p2.store.pay(chargeId)`).
|
|
166
|
-
and the payment from their
|
|
165
|
+
makes at its own prices with your game key (`p2.store.pay(chargeId)`). PlayerTwo's own card takes the player's yes
|
|
166
|
+
and the payment from their PlayerTwo balance (USDC on Base). You get 80% of every in-game purchase, PlayerTwo keeps
|
|
167
167
|
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
|
-
as its sales are:
|
|
168
|
+
as its sales are: PlayerTwo keeps 20%. Every purchase gives your game a signed receipt (a JWT typed `p2-buy`, RS256,
|
|
169
169
|
keys at https://playertwo.lol/.well-known/p2-keys.json) and your studio's webhooks a `purchase` event. Docs:
|
|
170
170
|
https://playertwo.lol/docs/sdk#purchases.
|
|
171
171
|
|
|
@@ -176,7 +176,7 @@ A game key (`p2g_...`, made in your studio) lets your server check a session or
|
|
|
176
176
|
consumables and report matches, under `https://playertwo.lol/api/v1/sdk/server/...`. Docs:
|
|
177
177
|
https://playertwo.lol/docs/sdk#server and https://playertwo.lol/docs/sdk/connect (a game played on its own site).
|
|
178
178
|
|
|
179
|
-
## Putting it on
|
|
179
|
+
## Putting it on PlayerTwo
|
|
180
180
|
|
|
181
181
|
A whole game to start from: `npm create playertwo-game@latest my-game` (a small multiplayer Three.js game with rooms
|
|
182
182
|
and `room.js`, see [create-playertwo-game](https://www.npmjs.com/package/create-playertwo-game)).
|
|
@@ -190,12 +190,12 @@ curl -fsSLO https://playertwo.lol/cli/p2.mjs
|
|
|
190
190
|
P2_AGENT_KEY=p2_agent_... node p2.mjs push dist --game your-game --version 1.0.0
|
|
191
191
|
```
|
|
192
192
|
|
|
193
|
-
It plays at `https://your-game.playertwo.cc/` in
|
|
194
|
-
5 GB and 20,000 files; only the files
|
|
193
|
+
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.lol/docs/hosting.
|
|
195
195
|
|
|
196
196
|
## For AI agents: MCP and the agent API
|
|
197
197
|
|
|
198
|
-
|
|
198
|
+
PlayerTwo runs an MCP server (Streamable HTTP) at `https://playertwo.lol/api/mcp`:
|
|
199
199
|
|
|
200
200
|
```
|
|
201
201
|
claude mcp add --transport http player-two https://playertwo.lol/api/mcp
|
|
@@ -207,7 +207,7 @@ The first line is enough for the public tools, which need no key: `get_submissio
|
|
|
207
207
|
(submit it, edit its page, upload media and web builds, define trophies, leaderboards and products, roll back builds,
|
|
208
208
|
post updates, read its rooms) need the player's agent key, made in their studio on playertwo.lol under Agents, or no
|
|
209
209
|
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
|
|
210
|
+
one, the player is asked to sign in to PlayerTwo and allow it (OAuth 2.1 with PKCE). Its MCP Registry name is
|
|
211
211
|
`lol.playertwo/mcp`; its server card is at https://playertwo.lol/.well-known/mcp.json.
|
|
212
212
|
|
|
213
213
|
- The agent API: `GET https://playertwo.lol/api/v1/agent/guide` explains every field; submit with
|
|
@@ -218,4 +218,4 @@ one, the player is asked to sign in to Player Two and allow it (OAuth 2.1 with P
|
|
|
218
218
|
|
|
219
219
|
## License
|
|
220
220
|
|
|
221
|
-
MIT. Copyright (c) 2026
|
|
221
|
+
MIT. Copyright (c) 2026 PlayerTwo.
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Types for
|
|
2
|
+
* Types for PlayerTwo's game SDK, https://playertwo.lol/sdk/v1.js (its source: public/sdk/v1.js in PlayerTwo's code;
|
|
3
3
|
* keep these in step with its part 4, "the helpers the game uses", and part 4b, "rooms"). Docs: https://playertwo.lol/docs/sdk
|
|
4
4
|
*
|
|
5
5
|
* import { init } from "playertwo-sdk"; // or the global PlayerTwo, from the script tag
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
/** Anything that goes through JSON: what messages, the room's state and saves carry. */
|
|
13
13
|
export type Json = string | number | boolean | null | Json[] | { [key: string]: Json };
|
|
14
14
|
|
|
15
|
-
/** Where the game is: on
|
|
15
|
+
/** Where the game is: on PlayerTwo or in its app, on localhost (the stand-in), or anywhere else. */
|
|
16
16
|
export type Env = "p2" | "local" | "disabled";
|
|
17
17
|
|
|
18
18
|
/** Every refusal rejects with one of these. */
|
|
@@ -52,9 +52,9 @@ export interface LocalOptions {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
export interface InitOptions {
|
|
55
|
-
/** Outside
|
|
55
|
+
/** Outside PlayerTwo, fill the page with "Play this on PlayerTwo" and stop. */
|
|
56
56
|
required?: boolean;
|
|
57
|
-
/** The
|
|
57
|
+
/** The PlayerTwo app's local address (the app sets P2_BRIDGE itself). */
|
|
58
58
|
bridge?: string;
|
|
59
59
|
/** The stand-in's trophies, add-ons and params; true (or { force: true }) uses the stand-in wherever the game runs. */
|
|
60
60
|
local?: true | LocalOptions;
|
|
@@ -68,7 +68,7 @@ export interface Me {
|
|
|
68
68
|
session: string;
|
|
69
69
|
test: boolean;
|
|
70
70
|
demo: boolean;
|
|
71
|
-
/** "web":
|
|
71
|
+
/** "web": PlayerTwo's player; "desktop": the app. */
|
|
72
72
|
from: "web" | "desktop";
|
|
73
73
|
params: Record<string, string> | null;
|
|
74
74
|
}
|
|
@@ -224,7 +224,7 @@ export type Bought =
|
|
|
224
224
|
product: { key: string; kind: string; n: number };
|
|
225
225
|
/** A consumable's: how many the player has now. */
|
|
226
226
|
count?: number;
|
|
227
|
-
/** A JWT typed "p2-buy", RS256 with
|
|
227
|
+
/** A JWT typed "p2-buy", RS256 with PlayerTwo's keys (/.well-known/p2-keys.json), for your own server: an hour. */
|
|
228
228
|
receipt: string;
|
|
229
229
|
exp: number;
|
|
230
230
|
/** A test purchase (the game's team, a tester): nothing was paid. */
|
|
@@ -240,7 +240,7 @@ export type Paid =
|
|
|
240
240
|
charge: { id: string; name: string; ref: string | null };
|
|
241
241
|
/** What was paid, in US dollars (0 for a test). */
|
|
242
242
|
amount: number;
|
|
243
|
-
/** A JWT typed "p2-buy" (kind "charge", key the charge's id), RS256 with
|
|
243
|
+
/** A JWT typed "p2-buy" (kind "charge", key the charge's id), RS256 with PlayerTwo's keys, for your own server: an hour. */
|
|
244
244
|
receipt: string;
|
|
245
245
|
exp: number;
|
|
246
246
|
/** A test payment (the game's team, a tester): nothing was paid. */
|
|
@@ -253,13 +253,13 @@ export interface Store {
|
|
|
253
253
|
list(): Promise<Product[]>;
|
|
254
254
|
/** What the player has. */
|
|
255
255
|
owned(): Promise<Holding[]>;
|
|
256
|
-
/** Asks
|
|
257
|
-
"no-store" where it can't (outside
|
|
256
|
+
/** Asks PlayerTwo to sell one: its own step, outside the game's frame, where the player says yes. Rejects with code
|
|
257
|
+
"no-store" where it can't (outside PlayerTwo's player on the web), "busy" (its sheet is open already),
|
|
258
258
|
"unknown-product", "owned" (a skin, item or mod they have), "no-game" (they don't have the game yet), "guest" or
|
|
259
259
|
"offline". */
|
|
260
260
|
buy(key: string): Promise<Bought>;
|
|
261
261
|
/** Asks the player to pay a charge your server made (POST /api/v1/sdk/server/store/charges, your game's key): its
|
|
262
|
-
name and price on
|
|
262
|
+
name and price on PlayerTwo's card, paid once; one paid already answers its receipt again. Rejects with code
|
|
263
263
|
"unknown-charge", "expired" (30 minutes unpaid), "withdrawn" (your server cancelled it), "no-game", "busy",
|
|
264
264
|
"no-store", "guest" or "offline". */
|
|
265
265
|
pay(charge: string): Promise<Paid>;
|
|
@@ -332,6 +332,10 @@ export interface JoinOptions extends MatchOptions {
|
|
|
332
332
|
together?: boolean;
|
|
333
333
|
/** Keep the room as a replay, from its first player (a scripted room's code says so itself: room.record). */
|
|
334
334
|
record?: boolean;
|
|
335
|
+
/** false: the room stays where it opened, near its first player. By default (rooms from r21) a room opens again
|
|
336
|
+
between far-apart players by itself once it knows their round trips, and goes back if that wasn't faster for the
|
|
337
|
+
furthest one: each page goes there as it would come back ("reconnecting", "reconnected", "moved"). */
|
|
338
|
+
between?: boolean;
|
|
335
339
|
}
|
|
336
340
|
|
|
337
341
|
/** One of the game's public rooms open now (p2.rooms.live), to watch. */
|
|
@@ -392,7 +396,7 @@ export interface NetSimulation {
|
|
|
392
396
|
loss?: number;
|
|
393
397
|
}
|
|
394
398
|
|
|
395
|
-
/** Voice chat in a room: each player's microphone to everyone else in voice, through
|
|
399
|
+
/** Voice chat in a room: each player's microphone to everyone else in voice, through PlayerTwo's servers. */
|
|
396
400
|
export interface Voice {
|
|
397
401
|
/** Who is in voice: their ids. */
|
|
398
402
|
readonly who: readonly string[];
|
|
@@ -412,7 +416,7 @@ export interface Voice {
|
|
|
412
416
|
volume(id: string, volume: number): void;
|
|
413
417
|
/** Whether a player (or you) is speaking now. */
|
|
414
418
|
speaking(id: string): boolean;
|
|
415
|
-
/** Whether a player reaches you through
|
|
419
|
+
/** Whether a player reaches you through PlayerTwo's party voice now (your party, both of you in its voice, their
|
|
416
420
|
microphone on there): their voice in this room is silent to you meanwhile, so you don't hear them twice. They stay
|
|
417
421
|
in `who`, and "speaking" still says when they talk (as the party's voice hears them). */
|
|
418
422
|
viaParty(id: string): boolean;
|
|
@@ -547,6 +551,11 @@ export interface RoomStats {
|
|
|
547
551
|
/** Bytes a second, sent and heard. */
|
|
548
552
|
up: number;
|
|
549
553
|
down: number;
|
|
554
|
+
/** Player to player over the fast lane (ms there and back, measured every 2 s once it is up), by player id; {}
|
|
555
|
+
without a lane (PlayerTwo's rooms from r21). */
|
|
556
|
+
peers: Record<string, { ping: number; jitter: number }>;
|
|
557
|
+
/** The worst of `peers`, or null. */
|
|
558
|
+
lanePing: number | null;
|
|
550
559
|
}
|
|
551
560
|
|
|
552
561
|
export interface Room {
|
|
@@ -568,7 +577,7 @@ export interface Room {
|
|
|
568
577
|
readonly state: Record<string, any>;
|
|
569
578
|
/** Whose each key is (the first writer's; a leaver's become the host's). */
|
|
570
579
|
readonly owners: Record<string, string | null>;
|
|
571
|
-
/** The build has a room.js, running on
|
|
580
|
+
/** The build has a room.js, running on PlayerTwo's servers: only it writes the shared state. */
|
|
572
581
|
readonly scripted: boolean;
|
|
573
582
|
/** Whether it is a world, kept for good (p2.rooms.world). */
|
|
574
583
|
readonly world: boolean;
|
|
@@ -642,11 +651,11 @@ export interface Avatar {
|
|
|
642
651
|
level: number;
|
|
643
652
|
picture: string | null;
|
|
644
653
|
critter: {
|
|
645
|
-
/** The name it is drawn from: the same name, the same critter, everywhere on
|
|
654
|
+
/** The name it is drawn from: the same name, the same critter, everywhere on PlayerTwo. */
|
|
646
655
|
face: string;
|
|
647
656
|
/** Its box: 18 pixels a side. */
|
|
648
657
|
size: number;
|
|
649
|
-
/** The colour of the tile it stands on in
|
|
658
|
+
/** The colour of the tile it stands on in PlayerTwo's own pages. */
|
|
650
659
|
tile: string;
|
|
651
660
|
/** Every pixel of it, eyes open. */
|
|
652
661
|
pixels: Pixel[];
|
|
@@ -658,7 +667,7 @@ export interface Avatar {
|
|
|
658
667
|
worn: { key: string; name: string; slot: string; model: string | null; picture: string | null }[];
|
|
659
668
|
}
|
|
660
669
|
|
|
661
|
-
/** One of the player's things from
|
|
670
|
+
/** One of the player's things from PlayerTwo's shop in the Plaza (p2.items.list). */
|
|
662
671
|
export interface Item {
|
|
663
672
|
key: string;
|
|
664
673
|
name: string;
|
|
@@ -693,13 +702,13 @@ export interface Door {
|
|
|
693
702
|
owned: boolean;
|
|
694
703
|
}
|
|
695
704
|
|
|
696
|
-
/** Travel to other games on
|
|
705
|
+
/** Travel to other games on PlayerTwo, with the player's party. In PlayerTwo's player on the web; elsewhere each
|
|
697
706
|
rejects with code "no-travel". */
|
|
698
707
|
export interface Travel {
|
|
699
708
|
/** To another game, by its address (playertwo.lol/game/<slug>): it is given p2.params (yours, then `from`, this
|
|
700
709
|
game's address, and `place`). Resolves as the player goes; this page is closed after. */
|
|
701
710
|
to(slug: string, options?: { params?: Record<string, string>; place?: string }): Promise<{ to: { slug: string; title: string }; params: Record<string, string> }>;
|
|
702
|
-
/** Back to the Plaza, where every game on
|
|
711
|
+
/** Back to the Plaza, where every game on PlayerTwo has its screen. */
|
|
703
712
|
home(): Promise<{ to: { slug: null; title: string; url: string } }>;
|
|
704
713
|
/** The doors to show in your own menu: where the party is, the games friends are in, the player's recent ones, the
|
|
705
714
|
busiest (up to 12). */
|
|
@@ -722,9 +731,9 @@ export interface P2 {
|
|
|
722
731
|
/** Any call by its name (the helpers are these). */
|
|
723
732
|
call<T = unknown>(method: string, args?: unknown[]): Promise<T>;
|
|
724
733
|
me(): Promise<Me>;
|
|
725
|
-
/** Who is playing, for your own servers: a JWT signed by
|
|
734
|
+
/** Who is playing, for your own servers: a JWT signed by PlayerTwo (RS256, typed "p2-id"), ten minutes. */
|
|
726
735
|
identity(): Promise<string>;
|
|
727
|
-
/** A link to your game on
|
|
736
|
+
/** A link to your game on PlayerTwo that opens it with these params (up to 16 strings, 512 bytes). */
|
|
728
737
|
invite(params?: Record<string, string>): Promise<string>;
|
|
729
738
|
trophies: Trophies;
|
|
730
739
|
addons: Addons;
|
|
@@ -743,15 +752,15 @@ export interface P2 {
|
|
|
743
752
|
/** Who the player is, to dress your game's character with: their critter as pixels and as a 3D model, and what they
|
|
744
753
|
wear. */
|
|
745
754
|
avatar(): Promise<Avatar>;
|
|
746
|
-
/** What the player has from
|
|
755
|
+
/** What the player has from PlayerTwo's shop in the Plaza (avatars, skins, wearables, emotes, effects), and what
|
|
747
756
|
they have on: a game can dress its own character in them. */
|
|
748
757
|
items: { list(): Promise<Item[]> };
|
|
749
|
-
/** Travel to other games on
|
|
758
|
+
/** Travel to other games on PlayerTwo, with the player's party. */
|
|
750
759
|
travel: Travel;
|
|
751
|
-
/** Whether a small live picture of the game goes to the Plaza's screens while it is played here (
|
|
760
|
+
/** Whether a small live picture of the game goes to the Plaza's screens while it is played here (PlayerTwo does
|
|
752
761
|
it; the player can stop it). */
|
|
753
762
|
onAir: boolean;
|
|
754
|
-
/**
|
|
763
|
+
/** PlayerTwo's own layer over the game opened (true) or closed (false). */
|
|
755
764
|
on(type: "overlay", fn: (open: boolean) => void): () => void;
|
|
756
765
|
/** The player is about to close the game: what you keep should go now (a promise is waited for). */
|
|
757
766
|
on(type: "flush", fn: (closing: true) => void | Promise<unknown>): () => void;
|
|
@@ -762,14 +771,14 @@ export interface PlayerTwoGlobal {
|
|
|
762
771
|
readonly v: 1;
|
|
763
772
|
/** Certain once init has settled. */
|
|
764
773
|
env: Env;
|
|
765
|
-
/** Finds
|
|
774
|
+
/** Finds PlayerTwo once: every call answers the same promise, and the first call's options are the ones used. */
|
|
766
775
|
init(options?: InitOptions): Promise<P2>;
|
|
767
|
-
/** Settles once init has been called and
|
|
776
|
+
/** Settles once init has been called and PlayerTwo found, or not. */
|
|
768
777
|
readonly ready: Promise<P2>;
|
|
769
778
|
}
|
|
770
779
|
|
|
771
780
|
declare global {
|
|
772
|
-
/**
|
|
781
|
+
/** PlayerTwo's SDK, from https://playertwo.lol/sdk/v1.js (on PlayerTwo, already in the page). */
|
|
773
782
|
var PlayerTwo: PlayerTwoGlobal;
|
|
774
783
|
}
|
|
775
784
|
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// playertwo-sdk:
|
|
2
|
-
// script, https://playertwo.lol/sdk/v1.js, which
|
|
1
|
+
// playertwo-sdk: PlayerTwo's game SDK for code that is bundled (Vite, webpack, esbuild). The SDK itself is one
|
|
2
|
+
// script, https://playertwo.lol/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
4
|
// https://playertwo.lol/docs/sdk
|
|
5
5
|
//
|
|
@@ -23,7 +23,7 @@ function refused(code, message) {
|
|
|
23
23
|
/** window.PlayerTwo, loading the script first if the page doesn't have it (from `src`, or from each of SDK_URLS in turn
|
|
24
24
|
until one loads). */
|
|
25
25
|
export function load(src) {
|
|
26
|
-
if (typeof window === "undefined" || typeof document === "undefined") return Promise.reject(refused("no-window", "
|
|
26
|
+
if (typeof window === "undefined" || typeof document === "undefined") return Promise.reject(refused("no-window", "PlayerTwo's SDK runs in a browser."));
|
|
27
27
|
if (window.PlayerTwo && window.PlayerTwo.v === 1) return Promise.resolve(window.PlayerTwo);
|
|
28
28
|
if (loading) return loading;
|
|
29
29
|
const from = src ? [src] : SDK_URLS.slice();
|
|
@@ -33,11 +33,11 @@ export function load(src) {
|
|
|
33
33
|
s.async = true;
|
|
34
34
|
s.onload = () => {
|
|
35
35
|
if (window.PlayerTwo && window.PlayerTwo.v === 1) resolve(window.PlayerTwo);
|
|
36
|
-
else reject(refused("offline", "
|
|
36
|
+
else reject(refused("offline", "PlayerTwo's SDK loaded, but didn't start."));
|
|
37
37
|
};
|
|
38
38
|
s.onerror = () => {
|
|
39
39
|
s.remove();
|
|
40
|
-
reject(refused("offline", `
|
|
40
|
+
reject(refused("offline", `PlayerTwo's SDK couldn't be loaded from ${url}.`));
|
|
41
41
|
};
|
|
42
42
|
(document.head || document.documentElement).appendChild(s);
|
|
43
43
|
});
|
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
|
|
3
|
+
"version": "0.2.9",
|
|
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.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"playertwo",
|
|
7
7
|
"player-two",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"directory": "packages/sdk"
|
|
35
35
|
},
|
|
36
36
|
"license": "MIT",
|
|
37
|
-
"author": "
|
|
37
|
+
"author": "PlayerTwo <hello@playertwo.lol>",
|
|
38
38
|
"type": "module",
|
|
39
39
|
"main": "./index.mjs",
|
|
40
40
|
"module": "./index.mjs",
|
package/room.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Types for a room's own server script: room.js at the root of a build pushed to
|
|
2
|
+
* Types for a room's own server script: room.js at the root of a build pushed to PlayerTwo, run on PlayerTwo's
|
|
3
3
|
* servers, one copy for each room (on localhost, the SDK's stand-in runs it in the first tab). It is an ES module that
|
|
4
4
|
* exports the hooks it needs; in a room with a room.js only the script writes the shared state.
|
|
5
5
|
*
|
|
@@ -114,8 +114,15 @@ export interface Room<D extends object = Record<string, any>> {
|
|
|
114
114
|
the room has the edge): in the region nearest the furthest of them, every page moved there by itself
|
|
115
115
|
("reconnecting", "reconnected", "moved"). Your code starts again there as after a restart, with room.data and the
|
|
116
116
|
shared state (onCreate doesn't run again; what lives only in your module's own variables doesn't come along).
|
|
117
|
-
Players near each other stay where they are; once a minute at most. Call it as a match starts, say.
|
|
118
|
-
|
|
117
|
+
Players near each other stay where they are; once a minute at most. Call it as a match starts, say. It waits
|
|
118
|
+
until every player has a measured round trip, and the room it opens goes back where it was, once, when the
|
|
119
|
+
furthest player isn't at least 10 ms faster there (then it doesn't move by itself again). It picks only wnam,
|
|
120
|
+
enam, weur, apac and oc by itself. `{ region }` names one instead, not checked or undone. */
|
|
121
|
+
between(options?: { region?: "wnam" | "enam" | "sam" | "weur" | "apac" | "oc" | "me" }): void;
|
|
122
|
+
/** Where the room is: `colo`, the Cloudflare data centre it runs in ("FRA", "SIN"; null for its first moment),
|
|
123
|
+
`region`, the one it was opened in (null: near its first player), and `moved`, its last move (where it came
|
|
124
|
+
from, when, the worst round trip there in ms, and whether it was a move back). */
|
|
125
|
+
readonly where: { colo: string | null; region: string | null; moved: { from: string | null; at: number; before: number | null; back: boolean } | null } | null;
|
|
119
126
|
/** Yours to keep things in, for as long as the room is open (a world's: for good). */
|
|
120
127
|
data: D;
|
|
121
128
|
/** The room's own key-value store, read and written at once, for what doesn't fit in data (a world's map, its
|
|
@@ -127,7 +134,7 @@ export interface Room<D extends object = Record<string, any>> {
|
|
|
127
134
|
/** Its keys starting with `prefix`, in order (up to `limit`, 1000). */
|
|
128
135
|
keys(prefix?: string, limit?: number): string[];
|
|
129
136
|
};
|
|
130
|
-
/** Keep what the game knows of a player on
|
|
137
|
+
/** Keep what the game knows of a player on PlayerTwo's servers, for good (up to 64 KB): their player.saved in any
|
|
131
138
|
of the game's rooms and worlds from then on. null forgets it. */
|
|
132
139
|
save(id: string, data: Json | null): void;
|
|
133
140
|
/** The room's clock (ms), the same as the pages' room.now(). */
|