playertwo-sdk 0.1.0 → 0.2.2

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 CHANGED
@@ -1,7 +1,10 @@
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).
4
+ invites, add-ons, in-game purchases (p2.store), the player's avatar, travel between games with their party, and
5
+ multiplayer rooms (voice chat,
6
+ worlds, replays and your own server code in room.js included), for games hosted on [Player Two](https://playertwo.lol).
7
+ Everything it offers, each with its part of the docs: https://playertwo.lol/llms.txt
5
8
 
6
9
  The SDK itself is one script, `https://playertwo.lol/sdk/v1.js`. On Player Two it is already in your game's page;
7
10
  `init()` uses that copy, and loads the script only when the page doesn't have it (on localhost, say, where its
@@ -59,5 +62,7 @@ export const { tickRate, onCreate, onJoin, onLeave, onMessage, onSync, onTick }
59
62
  });
60
63
  ```
61
64
 
62
- Docs: [the SDK](https://playertwo.lol/docs/sdk) and [rooms](https://playertwo.lol/docs/sdk#rooms). A whole game to start
65
+ Docs: [the SDK](https://playertwo.lol/docs/sdk), [in-game purchases](https://playertwo.lol/docs/sdk#purchases) (products
66
+ from your studio with `p2.store.buy`, or your own server's prices with `p2.store.pay`) and
67
+ [rooms](https://playertwo.lol/docs/sdk#rooms). A whole game to start
63
68
  from: `npm create playertwo-game@latest my-game`.
package/index.d.ts CHANGED
@@ -196,6 +196,75 @@ export interface Addons {
196
196
  has(key: string): Promise<boolean>;
197
197
  }
198
198
 
199
+ /** What a game sells inside itself (p2.store; docs: /docs/sdk#store). */
200
+ export type ProductKind = "consumable" | "item" | "skin" | "mod" | "pack";
201
+
202
+ export interface Product {
203
+ key: string;
204
+ kind: ProductKind;
205
+ name: string;
206
+ about: string;
207
+ /** In US dollars. */
208
+ price: number;
209
+ /** A consumable's: how many one purchase gives. */
210
+ qty?: number;
211
+ /** A pack's: the keys of the products it gives. */
212
+ contents?: string[];
213
+ owned: boolean;
214
+ /** A consumable's: how many the player has. */
215
+ count?: number;
216
+ }
217
+
218
+ /** What the player has of the game's products (a consumable: how many). */
219
+ export interface Holding { key: string; kind: ProductKind; count?: number; test?: boolean }
220
+
221
+ export type Bought =
222
+ | {
223
+ ok: true;
224
+ product: { key: string; kind: string; n: number };
225
+ /** A consumable's: how many the player has now. */
226
+ count?: number;
227
+ /** A JWT typed "p2-buy", RS256 with Player Two's keys (/.well-known/p2-keys.json), for your own server: an hour. */
228
+ receipt: string;
229
+ exp: number;
230
+ /** A test purchase (the game's team, a tester): nothing was paid. */
231
+ test: boolean;
232
+ }
233
+ | { ok: false; reason: "cancelled" };
234
+
235
+ /** A charge paid (p2.store.pay): what your server made, once. */
236
+ export type Paid =
237
+ | {
238
+ ok: true;
239
+ /** `ref`: your own reference for it, as your server gave it. */
240
+ charge: { id: string; name: string; ref: string | null };
241
+ /** What was paid, in US dollars (0 for a test). */
242
+ amount: number;
243
+ /** A JWT typed "p2-buy" (kind "charge", key the charge's id), RS256 with Player Two's keys, for your own server: an hour. */
244
+ receipt: string;
245
+ exp: number;
246
+ /** A test payment (the game's team, a tester): nothing was paid. */
247
+ test: boolean;
248
+ }
249
+ | { ok: false; reason: "cancelled" };
250
+
251
+ export interface Store {
252
+ /** Each product on sale, with what the player has of it. */
253
+ list(): Promise<Product[]>;
254
+ /** What the player has. */
255
+ owned(): Promise<Holding[]>;
256
+ /** Asks Player Two 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 Player Two's player on the web), "busy" (its sheet is open already),
258
+ "unknown-product", "owned" (a skin, item or mod they have), "no-game" (they don't have the game yet), "guest" or
259
+ "offline". */
260
+ buy(key: string): Promise<Bought>;
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 Player Two's card, paid once; one paid already answers its receipt again. Rejects with code
263
+ "unknown-charge", "expired" (30 minutes unpaid), "withdrawn" (your server cancelled it), "no-game", "busy",
264
+ "no-store", "guest" or "offline". */
265
+ pay(charge: string): Promise<Paid>;
266
+ }
267
+
199
268
  export interface Stats {
200
269
  /** Every stat. */
201
270
  get(): Promise<{ stats: Record<string, number> }>;
@@ -234,6 +303,8 @@ export interface RoomPlayer {
234
303
  kind: string;
235
304
  /** Their connection dropped; their seat is held for them (20 s). */
236
305
  away: boolean;
306
+ /** Their rating in this game (room.result in room.js), once they have one. */
307
+ rating?: number;
237
308
  }
238
309
 
239
310
  export interface MatchOptions {
@@ -249,16 +320,86 @@ export interface JoinOptions extends MatchOptions {
249
320
  /** The room you were in (room.id), to come back to it. */
250
321
  room?: string;
251
322
  resume?: string;
323
+ /** A match by skill: rooms of players rated near you first (the game's own ratings: room.result in room.js). */
324
+ skill?: boolean;
325
+ /** Keep the room as a replay, from its first player (a scripted room's code says so itself: room.record). */
326
+ record?: boolean;
327
+ }
328
+
329
+ /** One of the game's public rooms open now (p2.rooms.live), to watch. */
330
+ export interface LiveRoom { id: string; mode: string; size: number; players: number; watchers: number; skill: number | null; closed: boolean }
331
+ /** One of the game's replays (p2.rooms.replays). */
332
+ export interface ReplayInfo { id: string; room: string; mode: string; size: number; players: { id: string; handle: string; name: string }[]; from: number; ms: number }
333
+ /** A replay's controls (room.replay, on a room from p2.rooms.replay). */
334
+ export interface ReplayControls {
335
+ /** How long it is (ms), and when it began. */
336
+ readonly duration: number;
337
+ readonly from: number;
338
+ /** How far into it now (ms). */
339
+ time(): number;
340
+ play(): void;
341
+ pause(): void;
342
+ /** How fast it plays, 0.1 to 8 (1 when none is said); says what it is. */
343
+ speed(x?: number): number;
344
+ /** To a moment of it (ms): the room as it was then (read room.players again). */
345
+ seek(ms: number): void;
346
+ on(type: "end", fn: () => void): () => void;
347
+ on(type: "seek", fn: (ms: number) => void): () => void;
252
348
  }
253
349
 
254
350
  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. */
351
+ /** A match ({ mode, size }: the fullest open room of that mode and size, one near the player first, or a new one), a
352
+ room by its code ({ code }), or the room you were in ({ room, resume }). Resolves once you are in. */
257
353
  join(options?: JoinOptions): Promise<Room>;
258
354
  /** A new private room; friends join it by room.code. */
259
355
  create(options?: MatchOptions): Promise<Room>;
260
356
  /** Your party's own room, with only its members. */
261
357
  party(options?: MatchOptions): Promise<Room>;
358
+ /** A world the game keeps for good ("main" when unnamed), or an area of it: one room each, never closed, its shared
359
+ 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
360
+ on an older one hears "close" with "update" (reload it). */
361
+ world(name?: string, options?: { area?: string }): Promise<Room>;
362
+ /** Watch a room (its id, from live() or a friend): you hear and draw everything its players do, with no seat, and can
363
+ say nothing (room.watching). */
364
+ watch(options: { room: string }): Promise<Room>;
365
+ /** The game's public rooms open now, fullest first. */
366
+ live(options?: { mode?: string }): Promise<LiveRoom[]>;
367
+ /** The game's replays, newest first (kept 200 a game). */
368
+ replays(options?: { limit?: number }): Promise<ReplayInfo[]>;
369
+ /** A replay, as a room that plays itself (room.replay): draw it with room.sync, room.objects and room.on. */
370
+ replay(id: string): Promise<Room>;
371
+ /** A bad connection, on purpose, to try your game on: this page's rooms from now on (on localhost too), until
372
+ simulate(null). The page's address can ask for one too: ?p2-net=150,30,0.02 (lag, jitter, loss). Says what it is
373
+ now. */
374
+ simulate(options: NetSimulation | null): NetSimulation | null;
375
+ }
376
+
377
+ export interface NetSimulation {
378
+ /** ms more there and back (half each way). */
379
+ lag?: number;
380
+ /** ms of give or take, each way. */
381
+ jitter?: number;
382
+ /** 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),
383
+ gone on the fast lane. */
384
+ loss?: number;
385
+ }
386
+
387
+ /** Voice chat in a room: each player's microphone to everyone else in voice, through Player Two's servers. */
388
+ export interface Voice {
389
+ /** Who is in voice: their ids. */
390
+ readonly who: readonly string[];
391
+ /** Your microphone to everyone else in voice, and theirs to you (the browser asks for the microphone first):
392
+ resolves once you're in. Fails with "no-mic" (not allowed), "voice-full" (24 in voice already) or "no-voice". */
393
+ join(options?: { muted?: boolean }): Promise<void>;
394
+ /** Out of voice: your microphone let go, and no one heard. */
395
+ leave(): void;
396
+ /** Your microphone off (true) or on (false), or the other way: says whether it is off. */
397
+ mute(off?: boolean): boolean;
398
+ isMuted(): boolean;
399
+ /** How loud a player is to you, 0 (not at all) to 1; this page only. */
400
+ volume(id: string, volume: number): void;
401
+ /** Whether a player (or you) is speaking now. */
402
+ speaking(id: string): boolean;
262
403
  }
263
404
 
264
405
  export interface SendOptions {
@@ -275,14 +416,82 @@ export interface SyncOptions {
275
416
  /** The numbers to send, as paths on the object ("position.x", "rotation.y"); for a THREE.Object3D, position x, y, z
276
417
  and rotation.y when left out, otherwise ["x", "y"]. */
277
418
  fields?: string[];
278
- /** Sends a second. */
419
+ /** Sends a second (30; 15 in a match of more than 16 players). */
279
420
  rate?: number;
280
- /** How far behind (ms) bound objects are drawn, so there are two updates to move between. */
281
- delay?: number;
421
+ /** How far behind (ms) bound objects are drawn, so there are updates to move between: "auto" (the default) is each
422
+ player's own, as late as their updates come and the time between two; a number fixes it for everyone. */
423
+ delay?: number | "auto";
424
+ /** 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)
425
+ players within it move every tick and those further once a second. 0: no limit. */
426
+ near?: number;
282
427
  /** Fields that are angles in radians, which turn the short way round. */
283
428
  angles?: string[];
284
429
  /** A jump longer than this between two updates is a teleport, not a glide (0: never). */
285
430
  snap?: number;
431
+ /** When a scripted room's code corrects you: "replay" (the default) puts you right as of the move it corrected and
432
+ plays your moves since on top, as Unreal does, so only how far off you were shows; "snap" puts you where the room
433
+ says (losing your movement since). */
434
+ fix?: "replay" | "snap";
435
+ }
436
+
437
+ export interface ObjectsOptions {
438
+ /** A value's name to a path on a bound object ({ x: "position.x", ry: "rotation.y" }). On a three.js object (one
439
+ with a position) x, y, z, qx to qw (a physics body's quaternion) and rx to rz go to its position, quaternion and
440
+ rotation by themselves; on anything else the name itself. */
441
+ map?: Record<string, string>;
442
+ /** Names that are angles in radians (by default ry, rx, rz, yaw, pitch, roll, angle, heading and rotation.*). */
443
+ angles?: string[];
444
+ /** How far behind (ms) objects are drawn: "auto" (each its own, as late as its updates come), or a number. */
445
+ delay?: number | "auto";
446
+ /** A jump longer than this between two updates is a teleport, not a glide (0: never). */
447
+ snap?: number;
448
+ }
449
+
450
+ /** What a scripted room's code moves (room.objects), drawn smoothly as players are. */
451
+ export interface RoomObjects {
452
+ /** The code made one (its values by name): make yours and bind() it. */
453
+ on(type: "add", fn: (key: string, values: Record<string, number>) => void): () => void;
454
+ /** The code ended one. */
455
+ on(type: "remove", fn: (key: string) => void): () => void;
456
+ /** Move `target` to where the object `key` is, on every step() (each value to its path: options.map). */
457
+ bind(key: string, target: object): void;
458
+ /** Once a frame: moves every bound object. */
459
+ step(): void;
460
+ keys(): string[];
461
+ /** An object's smoothed values now, by name, or null. */
462
+ values(key: string): Record<string, number> | null;
463
+ /** How long ago (ms) an object is drawn now. */
464
+ delay(key: string): number | null;
465
+ /** The room's time an object is drawn at now (room.past(key, at) in room.js). */
466
+ seen(key: string): number;
467
+ stop(): void;
468
+ }
469
+
470
+ export interface InputsOptions<S, I> {
471
+ /** Your player's state to start from. */
472
+ state: S;
473
+ /** This tick's input (what the player pressed, where they aim): anything JSON carries, small. */
474
+ input(): I;
475
+ /** Your game's own step, the one your room.js runs on the same input: change `state` (or return a new one). */
476
+ step(state: S, input: I | null, dt: number): S | void;
477
+ /** Ticks a second: your room.js's tickRate (60 when left out). */
478
+ rate?: number;
479
+ /** ms to ease a correction off over in what view() draws (0: none). */
480
+ smooth?: number;
481
+ }
482
+ /** Your player in an input-run room (room.inputs). */
483
+ export interface Inputs<S> {
484
+ /** Each frame: runs the ticks owed since the last (input read, sent, stepped). */
485
+ update(): void;
486
+ /** Your player's state as this page has run it, the newest tick. */
487
+ readonly state: S;
488
+ /** What to draw this frame: between the last two ticks, a correction eased off. */
489
+ view(): S;
490
+ /** { seq, acked, pending, waiting (the room's queue for you, lately at least), rewinds, pace }. */
491
+ stats(): { seq: number; acked: number; pending: number; waiting: number | null; rewinds: number; pace: number };
492
+ /** The room's code had your player elsewhere after input `at`: run again from there with the `replayed` since. */
493
+ on(type: "fix", fn: (state: S, info: { at: number; replayed: number }) => void): () => void;
494
+ stop(): void;
286
495
  }
287
496
 
288
497
  /** One object of yours sent to the room, and everyone else's moved smoothly (room.sync). */
@@ -291,18 +500,37 @@ export interface Sync {
291
500
  on(type: "join", fn: (id: string, values: number[]) => void): () => void;
292
501
  /** They left, or stopped syncing. */
293
502
  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;
503
+ /** A scripted room's code refused or corrected your move (onSync), or moved you (room.correct): your object was put
504
+ right, as of your move at `at`, by `by` (each field's change, your moves since on top; null when it was put
505
+ where the room says). `values`: where it is now. */
506
+ on(type: "fix", fn: (values: number[], info: { at: number | null; by: number[] | null }) => void): () => void;
296
507
  /** Move `target`'s fields to where player `id` was `delay` ms ago, on every step(). */
297
508
  bind(id: string, target: object): void;
298
509
  /** Once a frame: sends yours at `rate`, and moves every bound object. */
299
510
  step(): void;
300
511
  /** A player's smoothed values now (in the order of `fields`), or null before their first update. */
301
512
  values(id: string): number[] | null;
513
+ /** How long ago (ms) a player is drawn now, or null before their first update. */
514
+ delay(id: string): number | null;
515
+ /** The room's time a player is drawn at now: send it with a shot, and room.js can look back to then with
516
+ room.past(id, at) to judge the hit as this player saw it. */
517
+ seen(id: string): number;
302
518
  /** Stop sending and moving. */
303
519
  stop(): void;
304
520
  }
305
521
 
522
+ export interface RoomStats {
523
+ /** There and back to the room (ms), measured every 2 s; null before the first. */
524
+ ping: number | null;
525
+ /** How much the ping varies (ms). */
526
+ jitter: number;
527
+ /** How fast messages go: by the fast lane, the room's socket, or other tabs on localhost. */
528
+ lane: "fast" | "socket" | "local";
529
+ /** Bytes a second, sent and heard. */
530
+ up: number;
531
+ down: number;
532
+ }
533
+
306
534
  export interface Room {
307
535
  /** The room's name (to come back to it with). */
308
536
  readonly id: string;
@@ -322,6 +550,14 @@ export interface Room {
322
550
  readonly owners: Record<string, string | null>;
323
551
  /** The build has a room.js, running on Player Two's servers: only it writes the shared state. */
324
552
  readonly scripted: boolean;
553
+ /** Whether it is a world, kept for good (p2.rooms.world). */
554
+ readonly world: boolean;
555
+ /** Whether you're watching it (p2.rooms.watch, or a replay): you hear everything and can say nothing. */
556
+ readonly watching: boolean;
557
+ /** A replay's controls (a room from p2.rooms.replay). */
558
+ readonly replay?: ReplayControls;
559
+ /** Voice chat. */
560
+ readonly voice: Voice;
325
561
 
326
562
  /** Send `data` as a message of `type` to everyone else, or to `to`. In a scripted room, room.js hears it (onMessage). */
327
563
  send(type: string, data?: Json, options?: SendOptions): void;
@@ -330,14 +566,24 @@ export interface Room {
330
566
  get<T = any>(key: string): T | undefined;
331
567
  /** Whether you are the host. */
332
568
  isHost(): boolean;
333
- /** There and back, in ms (it also sets room.now()). */
569
+ /** There and back, in ms (it also sets room.now()). Rejected ("lost") when the line drops before the answer, or after
570
+ 10 s with none ("timeout"): a game pinging in a loop goes on after a reconnect. */
334
571
  ping(): Promise<number>;
572
+ /** How the connection is now. */
573
+ stats(): RoomStats;
335
574
  /** The room's clock, the same for every player in it (ms). */
336
575
  now(): number;
576
+ /** An input-run room (its room.js runs everyone's inputs in onTick): your inputs sent a tick each and your own player
577
+ run on them at once by the same step as the code's, put right ("fix") when the code's state says otherwise. */
578
+ inputs<S = any, I = any>(options: InputsOptions<S, I>): Inputs<S>;
337
579
  /** Leave for good (your seat goes to someone else). */
338
580
  leave(): void;
339
581
  /** Send `target`'s fields `rate` times a second on the fast lane, and move bound objects to where their players were. */
340
582
  sync(target: object, options: SyncOptions): Sync;
583
+ /** A scripted room's objects (room.objects in room.js: what its code moves), drawn smoothly. */
584
+ objects(options?: ObjectsOptions): RoomObjects;
585
+ /** Whether the room's sockets are held by gates (a world's area, a match of over 16 seats). */
586
+ readonly gated: boolean;
341
587
 
342
588
  on(type: "join" | "away" | "back", fn: (player: RoomPlayer) => void): () => void;
343
589
  /** why: "left", "dropped", "kicked", ... */
@@ -349,11 +595,95 @@ export interface Room {
349
595
  on(type: "message", fn: (type: string, data: any, from: string | null) => void): () => void;
350
596
  on(type: `message:${string}`, fn: (data: any, from: string | null) => void): () => void;
351
597
  on(type: "reconnecting" | "reconnected", fn: () => void): () => void;
598
+ /** A scripted room opened again between far-apart players (room.between() in room.js), after "reconnected": its new id. */
599
+ on(type: "moved", fn: (id: string) => void): () => void;
600
+ /** Someone came into voice (true) or left it. */
601
+ on(type: "voice", fn: (id: string, inVoice: boolean) => void): () => void;
602
+ /** Someone in voice (or you) started speaking (true) or stopped. */
603
+ on(type: "speaking", fn: (id: string, speaking: boolean) => void): () => void;
352
604
  /** You're out: "left", "closed", "lost", "replaced", "stopped", "time", "empty", ... */
353
605
  on(type: "close", fn: (why: string) => void): () => void;
354
606
  on(type: "error", fn: (error: P2Error) => void): () => void;
355
607
  }
356
608
 
609
+ /* ---------- travel and avatars (docs: /docs/sdk/travel) ---------- */
610
+
611
+ /** 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()). */
612
+ export type Pixel = [x: number, y: number, colour: string];
613
+
614
+ /** Who the player is, to dress your game's character with. */
615
+ export interface Avatar {
616
+ /** The same as p2.player.id. */
617
+ id: string;
618
+ handle: string;
619
+ name: string;
620
+ level: number;
621
+ picture: string | null;
622
+ critter: {
623
+ /** The name it is drawn from: the same name, the same critter, everywhere on Player Two. */
624
+ face: string;
625
+ /** Its box: 18 pixels a side. */
626
+ size: number;
627
+ /** The colour of the tile it stands on in Player Two's own pages. */
628
+ tile: string;
629
+ /** Every pixel of it, eyes open. */
630
+ pixels: Pixel[];
631
+ /** The same critter as a 3D model (.glb), standing on the origin and facing +Z: anyone may load it. */
632
+ model: string;
633
+ };
634
+ /** 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
635
+ (none yet). */
636
+ worn: { key: string; name: string; slot: string; model: string | null; picture: string | null }[];
637
+ }
638
+
639
+ /** One of the player's things from Player Two's shop in the Plaza (p2.items.list). */
640
+ export interface Item {
641
+ key: string;
642
+ name: string;
643
+ kind: "skin" | "avatar" | "wear" | "emote" | "effect";
644
+ tier: "common" | "rare" | "epic" | "legendary" | "mythic" | "divine";
645
+ /** Where it goes: head, face, neck or back for a wearable; skin, avatar or effect; none for an emote. */
646
+ slot: string | null;
647
+ /** Whether the player has it on. */
648
+ worn: boolean;
649
+ /** A Divine item's number, of the 77 made. */
650
+ number: number | null;
651
+ /** An SVG of it; none for an emote or an effect. */
652
+ picture: string | null;
653
+ }
654
+
655
+ /** A door for your own menu (p2.travel.links). */
656
+ export interface Door {
657
+ slug: string;
658
+ title: string;
659
+ tagline: string;
660
+ /** Its key art (square), or its vertical poster. */
661
+ art: string;
662
+ /** Players in it now. */
663
+ players: number;
664
+ /** Up to five of the player's friends in it now. */
665
+ friends: { handle: string; name: string }[];
666
+ /** Why it is here: the party's leader brought the party to it, friends are in it, the player played it lately, or
667
+ it is busy. */
668
+ why: "party" | "friends" | "recent" | "popular";
669
+ free: boolean;
670
+ /** In the player's library: a paid game they don't own opens with its demo, or Buy. */
671
+ owned: boolean;
672
+ }
673
+
674
+ /** Travel to other games on Player Two, with the player's party. In Player Two's player on the web; elsewhere each
675
+ rejects with code "no-travel". */
676
+ export interface Travel {
677
+ /** To another game, by its address (playertwo.lol/game/<slug>): it is given p2.params (yours, then `from`, this
678
+ game's address, and `place`). Resolves as the player goes; this page is closed after. */
679
+ to(slug: string, options?: { params?: Record<string, string>; place?: string }): Promise<{ to: { slug: string; title: string }; params: Record<string, string> }>;
680
+ /** Back to the Plaza, where every game on Player Two has its screen. */
681
+ home(): Promise<{ to: { slug: null; title: string; url: string } }>;
682
+ /** The doors to show in your own menu: where the party is, the games friends are in, the player's recent ones, the
683
+ busiest (up to 12). */
684
+ links(): Promise<{ home: { title: string; url: string }; links: Door[] }>;
685
+ }
686
+
357
687
  /* ---------- the SDK ---------- */
358
688
 
359
689
  /** What PlayerTwo.init() answers: who is playing, and every call. */
@@ -376,6 +706,9 @@ export interface P2 {
376
706
  invite(params?: Record<string, string>): Promise<string>;
377
707
  trophies: Trophies;
378
708
  addons: Addons;
709
+ /** What the game sells inside itself: consumables, items and skins, mods, packs made in its studio, or charges its
710
+ own server makes at its own prices. */
711
+ store: Store;
379
712
  stats: Stats;
380
713
  scores: Scores;
381
714
  saves: Saves;
@@ -385,6 +718,17 @@ export interface P2 {
385
718
  gameplay: { start(): Promise<unknown>; stop(): Promise<unknown> };
386
719
  /** Multiplayer with no server of your own. */
387
720
  rooms: Rooms;
721
+ /** Who the player is, to dress your game's character with: their critter as pixels and as a 3D model, and what they
722
+ wear. */
723
+ avatar(): Promise<Avatar>;
724
+ /** What the player has from Player Two's shop in the Plaza (avatars, skins, wearables, emotes, effects), and what
725
+ they have on: a game can dress its own character in them. */
726
+ items: { list(): Promise<Item[]> };
727
+ /** Travel to other games on Player Two, with the player's party. */
728
+ travel: Travel;
729
+ /** Whether a small live picture of the game goes to the Plaza's screens while it is played here (Player Two does
730
+ it; the player can stop it). */
731
+ onAir: boolean;
388
732
  /** Player Two's own layer over the game opened (true) or closed (false). */
389
733
  on(type: "overlay", fn: (open: boolean) => void): () => void;
390
734
  /** The player is about to close the game: what you keep should go now (a promise is waited for). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playertwo-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.2",
4
4
  "license": "MIT",
5
5
  "description": "Player Two's game SDK: trophies, leaderboards, saves and multiplayer rooms for games hosted on Player Two, with TypeScript types",
6
6
  "type": "module",
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
- /** Yours to keep things in, for as long as the room is open. */
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
- onTick?(room: Room<D>, dt: number): void;
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. */