@playcraft/adsdk 1.0.2-2.beta.1

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.
@@ -0,0 +1,385 @@
1
+ /**
2
+ * i18n 数据结构(仅 runtime,SDK 只处理游戏内运行时多语言)
3
+ * runtime: { [key]: { [langCode]: "文案" } }
4
+ *
5
+ * 当传入数组时,按顺序深度合并(后面的优先级更高),
6
+ * 支持全局 i18n + 主题级 i18n 分文件管理:
7
+ * runtime: [I18nRuntime, themeI18n] // 主题级覆盖全局
8
+ *
9
+ * xplatform(配置平台 UI 标题)由外部配置平台自行解析,不传入 SDK。
10
+ */
11
+ type I18nRecord = {
12
+ runtime?: Record<string, Record<string, string>> | Record<string, Record<string, string>>[];
13
+ };
14
+
15
+ type EventName = 'playcraftInit' | 'playcraftReady' | 'playcraftStart' | 'playcraftInteractive' | 'playcraftInteraction' | 'playcraftChallengeStart' | 'playcraftChallengeProgress' | 'playcraftChallengeSuccess' | 'playcraftChallengeFailed' | 'playcraftResize' | 'playcraftPause' | 'playcraftResume' | 'playcraftVolume' | 'playcraftRetry' | 'playcraftFinish' | 'playcraftInstall';
16
+
17
+ /**
18
+ * 自定义鼠标指针模块。
19
+ *
20
+ * 默认挂载到 `document.body`,把整页的默认光标替换为内置的手指图标:
21
+ * - 悬浮时显示 FingerHovering
22
+ * - 按下时显示 FingerPressing
23
+ *
24
+ * 图片以 base64 dataURL 形式内置到产物中,无需额外加载资源。
25
+ */
26
+ interface CursorOptions {
27
+ /** 目标元素,未传则默认挂载到 document.body */
28
+ target?: HTMLElement;
29
+ /** 光标尺寸(像素),默认 64。热点固定为 size 的 1/8 */
30
+ size?: number;
31
+ /**
32
+ * 自定义鼠标悬浮态图片(URL 或 dataURL)。
33
+ * 传入后覆盖内置的手指图标。
34
+ */
35
+ hoverImage?: string;
36
+ /**
37
+ * 自定义鼠标按压态图片(URL 或 dataURL)。
38
+ * 仅当同时传了 hoverImage 时生效。
39
+ * 未传时按压态复用 hoverImage,无状态切换效果。
40
+ */
41
+ pressImage?: string;
42
+ }
43
+ /**
44
+ * 启用自定义鼠标指针。默认挂载到 `document.body`,作用于整个页面。
45
+ *
46
+ * 多次调用时,会先清理上一次的状态再重新绑定。
47
+ *
48
+ * @returns 清理函数,调用后会恢复默认光标并解除事件监听。
49
+ */
50
+ declare function enableCustomCursor(opts?: CursorOptions): () => void;
51
+ /**
52
+ * 关闭自定义鼠标指针,恢复默认光标。
53
+ */
54
+ declare function disableCustomCursor(): void;
55
+
56
+ type InitCallbackType = (maxWidth: number, maxHeight: number) => void;
57
+ /**
58
+ * Main SDK class providing playable ad functionality and state management.
59
+ * Handles initialization, lifecycle events, and interactions across different ad networks.
60
+ */
61
+ declare class sdk {
62
+ /** Current version of the SDK */
63
+ static version: string;
64
+ /** Current maximum width of the playable ad container in pixels */
65
+ static maxWidth: number;
66
+ /** Current maximum height of the playable ad container in pixels */
67
+ static maxHeight: number;
68
+ /** Indicates if the current orientation is landscape (width > height) */
69
+ static isLandscape: boolean;
70
+ /** Indicates if the Ad Network is ready and playable ad can be initialized */
71
+ static isReady: boolean;
72
+ /** Indicates if all playable ad resources are loaded and gameplay has started */
73
+ static isStarted: boolean;
74
+ /** Indicates if the playable ad is currently paused */
75
+ static isPaused: boolean;
76
+ /** Indicates if the playable ad has finished */
77
+ static isFinished: boolean;
78
+ /** Current volume level (0-1) */
79
+ static volume: number;
80
+ /** Number of user interactions with the playable ad (auto-incremented on each touch/click) */
81
+ static interactions: number;
82
+ /**
83
+ * Initializes the SDK and sets up protocol-specific handlers.
84
+ * This must be called as earlier as possible.
85
+ *
86
+ * @param callback Optional function called when ad container is ready
87
+ * @param options Optional configuration options
88
+ * @example
89
+ * // Basic initialization
90
+ * sdk.playcraftInit();
91
+ *
92
+ * // Initialization with callback
93
+ * sdk.playcraftInit((width, height) => {
94
+ * new App(width, height)
95
+ * });
96
+ *
97
+ * @fires playcraftInit When SDK initialization starts
98
+ * @fires playcraftReady When game instance is created and ready to load resources
99
+ */
100
+ static playcraftInit(callback?: InitCallbackType, options?: {
101
+ customCursor?: boolean | CursorOptions;
102
+ }): void;
103
+ /**
104
+ * Starts the playable ad experience.
105
+ * Should be called after all resources are loaded and first frame is rendered.
106
+ *
107
+ * @example
108
+ * sdk.playcraftStart();
109
+ *
110
+ * @fires playcraftStart When the playable ad starts
111
+ * @fires playcraftResize When the ad container is initially sized
112
+ */
113
+ static playcraftStart(): void;
114
+ /**
115
+ * Marks the main scene as fully loaded and ready for user interaction.
116
+ * Should be called at the end of your main game scene's create() method.
117
+ *
118
+ * @example
119
+ * create() {
120
+ * // ... all initialization code ...
121
+ * sdk.playcraftInteractive();
122
+ * }
123
+ *
124
+ * @fires playcraftInteractive When the main scene is ready for user interaction
125
+ */
126
+ static playcraftInteractive(): void;
127
+ /**
128
+ * Marks the challenge as started.
129
+ * Should be called by business logic when the user actually starts playing.
130
+ *
131
+ * @example
132
+ * onFirstClick() {
133
+ * sdk.playcraftChallengeStart();
134
+ * }
135
+ *
136
+ * @fires playcraftChallengeStart When the challenge starts
137
+ */
138
+ static playcraftChallengeStart(): void;
139
+ /**
140
+ * Marks the playable ad as finished.
141
+ *
142
+ * @example
143
+ * sdk.playcraftFinish();
144
+ *
145
+ * @fires playcraftFinish When the playable ad is marked as finished
146
+ */
147
+ static playcraftFinish(): void;
148
+ /**
149
+ * Triggers a retry/restart of the playable ad.
150
+ *
151
+ * @example
152
+ * retryButton.onclick = () => sdk.playcraftRetry();
153
+ *
154
+ * @fires playcraftRetry When a retry is triggered
155
+ */
156
+ static playcraftRetry(): void;
157
+ /**
158
+ * Triggers the install/download action for the advertised app.
159
+ *
160
+ * @example
161
+ * installButton.onclick = () => sdk.playcraftInstall();
162
+ *
163
+ * @fires playcraftFinish If the ad hasn't been marked as finished
164
+ * @fires playcraftInstall When the install action is triggered
165
+ */
166
+ static playcraftInstall(params?: {
167
+ action_type: 'auto' | 'click';
168
+ timeout: number;
169
+ }): void;
170
+ /**
171
+ * Trigger force resize event
172
+ *
173
+ * @example
174
+ * sdk.playcraftResize();
175
+ *
176
+ * @fires playcraftResize With current maxWidth and maxHeight
177
+ */
178
+ static playcraftResize(): void;
179
+ /**
180
+ * Records playable ad challenge progress (0-100).
181
+ * Triggers channel-specific milestone events based on the progress value.
182
+ * Milestones are not re-triggered once fired.
183
+ *
184
+ * @param percent Progress percentage (0-100)
185
+ *
186
+ * @example
187
+ * // Report 50% progress
188
+ * sdk.recordPlaycraftChallengeProgress(50);
189
+ *
190
+ * @fires playcraftChallengeProgress With the progress percentage value
191
+ */
192
+ static recordPlaycraftChallengeProgress(percent: number): void;
193
+ /**
194
+ * Records a game success/completion event.
195
+ * Sends platform-specific success tracking events:
196
+ * - AppLovin: CHALLENGE_SOLVED
197
+ * - Bigabid: complete
198
+ * - InMobi: Gameplay_Complete
199
+ *
200
+ * @example
201
+ * onLevelComplete() {
202
+ * sdk.playcraftRecordSuccess();
203
+ * }
204
+ */
205
+ static playcraftRecordSuccess(): void;
206
+ /**
207
+ * Records a game failure event.
208
+ * Sends platform-specific failure tracking events:
209
+ * - AppLovin: CHALLENGE_FAILED
210
+ *
211
+ * @example
212
+ * onLevelFailed() {
213
+ * sdk.playcraftRecordFailed();
214
+ * }
215
+ */
216
+ static playcraftRecordFailed(): void;
217
+ /**
218
+ * Forces the playable ad into a paused state.
219
+ *
220
+ * @example
221
+ * pauseButton.onclick = () => sdk.playcraftPause();
222
+ *
223
+ * @fires playcraftPause When the ad enters paused state
224
+ */
225
+ static playcraftPause(): void;
226
+ /**
227
+ * Resumes the playable ad from a forced pause state.
228
+ *
229
+ * @example
230
+ * resumeButton.onclick = () => sdk.playcraftResume();
231
+ *
232
+ * @fires playcraftResume When the ad resumes from pause
233
+ */
234
+ static playcraftResume(): void;
235
+ /**
236
+ * Registers an event listener.
237
+ *
238
+ * @param event Name of the event to listen for
239
+ * @param fn Callback function to execute when event occurs
240
+ * @param context Optional 'this' context for the callback
241
+ *
242
+ * @example
243
+ * sdk.on('playcraftInteraction', (count) => {
244
+ * console.log(`User interaction #${count}`);
245
+ * });
246
+ */
247
+ static on(event: EventName, fn: Function, context?: any): void;
248
+ /**
249
+ * Registers a one-time event listener that removes itself after execution.
250
+ *
251
+ * @param event Name of the event to listen for
252
+ * @param fn Callback function to execute when event occurs
253
+ * @param context Optional 'this' context for the callback
254
+ *
255
+ * @example
256
+ * sdk.once('playcraftInteraction', () => {
257
+ * console.log('First user interaction occurred!');
258
+ * });
259
+ */
260
+ static once(event: EventName, fn: Function, context?: any): void;
261
+ /**
262
+ * Removes an event listener.
263
+ *
264
+ * @param event Name of the event to stop listening for
265
+ * @param fn Optional callback function to remove
266
+ * @param context Optional 'this' context to match when removing
267
+ *
268
+ * @example
269
+ * sdk.off('playcraftInteraction', handler);
270
+ */
271
+ static off(event: EventName, fn?: Function, context?: any): void;
272
+ /**
273
+ * @returns 当前渠道
274
+ */
275
+ static getCurChannel(): "applovin" | "ironsource" | "unity" | "mintegral" | "preview" | "google" | "facebook" | "moloco" | "vungle" | "adcolony" | "tapjoy" | "snapchat" | "tiktok" | "appreciate" | "chartboost" | "pangle" | "mytarget" | "liftoff" | "smadex" | "adikteev" | "bigabid" | "inmobi" | "bigoads";
276
+ /**
277
+ * 判断是否是 Google
278
+ */
279
+ static isGoogle(): boolean;
280
+ /**
281
+ * 获取当前用户语言的 i18n 简码。
282
+ * 多源探测(querystring → localStorage → cookie → navigator → html tag)。
283
+ *
284
+ * @returns i18n 简码:en | zh | ja | ko | de | fr | es | pt | it | hi | bn | ur | ar | tr | ru | ms | id,默认 'en'
285
+ *
286
+ * @example
287
+ * const langCode = sdk.getLanguageCode(); // 'zh'
288
+ */
289
+ static getLanguageCode(): string;
290
+ /**
291
+ * 设置主题配置,支持传入多份 JSON5 配置。
292
+ * 每一份配置都可以是 JSON Schema(带 $schema 字段),会自动提取其中的 default 值。
293
+ * 所有配置按顺序依次深度合并,后面的优先级更高。
294
+ *
295
+ * @param configList - 配置数组,每一项可以是:
296
+ * - JSON Schema(带 $schema 字段):自动提取 default 值后参与合并
297
+ * - 普通配置对象:直接参与合并
298
+ * @param config - 额外配置项
299
+ * - config.assetRecordByPath: 相对路径到已解析 URL 的映射
300
+ *
301
+ * @example
302
+ * sdk.setConfig(
303
+ * [schemaA, schemaB, themeData],
304
+ * { assetRecordByPath: imageAssets }
305
+ * );
306
+ */
307
+ static setConfig(configList: any[], config: {
308
+ assetRecordByPath: Record<string, string>;
309
+ i18nRecord?: I18nRecord;
310
+ }): void;
311
+ /**
312
+ * 获取合并后的主题配置。
313
+ * 返回 schema 默认值与用户主题数据深度合并后的结果。
314
+ * 必须先调用 sdk.setConfig() 才能使用此方法。
315
+ *
316
+ * @returns 合并后的主题配置对象
317
+ *
318
+ * @example
319
+ * const config = sdk.getConfig();
320
+ * console.log(config.primaryColor); // '#ff0000'
321
+ */
322
+ static getConfig(): Record<string, any>;
323
+ /**
324
+ * 启用自定义鼠标指针。默认挂载到 `document.body`,作用于整个页面。
325
+ * 悬浮显示手指图标,按下时切换为按压态图标。
326
+ * 多次调用会先清理上一次的状态再重新绑定。
327
+ *
328
+ * @param opts 可选配置
329
+ * - opts.target: 目标元素,未传则默认 document.body
330
+ * - opts.size: 光标尺寸(像素),默认 64,热点固定为 size 的 1/8
331
+ * @returns 清理函数,调用后会恢复默认光标并解除事件监听
332
+ *
333
+ * @example
334
+ * sdk.enableCustomCursor();
335
+ * sdk.enableCustomCursor({ size: 96 });
336
+ */
337
+ static enableCustomCursor(opts?: CursorOptions): () => void;
338
+ /**
339
+ * 关闭自定义鼠标指针,恢复默认光标。
340
+ *
341
+ * @example
342
+ * sdk.disableCustomCursor();
343
+ */
344
+ static disableCustomCursor(): void;
345
+ }
346
+
347
+ /**
348
+ * 微信浏览器引导遮罩层
349
+ * 当用户在微信中点击 Install 按钮时显示,引导用户在浏览器中打开
350
+ */
351
+ /**
352
+ * 创建并显示微信浏览器引导遮罩
353
+ */
354
+ declare function showWechatGuide(): void;
355
+ /**
356
+ * 隐藏微信浏览器引导遮罩
357
+ */
358
+ declare function hideWechatGuide(): void;
359
+ /**
360
+ * 移除微信浏览器引导遮罩
361
+ */
362
+ declare function removeWechatGuide(): void;
363
+
364
+ /**
365
+ * 语言检测模块(零外部依赖)。
366
+ *
367
+ * 提供两个层级的语言获取:
368
+ * - getLanguageCode() → i18n 简码(en / zh / ja / ko …),用于 i18n.runtime 查表
369
+ * - getLanguage() → 业务语言代号(EN / CN_S / CN_T / DE …),用于游戏内资源选择
370
+ *
371
+ * 探测优先级(与 i18next-browser-languagedetector 保持一致):
372
+ * 1. URL querystring (?lng=xx)
373
+ * 2. localStorage (i18nextLng)
374
+ * 3. cookie (i18next)
375
+ * 4. navigator.languages / navigator.language
376
+ * 5. <html lang="xx">
377
+ */
378
+ /**
379
+ * 多源探测当前用户语言,并归一化为 i18n 简码。
380
+ *
381
+ * @returns i18n 简码:en | zh | zh-TW | ja | ko | de | fr | es | pt | it | hi | bn | ur | ar | tr | ru | ms | id,默认 'en'
382
+ */
383
+ declare function getLanguageCode(): string;
384
+
385
+ export { type CursorOptions, type I18nRecord, sdk as default, disableCustomCursor, enableCustomCursor, getLanguageCode, hideWechatGuide, removeWechatGuide, sdk, showWechatGuide };