@oasiz/sdk 1.0.1 → 1.0.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,11 +1,11 @@
1
1
  # @oasiz/sdk
2
2
 
3
- Typed SDK for integrating games with the Oasiz platform. Handles score submission, haptic feedback, cross-session state persistence, multiplayer room codes, and app lifecycle events.
3
+ Typed SDK for integrating games with the Oasiz platform. Handles score submission, haptic feedback, cross-session state persistence, multiplayer room codes, navigation hooks, and app lifecycle events for local development.
4
4
 
5
5
  ## Install
6
6
 
7
7
  ```bash
8
- npm install @oasiz/sdk
8
+ npm install @oasiz/sdk@^0.1.0
9
9
  ```
10
10
 
11
11
  ## Quick start
@@ -13,17 +13,27 @@ npm install @oasiz/sdk
13
13
  ```ts
14
14
  import { oasiz } from "@oasiz/sdk";
15
15
 
16
- // 1. Load persisted state at the start of each session
16
+ // 1. Emit score normalization config once on init
17
+ oasiz.emitScoreConfig({
18
+ anchors: [
19
+ { raw: 30, normalized: 100 },
20
+ { raw: 60, normalized: 300 },
21
+ { raw: 120, normalized: 600 },
22
+ { raw: 300, normalized: 950 },
23
+ ],
24
+ });
25
+
26
+ // 2. Load persisted state at the start of each session
17
27
  const state = oasiz.loadGameState();
18
28
  let level = typeof state.level === "number" ? state.level : 1;
19
29
 
20
- // 2. Save state at checkpoints
30
+ // 3. Save state at checkpoints
21
31
  oasiz.saveGameState({ level, coins: 42 });
22
32
 
23
- // 3. Trigger haptics on key events
33
+ // 4. Trigger haptics on key events
24
34
  oasiz.triggerHaptic("medium");
25
35
 
26
- // 4. Submit score when the game ends
36
+ // 5. Submit score when the game ends
27
37
  oasiz.submitScore(score);
28
38
  ```
29
39
 
@@ -33,14 +43,44 @@ oasiz.submitScore(score);
33
43
 
34
44
  ### `oasiz.submitScore(score: number)`
35
45
 
36
- Submit the player's score. The platform handles leaderboard persistence — do not track high scores locally.
46
+ Submit the player's final score at game over. Call this exactly once per session, when the game ends. The platform handles leaderboard persistence — do not track high scores locally.
37
47
 
38
48
  ```ts
39
- oasiz.submitScore(this.score);
49
+ private onGameOver(): void {
50
+ oasiz.submitScore(Math.floor(this.score));
51
+ }
40
52
  ```
41
53
 
42
54
  - `score` must be a non-negative integer. Floats are floored automatically.
43
- - When and how often you call this depends on your game type (once at game over, end of each level, or throttled for long sessions).
55
+ - Do not call on intermediate scores or level completions, only on final game over.
56
+
57
+ ---
58
+
59
+ ### `oasiz.emitScoreConfig(config)`
60
+
61
+ Maps raw score values to the platform's normalized 0–1000 scale. Call once during initialization, not every frame.
62
+
63
+ ```ts
64
+ oasiz.emitScoreConfig({
65
+ anchors: [
66
+ { raw: 10, normalized: 100 }, // beginner
67
+ { raw: 30, normalized: 300 }, // good
68
+ { raw: 75, normalized: 600 }, // great
69
+ { raw: 200, normalized: 950 }, // godlike
70
+ ],
71
+ });
72
+ ```
73
+
74
+ **Anchor rules:**
75
+ - Exactly 4 anchors required.
76
+ - `raw` values must be strictly increasing.
77
+ - `normalized` values must end at exactly `950`.
78
+ - Choose thresholds based on realistic player skill bands.
79
+
80
+ **Practical guidance by game type:**
81
+ - Survival / time games → use seconds survived as `raw`
82
+ - Score accumulation games → use points as `raw`
83
+ - Puzzle games → use level reached or stars earned as `raw`
44
84
 
