@playcademy/sdk 0.0.1-beta.3 → 0.0.1-beta.30

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.
@@ -1,38 +1,139 @@
1
- import { type Method } from './request';
2
- import { type Game, type GameWithManifest, type UpsertGameMetadataInput, type InsertReward, type UpdateReward } from '@playcademy/data/schemas';
3
- import type { GameState, InventoryItemWithReward, ClientConfig, ClientEvents, LoginResponse, GameTokenResponse, StartSessionResponse, InventoryMutationResponse, DeveloperStatusValue } from '../types';
1
+ import { init, login } from './static';
2
+ import type { ClientConfig, ClientEvents } from '../types';
3
+ import type { Method } from './request';
4
+ /**
5
+ * Main Playcademy SDK client for interacting with the platform API.
6
+ * Provides namespaced access to all platform features including games, users, inventory, and more.
7
+ */
4
8
  export declare class PlaycademyClient {
5
9
  private baseUrl;
6
10
  private token?;
7
11
  private gameId?;
8
12
  private listeners;
9
13
  private internalClientSessionId?;
10
- constructor(config: ClientConfig);
11
- private _initializeInternalSession;
14
+ /**
15
+ * Creates a new PlaycademyClient instance.
16
+ *
17
+ * @param config - Optional configuration object
18
+ * @param config.baseUrl - Base URL for API requests (defaults to '/api')
19
+ * @param config.token - Authentication token
20
+ * @param config.gameId - Game ID for automatic session management
21
+ */
22
+ constructor(config?: Partial<ClientConfig>);
23
+ /**
24
+ * Gets the effective base URL for API requests.
25
+ * Converts relative URLs to absolute URLs in browser environments.
26
+ *
27
+ * @returns The complete base URL for API requests
28
+ */
12
29
  getBaseUrl(): string;
13
- on<E extends keyof ClientEvents>(event: E, callback: (payload: ClientEvents[E]) => void): void;
14
- private emit;
30
+ /**
31
+ * Simple ping method for testing connectivity.
32
+ *
33
+ * @returns 'pong' string response
34
+ */
35
+ ping(): string;
36
+ /**
37
+ * Sets the authentication token for API requests.
38
+ * Emits an 'authChange' event when the token changes.
39
+ *
40
+ * @param token - The authentication token, or null to clear
41
+ */
15
42
  setToken(token: string | null): void;
43
+ /**
44
+ * Registers a callback to be called when authentication state changes.
45
+ *
46
+ * @param callback - Function to call when auth state changes
47
+ */
16
48
  onAuthChange(callback: (token: string | null) => void): void;
49
+ /**
50
+ * Registers an event listener for client events.
51
+ *
52
+ * @param event - The event type to listen for
53
+ * @param callback - Function to call when the event is emitted
54
+ */
55
+ on<E extends keyof ClientEvents>(event: E, callback: (payload: ClientEvents[E]) => void): void;
56
+ /**
57
+ * Emits an event to all registered listeners.
58
+ *
59
+ * @param event - The event type to emit
60
+ * @param payload - The event payload
61
+ */
62
+ private emit;
63
+ /**
64
+ * Makes an authenticated HTTP request to the API.
65
+ *
66
+ * @param path - API endpoint path
67
+ * @param method - HTTP method
68
+ * @param body - Request body (optional)
69
+ * @param headers - Additional headers (optional)
70
+ * @returns Promise resolving to the response data
71
+ */
17
72
  protected request<T>(path: string, method: Method, body?: unknown, headers?: Record<string, string>): Promise<T>;
73
+ /**
74
+ * Ensures a gameId is available, throwing an error if not.
75
+ *
76
+ * @returns The gameId
77
+ * @throws PlaycademyError if no gameId is configured
78
+ */
18
79
  private _ensureGameId;
80
+ /**
81
+ * Initializes an internal game session for automatic session management.
82
+ * Called automatically when a gameId is provided in the constructor.
83
+ */
84
+ private _initializeInternalSession;
85
+ /** Authentication methods (logout) */
19
86
  auth: {
20
87
  logout: () => Promise<void>;
21
88
  };
89
+ /** Runtime methods (getGameToken, exit) */
22
90
  runtime: {
23
91
  getGameToken: (gameId: string, options?: {
24
92
  apply?: boolean;
25
- }) => Promise<GameTokenResponse>;
93
+ }) => Promise<import("../types").GameTokenResponse>;
26
94
  exit: () => Promise<void>;
