playertwo-sdk 0.2.0 → 0.2.3

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,8 @@
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, the player's avatar, travel between games with their party, and multiplayer rooms (voice chat,
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,
5
6
  worlds, replays and your own server code in room.js included), for games hosted on [Player Two](https://playertwo.lol).
6
7
  Everything it offers, each with its part of the docs: https://playertwo.lol/llms.txt
7
8
 
@@ -61,5 +62,7 @@ export const { tickRate, onCreate, onJoin, onLeave, onMessage, onSync, onTick }
61
62
  });
62
63
  ```
63
64
 
64
- 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
65
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,8 +303,10 @@ export interface RoomPlayer {
234
303
  kind: string;
235
304
  /** Their connection dropped; their seat is held for them (20 s). */
236
305
  away: boolean;
237
- /** Their rating in this game (room.result in room.js), once they have one. */
306
+ /** Their rating in this room's mode (room.result in room.js; 1v1 and 3v3 rated apart), once they have one. */
238
307
  rating?: number;
308
+ /** Their team, 1 to room.teams, in a room with teams. */
309
+ team?: number;
239
310
  }
240
311
 
241
312
  export interface MatchOptions {
@@ -243,6 +314,8 @@ export interface MatchOptions {
243
314
  mode?: string;
244
315
  /** 1 to 64 players. */
245
316
  size?: number;
317
+ /** How many teams (2 to 16), the size split evenly between them: each player's team is in room.players. */
318
+ teams?: number;
246
319
  }
247
320
 
248
321
  export interface JoinOptions extends MatchOptions {
@@ -251,14 +324,18 @@ export interface JoinOptions extends MatchOptions {
251
324
  /** The room you were in (room.id), to come back to it. */
252
325
  room?: string;
253
326
  resume?: string;
254
- /** A match by skill: rooms of players rated near you first (the game's own ratings: room.result in room.js). */
327
+ /** A match by skill: rooms of players rated near you first (the game's own ratings in this mode: room.result in
328
+ room.js). */
255
329
  skill?: boolean;
330
+ /** A match for your whole party: every member who asks with together is seated in the same room, on one team (their
331
+ seats held 30 s while they come), and players on their own fill the other teams. Rejects no-party, party-too-big. */
332
+ together?: boolean;
256
333
  /** Keep the room as a replay, from its first player (a scripted room's code says so itself: room.record). */
257
334
  record?: boolean;
258
335
  }
259
336
 
260
337
  /** 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 }
338
+ export interface LiveRoom { id: string; mode: string; size: number; teams?: number; players: number; watchers: number; skill: number | null; closed: boolean }
262
339
  /** One of the game's replays (p2.rooms.replays). */
263
340
  export interface ReplayInfo { id: string; room: string; mode: string; size: number; players: { id: string; handle: string; name: string }[]; from: number; ms: number }
264
341
  /** A replay's controls (room.replay, on a room from p2.rooms.replay). */
@@ -336,7 +413,9 @@ export interface Voice {
336
413
  export interface SendOptions {
337
414
  /** One player's id, or a list; everyone else in the room when left out. */
338
415
  to?: string | string[];
339
- /** The fast lane: lossy, for updates that the next one replaces (where a thing is, not what happened). */
416
+ /** The fast lane: lossy, for updates that the next one replaces (where a thing is, not what happened). Where it
417
+ can't go player to player (a scripted room, a room over 16), it goes by the room's two sockets in turn, and an
418
+ older one that arrives after a newer one of its type is dropped. */
340
419
  fast?: boolean;
341
420
  }
342
421
 
@@ -469,6 +548,8 @@ export interface Room {
469
548
  readonly code: string | null;
470
549
  readonly mode: string;
471
550
  readonly size: number;
551
+ /** How many teams it has (1: none); each player's team is in room.players. */
552
+ readonly teams: number;
472
553
  /** Your id in the room (the same as p2.player.id). */
473
554
  readonly me: string;
474
555
  /** The host's id: whoever has been here longest. */
@@ -520,6 +601,8 @@ export interface Room {
520
601
  /** why: "left", "dropped", "kicked", ... */
521
602
  on(type: "leave", fn: (player: RoomPlayer | { id: string }, why: string) => void): () => void;
522
603
  on(type: "host", fn: (id: string) => void): () => void;
604
+ /** The room's code moved a player to another team (room.team in room.js). */
605
+ on(type: "team", fn: (id: string, team: number) => void): () => void;
523
606
  /** A key changed (value null: taken away); by: who wrote it. */
524
607
  on(type: "state", fn: (key: string, value: any, by: string | null) => void): () => void;
525
608
  /** `from` is the sending player's id, or null for the room's own code (a scripted room's room.js). */
@@ -637,6 +720,9 @@ export interface P2 {
637
720
  invite(params?: Record<string, string>): Promise<string>;
638
721
  trophies: Trophies;
639
722
  addons: Addons;
723
+ /** What the game sells inside itself: consumables, items and skins, mods, packs made in its studio, or charges its
724
+ own server makes at its own prices. */
725
+ store: Store;
640
726
  stats: Stats;
641
727
  scores: Scores;
642
728
  saves: Saves;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playertwo-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.3",
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
@@ -32,8 +32,11 @@ export interface Player {
32
32
  level: number;
33
33
  /** What the game kept of them with room.save (in any of its rooms, any time before), or null. */
34
34
  saved: Json | null;
35
- /** Their rating in this game (room.result moves it; 1500 before their first match). */
35
+ /** Their rating in this room's mode (room.result moves it; 1v1 and 3v3 apart; a new mode starts from their game
36
+ rating, or 1500). */
36
37
  rating?: number;
38
+ /** Their team, 1 to room.teams, in a room with teams. */
39
+ team?: number;
37
40
  }
38
41
 
39
42
  /** What room.js is handed: the room it runs for. `D` is what you keep in room.data. */
@@ -44,6 +47,9 @@ export type PhysicsBody =
44
47
  | { position: { x: number; y: number; z?: number }; quaternion: { x: number; y: number; z: number; w: number } };
45
48
 
46
49
  export interface Room<D extends object = Record<string, any>> {
50
+ /** The room's id, as its pages have it (room.id). Your build's rooms that run on one machine share your module's
51
+ own variables: keep what is one room's in room.data, or by this. */
52
+ readonly id: string;
47
53
  /** Who is here. */
48
54
  readonly players: readonly Player[];
49
55
  /** One of them, or null. */
@@ -71,8 +77,18 @@ export interface Room<D extends object = Record<string, any>> {
71
77
  /** 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
78
  moves by it (Elo, every two compared), as player.rating, and players are matched by it ({ skill: true }). */
73
79
  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). */
80
+ /** Closed to matching while a match is under way (false: a seat by the room's id is refused too, not-open), and open
81
+ again so its empty places are filled (true). */
75
82
  open(on?: boolean): void;
83
+ /** How many teams the room has (1: none). */
84
+ readonly teams: number;
85
+ /** Move a player to a team (1 to room.teams): every page hears "team". False when there is no such player or team. */
86
+ team(id: string, team: number): boolean;
87
+ /** A trusted score for a player on one of the game's leaderboards (kept if it is their best), as the game's own server
88
+ sends it: the way onto a board only its server may send to. Errors show in the studio. */
89
+ score(id: string, board: string, value: number): void;
90
+ /** One of the game's trophies for a player, as its own server gives it (server-only ones too). */
91
+ trophy(id: string, key: string): void;
76
92
  /** Keep the room as a replay from now (true), until record(false) or the room closes (up to 30 minutes). */
77
93
  record(on?: boolean): void;
78
94
  /** What your code moves (enemies, projectiles, a platform), drawn smoothly on every page (room.objects() there):