45
85
  ---
46
86
 
@@ -59,14 +99,23 @@ type HapticType = "light" | "medium" | "heavy" | "success" | "error";
59
99
  | `"light"` | UI button taps, menu navigation, D-pad press |
60
100
  | `"medium"` | Collecting items, standard collisions, scoring |
61
101
  | `"heavy"` | Explosions, major impacts, screen shake |
62
- | `"success"` | Level complete, achievement unlocked |
102
+ | `"success"` | Level complete, new high score, achievement unlocked |
63
103
  | `"error"` | Damage taken, game over, invalid action |
64
104
 
65
105
  ```ts
106
+ // UI buttons — always light
66
107
  button.addEventListener("click", () => {
67
108
  oasiz.triggerHaptic("light");
68
109
  });
69
110
 
111
+ // Tiered hit feedback
112
+ private onBallHit(zone: "center" | "edge"): void {
113
+ if (this.settings.haptics) {
114
+ oasiz.triggerHaptic(zone === "center" ? "success" : "medium");
115
+ }
116
+ }
117
+
118
+ // Game over
70
119
  private onGameOver(): void {
71
120
  oasiz.submitScore(this.score);
72
121
  if (this.settings.haptics) {
@@ -75,54 +124,75 @@ private onGameOver(): void {
75
124
  }
76
125
  ```
77
126
 
127
+ Haptics are throttled internally (50ms cooldown) to prevent spam.
128
+
78
129
  ---
79
130
 
80
131
  ## Game state persistence
81
132
 
82
- Persist cross-session data such as unlocked levels, inventory, or lifetime stats. Available across devices and app reinstalls.
133
+ Persist cross-session data such as unlocked levels, inventory, or lifetime stats. State is stored per-user per-game in the Oasiz backend — available across devices and app reinstalls.
83
134
 
84
135
  ### `oasiz.loadGameState(): Record<string, unknown>`
85
136
 
86
- Returns the player's saved state synchronously. Returns `{}` if no state has been saved yet.
137
+ Returns the player's saved state synchronously. Returns `{}` if no state has been saved yet. Call once at the start of the game.
87
138
 
88
139
  ```ts
89
- const state = oasiz.loadGameState();
90
- this.level = typeof state.level === "number" ? state.level : 1;
140
+ private initFromSavedState(): void {
141
+ const state = oasiz.loadGameState();
142
+ this.level = typeof state.level === "number" ? state.level : 1;
143
+ this.lifetimeHits = typeof state.lifetimeHits === "number" ? state.lifetimeHits : 0;
144
+ this.unlockedSkins = Array.isArray(state.unlockedSkins) ? state.unlockedSkins : [];
145
+ }
91
146
  ```
92
147
 
148
+ Always validate the shape of loaded data — it may be `{}` on first play.
149
+
93
150
  ### `oasiz.saveGameState(state: Record<string, unknown>)`
94
151
 
95
- Queues a debounced save. Call freely at checkpoints.
152
+ Queues a debounced save. Saves are batched automatically — call freely at checkpoints without worrying about request spam.
96
153
 
97
154
  ```ts
98
- oasiz.saveGameState({ level: this.level, unlockedSkins: this.unlockedSkins });
155
+ // Save after each level completion
156
+ private onLevelComplete(): void {
157
+ this.level += 1;
158
+ oasiz.saveGameState({
159
+ level: this.level,
160
+ lifetimeHits: this.lifetimeHits,
161
+ unlockedSkins: this.unlockedSkins,
162
+ });
163
+ }
99
164
  ```
100
165
 
166
+ **Rules:**
101
167
  - State must be a plain JSON object (not an array or primitive).
102
- - Use `saveGameState` instead of `localStorage` for cross-session progress.
103
- - Do not store scores here — use `submitScore`.
168
+ - Do not use `localStorage` for cross-session progress — use `saveGameState` so data syncs across platforms.
169
+ - Do not store scores here — scores are submitted via `submitScore`.
104
170
 
105
171
  ### `oasiz.flushGameState()`
106
172
 