95
+ onInit: (handler: (context: import("../types").GameContextPayload) => void) => void;
96
+ onTokenRefresh: (handler: (data: {
97
+ token: string;
98
+ exp: number;
99
+ }) => void) => void;
100
+ onPause: (handler: () => void) => void;
101
+ onResume: (handler: () => void) => void;
102
+ onForceExit: (handler: () => void) => void;
103
+ onOverlay: (handler: (isVisible: boolean) => void) => void;
104
+ ready: () => void;
105
+ sendTelemetry: (data: {
106
+ fps: number;
107
+ mem: number;
108
+ }) => void;
109
+ removeListener: (eventType: import("..").MessageEvents, handler: ((context: import("../types").GameContextPayload) => void) | ((data: {
110
+ token: string;
111
+ exp: number;
112
+ }) => void) | (() => void) | ((isVisible: boolean) => void)) => void;
113
+ removeAllListeners: () => void;
114
+ getListenerCounts: () => Record<string, number>;
27
115
  };
116
+ /** Game management methods (fetch, list, saveState, loadState, sessions) */
28
117
  games: {
29
- fetch: (gameIdOrSlug: string) => Promise<GameWithManifest>;
30
- list: () => Promise<Array<Game>>;
118
+ fetch: (gameIdOrSlug: string) => Promise<import("@playcademy/data/types").GameWithManifest>;
119
+ list: () => Promise<Array<import("@playcademy/data/types").Game>>;
31
120
  saveState: (state: Record<string, unknown>) => Promise<void>;
32
- loadState: () => Promise<GameState>;
33
- startSession: (gameId?: string) => Promise<StartSessionResponse>;
121
+ loadState: () => Promise<import("@playcademy/data/types").GameStateData>;
122
+ startSession: (gameId?: string) => Promise<import("../types").StartSessionResponse>;
34
123
  endSession: (sessionId: string, gameId?: string) => Promise<void>;
124
+ token: {
125
+ create: (gameId: string, options?: {
126
+ apply?: boolean;
127
+ }) => Promise<import("../types").GameTokenResponse>;
128
+ };
129
+ leaderboard: {
130
+ get: (gameId: string, options?: {
131
+ limit?: number;
132
+ offset?: number;
133
+ }) => Promise<import("@playcademy/data/types").LeaderboardEntry[]>;
134
+ };
35
135
  };
