@playabl/sdk 0.1.0
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 +186 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.js +184 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# @playabl/sdk
|
|
2
|
+
|
|
3
|
+
Browser SDK for Playabl games providing parent-platform communication, game state persistence, device APIs, live tweaks, asset replacement, and iOS-friendly audio helpers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @playabl/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Core
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import sdk from '@playabl/sdk'
|
|
17
|
+
|
|
18
|
+
const ready = await sdk.ready()
|
|
19
|
+
console.log('Connected to Playabl parent:', ready.parentOrigin)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Game State and Leaderboard
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import sdk from '@playabl/sdk'
|
|
26
|
+
|
|
27
|
+
type SaveData = {
|
|
28
|
+
highScore: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const saved = await sdk.gameState.load<SaveData>()
|
|
32
|
+
let highScore = saved?.highScore ?? 0
|
|
33
|
+
|
|
34
|
+
async function submitScore(score: number): Promise<void> {
|
|
35
|
+
if (score > highScore) {
|
|
36
|
+
highScore = score
|
|
37
|
+
|
|
38
|
+
const result = await sdk.gameState.save({ highScore })
|
|
39
|
+
if (!result.ok) {
|
|
40
|
+
console.warn('Could not save game state:', result.error?.code)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const submitResult = await sdk.leaderboard.submit(score)
|
|
45
|
+
console.log('Score accepted:', submitResult.accepted)
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Device APIs
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import sdk from '@playabl/sdk'
|
|
53
|
+
|
|
54
|
+
// Haptics
|
|
55
|
+
if (sdk.device.haptics.isSupported()) {
|
|
56
|
+
await sdk.device.haptics.vibrate([100, 50, 100])
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Camera
|
|
60
|
+
if (sdk.device.camera.isSupported()) {
|
|
61
|
+
const stream = await sdk.device.camera.getStream({ facingMode: 'environment' })
|
|
62
|
+
const photo = await sdk.device.camera.capturePhoto({ quality: 'high' })
|
|
63
|
+
sdk.device.camera.stopStream()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Geolocation
|
|
67
|
+
if (sdk.device.geolocation.isSupported()) {
|
|
68
|
+
const position = await sdk.device.geolocation.getCurrentPosition()
|
|
69
|
+
const stopWatching = sdk.device.geolocation.watchPosition((pos) => {
|
|
70
|
+
console.log('Position:', pos)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
stopWatching()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Sensors
|
|
77
|
+
if (sdk.device.sensors.isMotionSupported()) {
|
|
78
|
+
await sdk.device.sensors.requestMotionPermission()
|
|
79
|
+
|
|
80
|
+
const stopMotion = sdk.device.sensors.watchMotion((data) => {
|
|
81
|
+
console.log('Gravity:', data.gravity)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
stopMotion()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (sdk.device.sensors.isOrientationSupported()) {
|
|
88
|
+
const stopOrientation = sdk.device.sensors.watchOrientation((data) => {
|
|
89
|
+
console.log('Orientation:', data)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
stopOrientation()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// File System
|
|
96
|
+
if (sdk.device.fileSystem.isSupported()) {
|
|
97
|
+
const { files } = await sdk.device.fileSystem.openFile({
|
|
98
|
+
accept: ['image/*'],
|
|
99
|
+
multiple: true,
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
const text = await sdk.device.fileSystem.readAsText(files[0]!)
|
|
103
|
+
await sdk.device.fileSystem.saveFile(new Blob([text]), 'save.txt')
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Tweaks
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import sdk from '@playabl/sdk'
|
|
111
|
+
|
|
112
|
+
const tweaks = await sdk.tweaks.init({
|
|
113
|
+
speed: {
|
|
114
|
+
type: 'number',
|
|
115
|
+
value: 1,
|
|
116
|
+
min: 0.5,
|
|
117
|
+
max: 3,
|
|
118
|
+
step: 0.1,
|
|
119
|
+
name: 'Player speed',
|
|
120
|
+
},
|
|
121
|
+
debugMode: {
|
|
122
|
+
type: 'boolean',
|
|
123
|
+
value: false,
|
|
124
|
+
},
|
|
125
|
+
playerColor: {
|
|
126
|
+
type: 'color',
|
|
127
|
+
value: '#00ff88',
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
const speed = tweaks.get('speed')
|
|
132
|
+
|
|
133
|
+
const unsubscribe = tweaks.subscribe('speed', (value, previous) => {
|
|
134
|
+
console.log('Speed changed:', previous, value)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
unsubscribe()
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Assets
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import sdk from '@playabl/sdk'
|
|
144
|
+
|
|
145
|
+
const assets = await sdk.assets.register({
|
|
146
|
+
player: '/assets/player.png',
|
|
147
|
+
jump: '/assets/jump.mp3',
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const playerUrl = assets.get('player')
|
|
151
|
+
const replacedUrl = assets.getReplacedUrl('/assets/player.png')
|
|
152
|
+
|
|
153
|
+
console.log('Current asset mapping:', assets.snapshot())
|
|
154
|
+
console.log(playerUrl, replacedUrl)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Audio (iOS Silent Mode Compatible)
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import sdk from '@playabl/sdk'
|
|
161
|
+
|
|
162
|
+
if (sdk.audio.isSupported()) {
|
|
163
|
+
const audio = await sdk.audio.getContext()
|
|
164
|
+
|
|
165
|
+
button.onclick = async () => {
|
|
166
|
+
await audio.unlock()
|
|
167
|
+
|
|
168
|
+
const oscillator = audio.context.createOscillator()
|
|
169
|
+
oscillator.connect(audio.context.destination)
|
|
170
|
+
oscillator.start()
|
|
171
|
+
oscillator.stop(audio.context.currentTime + 1)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## API Surface
|
|
177
|
+
|
|
178
|
+
- `sdk.ready()` - Connects to the Playabl parent iframe host.
|
|
179
|
+
- `sdk.leaderboard.submit(score)` - Sends a score to the parent platform.
|
|
180
|
+
- `sdk.gameState.save/load/clear()` - Persists JSON-serializable game state with `localStorage`.
|
|
181
|
+
- `sdk.device` - Provides haptics, camera, geolocation, sensors, and file system helpers.
|
|
182
|
+
- `sdk.tweaks.init(schema)` - Registers live-tunable values controlled by the host.
|
|
183
|
+
- `sdk.assets.register(mapping)` - Registers game assets so the host can replace them.
|
|
184
|
+
- `sdk.audio` - Creates Web Audio contexts with an iOS Safari silent-mode patch.
|
|
185
|
+
|
|
186
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
interface ReadyResult {
|
|
2
|
+
isLocalDev: boolean;
|
|
3
|
+
parentOrigin: string | null;
|
|
4
|
+
}
|
|
5
|
+
interface SubmitResult {
|
|
6
|
+
accepted: boolean;
|
|
7
|
+
}
|
|
8
|
+
interface SaveResult {
|
|
9
|
+
ok: boolean;
|
|
10
|
+
fallback: 'localStorage' | 'none';
|
|
11
|
+
error?: PlayablError;
|
|
12
|
+
}
|
|
13
|
+
type TweakType = 'number' | 'boolean' | 'text' | 'color';
|
|
14
|
+
type TweakValue = number | string | boolean;
|
|
15
|
+
interface BaseTweak {
|
|
16
|
+
name?: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
group?: string;
|
|
19
|
+
index?: number;
|
|
20
|
+
}
|
|
21
|
+
interface NumberTweak extends BaseTweak {
|
|
22
|
+
type: 'number';
|
|
23
|
+
value: number;
|
|
24
|
+
min?: number;
|
|
25
|
+
max?: number;
|
|
26
|
+
step?: number;
|
|
27
|
+
}
|
|
28
|
+
interface BooleanTweak extends BaseTweak {
|
|
29
|
+
type: 'boolean';
|
|
30
|
+
value: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface TextTweak extends BaseTweak {
|
|
33
|
+
type: 'text';
|
|
34
|
+
value: string;
|
|
35
|
+
}
|
|
36
|
+
interface ColorTweak extends BaseTweak {
|
|
37
|
+
type: 'color';
|
|
38
|
+
value: string;
|
|
39
|
+
}
|
|
40
|
+
type TweakSchemaItem = NumberTweak | BooleanTweak | TextTweak | ColorTweak;
|
|
41
|
+
interface TweakSchema {
|
|
42
|
+
[key: string]: TweakSchemaItem;
|
|
43
|
+
}
|
|
44
|
+
interface PlayablTweaks {
|
|
45
|
+
readonly values: Readonly<Record<string, TweakValue>>;
|
|
46
|
+
get(key: string): TweakValue;
|
|
47
|
+
subscribe(key: string, cb: (value: TweakValue, prev: TweakValue) => void): () => void;
|
|
48
|
+
onChange(cb: (key: string, value: TweakValue, prev: TweakValue) => void): () => void;
|
|
49
|
+
snapshot(): Record<string, TweakValue>;
|
|
50
|
+
}
|
|
51
|
+
type AssetType = 'image' | 'audio' | 'video' | 'unknown';
|
|
52
|
+
type AssetStatus = 'original' | 'overridden';
|
|
53
|
+
interface AssetDescriptor {
|
|
54
|
+
id: string;
|
|
55
|
+
url: string;
|
|
56
|
+
originalUrl: string;
|
|
57
|
+
type: AssetType;
|
|
58
|
+
status: AssetStatus;
|
|
59
|
+
index: number;
|
|
60
|
+
}
|
|
61
|
+
interface AssetOverride {
|
|
62
|
+
id: string;
|
|
63
|
+
url: string;
|
|
64
|
+
}
|
|
65
|
+
interface AssetsMapping {
|
|
66
|
+
[key: string]: string;
|
|
67
|
+
}
|
|
68
|
+
interface PlayablAssets {
|
|
69
|
+
get(key: string): string;
|
|
70
|
+
getReplacedUrl(url: string): string;
|
|
71
|
+
snapshot(): AssetsMapping;
|
|
72
|
+
}
|
|
73
|
+
interface CameraOptions {
|
|
74
|
+
quality?: 'low' | 'medium' | 'high';
|
|
75
|
+
format?: 'jpeg' | 'png' | 'webp';
|
|
76
|
+
width?: number;
|
|
77
|
+
height?: number;
|
|
78
|
+
facingMode?: 'user' | 'environment';
|
|
79
|
+
}
|
|
80
|
+
interface CameraResult {
|
|
81
|
+
blob: Blob;
|
|
82
|
+
dataUrl: string;
|
|
83
|
+
width: number;
|
|
84
|
+
height: number;
|
|
85
|
+
}
|
|
86
|
+
interface CameraAPI {
|
|
87
|
+
isSupported(): boolean;
|
|
88
|
+
getStream(options?: CameraOptions): Promise<MediaStream>;
|
|
89
|
+
capturePhoto(options?: CameraOptions): Promise<CameraResult>;
|
|
90
|
+
stopStream(): void;
|
|
91
|
+
}
|
|
92
|
+
interface GeolocationOptions {
|
|
93
|
+
enableHighAccuracy?: boolean;
|
|
94
|
+
timeout?: number;
|
|
95
|
+
maximumAge?: number;
|
|
96
|
+
}
|
|
97
|
+
interface GeolocationResult {
|
|
98
|
+
latitude: number;
|
|
99
|
+
longitude: number;
|
|
100
|
+
accuracy: number;
|
|
101
|
+
altitude?: number;
|
|
102
|
+
altitudeAccuracy?: number;
|
|
103
|
+
heading?: number;
|
|
104
|
+
speed?: number;
|
|
105
|
+
timestamp: number;
|
|
106
|
+
}
|
|
107
|
+
interface GeolocationAPI {
|
|
108
|
+
isSupported(): boolean;
|
|
109
|
+
getCurrentPosition(options?: GeolocationOptions): Promise<GeolocationResult>;
|
|
110
|
+
watchPosition(callback: (position: GeolocationResult) => void, options?: GeolocationOptions): () => void;
|
|
111
|
+
}
|
|
112
|
+
interface MotionVector {
|
|
113
|
+
x: number;
|
|
114
|
+
y: number;
|
|
115
|
+
z: number;
|
|
116
|
+
}
|
|
117
|
+
interface MotionRotation {
|
|
118
|
+
alpha: number;
|
|
119
|
+
beta: number;
|
|
120
|
+
gamma: number;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Device attitude. Reserved for future native sensor backends; the
|
|
124
|
+
* Web-only runtime never populates this field.
|
|
125
|
+
*/
|
|
126
|
+
interface MotionAttitude {
|
|
127
|
+
yaw: number;
|
|
128
|
+
pitch: number;
|
|
129
|
+
roll: number;
|
|
130
|
+
quaternion?: {
|
|
131
|
+
x: number;
|
|
132
|
+
y: number;
|
|
133
|
+
z: number;
|
|
134
|
+
w: number;
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
interface MotionData {
|
|
138
|
+
gravity: MotionVector;
|
|
139
|
+
acceleration: MotionVector;
|
|
140
|
+
accelerationIncludingGravity: MotionVector;
|
|
141
|
+
rotation: MotionRotation;
|
|
142
|
+
attitude?: MotionAttitude;
|
|
143
|
+
timestamp: number;
|
|
144
|
+
}
|
|
145
|
+
interface OrientationData {
|
|
146
|
+
alpha: number;
|
|
147
|
+
beta: number;
|
|
148
|
+
gamma: number;
|
|
149
|
+
timestamp: number;
|
|
150
|
+
}
|
|
151
|
+
interface SensorsAPI {
|
|
152
|
+
isMotionSupported(): boolean;
|
|
153
|
+
isOrientationSupported(): boolean;
|
|
154
|
+
requestMotionPermission(): Promise<boolean>;
|
|
155
|
+
watchMotion(callback: (data: MotionData) => void, autoRequestPermission?: boolean): () => void;
|
|
156
|
+
watchOrientation(callback: (data: OrientationData) => void): () => void;
|
|
157
|
+
}
|
|
158
|
+
interface FileSystemOptions {
|
|
159
|
+
accept?: string[];
|
|
160
|
+
multiple?: boolean;
|
|
161
|
+
}
|
|
162
|
+
interface FileSystemResult {
|
|
163
|
+
files: File[];
|
|
164
|
+
paths: string[];
|
|
165
|
+
}
|
|
166
|
+
interface FileSystemAPI {
|
|
167
|
+
isSupported(): boolean;
|
|
168
|
+
isLegacySupported(): boolean;
|
|
169
|
+
openFile(options?: FileSystemOptions): Promise<FileSystemResult>;
|
|
170
|
+
saveFile(blob: Blob, filename: string): Promise<void>;
|
|
171
|
+
readAsText(file: File): Promise<string>;
|
|
172
|
+
readAsDataURL(file: File): Promise<string>;
|
|
173
|
+
}
|
|
174
|
+
interface HapticsAPI {
|
|
175
|
+
isSupported(): boolean;
|
|
176
|
+
vibrate(pattern: number | number[]): Promise<void>;
|
|
177
|
+
cancel(): Promise<void>;
|
|
178
|
+
}
|
|
179
|
+
interface PlayablDevice {
|
|
180
|
+
haptics: HapticsAPI;
|
|
181
|
+
camera: CameraAPI;
|
|
182
|
+
geolocation: GeolocationAPI;
|
|
183
|
+
fileSystem: FileSystemAPI;
|
|
184
|
+
sensors: SensorsAPI;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Tunables for the iOS Safari iframe patch silence detector. See
|
|
188
|
+
* `sdk-runtime/src/audio` for the runtime behavior; the public SDK exposes
|
|
189
|
+
* these only to allow opt-out / tuning from game code.
|
|
190
|
+
*/
|
|
191
|
+
interface PlayablAudioAutoPauseOptions {
|
|
192
|
+
enabled?: boolean;
|
|
193
|
+
silenceThreshold?: number;
|
|
194
|
+
silenceDuration?: number;
|
|
195
|
+
checkInterval?: number;
|
|
196
|
+
}
|
|
197
|
+
interface PlayablAudioOptions {
|
|
198
|
+
context?: AudioContextOptions;
|
|
199
|
+
forcePatch?: boolean;
|
|
200
|
+
debug?: boolean;
|
|
201
|
+
autoPause?: PlayablAudioAutoPauseOptions;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Handle returned by `sdk.audio.getContext()`. Wraps a native `AudioContext`
|
|
205
|
+
* and exposes the SDK lifecycle (`unlock`/`dispose`/`onStateChange`). On
|
|
206
|
+
* iPhone/iPad Safari, `context.destination` is transparently routed through
|
|
207
|
+
* a hidden `<video>` so the silent switch does not mute Web Audio output.
|
|
208
|
+
*/
|
|
209
|
+
interface PlayablAudioContext {
|
|
210
|
+
readonly context: AudioContext;
|
|
211
|
+
readonly isUnlocked: boolean;
|
|
212
|
+
readonly isPatched: boolean;
|
|
213
|
+
readonly state: AudioContextState;
|
|
214
|
+
unlock(): Promise<void>;
|
|
215
|
+
dispose(): Promise<void>;
|
|
216
|
+
onStateChange(handler: (state: AudioContextState) => void): () => void;
|
|
217
|
+
}
|
|
218
|
+
interface PlayablAudio {
|
|
219
|
+
isSupported(): boolean;
|
|
220
|
+
getContext(options?: PlayablAudioOptions): Promise<PlayablAudioContext>;
|
|
221
|
+
createContext(options?: PlayablAudioOptions): Promise<PlayablAudioContext>;
|
|
222
|
+
}
|
|
223
|
+
interface PlayablSDK {
|
|
224
|
+
ready(): Promise<ReadyResult>;
|
|
225
|
+
leaderboard: {
|
|
226
|
+
submit(score: number): Promise<SubmitResult>;
|
|
227
|
+
};
|
|
228
|
+
gameState: {
|
|
229
|
+
save<T>(state: T): Promise<SaveResult>;
|
|
230
|
+
load<T>(): Promise<T | null>;
|
|
231
|
+
clear(): Promise<void>;
|
|
232
|
+
};
|
|
233
|
+
tweaks: {
|
|
234
|
+
init(schema: TweakSchema): Promise<PlayablTweaks>;
|
|
235
|
+
};
|
|
236
|
+
assets: {
|
|
237
|
+
register(mapping: AssetsMapping): Promise<PlayablAssets>;
|
|
238
|
+
};
|
|
239
|
+
device: PlayablDevice;
|
|
240
|
+
audio: PlayablAudio;
|
|
241
|
+
}
|
|
242
|
+
type ErrorCode = 'NOT_IN_IFRAME' | 'PARENT_TIMEOUT' | 'SDK_NOT_READY' | 'STORAGE_SERIALIZE_ERROR' | 'STORAGE_QUOTA_EXCEEDED' | 'INVALID_SCHEMA' | 'INVALID_ASSETS' | 'NOT_SUPPORTED' | 'PERMISSION_DENIED' | 'TIMEOUT' | 'OPERATION_FAILED' | 'UNKNOWN_ERROR';
|
|
243
|
+
declare class PlayablError extends Error {
|
|
244
|
+
readonly code: ErrorCode;
|
|
245
|
+
readonly cause?: unknown;
|
|
246
|
+
constructor(code: ErrorCode, message?: string, cause?: unknown);
|
|
247
|
+
}
|
|
248
|
+
type RuntimeLoader = () => Promise<PlayablSDK>;
|
|
249
|
+
declare function configure(options: {
|
|
250
|
+
runtimeUrl?: string;
|
|
251
|
+
}): void;
|
|
252
|
+
declare const sdk: PlayablSDK;
|
|
253
|
+
|
|
254
|
+
declare const __testing: {
|
|
255
|
+
resetRuntimeLoader(): void;
|
|
256
|
+
setRuntimeLoader(loader: RuntimeLoader): void;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export { type AssetDescriptor, type AssetOverride, type AssetStatus, type AssetType, type AssetsMapping, type BooleanTweak, type CameraAPI, type CameraOptions, type CameraResult, type ColorTweak, type ErrorCode, type FileSystemAPI, type FileSystemOptions, type FileSystemResult, type GeolocationAPI, type GeolocationOptions, type GeolocationResult, type HapticsAPI, type MotionAttitude, type MotionData, type MotionRotation, type MotionVector, type NumberTweak, type OrientationData, type PlayablAssets, type PlayablAudio, type PlayablAudioAutoPauseOptions, type PlayablAudioContext, type PlayablAudioOptions, type PlayablDevice, PlayablError, type PlayablSDK, type PlayablTweaks, type ReadyResult, type SaveResult, type SensorsAPI, type SubmitResult, type TextTweak, type TweakSchema, type TweakSchemaItem, type TweakType, type TweakValue, __testing, configure, sdk as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var PlayablError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
cause;
|
|
5
|
+
constructor(code, message, cause) {
|
|
6
|
+
super(message ?? code);
|
|
7
|
+
this.name = "PlayablError";
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.cause = cause;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var defaultLoader = () => import(
|
|
13
|
+
/* @vite-ignore */
|
|
14
|
+
runtimeUrl
|
|
15
|
+
).then((mod) => mod.default);
|
|
16
|
+
var runtimeUrl = "https://cdn.playabl.ai/Runtime-Script/sdk/v1/runtime.js";
|
|
17
|
+
var runtimeLoader = defaultLoader;
|
|
18
|
+
var sdkPromise;
|
|
19
|
+
var loadedSDK;
|
|
20
|
+
function configure(options) {
|
|
21
|
+
if (options.runtimeUrl && options.runtimeUrl !== runtimeUrl) {
|
|
22
|
+
runtimeUrl = options.runtimeUrl;
|
|
23
|
+
sdkPromise = void 0;
|
|
24
|
+
loadedSDK = void 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function getSDK() {
|
|
28
|
+
if (typeof window === "undefined") {
|
|
29
|
+
throw new PlayablError(
|
|
30
|
+
"SDK_NOT_READY",
|
|
31
|
+
"Playabl SDK runtime can only be loaded in a browser."
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
sdkPromise ??= runtimeLoader().then(
|
|
35
|
+
(runtime) => {
|
|
36
|
+
loadedSDK = runtime;
|
|
37
|
+
return runtime;
|
|
38
|
+
},
|
|
39
|
+
(error) => {
|
|
40
|
+
sdkPromise = void 0;
|
|
41
|
+
throw new PlayablError("SDK_NOT_READY", "Playabl SDK runtime could not be loaded.", error);
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
return sdkPromise;
|
|
45
|
+
}
|
|
46
|
+
function peekSDK() {
|
|
47
|
+
return loadedSDK;
|
|
48
|
+
}
|
|
49
|
+
var sdk = {
|
|
50
|
+
ready: async () => (await getSDK()).ready(),
|
|
51
|
+
leaderboard: {
|
|
52
|
+
submit: async (score) => (await getSDK()).leaderboard.submit(score)
|
|
53
|
+
},
|
|
54
|
+
gameState: {
|
|
55
|
+
save: async (state) => (await getSDK()).gameState.save(state),
|
|
56
|
+
load: async () => (await getSDK()).gameState.load(),
|
|
57
|
+
clear: async () => (await getSDK()).gameState.clear()
|
|
58
|
+
},
|
|
59
|
+
tweaks: {
|
|
60
|
+
init: async (schema) => (await getSDK()).tweaks.init(schema)
|
|
61
|
+
},
|
|
62
|
+
assets: {
|
|
63
|
+
register: async (mapping) => (await getSDK()).assets.register(mapping)
|
|
64
|
+
},
|
|
65
|
+
device: {
|
|
66
|
+
haptics: {
|
|
67
|
+
isSupported: () => {
|
|
68
|
+
const runtime = peekSDK();
|
|
69
|
+
return runtime ? runtime.device.haptics.isSupported() : false;
|
|
70
|
+
},
|
|
71
|
+
vibrate: async (pattern) => (await getSDK()).device.haptics.vibrate(pattern),
|
|
72
|
+
cancel: async () => (await getSDK()).device.haptics.cancel()
|
|
73
|
+
},
|
|
74
|
+
camera: {
|
|
75
|
+
isSupported: () => {
|
|
76
|
+
const runtime = peekSDK();
|
|
77
|
+
return runtime ? runtime.device.camera.isSupported() : false;
|
|
78
|
+
},
|
|
79
|
+
getStream: async (options) => (await getSDK()).device.camera.getStream(options),
|
|
80
|
+
capturePhoto: async (options) => (await getSDK()).device.camera.capturePhoto(options),
|
|
81
|
+
stopStream: () => {
|
|
82
|
+
const runtime = peekSDK();
|
|
83
|
+
if (runtime) runtime.device.camera.stopStream();
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
geolocation: {
|
|
87
|
+
isSupported: () => {
|
|
88
|
+
const runtime = peekSDK();
|
|
89
|
+
return runtime ? runtime.device.geolocation.isSupported() : false;
|
|
90
|
+
},
|
|
91
|
+
getCurrentPosition: async (options) => (await getSDK()).device.geolocation.getCurrentPosition(options),
|
|
92
|
+
watchPosition: (callback, options) => {
|
|
93
|
+
let stop = null;
|
|
94
|
+
let cancelled = false;
|
|
95
|
+
void getSDK().then((runtime) => {
|
|
96
|
+
if (cancelled) return;
|
|
97
|
+
stop = runtime.device.geolocation.watchPosition(callback, options);
|
|
98
|
+
});
|
|
99
|
+
return () => {
|
|
100
|
+
cancelled = true;
|
|
101
|
+
if (stop) stop();
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
fileSystem: {
|
|
106
|
+
isSupported: () => {
|
|
107
|
+
const runtime = peekSDK();
|
|
108
|
+
return runtime ? runtime.device.fileSystem.isSupported() : false;
|
|
109
|
+
},
|
|
110
|
+
isLegacySupported: () => {
|
|
111
|
+
const runtime = peekSDK();
|
|
112
|
+
return runtime ? runtime.device.fileSystem.isLegacySupported() : false;
|
|
113
|
+
},
|
|
114
|
+
openFile: async (options) => (await getSDK()).device.fileSystem.openFile(options),
|
|
115
|
+
saveFile: async (blob, filename) => (await getSDK()).device.fileSystem.saveFile(blob, filename),
|
|
116
|
+
readAsText: async (file) => (await getSDK()).device.fileSystem.readAsText(file),
|
|
117
|
+
readAsDataURL: async (file) => (await getSDK()).device.fileSystem.readAsDataURL(file)
|
|
118
|
+
},
|
|
119
|
+
sensors: {
|
|
120
|
+
isMotionSupported: () => {
|
|
121
|
+
const runtime = peekSDK();
|
|
122
|
+
return runtime ? runtime.device.sensors.isMotionSupported() : false;
|
|
123
|
+
},
|
|
124
|
+
isOrientationSupported: () => {
|
|
125
|
+
const runtime = peekSDK();
|
|
126
|
+
return runtime ? runtime.device.sensors.isOrientationSupported() : false;
|
|
127
|
+
},
|
|
128
|
+
requestMotionPermission: async () => (await getSDK()).device.sensors.requestMotionPermission(),
|
|
129
|
+
watchMotion: (callback, autoRequestPermission) => {
|
|
130
|
+
let stop = null;
|
|
131
|
+
let cancelled = false;
|
|
132
|
+
void getSDK().then((runtime) => {
|
|
133
|
+
if (cancelled) return;
|
|
134
|
+
stop = runtime.device.sensors.watchMotion(callback, autoRequestPermission);
|
|
135
|
+
});
|
|
136
|
+
return () => {
|
|
137
|
+
cancelled = true;
|
|
138
|
+
if (stop) stop();
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
watchOrientation: (callback) => {
|
|
142
|
+
let stop = null;
|
|
143
|
+
let cancelled = false;
|
|
144
|
+
void getSDK().then((runtime) => {
|
|
145
|
+
if (cancelled) return;
|
|
146
|
+
stop = runtime.device.sensors.watchOrientation(callback);
|
|
147
|
+
});
|
|
148
|
+
return () => {
|
|
149
|
+
cancelled = true;
|
|
150
|
+
if (stop) stop();
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
audio: {
|
|
156
|
+
isSupported: () => {
|
|
157
|
+
const runtime = peekSDK();
|
|
158
|
+
return runtime ? runtime.audio.isSupported() : false;
|
|
159
|
+
},
|
|
160
|
+
getContext: async (options) => (await getSDK()).audio.getContext(options),
|
|
161
|
+
createContext: async (options) => (await getSDK()).audio.createContext(options)
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var index_default = sdk;
|
|
165
|
+
var __testing = {
|
|
166
|
+
resetRuntimeLoader() {
|
|
167
|
+
sdkPromise = void 0;
|
|
168
|
+
loadedSDK = void 0;
|
|
169
|
+
runtimeUrl = "https://cdn.playabl.ai/Runtime-Script/sdk/v1/runtime.js";
|
|
170
|
+
runtimeLoader = defaultLoader;
|
|
171
|
+
},
|
|
172
|
+
setRuntimeLoader(loader) {
|
|
173
|
+
sdkPromise = void 0;
|
|
174
|
+
loadedSDK = void 0;
|
|
175
|
+
runtimeLoader = loader;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
export {
|
|
179
|
+
PlayablError,
|
|
180
|
+
__testing,
|
|
181
|
+
configure,
|
|
182
|
+
index_default as default
|
|
183
|
+
};
|
|
184
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@playabl/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/index.js",
|
|
9
|
+
"dist/index.d.ts",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./dist/index.js"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"test": "vitest run"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"dotenv": "^17.4.2",
|
|
27
|
+
"happy-dom": "^18.0.1",
|
|
28
|
+
"tsup": "^8.5.0",
|
|
29
|
+
"typescript": "^5.9.2",
|
|
30
|
+
"vitest": "^3.2.4"
|
|
31
|
+
}
|
|
32
|
+
}
|