107
- Forces an immediate write, bypassing the debounce. Use at game over or before page unload.
173
+ Forces an immediate write, bypassing the debounce. Use at important checkpoints like game over or before the page unloads.
108
174
 
109
175
  ```ts
110
- oasiz.saveGameState({ level: this.level });
111
- oasiz.flushGameState();
112
- oasiz.submitScore(this.score);
176
+ private onGameOver(): void {
177
+ oasiz.saveGameState({ level: this.level, lifetimeHits: this.lifetimeHits });
178
+ oasiz.flushGameState(); // ensure it lands before the page closes
179
+ oasiz.submitScore(this.score);
180
+ }
113
181
  ```
114
182
 
115
183
  ---
116
184
 
117
185
  ## Lifecycle
118
186
 
187
+ The platform dispatches lifecycle events when the app goes to the background or returns to the foreground. Subscribe to pause game loops and audio accordingly.
188
+
119
189
  ### `oasiz.onPause(callback: () => void): Unsubscribe`
120
190
  ### `oasiz.onResume(callback: () => void): Unsubscribe`
121
191
 
122
192
  Both return an unsubscribe function.
123
193
 
124
194
  ```ts
125
- const offPause = oasiz.onPause(() => {
195
+ const offPause = oasiz.onPause(() => {
126
196
  this.gameLoop.stop();
127
197
  this.bgMusic.pause();
128
198
  });
@@ -132,39 +202,107 @@ const offResume = oasiz.onResume(() => {
132
202
  this.bgMusic.play();
133
203
  });
134
204
 
205
+ // Clean up when the game is destroyed
135
206
  offPause();
136
207
  offResume();
137
208
  ```
138
209
 
139
210
  ---
140
211
 
212
+ ## Navigation
213
+
214
+ Use navigation hooks when your game needs to control back behavior (Android back / web Escape) or participate in host-driven close events.
215
+
216
+ ### `oasiz.onBackButton(callback: () => void): Unsubscribe`
217
+
218
+ Registers a callback for platform back actions. While at least one back listener is subscribed, back actions are routed to your game instead of immediately closing it.
219
+
220
+ Use this for pause menus, in-game overlays, or custom back-stack behavior.
221
+
222
+ ```ts
223
+ const offBack = oasiz.onBackButton(() => {
224
+ if (this.isPauseMenuOpen) {
225
+ this.closePauseMenu();
226
+ return;
227
+ }
228
+ this.openPauseMenu();
229
+ });
230
+
231
+ // Restore default host back behavior when no longer needed
232
+ offBack();
233
+ ```
234
+
235
+ ### `oasiz.leaveGame(): void`
236
+
237
+ Programmatically request the host to close the current game (for example, from a Quit button inside your game UI).
238
+
239
+ ```ts
240
+ quitButton.addEventListener("click", () => {
241
+ oasiz.leaveGame();
242
+ });
243
+ ```
244
+
245
+ ### `oasiz.onLeaveGame(callback: () => void): Unsubscribe`
246
+
247
+ Registers a callback fired when the host initiates closing the game (for example, close button, gesture, or host navigation). Use this for lightweight cleanup.
248
+
249
+ ```ts
250
+ const offLeave = oasiz.onLeaveGame(() => {
251
+ oasiz.flushGameState();
252
+ this.bgMusic.pause();
253
+ });
254
+
255
+ // Clean up listener when destroyed
256
+ offLeave();
257
+ ```
258
+
259
+ ---
260
+
141
261
  ## Multiplayer
142
262
 
143
263
  ### `oasiz.shareRoomCode(code: string | null)`
144
264
 
145
- Notify the platform of the active multiplayer room. Pass `null` when leaving.
265
+ Notify the platform of the active multiplayer room so friends can join via the invite system. Pass `null` when leaving a room.
146
266
 
147
267
  ```ts
268
+ import { insertCoin, getRoomCode } from "playroomkit";
269
+ import { oasiz } from "@oasiz/sdk";
270
+
271
+ await insertCoin({ skipLobby: true });
148
272
  oasiz.shareRoomCode(getRoomCode());
273
+
274
+ // On disconnect
149
275
  oasiz.shareRoomCode(null);
150
276
  ```
