playertwo-sdk 0.1.0 → 0.2.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/README.md +3 -1
- package/index.d.ts +280 -8
- package/package.json +1 -1
- package/room.d.ts +68 -2
package/README.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
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
|
|
4
|
+
invites, add-ons, the player's avatar, travel between games with their party, and multiplayer rooms (voice chat,
|
|
5
|
+
worlds, replays and your own server code in room.js included), for games hosted on [Player Two](https://playertwo.lol).
|
|
6
|
+
Everything it offers, each with its part of the docs: https://playertwo.lol/llms.txt
|
|
5
7
|
|
|
6
8
|
The SDK itself is one script, `https://playertwo.lol/sdk/v1.js`. On Player Two it is already in your game's page;
|
|
7
9
|
`init()` uses that copy, and loads the script only when the page doesn't have it (on localhost, say, where its
|
package/index.d.ts
CHANGED
|
@@ -234,6 +234,8 @@ export interface RoomPlayer {
|
|
|
234
234
|
kind: string;
|
|
235
235
|
/** Their connection dropped; their seat is held for them (20 s). */
|
|
236
236
|
away: boolean;
|
|
237
|
+
/** Their rating in this game (room.result in room.js), once they have one. */
|
|
238
|
+
rating?: number;
|
|
237
239
|
}
|
|
238
240
|
|
|
239
241
|
export interface MatchOptions {
|
|
@@ -249,16 +251,86 @@ export interface JoinOptions extends MatchOptions {
|
|
|
249
251
|
/** The room you were in (room.id), to come back to it. */
|
|
250
252
|
room?: string;
|
|
251
253
|
resume?: string;
|
|
254
|
+
/** A match by skill: rooms of players rated near you first (the game's own ratings: room.result in room.js). */
|
|
255
|
+
skill?: boolean;
|
|
256
|
+
/** Keep the room as a replay, from its first player (a scripted room's code says so itself: room.record). */
|
|
257
|
+
record?: boolean;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** One of the game's public rooms open now (p2.rooms.live), to watch. */
|
|
261
|
+
export interface LiveRoom { id: string; mode: string; size: number; players: number; watchers: number; skill: number | null; closed: boolean }
|
|
262
|
+
/** One of the game's replays (p2.rooms.replays). */
|
|
263
|
+
export interface ReplayInfo { id: string; room: string; mode: string; size: number; players: { id: string; handle: string; name: string }[]; from: number; ms: number }
|
|
264
|
+
/** A replay's controls (room.replay, on a room from p2.rooms.replay). */
|
|
265
|
+
export interface ReplayControls {
|
|
266
|
+
/** How long it is (ms), and when it began. */
|
|
267
|
+
readonly duration: number;
|
|
268
|
+
readonly from: number;
|
|
269
|
+
/** How far into it now (ms). */
|
|
270
|
+
time(): number;
|
|
271
|
+
play(): void;
|
|
272
|
+
pause(): void;
|
|
273
|
+
/** How fast it plays, 0.1 to 8 (1 when none is said); says what it is. */
|
|
274
|
+
speed(x?: number): number;
|
|
275
|
+
/** To a moment of it (ms): the room as it was then (read room.players again). */
|
|
276
|
+
seek(ms: number): void;
|
|
277
|
+
on(type: "end", fn: () => void): () => void;
|
|
278
|
+
on(type: "seek", fn: (ms: number) => void): () => void;
|
|
252
279
|
}
|
|
253
280
|
|
|
254
281
|
export interface Rooms {
|
|
255
|
-
/** A match ({ mode, size }: the fullest open room of that mode and size, or a new one), a
|
|
256
|
-
({ code }), or the room you were in ({ room, resume }). Resolves once you are in. */
|
|
282
|
+
/** A match ({ mode, size }: the fullest open room of that mode and size, one near the player first, or a new one), a
|
|
283
|
+
room by its code ({ code }), or the room you were in ({ room, resume }). Resolves once you are in. */
|
|
257
284
|
join(options?: JoinOptions): Promise<Room>;
|
|
258
285
|
/** A new private room; friends join it by room.code. */
|
|
259
286
|
create(options?: MatchOptions): Promise<Room>;
|
|
260
287
|
/** Your party's own room, with only its members. */
|
|
261
288
|
party(options?: MatchOptions): Promise<Room>;
|
|
289
|
+
/** A world the game keeps for good ("main" when unnamed), or an area of it: one room each, never closed, its shared
|
|
290
|
+
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
|
|
291
|
+
on an older one hears "close" with "update" (reload it). */
|
|
292
|
+
world(name?: string, options?: { area?: string }): Promise<Room>;
|
|
293
|
+
/** Watch a room (its id, from live() or a friend): you hear and draw everything its players do, with no seat, and can
|
|
294
|
+
say nothing (room.watching). */
|
|
295
|
+
watch(options: { room: string }): Promise<Room>;
|
|
296
|
+
/** The game's public rooms open now, fullest first. */
|
|
297
|
+
live(options?: { mode?: string }): Promise<LiveRoom[]>;
|
|
298
|
+
/** The game's replays, newest first (kept 200 a game). */
|
|
299
|
+
replays(options?: { limit?: number }): Promise<ReplayInfo[]>;
|
|
300
|
+
/** A replay, as a room that plays itself (room.replay): draw it with room.sync, room.objects and room.on. */
|
|
301
|
+
replay(id: string): Promise<Room>;
|
|
302
|
+
/** A bad connection, on purpose, to try your game on: this page's rooms from now on (on localhost too), until
|
|
303
|
+
simulate(null). The page's address can ask for one too: ?p2-net=150,30,0.02 (lag, jitter, loss). Says what it is
|
|
304
|
+
now. */
|
|
305
|
+
simulate(options: NetSimulation | null): NetSimulation | null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export interface NetSimulation {
|
|
309
|
+
/** ms more there and back (half each way). */
|
|
310
|
+
lag?: number;
|
|
311
|
+
/** ms of give or take, each way. */
|
|
312
|
+
jitter?: number;
|
|
313
|
+
/** 0 to 1 of what is sent lost: held up on a socket as TCP holds it (a retransmit's wait, with everything after it),
|
|
314
|
+
gone on the fast lane. */
|
|
315
|
+
loss?: number;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Voice chat in a room: each player's microphone to everyone else in voice, through Player Two's servers. */
|
|
319
|
+
export interface Voice {
|
|
320
|
+
/** Who is in voice: their ids. */
|
|
321
|
+
readonly who: readonly string[];
|
|
322
|
+
/** Your microphone to everyone else in voice, and theirs to you (the browser asks for the microphone first):
|
|
323
|
+
resolves once you're in. Fails with "no-mic" (not allowed), "voice-full" (24 in voice already) or "no-voice". */
|
|
324
|
+
join(options?: { muted?: boolean }): Promise<void>;
|
|
325
|
+
/** Out of voice: your microphone let go, and no one heard. */
|
|
326
|
+
leave(): void;
|
|
327
|
+
/** Your microphone off (true) or on (false), or the other way: says whether it is off. */
|
|
328
|
+
mute(off?: boolean): boolean;
|
|
329
|
+
isMuted(): boolean;
|
|
330
|
+
/** How loud a player is to you, 0 (not at all) to 1; this page only. */
|
|
331
|
+
volume(id: string, volume: number): void;
|
|
332
|
+
/** Whether a player (or you) is speaking now. */
|
|
333
|
+
speaking(id: string): boolean;
|
|
262
334
|
}
|
|
263
335
|
|
|
264
336
|
export interface SendOptions {
|
|
@@ -275,14 +347,82 @@ export interface SyncOptions {
|
|
|
275
347
|
/** The numbers to send, as paths on the object ("position.x", "rotation.y"); for a THREE.Object3D, position x, y, z
|
|
276
348
|
and rotation.y when left out, otherwise ["x", "y"]. */
|
|
277
349
|
fields?: string[];
|
|
278
|
-
/** Sends a second. */
|
|
350
|
+
/** Sends a second (30; 15 in a match of more than 16 players). */
|
|
279
351
|
rate?: number;
|
|
280
|
-
/** How far behind (ms) bound objects are drawn, so there are
|
|
281
|
-
|
|
352
|
+
/** How far behind (ms) bound objects are drawn, so there are updates to move between: "auto" (the default) is each
|
|
353
|
+
player's own, as late as their updates come and the time between two; a number fixes it for everyone. */
|
|
354
|
+
delay?: number | "auto";
|
|
355
|
+
/** How far this page sees, in the units of the fields named x and z (or y): in a big room (an area of a world)
|
|
356
|
+
players within it move every tick and those further once a second. 0: no limit. */
|
|
357
|
+
near?: number;
|
|
282
358
|
/** Fields that are angles in radians, which turn the short way round. */
|
|
283
359
|
angles?: string[];
|
|
284
360
|
/** A jump longer than this between two updates is a teleport, not a glide (0: never). */
|
|
285
361
|
snap?: number;
|
|
362
|
+
/** When a scripted room's code corrects you: "replay" (the default) puts you right as of the move it corrected and
|
|
363
|
+
plays your moves since on top, as Unreal does, so only how far off you were shows; "snap" puts you where the room
|
|
364
|
+
says (losing your movement since). */
|
|
365
|
+
fix?: "replay" | "snap";
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export interface ObjectsOptions {
|
|
369
|
+
/** A value's name to a path on a bound object ({ x: "position.x", ry: "rotation.y" }). On a three.js object (one
|
|
370
|
+
with a position) x, y, z, qx to qw (a physics body's quaternion) and rx to rz go to its position, quaternion and
|
|
371
|
+
rotation by themselves; on anything else the name itself. */
|
|
372
|
+
map?: Record<string, string>;
|
|
373
|
+
/** Names that are angles in radians (by default ry, rx, rz, yaw, pitch, roll, angle, heading and rotation.*). */
|
|
374
|
+
angles?: string[];
|
|
375
|
+
/** How far behind (ms) objects are drawn: "auto" (each its own, as late as its updates come), or a number. */
|
|
376
|
+
delay?: number | "auto";
|
|
377
|
+
/** A jump longer than this between two updates is a teleport, not a glide (0: never). */
|
|
378
|
+
snap?: number;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** What a scripted room's code moves (room.objects), drawn smoothly as players are. */
|
|
382
|
+
export interface RoomObjects {
|
|
383
|
+
/** The code made one (its values by name): make yours and bind() it. */
|
|
384
|
+
on(type: "add", fn: (key: string, values: Record<string, number>) => void): () => void;
|
|
385
|
+
/** The code ended one. */
|
|
386
|
+
on(type: "remove", fn: (key: string) => void): () => void;
|
|
387
|
+
/** Move `target` to where the object `key` is, on every step() (each value to its path: options.map). */
|
|
388
|
+
bind(key: string, target: object): void;
|
|
389
|
+
/** Once a frame: moves every bound object. */
|
|
390
|
+
step(): void;
|
|
391
|
+
keys(): string[];
|
|
392
|
+
/** An object's smoothed values now, by name, or null. */
|
|
393
|
+
values(key: string): Record<string, number> | null;
|
|
394
|
+
/** How long ago (ms) an object is drawn now. */
|
|
395
|
+
delay(key: string): number | null;
|
|
396
|
+
/** The room's time an object is drawn at now (room.past(key, at) in room.js). */
|
|
397
|
+
seen(key: string): number;
|
|
398
|
+
stop(): void;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export interface InputsOptions<S, I> {
|
|
402
|
+
/** Your player's state to start from. */
|
|
403
|
+
state: S;
|
|
404
|
+
/** This tick's input (what the player pressed, where they aim): anything JSON carries, small. */
|
|
405
|
+
input(): I;
|
|
406
|
+
/** Your game's own step, the one your room.js runs on the same input: change `state` (or return a new one). */
|
|
407
|
+
step(state: S, input: I | null, dt: number): S | void;
|
|
408
|
+
/** Ticks a second: your room.js's tickRate (60 when left out). */
|
|
409
|
+
rate?: number;
|
|
410
|
+
/** ms to ease a correction off over in what view() draws (0: none). */
|
|
411
|
+
smooth?: number;
|
|
412
|
+
}
|
|
413
|
+
/** Your player in an input-run room (room.inputs). */
|
|
414
|
+
export interface Inputs<S> {
|
|
415
|
+
/** Each frame: runs the ticks owed since the last (input read, sent, stepped). */
|
|
416
|
+
update(): void;
|
|
417
|
+
/** Your player's state as this page has run it, the newest tick. */
|
|
418
|
+
readonly state: S;
|
|
419
|
+
/** What to draw this frame: between the last two ticks, a correction eased off. */
|
|
420
|
+
view(): S;
|
|
421
|
+
/** { seq, acked, pending, waiting (the room's queue for you, lately at least), rewinds, pace }. */
|
|
422
|
+
stats(): { seq: number; acked: number; pending: number; waiting: number | null; rewinds: number; pace: number };
|
|
423
|
+
/** The room's code had your player elsewhere after input `at`: run again from there with the `replayed` since. */
|
|
424
|
+
on(type: "fix", fn: (state: S, info: { at: number; replayed: number }) => void): () => void;
|
|
425
|
+
stop(): void;
|
|
286
426
|
}
|
|
287
427
|
|
|
288
428
|
/** One object of yours sent to the room, and everyone else's moved smoothly (room.sync). */
|
|
@@ -291,18 +431,37 @@ export interface Sync {
|
|
|
291
431
|
on(type: "join", fn: (id: string, values: number[]) => void): () => void;
|
|
292
432
|
/** They left, or stopped syncing. */
|
|
293
433
|
on(type: "leave", fn: (id: string) => void): () => void;
|
|
294
|
-
/** A scripted room's code refused or corrected your move (onSync): your object was put
|
|
295
|
-
|
|
434
|
+
/** A scripted room's code refused or corrected your move (onSync), or moved you (room.correct): your object was put
|
|
435
|
+
right, as of your move at `at`, by `by` (each field's change, your moves since on top; null when it was put
|
|
436
|
+
where the room says). `values`: where it is now. */
|
|
437
|
+
on(type: "fix", fn: (values: number[], info: { at: number | null; by: number[] | null }) => void): () => void;
|
|
296
438
|
/** Move `target`'s fields to where player `id` was `delay` ms ago, on every step(). */
|
|
297
439
|
bind(id: string, target: object): void;
|
|
298
440
|
/** Once a frame: sends yours at `rate`, and moves every bound object. */
|
|
299
441
|
step(): void;
|
|
300
442
|
/** A player's smoothed values now (in the order of `fields`), or null before their first update. */
|
|
301
443
|
values(id: string): number[] | null;
|
|
444
|
+
/** How long ago (ms) a player is drawn now, or null before their first update. */
|
|
445
|
+
delay(id: string): number | null;
|
|
446
|
+
/** The room's time a player is drawn at now: send it with a shot, and room.js can look back to then with
|
|
447
|
+
room.past(id, at) to judge the hit as this player saw it. */
|
|
448
|
+
seen(id: string): number;
|
|
302
449
|
/** Stop sending and moving. */
|
|
303
450
|
stop(): void;
|
|
304
451
|
}
|
|
305
452
|
|
|
453
|
+
export interface RoomStats {
|
|
454
|
+
/** There and back to the room (ms), measured every 2 s; null before the first. */
|
|
455
|
+
ping: number | null;
|
|
456
|
+
/** How much the ping varies (ms). */
|
|
457
|
+
jitter: number;
|
|
458
|
+
/** How fast messages go: by the fast lane, the room's socket, or other tabs on localhost. */
|
|
459
|
+
lane: "fast" | "socket" | "local";
|
|
460
|
+
/** Bytes a second, sent and heard. */
|
|
461
|
+
up: number;
|
|
462
|
+
down: number;
|
|
463
|
+
}
|
|
464
|
+
|
|
306
465
|
export interface Room {
|
|
307
466
|
/** The room's name (to come back to it with). */
|
|
308
467
|
readonly id: string;
|
|
@@ -322,6 +481,14 @@ export interface Room {
|
|
|
322
481
|
readonly owners: Record<string, string | null>;
|
|
323
482
|
/** The build has a room.js, running on Player Two's servers: only it writes the shared state. */
|
|
324
483
|
readonly scripted: boolean;
|
|
484
|
+
/** Whether it is a world, kept for good (p2.rooms.world). */
|
|
485
|
+
readonly world: boolean;
|
|
486
|
+
/** Whether you're watching it (p2.rooms.watch, or a replay): you hear everything and can say nothing. */
|
|
487
|
+
readonly watching: boolean;
|
|
488
|
+
/** A replay's controls (a room from p2.rooms.replay). */
|
|
489
|
+
readonly replay?: ReplayControls;
|
|
490
|
+
/** Voice chat. */
|
|
491
|
+
readonly voice: Voice;
|
|
325
492
|
|
|
326
493
|
/** Send `data` as a message of `type` to everyone else, or to `to`. In a scripted room, room.js hears it (onMessage). */
|
|
327
494
|
send(type: string, data?: Json, options?: SendOptions): void;
|
|
@@ -330,14 +497,24 @@ export interface Room {
|
|
|
330
497
|
get<T = any>(key: string): T | undefined;
|
|
331
498
|
/** Whether you are the host. */
|
|
332
499
|
isHost(): boolean;
|
|
333
|
-
/** There and back, in ms (it also sets room.now()).
|
|
500
|
+
/** There and back, in ms (it also sets room.now()). Rejected ("lost") when the line drops before the answer, or after
|
|
501
|
+
10 s with none ("timeout"): a game pinging in a loop goes on after a reconnect. */
|
|
334
502
|
ping(): Promise<number>;
|
|
503
|
+
/** How the connection is now. */
|
|
504
|
+
stats(): RoomStats;
|
|
335
505
|
/** The room's clock, the same for every player in it (ms). */
|
|
336
506
|
now(): number;
|
|
507
|
+
/** An input-run room (its room.js runs everyone's inputs in onTick): your inputs sent a tick each and your own player
|
|
508
|
+
run on them at once by the same step as the code's, put right ("fix") when the code's state says otherwise. */
|
|
509
|
+
inputs<S = any, I = any>(options: InputsOptions<S, I>): Inputs<S>;
|
|
337
510
|
/** Leave for good (your seat goes to someone else). */
|
|
338
511
|
leave(): void;
|
|
339
512
|
/** Send `target`'s fields `rate` times a second on the fast lane, and move bound objects to where their players were. */
|
|
340
513
|
sync(target: object, options: SyncOptions): Sync;
|
|
514
|
+
/** A scripted room's objects (room.objects in room.js: what its code moves), drawn smoothly. */
|
|
515
|
+
objects(options?: ObjectsOptions): RoomObjects;
|
|
516
|
+
/** Whether the room's sockets are held by gates (a world's area, a match of over 16 seats). */
|
|
517
|
+
readonly gated: boolean;
|
|
341
518
|
|
|
342
519
|
on(type: "join" | "away" | "back", fn: (player: RoomPlayer) => void): () => void;
|
|
343
520
|
/** why: "left", "dropped", "kicked", ... */
|
|
@@ -349,11 +526,95 @@ export interface Room {
|
|
|
349
526
|
on(type: "message", fn: (type: string, data: any, from: string | null) => void): () => void;
|
|
350
527
|
on(type: `message:${string}`, fn: (data: any, from: string | null) => void): () => void;
|
|
351
528
|
on(type: "reconnecting" | "reconnected", fn: () => void): () => void;
|
|
529
|
+
/** A scripted room opened again between far-apart players (room.between() in room.js), after "reconnected": its new id. */
|
|
530
|
+
on(type: "moved", fn: (id: string) => void): () => void;
|
|
531
|
+
/** Someone came into voice (true) or left it. */
|
|
532
|
+
on(type: "voice", fn: (id: string, inVoice: boolean) => void): () => void;
|
|
533
|
+
/** Someone in voice (or you) started speaking (true) or stopped. */
|
|
534
|
+
on(type: "speaking", fn: (id: string, speaking: boolean) => void): () => void;
|
|
352
535
|
/** You're out: "left", "closed", "lost", "replaced", "stopped", "time", "empty", ... */
|
|
353
536
|
on(type: "close", fn: (why: string) => void): () => void;
|
|
354
537
|
on(type: "error", fn: (error: P2Error) => void): () => void;
|
|
355
538
|
}
|
|
356
539
|
|
|
540
|
+
/* ---------- travel and avatars (docs: /docs/sdk/travel) ---------- */
|
|
541
|
+
|
|
542
|
+
/** A pixel of a critter: x and y from the top left of its 18 by 18 box, the colour as CSS (#rrggbb or hsl()). */
|
|
543
|
+
export type Pixel = [x: number, y: number, colour: string];
|
|
544
|
+
|
|
545
|
+
/** Who the player is, to dress your game's character with. */
|
|
546
|
+
export interface Avatar {
|
|
547
|
+
/** The same as p2.player.id. */
|
|
548
|
+
id: string;
|
|
549
|
+
handle: string;
|
|
550
|
+
name: string;
|
|
551
|
+
level: number;
|
|
552
|
+
picture: string | null;
|
|
553
|
+
critter: {
|
|
554
|
+
/** The name it is drawn from: the same name, the same critter, everywhere on Player Two. */
|
|
555
|
+
face: string;
|
|
556
|
+
/** Its box: 18 pixels a side. */
|
|
557
|
+
size: number;
|
|
558
|
+
/** The colour of the tile it stands on in Player Two's own pages. */
|
|
559
|
+
tile: string;
|
|
560
|
+
/** Every pixel of it, eyes open. */
|
|
561
|
+
pixels: Pixel[];
|
|
562
|
+
/** The same critter as a 3D model (.glb), standing on the origin and facing +Z: anyone may load it. */
|
|
563
|
+
model: string;
|
|
564
|
+
};
|
|
565
|
+
/** What they have on from the Plaza shop (a hat, a trail, a skin): each with its picture, and a 3D model when it has one
|
|
566
|
+
(none yet). */
|
|
567
|
+
worn: { key: string; name: string; slot: string; model: string | null; picture: string | null }[];
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/** One of the player's things from Player Two's shop in the Plaza (p2.items.list). */
|
|
571
|
+
export interface Item {
|
|
572
|
+
key: string;
|
|
573
|
+
name: string;
|
|
574
|
+
kind: "skin" | "avatar" | "wear" | "emote" | "effect";
|
|
575
|
+
tier: "common" | "rare" | "epic" | "legendary" | "mythic" | "divine";
|
|
576
|
+
/** Where it goes: head, face, neck or back for a wearable; skin, avatar or effect; none for an emote. */
|
|
577
|
+
slot: string | null;
|
|
578
|
+
/** Whether the player has it on. */
|
|
579
|
+
worn: boolean;
|
|
580
|
+
/** A Divine item's number, of the 77 made. */
|
|
581
|
+
number: number | null;
|
|
582
|
+
/** An SVG of it; none for an emote or an effect. */
|
|
583
|
+
picture: string | null;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** A door for your own menu (p2.travel.links). */
|
|
587
|
+
export interface Door {
|
|
588
|
+
slug: string;
|
|
589
|
+
title: string;
|
|
590
|
+
tagline: string;
|
|
591
|
+
/** Its key art (square), or its vertical poster. */
|
|
592
|
+
art: string;
|
|
593
|
+
/** Players in it now. */
|
|
594
|
+
players: number;
|
|
595
|
+
/** Up to five of the player's friends in it now. */
|
|
596
|
+
friends: { handle: string; name: string }[];
|
|
597
|
+
/** Why it is here: the party's leader brought the party to it, friends are in it, the player played it lately, or
|
|
598
|
+
it is busy. */
|
|
599
|
+
why: "party" | "friends" | "recent" | "popular";
|
|
600
|
+
free: boolean;
|
|
601
|
+
/** In the player's library: a paid game they don't own opens with its demo, or Buy. */
|
|
602
|
+
owned: boolean;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Travel to other games on Player Two, with the player's party. In Player Two's player on the web; elsewhere each
|
|
606
|
+
rejects with code "no-travel". */
|
|
607
|
+
export interface Travel {
|
|
608
|
+
/** To another game, by its address (playertwo.lol/game/<slug>): it is given p2.params (yours, then `from`, this
|
|
609
|
+
game's address, and `place`). Resolves as the player goes; this page is closed after. */
|
|
610
|
+
to(slug: string, options?: { params?: Record<string, string>; place?: string }): Promise<{ to: { slug: string; title: string }; params: Record<string, string> }>;
|
|
611
|
+
/** Back to the Plaza, where every game on Player Two has its screen. */
|
|
612
|
+
home(): Promise<{ to: { slug: null; title: string; url: string } }>;
|
|
613
|
+
/** The doors to show in your own menu: where the party is, the games friends are in, the player's recent ones, the
|
|
614
|
+
busiest (up to 12). */
|
|
615
|
+
links(): Promise<{ home: { title: string; url: string }; links: Door[] }>;
|
|
616
|
+
}
|
|
617
|
+
|
|
357
618
|
/* ---------- the SDK ---------- */
|
|
358
619
|
|
|
359
620
|
/** What PlayerTwo.init() answers: who is playing, and every call. */
|
|
@@ -385,6 +646,17 @@ export interface P2 {
|
|
|
385
646
|
gameplay: { start(): Promise<unknown>; stop(): Promise<unknown> };
|
|
386
647
|
/** Multiplayer with no server of your own. */
|
|
387
648
|
rooms: Rooms;
|
|
649
|
+
/** Who the player is, to dress your game's character with: their critter as pixels and as a 3D model, and what they
|
|
650
|
+
wear. */
|
|
651
|
+
avatar(): Promise<Avatar>;
|
|
652
|
+
/** What the player has from Player Two's shop in the Plaza (avatars, skins, wearables, emotes, effects), and what
|
|
653
|
+
they have on: a game can dress its own character in them. */
|
|
654
|
+
items: { list(): Promise<Item[]> };
|
|
655
|
+
/** Travel to other games on Player Two, with the player's party. */
|
|
656
|
+
travel: Travel;
|
|
657
|
+
/** Whether a small live picture of the game goes to the Plaza's screens while it is played here (Player Two does
|
|
658
|
+
it; the player can stop it). */
|
|
659
|
+
onAir: boolean;
|
|
388
660
|
/** Player Two's own layer over the game opened (true) or closed (false). */
|
|
389
661
|
on(type: "overlay", fn: (open: boolean) => void): () => void;
|
|
390
662
|
/** The player is about to close the game: what you keep should go now (a promise is waited for). */
|
package/package.json
CHANGED
package/room.d.ts
CHANGED
|
@@ -30,9 +30,19 @@ export interface Player {
|
|
|
30
30
|
handle: string;
|
|
31
31
|
name: string;
|
|
32
32
|
level: number;
|
|
33
|
+
/** What the game kept of them with room.save (in any of its rooms, any time before), or null. */
|
|
34
|
+
saved: Json | null;
|
|
35
|
+
/** Their rating in this game (room.result moves it; 1500 before their first match). */
|
|
36
|
+
rating?: number;
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
/** What room.js is handed: the room it runs for. `D` is what you keep in room.data. */
|
|
40
|
+
/** A physics body room.objects.set takes as it is: Rapier's (translation() and rotation()), or one with a position and
|
|
41
|
+
a quaternion. */
|
|
42
|
+
export type PhysicsBody =
|
|
43
|
+
| { translation(): { x: number; y: number; z?: number }; rotation(): { x: number; y: number; z: number; w: number } | number }
|
|
44
|
+
| { position: { x: number; y: number; z?: number }; quaternion: { x: number; y: number; z: number; w: number } };
|
|
45
|
+
|
|
36
46
|
export interface Room<D extends object = Record<string, any>> {
|
|
37
47
|
/** Who is here. */
|
|
38
48
|
readonly players: readonly Player[];
|
|
@@ -47,10 +57,58 @@ export interface Room<D extends object = Record<string, any>> {
|
|
|
47
57
|
};
|
|
48
58
|
/** Take a player out of the room. */
|
|
49
59
|
kick(id: string, why?: string): void;
|
|
60
|
+
/** Where a player (or an object, by its key) was at the room's time `at` (from the last second), by field name: with
|
|
61
|
+
the time a shot was seen at (the page's sync.seen(id) or objects.seen(key)), judge the hit as the shooter saw it.
|
|
62
|
+
null: nothing of it. */
|
|
63
|
+
past(id: string, at?: number): SyncValues | number[] | null;
|
|
64
|
+
/** Put a player somewhere yourself (knocked back, pulled, teleported), by field name (or a list): as of their last
|
|
65
|
+
move the room took, so their page plays its moves since on top (they feel the push, not a jump back) and everyone
|
|
66
|
+
else sees them there at once. False before their first move. */
|
|
67
|
+
correct(id: string, values: SyncValues | number[]): boolean;
|
|
68
|
+
/** In an input-run room: the player's state after this step (onTick), sent to their page about 30 times a second
|
|
69
|
+
with the number of their last input it ran; their page runs its inputs again from it when it had them elsewhere. */
|
|
70
|
+
confirm(id: string, state: Json): void;
|
|
71
|
+
/** A match's outcome, best first (each place an id, or a list of those level), or ids by score: every player's rating
|
|
72
|
+
moves by it (Elo, every two compared), as player.rating, and players are matched by it ({ skill: true }). */
|
|
73
|
+
result(places: (string | string[])[] | Record<string, number>): void;
|
|
74
|
+
/** Closed to matching while a match is under way (false), and open again so its empty places are filled (true). */
|
|
75
|
+
open(on?: boolean): void;
|
|
76
|
+
/** Keep the room as a replay from now (true), until record(false) or the room closes (up to 30 minutes). */
|
|
77
|
+
record(on?: boolean): void;
|
|
78
|
+
/** What your code moves (enemies, projectiles, a platform), drawn smoothly on every page (room.objects() there):
|
|
79
|
+
up to 1000 a room, their values numbers by name (up to 32 names). In a big room each page gets those near it
|
|
80
|
+
every tick and the rest once a second. They live with your code: set them again in onStart after a restart. */
|
|
81
|
+
readonly objects: {
|
|
82
|
+
/** Make it, or move it: the values given change, the others stay. A physics body as it is (Rapier's, or anything
|
|
83
|
+
with a position and a quaternion: cannon-es, three.js) sets x, y, z and qx, qy, qz, qw (Rapier 2D: x, y and
|
|
84
|
+
angle). Set again unchanged (a body asleep), it is sent once more, then not until it moves. */
|
|
85
|
+
set(key: string, values: Record<string, number> | PhysicsBody): void;
|
|
86
|
+
get(key: string): Record<string, number> | undefined;
|
|
87
|
+
delete(key: string): void;
|
|
88
|
+
keys(): string[];
|
|
89
|
+
};
|
|
50
90
|
/** Close the room: every page hears "close" with `why`. */
|
|
51
91
|
close(why?: string): void;
|
|
52
|
-
/**
|
|
92
|
+
/** Open the room again between its players when they are far apart (where your code decides hits, the one nearest
|
|
93
|
+
the room has the edge): in the region nearest the furthest of them, every page moved there by itself
|
|
94
|
+
("reconnecting", "reconnected", "moved"). Your code starts again there as after a restart, with room.data and the
|
|
95
|
+
shared state (onCreate doesn't run again; what lives only in your module's own variables doesn't come along).
|
|
96
|
+
Players near each other stay where they are; once a minute at most. Call it as a match starts, say. */
|
|
97
|
+
between(): void;
|
|
98
|
+
/** Yours to keep things in, for as long as the room is open (a world's: for good). */
|
|
53
99
|
data: D;
|
|
100
|
+
/** The room's own key-value store, read and written at once, for what doesn't fit in data (a world's map, its
|
|
101
|
+
chests); a world's is kept for good, through every new build. Values are anything JSON. */
|
|
102
|
+
readonly store: {
|
|
103
|
+
get<T = any>(key: string): T | undefined;
|
|
104
|
+
set(key: string, value: Json | undefined): void;
|
|
105
|
+
delete(key: string): void;
|
|
106
|
+
/** Its keys starting with `prefix`, in order (up to `limit`, 1000). */
|
|
107
|
+
keys(prefix?: string, limit?: number): string[];
|
|
108
|
+
};
|
|
109
|
+
/** Keep what the game knows of a player on Player Two's servers, for good (up to 64 KB): their player.saved in any
|
|
110
|
+
of the game's rooms and worlds from then on. null forgets it. */
|
|
111
|
+
save(id: string, data: Json | null): void;
|
|
54
112
|
/** The room's clock (ms), the same as the pages' room.now(). */
|
|
55
113
|
now(): number;
|
|
56
114
|
/** To the room's log, in your studio. */
|
|
@@ -61,8 +119,14 @@ export interface Room<D extends object = Record<string, any>> {
|
|
|
61
119
|
export interface RoomScript<D extends object = Record<string, any>> {
|
|
62
120
|
/** onTick calls a second, 1 to 60. */
|
|
63
121
|
tickRate?: number;
|
|
122
|
+
/** How far every player sees others move every tick (further: once a second), in the units of the fields room.sync
|
|
123
|
+
names x and z (or y): for big rooms, an area of a world. Over any page's own `near`. */
|
|
124
|
+
near?: number;
|
|
64
125
|
/** The room opened. */
|
|
65
126
|
onCreate?(room: Room<D>): void;
|
|
127
|
+
/** The code started: after onCreate the first time, and again after a new version, a restart or room.between()
|
|
128
|
+
(what lives only in your module's variables, and room.objects, start empty: set them again from room.data). */
|
|
129
|
+
onStart?(room: Room<D>): void;
|
|
66
130
|
onJoin?(room: Room<D>, player: Player): void;
|
|
67
131
|
/** why: "left", "dropped", "kicked", ... */
|
|
68
132
|
onLeave?(room: Room<D>, player: Player, why: string): void;
|
|
@@ -72,7 +136,9 @@ export interface RoomScript<D extends object = Record<string, any>> {
|
|
|
72
136
|
drop them. A dropped or changed move is put right on the player's own page by itself (room.sync's "fix"). */
|
|
73
137
|
onSync?(room: Room<D>, player: Player, values: SyncValues): SyncValues | false;
|
|
74
138
|
/** tickRate times a second; dt in seconds. */
|
|
75
|
-
|
|
139
|
+
/** `inputs`: in an input-run room (its pages send room.inputs), each player's input for this step, by id (none when
|
|
140
|
+
theirs didn't come in time); dt is then always 1 / tickRate, the steps kept to real time. */
|
|
141
|
+
onTick?(room: Room<D>, dt: number, inputs: Record<string, any>): void;
|
|
76
142
|
}
|
|
77
143
|
|
|
78
144
|
/** Types every hook at once: answers `script` as it is. */
|