136
+ /** User methods (me, inventory management) */
36
137
  users: {
37
138
  me: () => Promise<{
38
139
  id: string;
@@ -43,102 +144,299 @@ export declare class PlaycademyClient {
43
144
  image: string | null;
44
145
  role: "admin" | "player" | "developer";
45
146
  developerStatus: "none" | "pending" | "approved";
147
+ characterCreated: boolean;
46
148
  createdAt: Date;
47
149
  updatedAt: Date;
48
150
  }>;
49
151
  inventory: {
50
- get: () => Promise<InventoryItemWithReward[]>;
51
- add: (rewardId: string, qty: number) => Promise<InventoryMutationResponse>;
52
- spend: (rewardId: string, qty: number) => Promise<InventoryMutationResponse>;
152
+ get: () => Promise<import("@playcademy/data/types").InventoryItemWithItem[]>;
153
+ add: (identifier: string, qty: number) => Promise<import("../types").InventoryMutationResponse>;
154
+ remove: (identifier: string, qty: number) => Promise<import("../types").InventoryMutationResponse>;
155
+ quantity: (identifier: string) => Promise<number>;
156
+ has: (identifier: string, minQuantity?: number) => Promise<boolean>;
157
+ };
158
+ scores: {
159
+ get: (userIdOrOptions?: string | {
160
+ limit?: number;
161
+ gameId?: string;
162
+ }, options?: {
163
+ limit?: number;
164
+ gameId?: string;
165
+ }) => Promise<import("./namespaces/users").UserScore[]>;
53
166
  };
54
167
  };
168
+ /** Developer tools (auth, games, keys management) */
55
169
  dev: {
56
170
  auth: {
57
171
  applyForDeveloper: () => Promise<void>;
58
- getDeveloperStatus: () => Promise<DeveloperStatusValue>;
172
+ getStatus: () => Promise<import("@playcademy/data/types").DeveloperStatusValue>;
59
173
  };
60
174
  games: {
61
- upsert: (slug: string, metadata: UpsertGameMetadataInput, file: File | Blob) => Promise<Game>;
62
- update: (gameId: string, props: Partial<Game>) => Promise<void>;
175
+ upsert: (slug: string, metadata: import("@playcademy/data/types").UpsertGameMetadataInput, file: File | Blob) => Promise<import("@playcademy/data/types").Game>;
176
+ update: (gameId: string, props: Partial<import("@playcademy/data/types").Game>) => Promise<void>;
63
177
  delete: (gameId: string) => Promise<void>;
64
178
  };
65
179
  keys: {
66
- createKey: (gameId: string, label?: string) => Promise<{
180
+ create: (label?: string) => Promise<{
67
181
  id: string;
68
182
  createdAt: Date;
69
183
  userId: string;
70
184
  label: string | null;
71
185
  keyHash: string;
72
186
  }>;
73
- listKeys: (gameId: string) => Promise<{
187
+ list: () => Promise<{
74
188
  id: string;
75
189
  createdAt: Date;
76
190
  userId: string;
77
191
  label: string | null;
78
192
  keyHash: string;
79
193
  }[]>;
80
- revokeKey: (keyId: string) => Promise<void>;
194
+ revoke: (keyId: string) => Promise<void>;
195
+ };
196
+ items: {
197
+ create: (gameId: string, slug: string, itemData: Omit<import("@playcademy/data/types").InsertItemInput, "slug" | "gameId">) => Promise<import("@playcademy/data/types").Item>;
198
+ update: (gameId: string, itemId: string, updates: import("@playcademy/data/types").UpdateItemInput) => Promise<import("@playcademy/data/types").Item>;
199
+ list: (gameId: string) => Promise<Array<import("@playcademy/data/types").Item>>;
200
+ get: (gameId: string, slug: string) => Promise<import("@playcademy/data/types").Item>;
201
+ delete: (gameId: string, itemId: string) => Promise<void>;
202
+ shop: {
203
+ create: (gameId: string, itemId: string, listingData: Omit<import("@playcademy/data/types").InsertShopListingInput, "itemId">) => Promise<import("@playcademy/data/types").ShopListing>;
204
+ get: (gameId: string, itemId: string) => Promise<import("@playcademy/data/types").ShopListing | null>;
205
+ update: (gameId: string, itemId: string, updates: import("@playcademy/data/types").UpdateShopListingInput) => Promise<import("@playcademy/data/types").ShopListing>;
206
+ delete: (gameId: string, itemId: string) => Promise<void>;
207
+ list: (gameId: string) => Promise<Array<import("@playcademy/data/types").ShopListing & {
208
+ item: import("@playcademy/data/types").Item;
209
+ }>>;
210
+ };
81
211
  };
82
212
  };
213
+ /** Map methods (elements) */
83
214
  maps: {
84
- elements: (mapId: string) => Promise<{
85
- id: string;
86
- mapId: string | null;
87
- elementSlug: string;
88
- interactionType: "game_entry" | "game_registry" | "info" | "teleport" | "door_in" | "door_out" | "npc_interaction" | "quest_trigger";
89
- metadata: ({
90
- description?: string | undefined;
91
- sourceTiledObjects?: Record<string, unknown>[] | undefined;
92
- } & {
93
- [k: string]: unknown;
94
- }) | null;
95
- gameId: string | null;
96
- }[]>;
215
+ get: (identifier: string) => Promise<import("@playcademy/data/types").MapData>;
216
+ elements: (mapId: string) => Promise<import("@playcademy/data/types").MapElementWithGame[]>;
217
+ objects: {
218
+ list: (mapId: string) => Promise<import("@playcademy/data/types").MapObjectWithItem[]>;
219
+ create: (mapId: string, objectData: import("@playcademy/data/types").CreateMapObjectData) => Promise<import("@playcademy/data/types").MapObjectWithItem>;
220
+ delete: (mapId: string, objectId: string) => Promise<void>;
221
+ };
97
222
  };
223
+ /** Admin methods (games, items, currencies, shop listings) */
98
224
  admin: {
99
225
  games: {
100
226
  pauseGame: (gameId: string) => Promise<void>;
101
227
  resumeGame: (gameId: string) => Promise<void>;
102
228
  };
103
- rewards: {
104
- createReward: (props: InsertReward) => Promise<{
229
+ items: {
230
+ create: (props: import("@playcademy/data/types").InsertItemInput) => Promise<{
105
231
  id: string;
106
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
232
+ createdAt: Date;
233
+ slug: string;
107
234
  displayName: string;
108
- description: string | null;
109
235
  metadata: unknown;
110
- internalName: string;
236
+ gameId: string | null;
237
+ description: string | null;
238
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "accessory" | "other";
239
+ isPlaceable: boolean;
240
+ imageUrl: string | null;
111
241
  }>;
112
- getReward: (rewardId: string) => Promise<{
242
+ get: (itemId: string) => Promise<{
113
243
  id: string;
114
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
244
+ createdAt: Date;
245
+ slug: string;
115
246
  displayName: string;
116
- description: string | null;
117
247
  metadata: unknown;
118
- internalName: string;
248
+ gameId: string | null;
249
+ description: string | null;
250
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "accessory" | "other";
251
+ isPlaceable: boolean;
252
+ imageUrl: string | null;
119
253
  }>;
120
- listRewards: () => Promise<{
254
+ list: () => Promise<{
121
255
  id: string;
122
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
256
+ createdAt: Date;
257
+ slug: string;
123
258
  displayName: string;
124
- description: string | null;
125
259
  metadata: unknown;
126
- internalName: string;
260
+ gameId: string | null;
261
+ description: string | null;
262
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "accessory" | "other";
263
+ isPlaceable: boolean;
264
+ imageUrl: string | null;
127
265
  }[]>;
128
- updateReward: (rewardId: string, props: UpdateReward) => Promise<{
266
+ update: (itemId: string, props: import("@playcademy/data/types").UpdateItemInput) => Promise<{
129
267
  id: string;
130
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
268
+ createdAt: Date;
269
+ slug: string;
131
270
  displayName: string;
132
- description: string | null;
133
271
  metadata: unknown;
134
- internalName: string;
272
+ gameId: string | null;
273
+ description: string | null;
274
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "accessory" | "other";
275
+ isPlaceable: boolean;
276
+ imageUrl: string | null;
135
277
  }>;
136
- deleteReward: (rewardId: string) => Promise<void>;
278
+ delete: (itemId: string) => Promise<void>;
279
+ };
280
+ currencies: {
281
+ create: (props: import("@playcademy/data/types").InsertCurrencyInput) => Promise<{
282
+ symbol: string | null;
283
+ id: string;
284
+ createdAt: Date;
285
+ updatedAt: Date | null;
286
+ itemId: string;
287
+ isPrimary: boolean;
288
+ }>;
289
+ get: (currencyId: string) => Promise<{
290
+ symbol: string | null;
291
+ id: string;
292
+ createdAt: Date;
293
+ updatedAt: Date | null;
294
+ itemId: string;
295
+ isPrimary: boolean;
296
+ }>;
297
+ list: () => Promise<{
298
+ symbol: string | null;
299
+ id: string;
300
+ createdAt: Date;
301
+ updatedAt: Date | null;
302
+ itemId: string;
303
+ isPrimary: boolean;
304
+ }[]>;
305
+ update: (currencyId: string, props: import("@playcademy/data/types").UpdateCurrencyInput) => Promise<{
306
+ symbol: string | null;
307
+ id: string;
308
+ createdAt: Date;
309
+ updatedAt: Date | null;
310
+ itemId: string;
311
+ isPrimary: boolean;
312
+ }>;
313
+ delete: (currencyId: string) => Promise<void>;
314
+ };
315
+ shopListings: {
316
+ create: (props: import("@playcademy/data/types").InsertShopListingInput) => Promise<{
317
+ id: string;
318
+ createdAt: Date;
319
+ updatedAt: Date | null;
320
+ itemId: string;
321
+ currencyId: string;
322
+ price: number;
323
+ sellBackPercentage: number | null;
324
+ stock: number | null;
325
+ isActive: boolean;
326
+ availableFrom: Date | null;
327
+ availableUntil: Date | null;
328
+ }>;
329
+ get: (listingId: string) => Promise<{
330
+ id: string;
331
+ createdAt: Date;
332
+ updatedAt: Date | null;
333
+ itemId: string;
334
+ currencyId: string;
335
+ price: number;
336
+ sellBackPercentage: number | null;
337
+ stock: number | null;
338
+ isActive: boolean;
339
+ availableFrom: Date | null;
340
+ availableUntil: Date | null;
341
+ }>;
342
+ list: () => Promise<{
343
+ id: string;
344
+ createdAt: Date;
345
+ updatedAt: Date | null;
346
+ itemId: string;
347
+ currencyId: string;
348
+ price: number;
349
+ sellBackPercentage: number | null;
350
+ stock: number | null;
351
+ isActive: boolean;
352
+ availableFrom: Date | null;
353
+ availableUntil: Date | null;
354
+ }[]>;
355
+ update: (listingId: string, props: import("@playcademy/data/types").UpdateShopListingInput) => Promise<{
356
+ id: string;
357
+ createdAt: Date;
358
+ updatedAt: Date | null;
359
+ itemId: string;
360
+ currencyId: string;
361
+ price: number;
362
+ sellBackPercentage: number | null;
363
+ stock: number | null;
364
+ isActive: boolean;
365
+ availableFrom: Date | null;
366
+ availableUntil: Date | null;
367
+ }>;
368
+ delete: (listingId: string) => Promise<void>;
369
+ };
370
+ };
371
+ /** Shop methods (view) */
372
+ shop: {
373
+ view: () => Promise<import("@playcademy/data/types").ShopViewResponse>;
374
+ };
375
+ /** Level methods (levels) */
376
+ levels: {
377
+ get: () => Promise<import("@playcademy/data/types").UserLevel>;
378
+ progress: () => Promise<{
379
+ level: number;
380
+ currentXp: number;
381
+ xpToNextLevel: number;
382
+ totalXP: number;
383
+ }>;
384
+ addXP: (amount: number) => Promise<import("@playcademy/data/types").XPAddResult>;
385
+ config: {
386
+ list: () => Promise<import("@playcademy/data/types").LevelConfig[]>;
387
+ get: (level: number) => Promise<import("@playcademy/data/types").LevelConfig | null>;
137
388
  };
138
389
  };
390
+ /** Telemetry methods (pushMetrics) */
139
391
  telemetry: {
140
392
  pushMetrics: (metrics: Record<string, number>) => Promise<void>;
141
393
  };
142
- ping(): string;
143
- static login(baseUrl: string, email: string, password: string): Promise<LoginResponse>;
394
+ /** Credits methods (credits management) */
395
+ credits: {
396
+ balance: () => Promise<number>;
397
+ add: (amount: number) => Promise<number>;
398
+ spend: (amount: number) => Promise<number>;
399
+ };
400
+ /** Platform-wide leaderboard methods (fetch, getUserRank) */
401
+ leaderboard: {
402
+ fetch: (options?: import("@playcademy/data/types").LeaderboardOptions) => Promise<import("@playcademy/data/types").GameLeaderboardEntry[]>;
403
+ getUserRank: (gameId: string, userId: string) => Promise<import("@playcademy/data/types").UserRankResponse>;
404
+ };
405
+ /** Platform-wide scores methods (submit, getUserScores) */
406
+ scores: {
407
+ submit: (gameId: string, score: number, metadata?: Record<string, unknown>) => Promise<import("./namespaces/scores").ScoreSubmission>;
408
+ getByUser: (gameId: string, userId: string, options?: {
409
+ limit?: number;
410
+ }) => Promise<import("@playcademy/data/types").UserScore[]>;
411
+ };
412
+ /** Character methods (get, create, update, components) */
413
+ character: {
414
+ get: (userId?: string) => Promise<import("@playcademy/data/types").PlayerCharacter | null>;
415
+ create: (characterData: import("./namespaces/character").CreateCharacterData) => Promise<import("@playcademy/data/types").PlayerCharacter>;
416
+ update: (updates: import("./namespaces/character").UpdateCharacterData) => Promise<import("@playcademy/data/types").PlayerCharacter>;
417
+ components: {
418
+ list: (options?: import("./namespaces/character").CharacterComponentsOptions & {
419
+ skipCache?: boolean;
420
+ }) => Promise<import("@playcademy/data/types").CharacterComponentWithSpriteUrl[]>;
421
+ clearCache: () => void;
422
+ getCacheKeys: () => string[];
423
+ };
424
+ };
425
+ /** Sprites methods (templates) */
426
+ sprites: {
427
+ templates: {
428
+ get: (slug: string) => Promise<import("@playcademy/data/types").SpriteTemplateData>;
429
+ };
430
+ };
431
+ /** Realtime methods (token) */
432
+ realtime: {
433
+ token: {
434
+ get: () => Promise<import("./namespaces/realtime").RealtimeTokenResponse>;
435
+ };
436
+ open(channel?: string): Promise<import("@playcademy/realtime/server/types").RealtimeChannel>;
437
+ };
438
+ /** Auto-initializes a PlaycademyClient with context from the environment */
439
+ static init: typeof init;
440
+ /** Authenticates a user with email and password */
441
+ static login: typeof login;
144
442
  }