playertwo-sdk 0.2.12 → 0.2.13

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.
Files changed (4) hide show
  1. package/README.md +32 -11
  2. package/index.d.ts +61 -12
  3. package/package.json +1 -1
  4. package/room.d.ts +13 -0
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # playertwo-sdk
2
2
 
3
3
  The game SDK for [PlayerTwo](https://playertwo.plus), with TypeScript types. Trophies, XP and levels, stats,
4
- leaderboards, cloud saves, presence, invites, add-ons, in-game purchases, donations to you, the player's avatar, travel
5
- between games, and multiplayer rooms with no server of your own (voice chat, worlds, replays and your own server code in `room.js`
4
+ leaderboards, cloud saves, presence, invites, add-ons, in-game purchases, donations to you, moments to share, the
5
+ player's avatar, travel between games, and multiplayer rooms with no server of your own (voice chat, worlds, replays and your own server code in `room.js`
6
6
  included).
7
7
 
8
8
  PlayerTwo is a game store and a launchpad. Games are sold (or free) to play in the browser or download, and raises
@@ -74,8 +74,9 @@ with code `not-on-player-two`; with `{ required: true }` the page says "Play thi
74
74
  | Stats | `p2.stats.get()`, `.get(key)`, `.set(key, n)`, `.add(key, n?)` | [call-stats](https://playertwo.plus/docs/sdk#call-stats) |
75
75
  | Cloud saves (8 slots, 1 MB each) | `p2.saves.list()`, `.get(slot)`, `.put(slot, data, { version })`, `.delete(slot)` | [call-saves](https://playertwo.plus/docs/sdk#call-saves) |
76
76
  | Add-ons (sold on the game's page) | `p2.addons.list()`, `p2.addons.has(key)` | [addons](https://playertwo.plus/docs/sdk#addons) |
77
- | In-game purchases | `p2.store.list()`, `.owned()`, `.buy(key)`, `.pay(chargeId)` | [purchases](https://playertwo.plus/docs/sdk#purchases) |
77
+ | In-game purchases | `p2.store.list({ coming })`, `.owned()`, `.access()`, `.sold(refs)`, `.buy(key)`, `.pay(chargeId)` | [purchases](https://playertwo.plus/docs/sdk#purchases) |
78
78
  | Donations to you | `p2.donate.available()`, `.open()`, `.button({ corner, parent })` | [donate](https://playertwo.plus/docs/sdk#donate) |
79
+ | Moments to share, on your results screen | `p2.moments.offer()` | [moments](https://playertwo.plus/docs/sdk#moments) |
79
80
  | Presence | `p2.presence.set(line, { join })` | [call-presence](https://playertwo.plus/docs/sdk#call-presence) |
80
81
  | Identity and invites | `p2.identity()`, `p2.invite(params)`, `p2.params` | [call-identity](https://playertwo.plus/docs/sdk#call-identity) |
81
82
  | Loading and play events | `p2.loading.done()`, `p2.gameplay.start()`, `.stop()` | [call-gameplay](https://playertwo.plus/docs/sdk#call-gameplay) |
@@ -97,6 +98,7 @@ Multiplayer, through `p2.rooms`:
97
98
  | Smooth movement | `room.sync(object, options)` | [rooms-sync](https://playertwo.plus/docs/sdk#rooms-sync) |
98
99
  | Ping, stats, the room's clock | `room.ping()`, `room.stats()`, `room.now()` | [rooms-fast](https://playertwo.plus/docs/sdk#rooms-fast) |
99
100
  | Your server code (`room.js`) | see below | [rooms-scripted](https://playertwo.plus/docs/sdk#rooms-scripted) |
101
+ | What each player owns of your store, in `room.js` | `player.owned`, `room.owned(id)`, `room.owns(id, keyOrRef)`, `onOwned` | [rooms-scripted](https://playertwo.plus/docs/sdk#rooms-scripted) |
100
102
  | Input-run rooms (client prediction, corrected by the server's state) | `room.inputs({ state, input, step })` | [rooms-inputs](https://playertwo.plus/docs/sdk#rooms-inputs) |
101
103
  | Corrections and lag-compensated hits | `onSync`, `room.correct`, `room.past` (room.js); `sync.seen(id)` | [rooms-corrections](https://playertwo.plus/docs/sdk#rooms-corrections) |
102
104
  | Server-moved objects | `room.objects()` (page), `room.objects.set` (room.js) | [rooms-objects](https://playertwo.plus/docs/sdk#rooms-objects) |
@@ -111,7 +113,7 @@ Multiplayer, through `p2.rooms`:
111
113
  | A bad connection, on purpose | `p2.rooms.simulate({ lag, jitter, loss })` or `?p2-net=150,30,0.02` | [rooms-simulate](https://playertwo.plus/docs/sdk#rooms-simulate) |
112
114
 
113
115
  Rooms are 1 to 64 players (256 in an area of a world) and are for games hosted on PlayerTwo (a build you push, or
114
- your own site served through PlayerTwo). A release never mixes versions in a room. All limits:
116
+ a game already played from its own site that PlayerTwo serves). A release never mixes versions in a room. All limits:
115
117
  https://playertwo.plus/docs/sdk#rooms-limits.
116
118
 
117
119
  ## A room's server: `playertwo-sdk/room`
@@ -193,11 +195,24 @@ Using the script tag and only want the types of the global `PlayerTwo`? Add `"ty
193
195
 
194
196
  Two ways, and a game can use both: products you make in your studio (`p2.store.buy(key)`), or charges your own server
195
197
  makes at its own prices with your game key (`p2.store.pay(chargeId)`). PlayerTwo's own card takes the player's yes
196
- and the payment from their PlayerTwo balance (USDC on Base). You get 80% of every in-game purchase, PlayerTwo keeps
198
+ and the payment from their PlayerTwo balance (one balance, added by card, PayPal or stablecoins), showing the piece's
199
+ picture big when you give one. You get 80% of every in-game purchase, PlayerTwo keeps
197
200
  20% (less your backers' share if your game raised with a share of its sales). Add-ons sold on the game's page are split
198
201
  as its sales are: PlayerTwo keeps 20%. Every purchase gives your game a signed receipt (a JWT typed `p2-buy`, RS256,
199
- keys at https://playertwo.plus/.well-known/p2-keys.json) and your studio's webhooks a `purchase` event. Docs:
200
- https://playertwo.plus/docs/sdk#purchases.
202
+ keys at https://playertwo.plus/.well-known/p2-keys.json) and your studio's webhooks a `purchase` event.
203
+
204
+ - **Who may buy:** `p2.store.access()` says where the player stands: `owned`, `free-game`, `free-period` (a paid game
205
+ played free for a while, with `until`), `test` or `none`. Every state but `none` may buy in the game, and what they
206
+ buy stays theirs.
207
+ - **Counts:** each product's `owners` ("Owned by N players"), and `p2.store.sold(refs)`: how many of your charges were
208
+ paid with each ref. A paid charge carries its `number` among them, for "#12 of Season 1".
209
+ - **Seasons:** a product's `onSaleFrom` and `onSaleUntil` take it on and off sale by themselves, and its owners keep it.
210
+ `p2.store.list({ coming: true })` lists the ones not on sale yet, `coming: true`, never buyable.
211
+ - **Packs:** a pack can be sold only to a player who owns none of its pieces (`noneOwned`).
212
+ - **In a room:** each player's `owned` arrives as they take their seat and again when they buy (`onOwned`);
213
+ `room.owns(id, keyOrRef)` refuses a look they don't own.
214
+
215
+ Docs: https://playertwo.plus/docs/sdk#purchases and https://playertwo.plus/docs/sdk#store-access.
201
216
 
202
217
  ## Donations
203
218
 
@@ -205,7 +220,9 @@ Players can donate to your studio for your game, even while it's free. Turn dona
205
220
  Donations) and pick their one place: your game's PlayerTwo page (PlayerTwo's card, in your words, colour and picture),
206
221
  or inside the game. Inside the game, your own button calls `p2.donate.open()` to open PlayerTwo's donation sheet, or
207
222
  `p2.donate.button()` puts PlayerTwo's pixel-heart "Support the dev" button in a corner (or inside an element of yours);
208
- `p2.donate.available()` says whether donations are on inside the game. The entire donation, minus the transaction fees,
223
+ `p2.donate.available()` says whether donations are on inside the game. A press of the player's opens the sheet at once,
224
+ however soon after the last (call `open()` within a second of it); asked without one, it opens at most once every two
225
+ minutes (`too-soon`). The entire donation, minus the transaction fees,
209
226
  goes to your studio: PlayerTwo takes no cut. A donation buys nothing in the game, and the game never learns who gave
210
227
  or how much, so it never belongs in your shop. Docs: https://playertwo.plus/docs/sdk#donate.
211
228
 
@@ -214,15 +231,19 @@ or how much, so it never belongs in your shop. Docs: https://playertwo.plus/docs
214
231
  A game key (`p2g_...`, made in your studio) lets your server check a session or a signed identity from
215
232
  `p2.identity()`, give server-only trophies, post to trusted leaderboards, read add-ons, make and check charges, use up
216
233
  consumables and report matches, under `https://playertwo.plus/api/v1/sdk/server/...`. Docs:
217
- https://playertwo.plus/docs/sdk#server and https://playertwo.plus/docs/sdk/connect (a game played on its own site).
234
+ https://playertwo.plus/docs/sdk#server and https://playertwo.plus/docs/sdk/connect (a game already played on its own
235
+ site).
218
236
 
219
237
  ## Putting it on PlayerTwo
220
238
 
221
239
  A whole game to start from: `npm create playertwo-game@latest my-game` (a small multiplayer Three.js game with rooms
222
240
  and `room.js`, see [create-playertwo-game](https://www.npmjs.com/package/create-playertwo-game)).
223
241
 
224
- 1. Submit the game at https://playertwo.plus/submit (that gives it its address, `your-game`).
225
- 2. Make an agent key in your studio, under Agents.
242
+ 1. Submit the game at https://playertwo.plus/submit with its web build: a zip of its folder (up to 5 GB unpacked,
243
+ 20,000 files, 512 MB a file), sent from your browser a file at a time, or one .html page. That gives it its
244
+ address, `your-game`. Playing from your own site is closed to new games.
245
+ 2. Each update goes up the same way (a zip, from its page's editor in your studio), or pushed: make an agent key in your
246
+ studio, under Agents.
226
247
  3. Push the build:
227
248
 
228
249
  ```
package/index.d.ts CHANGED
@@ -209,7 +209,7 @@ export interface Product {
209
209
  name: string;
210
210
  about: string;
211
211
  /** Its picture, when its studio gave it one: an https address in PlayerTwo's file store. PlayerTwo's card shows it
212
- beside the name; use it in your own shop too. */
212
+ big, over the name; use it in your own shop too. */
213
213
  picture?: string;
214
214
  /** In US dollars. */
215
215
  price: number;
@@ -220,6 +220,31 @@ export interface Product {
220
220
  owned: boolean;
221
221
  /** A consumable's: how many the player has. */
222
222
  count?: number;
223
+ /** How many players own it ("Owned by N players"): tests not counted; a pack's buyers; a consumable's, everyone who
224
+ ever bought it. Up to 30 seconds behind. */
225
+ owners: number;
226
+ /** Its on-sale dates (ms), or null: outside them it's off sale by itself (its owners keep it). Read your season clock
227
+ from these. */
228
+ onSaleFrom: number | null;
229
+ onSaleUntil: number | null;
230
+ /** Listed by list({ coming: true }) before its on-sale date: never buyable (buy rejects "not-yet"). */
231
+ coming?: boolean;
232
+ /** A pack's: sold only to a player who owns none of its pieces (buy rejects "owns-piece"). */
233
+ noneOwned?: boolean;
234
+ }
235
+
236
+ /** Where the player stands with the game (p2.store.access; docs: /docs/sdk#store-access). Every state but "none" may
237
+ buy in the game, and what they buy stays theirs. */
238
+ export interface Access {
239
+ /** owned: they own it. free-game: it costs nothing. free-period: a paid game played free for a while (never owning
240
+ it). test: its team and testers. none: no-game. */
241
+ state: "owned" | "free-game" | "free-period" | "test" | "none";
242
+ /** A free period by sale: when it ends (ms). */
243
+ until: number | null;
244
+ /** A free period by trial: seconds of play left. */
245
+ left?: number;
246
+ /** What the free period is: the game's free-to-play sale, or the player's own free trial. */
247
+ via?: "sale" | "trial";
223
248
  }
224
249
 
225
250
  /** What the player has of the game's products (a consumable: how many). */
@@ -243,8 +268,9 @@ export type Bought =
243
268
  export type Paid =
244
269
  | {
245
270
  ok: true;
246
- /** `ref`: your own reference for it, as your server gave it. */
247
- charge: { id: string; name: string; ref: string | null };
271
+ /** `ref`: your own reference for it, as your server gave it. `number`: its place among the charges paid with
272
+ that ref (1 for the first; null for a test, or with no ref), for "#12 of Season 1". */
273
+ charge: { id: string; name: string; ref: string | null; number: number | null };
248
274
  /** What was paid, in US dollars (0 for a test). */
249
275
  amount: number;
250
276
  /** A JWT typed "p2-buy" (kind "charge", key the charge's id), RS256 with PlayerTwo's keys, for your own server: an hour. */
@@ -259,14 +285,22 @@ export type Paid =
259
285
  export type SignUpAnswer = { ok: true; card?: true; local?: true } | { ok: false; reason: "cancelled" };
260
286
 
261
287
  export interface Store {
262
- /** Each product on sale, with what the player has of it. */
263
- list(): Promise<Product[]>;
288
+ /** Each product on sale, with what the player has of it; { coming: true } lists those whose on-sale date is ahead
289
+ too, each coming: true and never buyable. */
290
+ list(options?: { coming?: boolean }): Promise<Product[]>;
264
291
  /** What the player has. */
265
292
  owned(): Promise<Holding[]>;
293
+ /** Where the player stands with the game: owned, a free game, a free period (with its end), a test, or none. */
294
+ access(): Promise<Access>;
295
+ /** How many of your charges were paid with each ref (a ref ending in * counts every ref starting so): { [ref]: n }.
296
+ Tests not counted. */
297
+ sold(refs: string[] | string): Promise<Record<string, number>>;
266
298
  /** Asks PlayerTwo to sell one: its own step, outside the game's frame, where the player says yes. Rejects with code
267
299
  "no-store" where it can't (outside PlayerTwo's player on the web), "busy" (its sheet is open already),
268
- "unknown-product", "owned" (a skin, item or mod they have), "no-game" (they don't have the game yet), "guest" or
269
- "offline". */
300
+ "unknown-product" (nothing on sale by that key: retired, or past its on-sale end), "not-yet" (before its on-sale
301
+ start; from: when, ms), "owned" (a skin, item or mod they have), "pack-owned" (a pack whose pieces are all theirs),
302
+ "owns-piece" (a pack sold to players who own none of it; owned: the keys), "no-game" (they may not buy in it: see
303
+ access()), "guest" or "offline". */
270
304
  buy(key: string): Promise<Bought>;
271
305
  /** Asks the player to pay a charge your server made (POST /api/v1/sdk/server/store/charges, your game's key): its
272
306
  name and price on PlayerTwo's card, paid once; one paid already answers its receipt again. Rejects with code
@@ -278,15 +312,28 @@ export interface Store {
278
312
  /** What p2.donate.open() answers: the sheet was shown (never how much, nor whether anything was given), or why not. */
279
313
  export type DonateShown = { shown: true; local?: true } | { shown: false; reason: "off" | "placement" | "too-soon" };
280
314
 
315
+ /** Moments to share (p2.moments; docs: /docs/sdk#moments): the player's best moment of this session, which PlayerTwo
316
+ picks from what the game already sent it (a new best, a rare trophy, a level, the hours), offered on the game's own
317
+ results screen. */
318
+ export interface Moments {
319
+ /** Call it once your results screen is showing, never during play. When the session did something worth sharing,
320
+ PlayerTwo's share sheet opens over the game and this resolves once it closes ({ offered: true, shared }); otherwise
321
+ (nothing worth it, offers turned off, a guest, a test, outside PlayerTwo's player, a second ask this session) it
322
+ resolves { offered: false, shared: false } at once. Never rejects. One ask a session: offered here, closing the game
323
+ asks nothing more. */
324
+ offer(): Promise<{ offered: boolean; shared: boolean }>;
325
+ }
326
+
281
327
  /** Donations to the game's developer (p2.donate; docs: /docs/sdk#donate), when its studio put them inside the game (its
282
328
  one place: the game or its PlayerTwo page). The entire donation, minus the transaction fees, goes to the studio; PlayerTwo takes
283
329
  nothing. A donation buys nothing in the game, and the game never learns of one. */
284
330
  export interface Donate {
285
331
  /** Whether the game takes donations inside itself now. */
286
332
  available(): Promise<boolean>;
287
- /** Opens PlayerTwo's donation sheet over the game (from the player's press on your own button). At most once every
288
- two minutes. Rejects with code "no-donate" outside PlayerTwo's player on the web, "busy" while another of its
289
- sheets is open. */
333
+ /** Opens PlayerTwo's donation sheet over the game (from the player's press on your own button). A press opens it at
334
+ once, however soon after the last (call it within a second of the press: the browser's navigator.userActivation is
335
+ sent with it); without one, at most once every two minutes ("too-soon"). Rejects with code "no-donate" outside
336
+ PlayerTwo's player on the web, "busy" while another of its sheets is open. */
290
337
  open(): Promise<DonateShown>;
291
338
  /** PlayerTwo's own Donate button (a pixel heart, "Support the dev"), for a game that draws none: in a corner of the
292
339
  page, or inside an element of yours. Shown only while the game takes donations inside itself; null otherwise. */
@@ -728,9 +775,10 @@ export interface Avatar {
728
775
  export interface Item {
729
776
  key: string;
730
777
  name: string;
731
- kind: "skin" | "avatar" | "wear" | "emote" | "effect";
778
+ kind: "skin" | "avatar" | "wear" | "emote" | "effect" | "frame";
732
779
  tier: "common" | "rare" | "epic" | "legendary" | "mythic" | "divine";
733
- /** Where it goes: head, face, neck or back for a wearable; skin, avatar or effect; none for an emote. */
780
+ /** Where it goes: head, face, neck or back for a wearable; skin, avatar, effect or frame (what the moments a player
781
+ shares on PlayerTwo wear, earned, never sold: "crew-frame"); none for an emote. */
734
782
  slot: string | null;
735
783
  /** Whether the player has it on. */
736
784
  worn: boolean;
@@ -843,6 +891,7 @@ export interface P2 {
843
891
  store: Store;
844
892
  /** Donations to the game's developer, when its studio put them inside the game. */
845
893
  donate: Donate;
894
+ moments: Moments;
846
895
  stats: Stats;
847
896
  scores: Scores;
848
897
  saves: Saves;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playertwo-sdk",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "description": "SDK for HTML5 and browser games on PlayerTwo (playertwo.plus): trophies, leaderboards, stats, cloud saves, in-game purchases, donations to the developer, your own events and errors in your analytics, feeds of outside data, voice chat and multiplayer rooms with server-side room code.",
5
5
  "keywords": [
6
6
  "playertwo",
package/room.d.ts CHANGED
@@ -42,8 +42,15 @@ export interface Player {
42
42
  ping: number | null;
43
43
  /** How much their round trip varies, in ms (null until known). */
44
44
  jitter: number | null;
45
+ /** (r29) What they own of the game's store, read from PlayerTwo as they take their seat in a room of a game that
46
+ sells, and again when they buy in the room (onOwned); null when the game sells nothing or it couldn't be read. */
47
+ owned: Owned | null;
45
48
  }
46
49
 
50
+ /** What a player owns of the game's store (r29): the keys of the products they own (a consumable while they have
51
+ some), a consumable's count by key, and the refs of the charges they paid. */
52
+ export interface Owned { products: string[]; counts: Record<string, number>; refs: string[] }
53
+
47
54
  /** What room.js is handed: the room it runs for. `D` is what you keep in room.data. */
48
55
  /** A physics body room.objects.set takes as it is: Rapier's (translation() and rotation()), or one with a position and
49
56
  a quaternion. */
@@ -57,6 +64,10 @@ export interface Room<D extends object = Record<string, any>> {
57
64
  readonly id: string;
58
65
  /** Who is here. */
59
66
  readonly players: readonly Player[];
67
+ /** (r29) What a player owns of the game's store (player.owned), or null. */
68
+ owned(id: string): Owned | null;
69
+ /** (r29) Whether a player owns a product (by its key) or paid a charge with that ref: refuse a look they don't own. */
70
+ owns(id: string, keyOrRef: string): boolean;
60
71
  /** One of them, or null. */
61
72
  player(id: string): Player | null;
62
73
  /** A message to every page in the room, or to `to` (an id, or a list). Pages hear it with room.on("message:<type>"). */
@@ -167,6 +178,8 @@ export interface RoomScript<D extends object = Record<string, any>> {
167
178
  /** `inputs`: in an input-run room (its pages send room.inputs), each player's input for this step, by id (none when
168
179
  theirs didn't come in time); dt is then always 1 / tickRate, the steps kept to real time. */
169
180
  onTick?(room: Room<D>, dt: number, inputs: Record<string, any>): void;
181
+ /** (r29) What a player owns changed while they're here: they bought something in the game. */
182
+ onOwned?(room: Room<D>, player: Player, owned: Owned | null): void;
170
183
  }
171
184
 
172
185
  /** Types every hook at once: answers `script` as it is. */