drgame-cc 1.0.41 → 1.0.43

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,322 +1,322 @@
1
- /**
2
- * 聚合广告 SDK - 主入口
3
- *
4
- * 使用方式:
5
- * const { AdSdk, AdPlatform, AdEvent } = require('./ad-sdk');
6
- * await AdSdk.init({ platform: AdPlatform.Xiaomi });
7
- * const result = await AdSdk.requestAd({ cpId: 'cp_001', rewardType: 'extra_life', triggerScene: 'game_over' });
8
- *
9
- * TS 项目中:
10
- * import { AdSdk, AdPlatform } from './ad-sdk';
11
- */
12
-
13
- const { AdPlatform, AdEvent, AdErrorCode } = require('./types/enums');
14
- const { XiaomiAdapter } = require('./adapters/xiaomi-adapter');
15
- const { EventEmitter } = require('./event-emitter');
16
- const { Reporter, ActionType } = require('./reporters/reporter');
17
- const { Leaderboard } = require('./leaderboard/leaderboard');
18
- const KEY_EVENT_NAME = '__key_event__';
19
-
20
- // ────────────────────────────────────────
21
- // 适配器注册表
22
- // ────────────────────────────────────────
23
-
24
- const BUILTIN_ADAPTERS = {};
25
- BUILTIN_ADAPTERS[AdPlatform.Xiaomi] = XiaomiAdapter;
26
- // 后续扩展:
27
- // BUILTIN_ADAPTERS[AdPlatform.Huawei] = HuaweiAdapter;
28
- // BUILTIN_ADAPTERS[AdPlatform.Tencent] = TencentAdapter;
29
-
30
- // ────────────────────────────────────────
31
- // SDK 实现
32
- // ────────────────────────────────────────
33
-
34
- class AdSdkImpl {
35
- constructor() {
36
- this._initialized = false;
37
- this._currentPlatform = AdPlatform.Xiaomi;
38
- this._adapters = new Map();
39
- this._emitter = new EventEmitter();
40
- this._globalEventHandler = null;
41
- this._keyHandlers = [];
42
- this._adapterKeyHandler = null;
43
- }
44
-
45
- get currentPlatform() {
46
- return this._currentPlatform;
47
- }
48
-
49
- /**
50
- * 初始化 SDK
51
- * @param {import('./types/interfaces').IAdSdkConfig} config
52
- */
53
- async init(config) {
54
- if (this._initialized) {
55
- console.warn('[AdSdk] Already initialized. Call destroy() first if you need to re-init.');
56
- return;
57
- }
58
-
59
- this._currentPlatform = config.platform;
60
- this._globalEventHandler = config.onEvent || null;
61
-
62
- // 注册自定义适配器
63
- if (config.customAdapters) {
64
- for (const adapter of config.customAdapters) {
65
- this._adapters.set(adapter.platform, adapter);
66
- }
67
- }
68
-
69
- // 注册内置适配器(不覆盖自定义的)
70
- if (!this._adapters.has(config.platform)) {
71
- const AdapterClass = BUILTIN_ADAPTERS[config.platform];
72
- if (!AdapterClass) {
73
- throw new Error(
74
- '[AdSdk] No adapter found for platform "' + config.platform + '". ' +
75
- 'Available: ' + Object.keys(BUILTIN_ADAPTERS).join(', ') + '. ' +
76
- 'You can also provide a custom adapter via config.customAdapters.'
77
- );
78
- }
79
- const instance = new AdapterClass();
80
- this._adapters.set(config.platform, instance);
81
- }
82
-
83
- // 初始化当前平台的适配器
84
- const adapter = this._adapters.get(config.platform);
85
- const adapterConfig = (config.adapters && config.adapters[config.platform]) || {};
86
- await adapter.init(adapterConfig);
87
-
88
- // 桥接适配器事件到全局
89
- const self = this;
90
- const events = Object.values(AdEvent);
91
- for (let i = 0; i < events.length; i++) {
92
- const evt = events[i];
93
- adapter.on(evt, function (data) {
94
- self._emitter.emit(evt, data);
95
- if (self._globalEventHandler) {
96
- self._globalEventHandler(evt, data);
97
- }
98
- });
99
- }
100
-
101
- // 桥接适配器的按键事件
102
- if (adapter.onKeyEvent) {
103
- self._adapterKeyHandler = function (keyEvent) {
104
- for (var j = 0; j < self._keyHandlers.length; j++) {
105
- try {
106
- self._keyHandlers[j](keyEvent);
107
- } catch (e) {
108
- console.error('[AdSdk] Key event handler error:', e);
109
- }
110
- }
111
- };
112
- adapter.onKeyEvent(self._adapterKeyHandler);
113
- }
114
-
115
- this._initialized = true;
116
- console.log('[AdSdk] Initialized with platform: ' + config.platform);
117
- }
118
-
119
- /**
120
- * 获取设备信息
121
- * @returns {Promise<import('./types/interfaces').IDeviceInfo>}
122
- */
123
- async getDeviceInfo() {
124
- this._ensureInit();
125
- const adapter = this._getAdapter();
126
- if (adapter.getDeviceInfo) {
127
- return adapter.getDeviceInfo();
128
- }
129
- throw new Error('[AdSdk] getDeviceInfo is not supported by the current adapter.');
130
- }
131
-
132
- /**
133
- * 获取玩家账户信息
134
- * @returns {Promise<import('./types/interfaces').IAccountInfo>}
135
- */
136
- async getAccount() {
137
- this._ensureInit();
138
- const adapter = this._getAdapter();
139
- if (adapter.getAccount) {
140
- return adapter.getAccount();
141
- }
142
- throw new Error('[AdSdk] getAccount is not supported by the current adapter.');
143
- }
144
-
145
- /**
146
- * 请求播放广告
147
- * @param {import('./types/interfaces').IAdRequest} request
148
- * @returns {Promise<import('./types/interfaces').IAdRewardResult>}
149
- */
150
- async requestAd(request) {
151
- this._ensureInit();
152
- return this._getAdapter().requestAd(request);
153
- }
154
-
155
- /**
156
- * 通知游戏加载完成
157
- * @param {string} version
158
- */
159
- onGameReady(version) {
160
- this._ensureInit();
161
- const adapter = this._getAdapter();
162
- if (adapter.onGameReady) {
163
- adapter.onGameReady(version);
164
- }
165
- }
166
-
167
- /** 通知游戏暂停 */
168
- onGamePause() {
169
- this._ensureInit();
170
- const adapter = this._getAdapter();
171
- if (adapter.onGamePause) {
172
- adapter.onGamePause();
173
- }
174
- }
175
-
176
- /** 通知游戏恢复 */
177
- onGameResume() {
178
- this._ensureInit();
179
- const adapter = this._getAdapter();
180
- if (adapter.onGameResume) {
181
- adapter.onGameResume();
182
- }
183
- }
184
-
185
- /**
186
- * 退出游戏
187
- * @returns {Promise<void>}
188
- */
189
- async exitGame() {
190
- this._ensureInit();
191
- const adapter = this._getAdapter();
192
- if (adapter.exitGame) {
193
- return adapter.exitGame();
194
- }
195
- }
196
-
197
- /**
198
- * 监听广告事件
199
- * @param {string} event
200
- * @param {Function} handler
201
- */
202
- on(event, handler) {
203
- this._emitter.on(event, handler);
204
- }
205
-
206
- /**
207
- * 取消监听
208
- * @param {string} event
209
- * @param {Function} [handler]
210
- */
211
- off(event, handler) {
212
- this._emitter.off(event, handler);
213
- }
214
-
215
- /**
216
- * 监听遥控器按键事件
217
- * @param {Function} handler - 回调函数,接收 { key, action } 对象
218
- */
219
- onKeyEvent(handler) {
220
- if (typeof handler === 'function' && this._keyHandlers.indexOf(handler) === -1) {
221
- this._keyHandlers.push(handler);
222
- }
223
- }
224
-
225
- /**
226
- * 取消监听遥控器按键事件
227
- * @param {Function} [handler] - 不传则清除所有
228
- */
229
- offKeyEvent(handler) {
230
- if (!handler) {
231
- this._keyHandlers = [];
232
- } else {
233
- var idx = this._keyHandlers.indexOf(handler);
234
- if (idx !== -1) {
235
- this._keyHandlers.splice(idx, 1);
236
- }
237
- }
238
- }
239
-
240
- /**
241
- * 切换广告平台
242
- * @param {string} platform
243
- */
244
- switchPlatform(platform) {
245
- if (platform === this._currentPlatform) return;
246
-
247
- const adapter = this._adapters.get(platform);
248
- if (!adapter) {
249
- throw new Error('[AdSdk] Platform "' + platform + '" not registered. Init with it first.');
250
- }
251
-
252
- this._currentPlatform = platform;
253
- console.log('[AdSdk] Switched to platform: ' + platform);
254
- }
255
-
256
- /** 销毁 SDK */
257
- destroy() {
258
- // 清理适配器的按键桥接
259
- const adapter = this._getAdapter();
260
- if (adapter && adapter.offKeyEvent && this._adapterKeyHandler) {
261
- adapter.offKeyEvent(this._adapterKeyHandler);
262
- }
263
-
264
- for (const adapter of this._adapters.values()) {
265
- adapter.destroy();
266
- }
267
- this._adapters.clear();
268
- this._emitter.removeAllListeners();
269
- this._globalEventHandler = null;
270
- this._keyHandlers = [];
271
- this._adapterKeyHandler = null;
272
- this._initialized = false;
273
- }
274
-
275
- // ────────────────────────────────────────
276
- // 内部
277
- // ────────────────────────────────────────
278
-
279
- _ensureInit() {
280
- if (!this._initialized) {
281
- throw new Error('[AdSdk] SDK not initialized. Call AdSdk.init() first.');
282
- }
283
- }
284
-
285
- _getAdapter() {
286
- return this._adapters.get(this._currentPlatform);
287
- }
288
- }
289
-
290
- // ────────────────────────────────────────
291
- // 导出单例
292
- // ────────────────────────────────────────
293
-
294
- const AdSdk = new AdSdkImpl();
295
-
296
- // 命名导出
297
- const { AdType, DevicePlatform, KeyCode, KeyAction } = require('./types/enums');
298
-
299
- module.exports = {
300
- // 单例
301
- AdSdk,
302
-
303
- // 枚举
304
- AdPlatform,
305
- AdType,
306
- AdEvent,
307
- AdErrorCode,
308
- DevicePlatform,
309
- KeyCode,
310
- KeyAction,
311
-
312
- // 数据上报
313
- Reporter,
314
- ActionType,
315
-
316
- // 排行榜
317
- Leaderboard,
318
-
319
- // 类
320
- XiaomiAdapter,
321
- EventEmitter,
322
- };
1
+ /**
2
+ * 聚合广告 SDK - 主入口
3
+ *
4
+ * 使用方式:
5
+ * const { AdSdk, AdPlatform, AdEvent } = require('./ad-sdk');
6
+ * await AdSdk.init({ platform: AdPlatform.Xiaomi });
7
+ * const result = await AdSdk.requestAd({ cpId: 'cp_001', rewardType: 'extra_life', triggerScene: 'game_over' });
8
+ *
9
+ * TS 项目中:
10
+ * import { AdSdk, AdPlatform } from './ad-sdk';
11
+ */
12
+
13
+ const { AdPlatform, AdEvent, AdErrorCode } = require('./types/enums');
14
+ const { XiaomiAdapter } = require('./adapters/xiaomi-adapter');
15
+ const { EventEmitter } = require('./event-emitter');
16
+ const { Reporter, ActionType } = require('./reporters/reporter');
17
+ const { Leaderboard } = require('./leaderboard/leaderboard');
18
+ const KEY_EVENT_NAME = '__key_event__';
19
+
20
+ // ────────────────────────────────────────
21
+ // 适配器注册表
22
+ // ────────────────────────────────────────
23
+
24
+ const BUILTIN_ADAPTERS = {};
25
+ BUILTIN_ADAPTERS[AdPlatform.Xiaomi] = XiaomiAdapter;
26
+ // 后续扩展:
27
+ // BUILTIN_ADAPTERS[AdPlatform.Huawei] = HuaweiAdapter;
28
+ // BUILTIN_ADAPTERS[AdPlatform.Tencent] = TencentAdapter;
29
+
30
+ // ────────────────────────────────────────
31
+ // SDK 实现
32
+ // ────────────────────────────────────────
33
+
34
+ class AdSdkImpl {
35
+ constructor() {
36
+ this._initialized = false;
37
+ this._currentPlatform = AdPlatform.Xiaomi;
38
+ this._adapters = new Map();
39
+ this._emitter = new EventEmitter();
40
+ this._globalEventHandler = null;
41
+ this._keyHandlers = [];
42
+ this._adapterKeyHandler = null;
43
+ }
44
+
45
+ get currentPlatform() {
46
+ return this._currentPlatform;
47
+ }
48
+
49
+ /**
50
+ * 初始化 SDK
51
+ * @param {import('./types/interfaces').IAdSdkConfig} config
52
+ */
53
+ async init(config) {
54
+ if (this._initialized) {
55
+ console.warn('[AdSdk] Already initialized. Call destroy() first if you need to re-init.');
56
+ return;
57
+ }
58
+
59
+ this._currentPlatform = config.platform;
60
+ this._globalEventHandler = config.onEvent || null;
61
+
62
+ // 注册自定义适配器
63
+ if (config.customAdapters) {
64
+ for (const adapter of config.customAdapters) {
65
+ this._adapters.set(adapter.platform, adapter);
66
+ }
67
+ }
68
+
69
+ // 注册内置适配器(不覆盖自定义的)
70
+ if (!this._adapters.has(config.platform)) {
71
+ const AdapterClass = BUILTIN_ADAPTERS[config.platform];
72
+ if (!AdapterClass) {
73
+ throw new Error(
74
+ '[AdSdk] No adapter found for platform "' + config.platform + '". ' +
75
+ 'Available: ' + Object.keys(BUILTIN_ADAPTERS).join(', ') + '. ' +
76
+ 'You can also provide a custom adapter via config.customAdapters.'
77
+ );
78
+ }
79
+ const instance = new AdapterClass();
80
+ this._adapters.set(config.platform, instance);
81
+ }
82
+
83
+ // 初始化当前平台的适配器
84
+ const adapter = this._adapters.get(config.platform);
85
+ const adapterConfig = (config.adapters && config.adapters[config.platform]) || {};
86
+ await adapter.init(adapterConfig);
87
+
88
+ // 桥接适配器事件到全局
89
+ const self = this;
90
+ const events = Object.values(AdEvent);
91
+ for (let i = 0; i < events.length; i++) {
92
+ const evt = events[i];
93
+ adapter.on(evt, function (data) {
94
+ self._emitter.emit(evt, data);
95
+ if (self._globalEventHandler) {
96
+ self._globalEventHandler(evt, data);
97
+ }
98
+ });
99
+ }
100
+
101
+ // 桥接适配器的按键事件
102
+ if (adapter.onKeyEvent) {
103
+ self._adapterKeyHandler = function (keyEvent) {
104
+ for (var j = 0; j < self._keyHandlers.length; j++) {
105
+ try {
106
+ self._keyHandlers[j](keyEvent);
107
+ } catch (e) {
108
+ console.error('[AdSdk] Key event handler error:', e);
109
+ }
110
+ }
111
+ };
112
+ adapter.onKeyEvent(self._adapterKeyHandler);
113
+ }
114
+
115
+ this._initialized = true;
116
+ console.log('[AdSdk] Initialized with platform: ' + config.platform);
117
+ }
118
+
119
+ /**
120
+ * 获取设备信息
121
+ * @returns {Promise<import('./types/interfaces').IDeviceInfo>}
122
+ */
123
+ async getDeviceInfo() {
124
+ this._ensureInit();
125
+ const adapter = this._getAdapter();
126
+ if (adapter.getDeviceInfo) {
127
+ return adapter.getDeviceInfo();
128
+ }
129
+ throw new Error('[AdSdk] getDeviceInfo is not supported by the current adapter.');
130
+ }
131
+
132
+ /**
133
+ * 获取玩家账户信息
134
+ * @returns {Promise<import('./types/interfaces').IAccountInfo>}
135
+ */
136
+ async getAccount() {
137
+ this._ensureInit();
138
+ const adapter = this._getAdapter();
139
+ if (adapter.getAccount) {
140
+ return adapter.getAccount();
141
+ }
142
+ throw new Error('[AdSdk] getAccount is not supported by the current adapter.');
143
+ }
144
+
145
+ /**
146
+ * 请求播放广告
147
+ * @param {import('./types/interfaces').IAdRequest} request
148
+ * @returns {Promise<import('./types/interfaces').IAdRewardResult>}
149
+ */
150
+ async requestAd(request) {
151
+ this._ensureInit();
152
+ return this._getAdapter().requestAd(request);
153
+ }
154
+
155
+ /**
156
+ * 通知游戏加载完成
157
+ * @param {string} version
158
+ */
159
+ onGameReady(version) {
160
+ this._ensureInit();
161
+ const adapter = this._getAdapter();
162
+ if (adapter.onGameReady) {
163
+ adapter.onGameReady(version);
164
+ }
165
+ }
166
+
167
+ /** 通知游戏暂停 */
168
+ onGamePause() {
169
+ this._ensureInit();
170
+ const adapter = this._getAdapter();
171
+ if (adapter.onGamePause) {
172
+ adapter.onGamePause();
173
+ }
174
+ }
175
+
176
+ /** 通知游戏恢复 */
177
+ onGameResume() {
178
+ this._ensureInit();
179
+ const adapter = this._getAdapter();
180
+ if (adapter.onGameResume) {
181
+ adapter.onGameResume();
182
+ }
183
+ }
184
+
185
+ /**
186
+ * 退出游戏
187
+ * @returns {Promise<void>}
188
+ */
189
+ async exitGame() {
190
+ this._ensureInit();
191
+ const adapter = this._getAdapter();
192
+ if (adapter.exitGame) {
193
+ return adapter.exitGame();
194
+ }
195
+ }
196
+
197
+ /**
198
+ * 监听广告事件
199
+ * @param {string} event
200
+ * @param {Function} handler
201
+ */
202
+ on(event, handler) {
203
+ this._emitter.on(event, handler);
204
+ }
205
+
206
+ /**
207
+ * 取消监听
208
+ * @param {string} event
209
+ * @param {Function} [handler]
210
+ */
211
+ off(event, handler) {
212
+ this._emitter.off(event, handler);
213
+ }
214
+
215
+ /**
216
+ * 监听遥控器按键事件
217
+ * @param {Function} handler - 回调函数,接收 { key, action } 对象
218
+ */
219
+ onKeyEvent(handler) {
220
+ if (typeof handler === 'function' && this._keyHandlers.indexOf(handler) === -1) {
221
+ this._keyHandlers.push(handler);
222
+ }
223
+ }
224
+
225
+ /**
226
+ * 取消监听遥控器按键事件
227
+ * @param {Function} [handler] - 不传则清除所有
228
+ */
229
+ offKeyEvent(handler) {
230
+ if (!handler) {
231
+ this._keyHandlers = [];
232
+ } else {
233
+ var idx = this._keyHandlers.indexOf(handler);
234
+ if (idx !== -1) {
235
+ this._keyHandlers.splice(idx, 1);
236
+ }
237
+ }
238
+ }
239
+
240
+ /**
241
+ * 切换广告平台
242
+ * @param {string} platform
243
+ */
244
+ switchPlatform(platform) {
245
+ if (platform === this._currentPlatform) return;
246
+
247
+ const adapter = this._adapters.get(platform);
248
+ if (!adapter) {
249
+ throw new Error('[AdSdk] Platform "' + platform + '" not registered. Init with it first.');
250
+ }
251
+
252
+ this._currentPlatform = platform;
253
+ console.log('[AdSdk] Switched to platform: ' + platform);
254
+ }
255
+
256
+ /** 销毁 SDK */
257
+ destroy() {
258
+ // 清理适配器的按键桥接
259
+ const adapter = this._getAdapter();
260
+ if (adapter && adapter.offKeyEvent && this._adapterKeyHandler) {
261
+ adapter.offKeyEvent(this._adapterKeyHandler);
262
+ }
263
+
264
+ for (const adapter of this._adapters.values()) {
265
+ adapter.destroy();
266
+ }
267
+ this._adapters.clear();
268
+ this._emitter.removeAllListeners();
269
+ this._globalEventHandler = null;
270
+ this._keyHandlers = [];
271
+ this._adapterKeyHandler = null;
272
+ this._initialized = false;
273
+ }
274
+
275
+ // ────────────────────────────────────────
276
+ // 内部
277
+ // ────────────────────────────────────────
278
+
279
+ _ensureInit() {
280
+ if (!this._initialized) {
281
+ throw new Error('[AdSdk] SDK not initialized. Call AdSdk.init() first.');
282
+ }
283
+ }
284
+
285
+ _getAdapter() {
286
+ return this._adapters.get(this._currentPlatform);
287
+ }
288
+ }
289
+
290
+ // ────────────────────────────────────────
291
+ // 导出单例
292
+ // ────────────────────────────────────────
293
+
294
+ const AdSdk = new AdSdkImpl();
295
+
296
+ // 命名导出
297
+ const { AdType, DevicePlatform, KeyCode, KeyAction } = require('./types/enums');
298
+
299
+ module.exports = {
300
+ // 单例
301
+ AdSdk,
302
+
303
+ // 枚举
304
+ AdPlatform,
305
+ AdType,
306
+ AdEvent,
307
+ AdErrorCode,
308
+ DevicePlatform,
309
+ KeyCode,
310
+ KeyAction,
311
+
312
+ // 数据上报
313
+ Reporter,
314
+ ActionType,
315
+
316
+ // 排行榜
317
+ Leaderboard,
318
+
319
+ // 类
320
+ XiaomiAdapter,
321
+ EventEmitter,
322
+ };