playertwo-sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/index.d.ts +420 -0
- package/index.mjs +48 -0
- package/package.json +36 -4
- package/room.d.ts +79 -0
- package/room.mjs +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Player Two
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# playertwo-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Player Two's game SDK for bundled code, with TypeScript types: trophies, stats, leaderboards, cloud saves, presence,
|
|
4
|
+
invites, add-ons and multiplayer rooms, for games hosted on [Player Two](https://playertwo.lol).
|
|
5
|
+
|
|
6
|
+
The SDK itself is one script, `https://playertwo.lol/sdk/v1.js`. On Player Two it is already in your game's page;
|
|
7
|
+
`init()` uses that copy, and loads the script only when the page doesn't have it (on localhost, say, where its
|
|
8
|
+
stand-in answers every call and two tabs are two players).
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm install playertwo-sdk
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { init } from "playertwo-sdk";
|
|
16
|
+
|
|
17
|
+
const p2 = await init();
|
|
18
|
+
await p2.trophies.unlock("first-win");
|
|
19
|
+
|
|
20
|
+
const room = await p2.rooms.join({ mode: "arena", size: 16 });
|
|
21
|
+
room.on("message:emoted", ({ id, kind }) => showEmote(id, kind));
|
|
22
|
+
room.send("emote", "wave");
|
|
23
|
+
|
|
24
|
+
const sync = room.sync(myMesh, { fields: ["position.x", "position.y", "position.z", "rotation.y"], rate: 20, delay: 100, angles: ["rotation.y"] });
|
|
25
|
+
sync.on("join", (id) => sync.bind(id, makeMesh(id)));
|
|
26
|
+
// every frame: sync.step();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Only want the types for the script tag's global `PlayerTwo`? Add `"types": ["playertwo-sdk"]` to your `tsconfig.json`,
|
|
30
|
+
or put `/// <reference types="playertwo-sdk" />` at the top of a file.
|
|
31
|
+
|
|
32
|
+
## A room's server: playertwo-sdk/room
|
|
33
|
+
|
|
34
|
+
`room.js` at the root of your build runs on Player Two's servers, one copy per room. It is pushed as it is, so plain
|
|
35
|
+
JavaScript with JSDoc types is the simplest:
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
// @ts-check
|
|
39
|
+
/** @typedef {import("playertwo-sdk/room").Room} Room */
|
|
40
|
+
/** @typedef {import("playertwo-sdk/room").Player} Player */
|
|
41
|
+
|
|
42
|
+
export const tickRate = 20;
|
|
43
|
+
|
|
44
|
+
/** @param {Room} room @param {Player} player */
|
|
45
|
+
export function onJoin(room, player) {
|
|
46
|
+
room.state.set("hello", player.handle);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
If you bundle it into one file yourself (from TypeScript, say), `defineRoom` types every hook at once:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { defineRoom } from "playertwo-sdk/room";
|
|
54
|
+
|
|
55
|
+
export const { tickRate, onCreate, onJoin, onLeave, onMessage, onSync, onTick } = defineRoom<{ coins: number }>({
|
|
56
|
+
tickRate: 20,
|
|
57
|
+
onCreate(room) { room.data.coins = 8; },
|
|
58
|
+
onSync(room, player, values) { return Math.abs(values["position.x"]) < 18 ? values : false; },
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Docs: [the SDK](https://playertwo.lol/docs/sdk) and [rooms](https://playertwo.lol/docs/sdk#rooms). A whole game to start
|
|
63
|
+
from: `npm create playertwo-game@latest my-game`.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for Player Two's game SDK, https://playertwo.lol/sdk/v1.js (its source: public/sdk/v1.js in Player Two's code;
|
|
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
|
+
*
|
|
5
|
+
* import { init } from "playertwo-sdk"; // or the global PlayerTwo, from the script tag
|
|
6
|
+
* const p2 = await init();
|
|
7
|
+
* const room = await p2.rooms.join({ mode: "arena", size: 16 });
|
|
8
|
+
*
|
|
9
|
+
* The types of a room's own server script (room.js) are in "playertwo-sdk/room".
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Anything that goes through JSON: what messages, the room's state and saves carry. */
|
|
13
|
+
export type Json = string | number | boolean | null | Json[] | { [key: string]: Json };
|
|
14
|
+
|
|
15
|
+
/** Where the game is: on Player Two or in its app, on localhost (the stand-in), or anywhere else. */
|
|
16
|
+
export type Env = "p2" | "local" | "disabled";
|
|
17
|
+
|
|
18
|
+
/** Every refusal rejects with one of these. */
|
|
19
|
+
export interface P2Error extends Error {
|
|
20
|
+
/** "not-on-player-two", "offline", "timeout", "unknown-trophy", "too-soon", "demo", "read-first", "version",
|
|
21
|
+
"cooldown", "full", "empty", "too-big", "old-build", "not-yours", "signed-out", "session-ended", ... */
|
|
22
|
+
code: string;
|
|
23
|
+
/** The HTTP status behind it, or 0. */
|
|
24
|
+
status: number;
|
|
25
|
+
/** A save refused for its version: the version there now. */
|
|
26
|
+
version?: number;
|
|
27
|
+
[extra: string]: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The player, as your game knows them: `id` is theirs for your game only. */
|
|
31
|
+
export interface Player {
|
|
32
|
+
id: string;
|
|
33
|
+
handle: string;
|
|
34
|
+
name: string;
|
|
35
|
+
picture: string | null;
|
|
36
|
+
creature: string;
|
|
37
|
+
level: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface Game {
|
|
41
|
+
slug: string;
|
|
42
|
+
title: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The stand-in's own trophies, add-ons and params, on localhost (or wherever, with `force`). */
|
|
46
|
+
export interface LocalOptions {
|
|
47
|
+
trophies?: { key: string; name?: string; how?: string; tier?: number; hidden?: boolean }[];
|
|
48
|
+
addons?: { key: string; name?: string; about?: string; price?: number; owned?: boolean }[];
|
|
49
|
+
params?: Record<string, string>;
|
|
50
|
+
/** Use the stand-in wherever the game runs. */
|
|
51
|
+
force?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface InitOptions {
|
|
55
|
+
/** Outside Player Two, fill the page with "Play this on Player Two" and stop. */
|
|
56
|
+
required?: boolean;
|
|
57
|
+
/** The Player Two app's local address (the app sets P2_BRIDGE itself). */
|
|
58
|
+
bridge?: string;
|
|
59
|
+
/** The stand-in's trophies, add-ons and params; true (or { force: true }) uses the stand-in wherever the game runs. */
|
|
60
|
+
local?: true | LocalOptions;
|
|
61
|
+
/** How long to wait for the player's page to answer, in ms (1500). */
|
|
62
|
+
timeout?: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface Me {
|
|
66
|
+
player: Player;
|
|
67
|
+
game: Game;
|
|
68
|
+
session: string;
|
|
69
|
+
test: boolean;
|
|
70
|
+
demo: boolean;
|
|
71
|
+
/** "web": Player Two's player; "desktop": the app. */
|
|
72
|
+
from: "web" | "desktop";
|
|
73
|
+
params: Record<string, string> | null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface Progress {
|
|
77
|
+
into: number;
|
|
78
|
+
need: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface Trophy {
|
|
82
|
+
key: string;
|
|
83
|
+
name: string;
|
|
84
|
+
how: string;
|
|
85
|
+
tier: number;
|
|
86
|
+
xp: number;
|
|
87
|
+
hidden: boolean;
|
|
88
|
+
art: string;
|
|
89
|
+
share: number;
|
|
90
|
+
rarity: string;
|
|
91
|
+
/** When the player earned it (ms), and which player to earn it they were (No. 1 is the first). */
|
|
92
|
+
earned: { at: number; n: number | null; test?: boolean } | null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface TrophyList {
|
|
96
|
+
trophies: Trophy[];
|
|
97
|
+
xp: number;
|
|
98
|
+
level: number;
|
|
99
|
+
progress?: Progress;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface Unlocked {
|
|
103
|
+
trophy: Trophy;
|
|
104
|
+
/** false if they had it already. */
|
|
105
|
+
new: boolean;
|
|
106
|
+
n: number | null;
|
|
107
|
+
/** pending: XP that waits until your game counts toward levels. */
|
|
108
|
+
xp: { gained: number; total: number; pending?: number };
|
|
109
|
+
level: number;
|
|
110
|
+
progress?: Progress;
|
|
111
|
+
/** The completion trophy, earned with this one. */
|
|
112
|
+
complete?: Trophy;
|
|
113
|
+
test?: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface Addon {
|
|
117
|
+
key: string;
|
|
118
|
+
name: string;
|
|
119
|
+
about: string;
|
|
120
|
+
price: number;
|
|
121
|
+
owned: boolean;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface StatsChange {
|
|
125
|
+
stats: Record<string, number>;
|
|
126
|
+
/** Trophies waiting on a stat, earned by this change. */
|
|
127
|
+
unlocked: Unlocked[];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface Board {
|
|
131
|
+
key: string;
|
|
132
|
+
name: string;
|
|
133
|
+
order: "high" | "low";
|
|
134
|
+
format: string;
|
|
135
|
+
[more: string]: unknown;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface Submitted {
|
|
139
|
+
board: Board;
|
|
140
|
+
value: number;
|
|
141
|
+
/** Only a player's best is kept. */
|
|
142
|
+
best: number;
|
|
143
|
+
new: boolean;
|
|
144
|
+
previous: number | null;
|
|
145
|
+
rank: number | null;
|
|
146
|
+
total: number | null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface BoardRow {
|
|
150
|
+
rank: number;
|
|
151
|
+
value: number;
|
|
152
|
+
at: number;
|
|
153
|
+
player: { id: string; handle: string; name: string; picture: string | null };
|
|
154
|
+
me?: boolean;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface BoardList {
|
|
158
|
+
board: Board;
|
|
159
|
+
rows: BoardRow[];
|
|
160
|
+
total: number;
|
|
161
|
+
me: { rank: number; value: number } | null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface SaveSlot {
|
|
165
|
+
slot: string;
|
|
166
|
+
size: number;
|
|
167
|
+
version: number;
|
|
168
|
+
at: number;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface SaveList {
|
|
172
|
+
slots: SaveSlot[];
|
|
173
|
+
max: number;
|
|
174
|
+
maxBytes: number;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** A save read back, as bytes, text or JSON. */
|
|
178
|
+
export interface Save extends SaveSlot {
|
|
179
|
+
/** base64 */
|
|
180
|
+
data: string;
|
|
181
|
+
bytes(): Uint8Array;
|
|
182
|
+
text(): string;
|
|
183
|
+
json<T = any>(): T;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export interface Trophies {
|
|
187
|
+
list(): Promise<TrophyList>;
|
|
188
|
+
/** The player sees the toast; your game doesn't have to. */
|
|
189
|
+
unlock(key: string): Promise<Unlocked>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export interface Addons {
|
|
193
|
+
/** Each add-on on sale. */
|
|
194
|
+
list(): Promise<Addon[]>;
|
|
195
|
+
/** Whether the player has it (one taken off sale stays theirs). */
|
|
196
|
+
has(key: string): Promise<boolean>;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export interface Stats {
|
|
200
|
+
/** Every stat. */
|
|
201
|
+
get(): Promise<{ stats: Record<string, number> }>;
|
|
202
|
+
/** One stat's value (0 if it was never set). */
|
|
203
|
+
get(key: string): Promise<number>;
|
|
204
|
+
set(key: string, value: number): Promise<StatsChange>;
|
|
205
|
+
/** By 1, or any whole number. */
|
|
206
|
+
add(key: string, amount?: number): Promise<StatsChange>;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface Scores {
|
|
210
|
+
submit(board: string, value: number): Promise<Submitted>;
|
|
211
|
+
/** The top 10, or around the player, or their friends. */
|
|
212
|
+
list(board: string, options?: { around?: "top" | "me"; friends?: boolean; limit?: number }): Promise<BoardList>;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface Saves {
|
|
216
|
+
list(): Promise<SaveList>;
|
|
217
|
+
get(slot: string): Promise<Save>;
|
|
218
|
+
/** Text, an object (kept as JSON) or bytes, up to 1 MB; { version } to have it refused if another device saved since. */
|
|
219
|
+
put(slot: string, data: string | Json | ArrayBuffer | ArrayBufferView, options?: { version?: number }): Promise<SaveSlot>;
|
|
220
|
+
delete(slot: string): Promise<{ deleted: true; slot: string }>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/* ---------- rooms ---------- */
|
|
224
|
+
|
|
225
|
+
/** Someone in a room. */
|
|
226
|
+
export interface RoomPlayer {
|
|
227
|
+
/** The same as their p2.player.id. */
|
|
228
|
+
id: string;
|
|
229
|
+
handle: string;
|
|
230
|
+
name: string;
|
|
231
|
+
picture: string | null;
|
|
232
|
+
face: string;
|
|
233
|
+
level: number;
|
|
234
|
+
kind: string;
|
|
235
|
+
/** Their connection dropped; their seat is held for them (20 s). */
|
|
236
|
+
away: boolean;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export interface MatchOptions {
|
|
240
|
+
/** A name you choose ("duel", "coop-hard"). */
|
|
241
|
+
mode?: string;
|
|
242
|
+
/** 1 to 64 players. */
|
|
243
|
+
size?: number;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export interface JoinOptions extends MatchOptions {
|
|
247
|
+
/** A private room's six-letter code. */
|
|
248
|
+
code?: string;
|
|
249
|
+
/** The room you were in (room.id), to come back to it. */
|
|
250
|
+
room?: string;
|
|
251
|
+
resume?: string;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export interface Rooms {
|
|
255
|
+
/** A match ({ mode, size }: the fullest open room of that mode and size, or a new one), a room by its code
|
|
256
|
+
({ code }), or the room you were in ({ room, resume }). Resolves once you are in. */
|
|
257
|
+
join(options?: JoinOptions): Promise<Room>;
|
|
258
|
+
/** A new private room; friends join it by room.code. */
|
|
259
|
+
create(options?: MatchOptions): Promise<Room>;
|
|
260
|
+
/** Your party's own room, with only its members. */
|
|
261
|
+
party(options?: MatchOptions): Promise<Room>;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface SendOptions {
|
|
265
|
+
/** One player's id, or a list; everyone else in the room when left out. */
|
|
266
|
+
to?: string | string[];
|
|
267
|
+
/** The fast lane: lossy, for updates that the next one replaces (where a thing is, not what happened). */
|
|
268
|
+
fast?: boolean;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** A sync's values: each field's number, by its path ("position.x"). */
|
|
272
|
+
export type SyncValues = Record<string, number>;
|
|
273
|
+
|
|
274
|
+
export interface SyncOptions {
|
|
275
|
+
/** The numbers to send, as paths on the object ("position.x", "rotation.y"); for a THREE.Object3D, position x, y, z
|
|
276
|
+
and rotation.y when left out, otherwise ["x", "y"]. */
|
|
277
|
+
fields?: string[];
|
|
278
|
+
/** Sends a second. */
|
|
279
|
+
rate?: number;
|
|
280
|
+
/** How far behind (ms) bound objects are drawn, so there are two updates to move between. */
|
|
281
|
+
delay?: number;
|
|
282
|
+
/** Fields that are angles in radians, which turn the short way round. */
|
|
283
|
+
angles?: string[];
|
|
284
|
+
/** A jump longer than this between two updates is a teleport, not a glide (0: never). */
|
|
285
|
+
snap?: number;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** One object of yours sent to the room, and everyone else's moved smoothly (room.sync). */
|
|
289
|
+
export interface Sync {
|
|
290
|
+
/** A player's first values arrived (in the order of `fields`): bind something to them. */
|
|
291
|
+
on(type: "join", fn: (id: string, values: number[]) => void): () => void;
|
|
292
|
+
/** They left, or stopped syncing. */
|
|
293
|
+
on(type: "leave", fn: (id: string) => void): () => void;
|
|
294
|
+
/** A scripted room's code refused or corrected your move (onSync): your object was put where it says. */
|
|
295
|
+
on(type: "fix", fn: (values: number[]) => void): () => void;
|
|
296
|
+
/** Move `target`'s fields to where player `id` was `delay` ms ago, on every step(). */
|
|
297
|
+
bind(id: string, target: object): void;
|
|
298
|
+
/** Once a frame: sends yours at `rate`, and moves every bound object. */
|
|
299
|
+
step(): void;
|
|
300
|
+
/** A player's smoothed values now (in the order of `fields`), or null before their first update. */
|
|
301
|
+
values(id: string): number[] | null;
|
|
302
|
+
/** Stop sending and moving. */
|
|
303
|
+
stop(): void;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export interface Room {
|
|
307
|
+
/** The room's name (to come back to it with). */
|
|
308
|
+
readonly id: string;
|
|
309
|
+
/** A private room's code, or null. */
|
|
310
|
+
readonly code: string | null;
|
|
311
|
+
readonly mode: string;
|
|
312
|
+
readonly size: number;
|
|
313
|
+
/** Your id in the room (the same as p2.player.id). */
|
|
314
|
+
readonly me: string;
|
|
315
|
+
/** The host's id: whoever has been here longest. */
|
|
316
|
+
readonly host: string;
|
|
317
|
+
/** Who is here, you too. */
|
|
318
|
+
readonly players: RoomPlayer[];
|
|
319
|
+
/** The shared state, key to value. */
|
|
320
|
+
readonly state: Record<string, any>;
|
|
321
|
+
/** Whose each key is (the first writer's; a leaver's become the host's). */
|
|
322
|
+
readonly owners: Record<string, string | null>;
|
|
323
|
+
/** The build has a room.js, running on Player Two's servers: only it writes the shared state. */
|
|
324
|
+
readonly scripted: boolean;
|
|
325
|
+
|
|
326
|
+
/** Send `data` as a message of `type` to everyone else, or to `to`. In a scripted room, room.js hears it (onMessage). */
|
|
327
|
+
send(type: string, data?: Json, options?: SendOptions): void;
|
|
328
|
+
/** A key of the shared state (null takes it away): set here at once, refused ("error", not-yours) if it isn't yours. */
|
|
329
|
+
set(key: string, value: Json | undefined): void;
|
|
330
|
+
get<T = any>(key: string): T | undefined;
|
|
331
|
+
/** Whether you are the host. */
|
|
332
|
+
isHost(): boolean;
|
|
333
|
+
/** There and back, in ms (it also sets room.now()). */
|
|
334
|
+
ping(): Promise<number>;
|
|
335
|
+
/** The room's clock, the same for every player in it (ms). */
|
|
336
|
+
now(): number;
|
|
337
|
+
/** Leave for good (your seat goes to someone else). */
|
|
338
|
+
leave(): void;
|
|
339
|
+
/** Send `target`'s fields `rate` times a second on the fast lane, and move bound objects to where their players were. */
|
|
340
|
+
sync(target: object, options: SyncOptions): Sync;
|
|
341
|
+
|
|
342
|
+
on(type: "join" | "away" | "back", fn: (player: RoomPlayer) => void): () => void;
|
|
343
|
+
/** why: "left", "dropped", "kicked", ... */
|
|
344
|
+
on(type: "leave", fn: (player: RoomPlayer | { id: string }, why: string) => void): () => void;
|
|
345
|
+
on(type: "host", fn: (id: string) => void): () => void;
|
|
346
|
+
/** A key changed (value null: taken away); by: who wrote it. */
|
|
347
|
+
on(type: "state", fn: (key: string, value: any, by: string | null) => void): () => void;
|
|
348
|
+
/** `from` is the sending player's id, or null for the room's own code (a scripted room's room.js). */
|
|
349
|
+
on(type: "message", fn: (type: string, data: any, from: string | null) => void): () => void;
|
|
350
|
+
on(type: `message:${string}`, fn: (data: any, from: string | null) => void): () => void;
|
|
351
|
+
on(type: "reconnecting" | "reconnected", fn: () => void): () => void;
|
|
352
|
+
/** You're out: "left", "closed", "lost", "replaced", "stopped", "time", "empty", ... */
|
|
353
|
+
on(type: "close", fn: (why: string) => void): () => void;
|
|
354
|
+
on(type: "error", fn: (error: P2Error) => void): () => void;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/* ---------- the SDK ---------- */
|
|
358
|
+
|
|
359
|
+
/** What PlayerTwo.init() answers: who is playing, and every call. */
|
|
360
|
+
export interface P2 {
|
|
361
|
+
env: Env;
|
|
362
|
+
player: Player | null;
|
|
363
|
+
game: Game | null;
|
|
364
|
+
/** True while its developer (or an admin) plays. */
|
|
365
|
+
test: boolean;
|
|
366
|
+
/** True in a demo: demos earn no trophies. */
|
|
367
|
+
demo: boolean;
|
|
368
|
+
/** The invite params the player came with (a join link, or a friend's Join), or null. */
|
|
369
|
+
params: Record<string, string> | null;
|
|
370
|
+
/** Any call by its name (the helpers are these). */
|
|
371
|
+
call<T = unknown>(method: string, args?: unknown[]): Promise<T>;
|
|
372
|
+
me(): Promise<Me>;
|
|
373
|
+
/** Who is playing, for your own servers: a JWT signed by Player Two (RS256, typed "p2-id"), ten minutes. */
|
|
374
|
+
identity(): Promise<string>;
|
|
375
|
+
/** A link to your game on Player Two that opens it with these params (up to 16 strings, 512 bytes). */
|
|
376
|
+
invite(params?: Record<string, string>): Promise<string>;
|
|
377
|
+
trophies: Trophies;
|
|
378
|
+
addons: Addons;
|
|
379
|
+
stats: Stats;
|
|
380
|
+
scores: Scores;
|
|
381
|
+
saves: Saves;
|
|
382
|
+
/** The line friends see, and with { join } what they join the player with. */
|
|
383
|
+
presence: { set(status: string | null, options?: { join?: Record<string, string> }): Promise<unknown> };
|
|
384
|
+
loading: { done(): Promise<unknown> };
|
|
385
|
+
gameplay: { start(): Promise<unknown>; stop(): Promise<unknown> };
|
|
386
|
+
/** Multiplayer with no server of your own. */
|
|
387
|
+
rooms: Rooms;
|
|
388
|
+
/** Player Two's own layer over the game opened (true) or closed (false). */
|
|
389
|
+
on(type: "overlay", fn: (open: boolean) => void): () => void;
|
|
390
|
+
/** The player is about to close the game: what you keep should go now (a promise is waited for). */
|
|
391
|
+
on(type: "flush", fn: (closing: true) => void | Promise<unknown>): () => void;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** window.PlayerTwo. */
|
|
395
|
+
export interface PlayerTwoGlobal {
|
|
396
|
+
readonly v: 1;
|
|
397
|
+
/** Certain once init has settled. */
|
|
398
|
+
env: Env;
|
|
399
|
+
/** Finds Player Two once: every call answers the same promise, and the first call's options are the ones used. */
|
|
400
|
+
init(options?: InitOptions): Promise<P2>;
|
|
401
|
+
/** Settles once init has been called and Player Two found, or not. */
|
|
402
|
+
readonly ready: Promise<P2>;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
declare global {
|
|
406
|
+
/** Player Two's SDK, from https://playertwo.lol/sdk/v1.js (on Player Two, already in the page). */
|
|
407
|
+
var PlayerTwo: PlayerTwoGlobal;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Where the SDK is loaded from when the page doesn't have it. */
|
|
411
|
+
export const SDK_URL: string;
|
|
412
|
+
|
|
413
|
+
/** window.PlayerTwo, loading the script from `src` (SDK_URL) first if the page doesn't have it. */
|
|
414
|
+
export function load(src?: string): Promise<PlayerTwoGlobal>;
|
|
415
|
+
|
|
416
|
+
/** PlayerTwo.init(options), loading the SDK first if the page doesn't have it. */
|
|
417
|
+
export function init(options?: InitOptions): Promise<P2>;
|
|
418
|
+
|
|
419
|
+
declare const sdk: { init: typeof init; load: typeof load; SDK_URL: string };
|
|
420
|
+
export default sdk;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// playertwo-sdk: Player Two'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 Player Two's games host puts in a hosted game's page; this uses that
|
|
3
|
+
// copy when the page has it, and otherwise loads the script and waits for it. Types: index.d.ts. Docs:
|
|
4
|
+
// https://playertwo.lol/docs/sdk
|
|
5
|
+
//
|
|
6
|
+
// import { init } from "playertwo-sdk";
|
|
7
|
+
// const p2 = await init(); // the same as PlayerTwo.init(), with the same options
|
|
8
|
+
|
|
9
|
+
export const SDK_URL = "https://playertwo.lol/sdk/v1.js";
|
|
10
|
+
|
|
11
|
+
let loading = null;
|
|
12
|
+
|
|
13
|
+
function refused(code, message) {
|
|
14
|
+
const e = new Error(message);
|
|
15
|
+
e.code = code;
|
|
16
|
+
e.status = 0;
|
|
17
|
+
return e;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** window.PlayerTwo, loading the script first if the page doesn't have it. */
|
|
21
|
+
export function load(src = SDK_URL) {
|
|
22
|
+
if (typeof window === "undefined" || typeof document === "undefined") return Promise.reject(refused("no-window", "Player Two's SDK runs in a browser."));
|
|
23
|
+
if (window.PlayerTwo && window.PlayerTwo.v === 1) return Promise.resolve(window.PlayerTwo);
|
|
24
|
+
if (loading) return loading;
|
|
25
|
+
loading = new Promise((resolve, reject) => {
|
|
26
|
+
const s = document.createElement("script");
|
|
27
|
+
s.src = src;
|
|
28
|
+
s.async = true;
|
|
29
|
+
s.onload = () => {
|
|
30
|
+
if (window.PlayerTwo && window.PlayerTwo.v === 1) resolve(window.PlayerTwo);
|
|
31
|
+
else { loading = null; reject(refused("offline", "Player Two's SDK loaded, but didn't start.")); }
|
|
32
|
+
};
|
|
33
|
+
s.onerror = () => {
|
|
34
|
+
loading = null;
|
|
35
|
+
s.remove();
|
|
36
|
+
reject(refused("offline", `Player Two's SDK couldn't be loaded from ${src}.`));
|
|
37
|
+
};
|
|
38
|
+
(document.head || document.documentElement).appendChild(s);
|
|
39
|
+
});
|
|
40
|
+
return loading;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** PlayerTwo.init(options): who is playing, and every call. */
|
|
44
|
+
export function init(options) {
|
|
45
|
+
return load().then((PlayerTwo) => PlayerTwo.init(options));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default { init, load, SDK_URL };
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playertwo-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Player Two's game SDK: trophies, leaderboards, saves and multiplayer rooms for games hosted on Player Two, with TypeScript types",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./index.mjs",
|
|
8
|
+
"module": "./index.mjs",
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"default": "./index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"./room": {
|
|
16
|
+
"types": "./room.d.ts",
|
|
17
|
+
"default": "./room.mjs"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"index.mjs",
|
|
23
|
+
"index.d.ts",
|
|
24
|
+
"room.mjs",
|
|
25
|
+
"room.d.ts",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"keywords": [
|
|
30
|
+
"playertwo",
|
|
31
|
+
"game",
|
|
32
|
+
"multiplayer",
|
|
33
|
+
"rooms",
|
|
34
|
+
"threejs",
|
|
35
|
+
"sdk"
|
|
36
|
+
],
|
|
37
|
+
"homepage": "https://playertwo.lol/docs/sdk"
|
|
38
|
+
}
|
package/room.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for a room's own server script: room.js at the root of a build pushed to Player Two, run on Player Two's
|
|
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
|
+
* exports the hooks it needs; in a room with a room.js only the script writes the shared state.
|
|
5
|
+
*
|
|
6
|
+
* Plain JavaScript, no build step (room.js is pushed as it is):
|
|
7
|
+
*
|
|
8
|
+
* // @ts-check
|
|
9
|
+
* /** @param {import("playertwo-sdk/room").Room} room @param {import("playertwo-sdk/room").Player} player *\/
|
|
10
|
+
* export function onJoin(room, player) { room.state.set("hello", player.handle); }
|
|
11
|
+
*
|
|
12
|
+
* Or bundled into one file (TypeScript, say), with every hook typed at once:
|
|
13
|
+
*
|
|
14
|
+
* import { defineRoom } from "playertwo-sdk/room";
|
|
15
|
+
* export const { tickRate, onCreate, onJoin, onLeave, onMessage, onSync, onTick } = defineRoom<{ coins: number }>({
|
|
16
|
+
* tickRate: 20,
|
|
17
|
+
* onCreate(room) { room.data.coins = 8; },
|
|
18
|
+
* onSync(room, player, values) { return Math.abs(values["position.x"]) < 18 ? values : false; },
|
|
19
|
+
* });
|
|
20
|
+
*/
|
|
21
|
+
/* (the same as index.d.ts's, kept here so a server script doesn't take in the browser's global PlayerTwo) */
|
|
22
|
+
/** Anything that goes through JSON: what messages and the room's state carry. */
|
|
23
|
+
export type Json = string | number | boolean | null | Json[] | { [key: string]: Json };
|
|
24
|
+
/** A sync's values: each field's number, by its path ("position.x"). */
|
|
25
|
+
export type SyncValues = Record<string, number>;
|
|
26
|
+
|
|
27
|
+
/** Someone in the room, as room.js sees them. */
|
|
28
|
+
export interface Player {
|
|
29
|
+
id: string;
|
|
30
|
+
handle: string;
|
|
31
|
+
name: string;
|
|
32
|
+
level: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** What room.js is handed: the room it runs for. `D` is what you keep in room.data. */
|
|
36
|
+
export interface Room<D extends object = Record<string, any>> {
|
|
37
|
+
/** Who is here. */
|
|
38
|
+
readonly players: readonly Player[];
|
|
39
|
+
/** One of them, or null. */
|
|
40
|
+
player(id: string): Player | null;
|
|
41
|
+
/** A message to every page in the room, or to `to` (an id, or a list). Pages hear it with room.on("message:<type>"). */
|
|
42
|
+
send(type: string, data?: Json, options?: { to?: string | string[] }): void;
|
|
43
|
+
/** The shared state every page reads (room.get, "state"); only room.js writes it. null takes a key away. */
|
|
44
|
+
readonly state: {
|
|
45
|
+
get<T = any>(key: string): T | undefined;
|
|
46
|
+
set(key: string, value: Json | undefined): void;
|
|
47
|
+
};
|
|
48
|
+
/** Take a player out of the room. */
|
|
49
|
+
kick(id: string, why?: string): void;
|
|
50
|
+
/** Close the room: every page hears "close" with `why`. */
|
|
51
|
+
close(why?: string): void;
|
|
52
|
+
/** Yours to keep things in, for as long as the room is open. */
|
|
53
|
+
data: D;
|
|
54
|
+
/** The room's clock (ms), the same as the pages' room.now(). */
|
|
55
|
+
now(): number;
|
|
56
|
+
/** To the room's log, in your studio. */
|
|
57
|
+
log(...args: unknown[]): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Every hook room.js may export. */
|
|
61
|
+
export interface RoomScript<D extends object = Record<string, any>> {
|
|
62
|
+
/** onTick calls a second, 1 to 60. */
|
|
63
|
+
tickRate?: number;
|
|
64
|
+
/** The room opened. */
|
|
65
|
+
onCreate?(room: Room<D>): void;
|
|
66
|
+
onJoin?(room: Room<D>, player: Player): void;
|
|
67
|
+
/** why: "left", "dropped", "kicked", ... */
|
|
68
|
+
onLeave?(room: Room<D>, player: Player, why: string): void;
|
|
69
|
+
/** What a page sent with room.send(type, data). */
|
|
70
|
+
onMessage?(room: Room<D>, player: Player, type: string, data: Json): void;
|
|
71
|
+
/** A player's movement (room.sync), before anyone else gets it: the values to pass on (changed or not), or false to
|
|
72
|
+
drop them. A dropped or changed move is put right on the player's own page by itself (room.sync's "fix"). */
|
|
73
|
+
onSync?(room: Room<D>, player: Player, values: SyncValues): SyncValues | false;
|
|
74
|
+
/** tickRate times a second; dt in seconds. */
|
|
75
|
+
onTick?(room: Room<D>, dt: number): void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Types every hook at once: answers `script` as it is. */
|
|
79
|
+
export function defineRoom<D extends object = Record<string, any>>(script: RoomScript<D>): RoomScript<D>;
|