151
277
 
152
- ### Read-only properties
278
+ ### Read-only injected values
153
279
 
154
- | Property | Type | Description |
155
- |---|---|---|
156
- | `oasiz.gameId` | string \| undefined | The platform's game ID |
157
- | `oasiz.roomCode` | string \| undefined | Pre-filled room code from invite |
158
- | `oasiz.playerName` | string \| undefined | Player's display name |
159
- | `oasiz.playerAvatar` | string \| undefined | Player's profile picture URL |
280
+ These are populated by the platform before the game loads. Always check for `undefined` before using.
281
+
282
+ ```ts
283
+ // The platform's internal game ID
284
+ const gameId = oasiz.gameId;
285
+
286
+ // Pre-filled room code for auto-joining a friend's session
287
+ if (oasiz.roomCode) {
288
+ await connectToRoom(oasiz.roomCode);
289
+ }
290
+
291
+ // Player identity for multiplayer games
292
+ const name = oasiz.playerName;
293
+ const avatar = oasiz.playerAvatar;
294
+ ```
160
295
 
161
296
  ---
162
297
 
163
298
  ## Named exports
164
299
 
300
+ All methods are also available as named exports if you prefer not to use the `oasiz` namespace object:
301
+
165
302
  ```ts
166
303
  import {
167
304
  submitScore,
305
+ emitScoreConfig,
168
306
  triggerHaptic,
169
307
  loadGameState,
170
308
  saveGameState,
@@ -172,6 +310,9 @@ import {
172
310
  shareRoomCode,
173
311
  onPause,
174
312
  onResume,
313
+ onBackButton,
314
+ onLeaveGame,
315
+ leaveGame,
175
316
  getGameId,
176
317
  getRoomCode,
177
318
  getPlayerName,
@@ -184,15 +325,17 @@ import {
184
325
  ## TypeScript types
185
326
 
186
327
  ```ts
187
- import type { HapticType, GameState } from "@oasiz/sdk";
328
+ import type { HapticType, ScoreConfig, ScoreAnchor, GameState } from "@oasiz/sdk";
188
329
  ```
189
330
 
190
331
  ---
191
332
 
192
333
  ## Local development
193
334
 
194
- All methods safely no-op when the platform bridges are not injected. In development mode a console warning is logged:
335
+ All methods safely no-op when the platform bridges are not injected. In development mode a console warning is logged so you know the call was made:
195
336
 
196
337
  ```
197
338
  [oasiz/sdk] submitScore bridge is unavailable. This is expected in local development.
