playertwo-sdk 0.2.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.
Files changed (3) hide show
  1. package/README.md +5 -2
  2. package/index.d.ts +72 -0
  3. package/package.json +1 -1
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> }>;
@@ -637,6 +706,9 @@ export interface P2 {
637
706
  invite(params?: Record<string, string>): Promise<string>;
638
707
  trophies: Trophies;
639
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;
640
712
  stats: Stats;
641
713
  scores: Scores;
642
714
  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.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",