playertwo-sdk 0.2.5 → 0.2.8
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 +34 -28
- package/index.mjs +17 -11
- 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 10%. 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>;
|
|
@@ -392,7 +392,7 @@ export interface NetSimulation {
|
|
|
392
392
|
loss?: number;
|
|
393
393
|
}
|
|
394
394
|
|
|
395
|
-
/** Voice chat in a room: each player's microphone to everyone else in voice, through
|
|
395
|
+
/** Voice chat in a room: each player's microphone to everyone else in voice, through PlayerTwo's servers. */
|
|
396
396
|
export interface Voice {
|
|
397
397
|
/** Who is in voice: their ids. */
|
|
398
398
|
readonly who: readonly string[];
|
|
@@ -412,6 +412,10 @@ export interface Voice {
|
|
|
412
412
|
volume(id: string, volume: number): void;
|
|
413
413
|
/** Whether a player (or you) is speaking now. */
|
|
414
414
|
speaking(id: string): boolean;
|
|
415
|
+
/** Whether a player reaches you through PlayerTwo's party voice now (your party, both of you in its voice, their
|
|
416
|
+
microphone on there): their voice in this room is silent to you meanwhile, so you don't hear them twice. They stay
|
|
417
|
+
in `who`, and "speaking" still says when they talk (as the party's voice hears them). */
|
|
418
|
+
viaParty(id: string): boolean;
|
|
415
419
|
}
|
|
416
420
|
|
|
417
421
|
export interface SendOptions {
|
|
@@ -564,7 +568,7 @@ export interface Room {
|
|
|
564
568
|
readonly state: Record<string, any>;
|
|
565
569
|
/** Whose each key is (the first writer's; a leaver's become the host's). */
|
|
566
570
|
readonly owners: Record<string, string | null>;
|
|
567
|
-
/** The build has a room.js, running on
|
|
571
|
+
/** The build has a room.js, running on PlayerTwo's servers: only it writes the shared state. */
|
|
568
572
|
readonly scripted: boolean;
|
|
569
573
|
/** Whether it is a world, kept for good (p2.rooms.world). */
|
|
570
574
|
readonly world: boolean;
|
|
@@ -638,11 +642,11 @@ export interface Avatar {
|
|
|
638
642
|
level: number;
|
|
639
643
|
picture: string | null;
|
|
640
644
|
critter: {
|
|
641
|
-
/** The name it is drawn from: the same name, the same critter, everywhere on
|
|
645
|
+
/** The name it is drawn from: the same name, the same critter, everywhere on PlayerTwo. */
|
|
642
646
|
face: string;
|
|
643
647
|
/** Its box: 18 pixels a side. */
|
|
644
648
|
size: number;
|
|
645
|
-
/** The colour of the tile it stands on in
|
|
649
|
+
/** The colour of the tile it stands on in PlayerTwo's own pages. */
|
|
646
650
|
tile: string;
|
|
647
651
|
/** Every pixel of it, eyes open. */
|
|
648
652
|
pixels: Pixel[];
|
|
@@ -654,7 +658,7 @@ export interface Avatar {
|
|
|
654
658
|
worn: { key: string; name: string; slot: string; model: string | null; picture: string | null }[];
|
|
655
659
|
}
|
|
656
660
|
|
|
657
|
-
/** One of the player's things from
|
|
661
|
+
/** One of the player's things from PlayerTwo's shop in the Plaza (p2.items.list). */
|
|
658
662
|
export interface Item {
|
|
659
663
|
key: string;
|
|
660
664
|
name: string;
|
|
@@ -689,13 +693,13 @@ export interface Door {
|
|
|
689
693
|
owned: boolean;
|
|
690
694
|
}
|
|
691
695
|
|
|
692
|
-
/** Travel to other games on
|
|
696
|
+
/** Travel to other games on PlayerTwo, with the player's party. In PlayerTwo's player on the web; elsewhere each
|
|
693
697
|
rejects with code "no-travel". */
|
|
694
698
|
export interface Travel {
|
|
695
699
|
/** To another game, by its address (playertwo.lol/game/<slug>): it is given p2.params (yours, then `from`, this
|
|
696
700
|
game's address, and `place`). Resolves as the player goes; this page is closed after. */
|
|
697
701
|
to(slug: string, options?: { params?: Record<string, string>; place?: string }): Promise<{ to: { slug: string; title: string }; params: Record<string, string> }>;
|
|
698
|
-
/** Back to the Plaza, where every game on
|
|
702
|
+
/** Back to the Plaza, where every game on PlayerTwo has its screen. */
|
|
699
703
|
home(): Promise<{ to: { slug: null; title: string; url: string } }>;
|
|
700
704
|
/** The doors to show in your own menu: where the party is, the games friends are in, the player's recent ones, the
|
|
701
705
|
busiest (up to 12). */
|
|
@@ -718,9 +722,9 @@ export interface P2 {
|
|
|
718
722
|
/** Any call by its name (the helpers are these). */
|
|
719
723
|
call<T = unknown>(method: string, args?: unknown[]): Promise<T>;
|
|
720
724
|
me(): Promise<Me>;
|
|
721
|
-
/** Who is playing, for your own servers: a JWT signed by
|
|
725
|
+
/** Who is playing, for your own servers: a JWT signed by PlayerTwo (RS256, typed "p2-id"), ten minutes. */
|
|
722
726
|
identity(): Promise<string>;
|
|
723
|
-
/** A link to your game on
|
|
727
|
+
/** A link to your game on PlayerTwo that opens it with these params (up to 16 strings, 512 bytes). */
|
|
724
728
|
invite(params?: Record<string, string>): Promise<string>;
|
|
725
729
|
trophies: Trophies;
|
|
726
730
|
addons: Addons;
|
|
@@ -739,15 +743,15 @@ export interface P2 {
|
|
|
739
743
|
/** Who the player is, to dress your game's character with: their critter as pixels and as a 3D model, and what they
|
|
740
744
|
wear. */
|
|
741
745
|
avatar(): Promise<Avatar>;
|
|
742
|
-
/** What the player has from
|
|
746
|
+
/** What the player has from PlayerTwo's shop in the Plaza (avatars, skins, wearables, emotes, effects), and what
|
|
743
747
|
they have on: a game can dress its own character in them. */
|
|
744
748
|
items: { list(): Promise<Item[]> };
|
|
745
|
-
/** Travel to other games on
|
|
749
|
+
/** Travel to other games on PlayerTwo, with the player's party. */
|
|
746
750
|
travel: Travel;
|
|
747
|
-
/** Whether a small live picture of the game goes to the Plaza's screens while it is played here (
|
|
751
|
+
/** Whether a small live picture of the game goes to the Plaza's screens while it is played here (PlayerTwo does
|
|
748
752
|
it; the player can stop it). */
|
|
749
753
|
onAir: boolean;
|
|
750
|
-
/**
|
|
754
|
+
/** PlayerTwo's own layer over the game opened (true) or closed (false). */
|
|
751
755
|
on(type: "overlay", fn: (open: boolean) => void): () => void;
|
|
752
756
|
/** The player is about to close the game: what you keep should go now (a promise is waited for). */
|
|
753
757
|
on(type: "flush", fn: (closing: true) => void | Promise<unknown>): () => void;
|
|
@@ -758,25 +762,27 @@ export interface PlayerTwoGlobal {
|
|
|
758
762
|
readonly v: 1;
|
|
759
763
|
/** Certain once init has settled. */
|
|
760
764
|
env: Env;
|
|
761
|
-
/** Finds
|
|
765
|
+
/** Finds PlayerTwo once: every call answers the same promise, and the first call's options are the ones used. */
|
|
762
766
|
init(options?: InitOptions): Promise<P2>;
|
|
763
|
-
/** Settles once init has been called and
|
|
767
|
+
/** Settles once init has been called and PlayerTwo found, or not. */
|
|
764
768
|
readonly ready: Promise<P2>;
|
|
765
769
|
}
|
|
766
770
|
|
|
767
771
|
declare global {
|
|
768
|
-
/**
|
|
772
|
+
/** PlayerTwo's SDK, from https://playertwo.lol/sdk/v1.js (on PlayerTwo, already in the page). */
|
|
769
773
|
var PlayerTwo: PlayerTwoGlobal;
|
|
770
774
|
}
|
|
771
775
|
|
|
772
776
|
/** Where the SDK is loaded from when the page doesn't have it. */
|
|
773
777
|
export const SDK_URL: string;
|
|
778
|
+
/** Where it is looked for, in turn: SDK_URL (playertwo.lol), then playertwo.plus. */
|
|
779
|
+
export const SDK_URLS: readonly string[];
|
|
774
780
|
|
|
775
|
-
/** window.PlayerTwo, loading the script from `src
|
|
781
|
+
/** window.PlayerTwo, loading the script from `src`, else from each of SDK_URLS in turn, if the page doesn't have it. */
|
|
776
782
|
export function load(src?: string): Promise<PlayerTwoGlobal>;
|
|
777
783
|
|
|
778
784
|
/** PlayerTwo.init(options), loading the SDK first if the page doesn't have it. */
|
|
779
785
|
export function init(options?: InitOptions): Promise<P2>;
|
|
780
786
|
|
|
781
|
-
declare const sdk: { init: typeof init; load: typeof load; SDK_URL: string };
|
|
787
|
+
declare const sdk: { init: typeof init; load: typeof load; SDK_URL: string; SDK_URLS: readonly string[] };
|
|
782
788
|
export default sdk;
|
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
|
//
|
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
// const p2 = await init(); // the same as PlayerTwo.init(), with the same options
|
|
8
8
|
|
|
9
9
|
export const SDK_URL = "https://playertwo.lol/sdk/v1.js";
|
|
10
|
+
/** Where the script is looked for, in turn: playertwo.lol, then playertwo.plus (which stood in for it while it was held
|
|
11
|
+
by its registry, 2026-11-09, and still serves the same SDK). */
|
|
12
|
+
export const SDK_URLS = [SDK_URL, "https://playertwo.plus/sdk/v1.js"];
|
|
10
13
|
|
|
11
14
|
let loading = null;
|
|
12
15
|
|
|
@@ -17,26 +20,29 @@ function refused(code, message) {
|
|
|
17
20
|
return e;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
/** window.PlayerTwo, loading the script first if the page doesn't have it
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
until one loads). */
|
|
25
|
+
export function load(src) {
|
|
26
|
+
if (typeof window === "undefined" || typeof document === "undefined") return Promise.reject(refused("no-window", "PlayerTwo's SDK runs in a browser."));
|
|
23
27
|
if (window.PlayerTwo && window.PlayerTwo.v === 1) return Promise.resolve(window.PlayerTwo);
|
|
24
28
|
if (loading) return loading;
|
|
25
|
-
|
|
29
|
+
const from = src ? [src] : SDK_URLS.slice();
|
|
30
|
+
const one = (url) => new Promise((resolve, reject) => {
|
|
26
31
|
const s = document.createElement("script");
|
|
27
|
-
s.src =
|
|
32
|
+
s.src = url;
|
|
28
33
|
s.async = true;
|
|
29
34
|
s.onload = () => {
|
|
30
35
|
if (window.PlayerTwo && window.PlayerTwo.v === 1) resolve(window.PlayerTwo);
|
|
31
|
-
else
|
|
36
|
+
else reject(refused("offline", "PlayerTwo's SDK loaded, but didn't start."));
|
|
32
37
|
};
|
|
33
38
|
s.onerror = () => {
|
|
34
|
-
loading = null;
|
|
35
39
|
s.remove();
|
|
36
|
-
reject(refused("offline", `
|
|
40
|
+
reject(refused("offline", `PlayerTwo's SDK couldn't be loaded from ${url}.`));
|
|
37
41
|
};
|
|
38
42
|
(document.head || document.documentElement).appendChild(s);
|
|
39
43
|
});
|
|
44
|
+
const next = (i) => one(from[i]).catch((e) => (i + 1 < from.length && e.message.includes("couldn't be loaded") ? next(i + 1) : Promise.reject(e)));
|
|
45
|
+
loading = next(0).catch((e) => { loading = null; throw e; });
|
|
40
46
|
return loading;
|
|
41
47
|
}
|
|
42
48
|
|
|
@@ -45,4 +51,4 @@ export function init(options) {
|
|
|
45
51
|
return load().then((PlayerTwo) => PlayerTwo.init(options));
|
|
46
52
|
}
|
|
47
53
|
|
|
48
|
-
export default { init, load, SDK_URL };
|
|
54
|
+
export default { init, load, SDK_URL, SDK_URLS };
|
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.8",
|
|
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(). */
|