198
339
  ```
340
+
341
+ No crashes, no special setup required for local dev.
package/dist/index.cjs CHANGED
@@ -20,13 +20,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ emitScoreConfig: () => emitScoreConfig,
23
24
  flushGameState: () => flushGameState,
24
25
  getGameId: () => getGameId,
25
26
  getPlayerAvatar: () => getPlayerAvatar,
26
27
  getPlayerName: () => getPlayerName,
27
28
  getRoomCode: () => getRoomCode,
29
+ leaveGame: () => leaveGame,
28
30
  loadGameState: () => loadGameState,
29
31
  oasiz: () => oasiz,
32
+ onBackButton: () => onBackButton,
33
+ onLeaveGame: () => onLeaveGame,
30
34
  onPause: () => onPause,
31
35
  onResume: () => onResume,
32
36
  saveGameState: () => saveGameState,
@@ -133,6 +137,14 @@ function submitScore(score) {
133
137
  }
134
138
  warnMissingBridge("submitScore");
135
139
  }
140
+ function emitScoreConfig(config) {
141
+ const bridge = getBridgeWindow3();
142
+ if (typeof bridge?.emitScoreConfig === "function") {
143
+ bridge.emitScoreConfig(config);
144
+ return;
145
+ }
146
+ warnMissingBridge("emitScoreConfig");
147
+ }
136
148
 
137
149
  // src/state.ts
138
150
  function isDevelopment4() {
@@ -225,9 +237,79 @@ function onResume(callback) {
225
237
  return addLifecycleListener("oasiz:resume", callback);
226
238
  }
227
239
 
240
+ // src/navigation.ts
241
+ var activeBackListeners = 0;
242
+ function isDevelopment6() {
243
+ const nodeEnv = globalThis.process?.env?.NODE_ENV;
244
+ return nodeEnv !== "production";
245
+ }
246
+ function getBridgeWindow5() {
247
+ if (typeof window === "undefined") {
248
+ return void 0;
249
+ }
250
+ return window;
251
+ }
252
+ function warnMissingBridge3(methodName) {
253
+ if (isDevelopment6()) {
254
+ console.warn(
255
+ "[oasiz/sdk] " + methodName + " bridge is unavailable. This is expected in local development."
256
+ );
257
+ }
258
+ }
259
+ function addNavigationListener(eventName, callback) {
260
+ if (typeof window === "undefined") {
261
+ if (isDevelopment6()) {
262
+ console.warn(
263
+ "[oasiz/sdk] " + eventName + " listener registered without a browser window. This is expected in local development."
264
+ );
265
+ }
266
+ return () => {
267
+ };
268
+ }
269
+ const handler = () => callback();
270
+ window.addEventListener(eventName, handler);
271
+ return () => window.removeEventListener(eventName, handler);
272
+ }
273
+ function onBackButton(callback) {
274
+ const off = addNavigationListener("oasiz:back", callback);
275
+ const bridge = getBridgeWindow5();
276
+ activeBackListeners += 1;
277
+ if (activeBackListeners === 1) {
278
+ if (typeof bridge?.__oasizSetBackOverride === "function") {
279
+ bridge.__oasizSetBackOverride(true);
280
+ } else {
281
+ warnMissingBridge3("__oasizSetBackOverride");
282
+ }
283
+ }
284
+ return () => {
285
+ off();
286
+ activeBackListeners = Math.max(0, activeBackListeners - 1);
287
+ if (activeBackListeners === 0) {
288
+ const currentBridge = getBridgeWindow5();
289
+ if (typeof currentBridge?.__oasizSetBackOverride === "function") {
290
+ currentBridge.__oasizSetBackOverride(false);
291
+ } else {
292
+ warnMissingBridge3("__oasizSetBackOverride");
293
+ }
294
+ }
295
+ };
296
+ }
297
+ function onLeaveGame(callback) {
298
+ return addNavigationListener("oasiz:leave", callback);
299
+ }
300
+ function leaveGame() {
301
+ const bridge = getBridgeWindow5();
302
+ if (typeof bridge?.__oasizLeaveGame === "function") {
303
+ bridge.__oasizLeaveGame();
304
+ return;
305
+ }
306
+ warnMissingBridge3("__oasizLeaveGame");
307
+ }
308
+
228
309
  // src/index.ts
229
310
  var oasiz = {
230
311
  submitScore,
312
+ emitScoreConfig,
231
313
  triggerHaptic,
232
314
  loadGameState,
233
315
  saveGameState,
@@ -235,6 +317,9 @@ var oasiz = {
235
317
  shareRoomCode,
236
318
  onPause,
237
319
  onResume,
320
+ onBackButton,
321
+ onLeaveGame,
322
+ leaveGame,
238
323
  get gameId() {
239
324
  return getGameId();
240
325
  },
@@ -250,13 +335,17 @@ var oasiz = {
250
335
  };
251
336
  // Annotate the CommonJS export names for ESM import in node:
252
337
  0 && (module.exports = {
338
+ emitScoreConfig,
253
339
  flushGameState,
254
340
  getGameId,
255
341
  getPlayerAvatar,
256
342
  getPlayerName,
257
343
  getRoomCode,
344
+ leaveGame,
258
345
  loadGameState,
259
346
  oasiz,
347
+ onBackButton,
348
+ onLeaveGame,
260
349
  onPause,
261
350
  onResume,
262
351
  saveGameState,