ch_start_pack.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { _decorator, Component, director, Enum, Node } from 'cc';
  2. import { ch } from '../ch';
  3. import { loadType } from '../audio/audio';
  4. import { ch_sdk_comp } from './ch_sdk_comp';
  5. const { ccclass, property } = _decorator;
  6. /**包id*/
  7. export enum pack {
  8. id0 = 0,
  9. id1 = 1,
  10. id2 = 2,
  11. id3 = 3,
  12. id4 = 4,
  13. id5 = 5,
  14. id6 = 6,
  15. id7 = 7,
  16. id8 = 8,
  17. id9 = 9,
  18. }
  19. @ccclass('ch_start_pack')
  20. export class ch_start_pack extends Component {
  21. @property({ visible: false })
  22. private _gname: string[] = [];
  23. @property({ visible: false })
  24. private _gid: string[] = [];
  25. @property({ visible: false })
  26. private _is_local: boolean[] = [];
  27. @property({ visible: false })
  28. private _server: chsdk.serverType[] = [chsdk.serverType.dev];
  29. @property({ visible: false })
  30. private _report: chsdk.reportType[] = [chsdk.reportType.off];
  31. @property({ visible: false })
  32. private _log: chsdk.loglevel[] = [];
  33. @property({ visible: false })
  34. private _ttad: string[] = [];
  35. @property({ visible: false })
  36. private _ttiad: string[] = [];
  37. @property({ visible: false })
  38. private _tttmpid: string[] = [];
  39. @property({ visible: false })
  40. private _wxad: string[] = [];
  41. @property({ visible: false })
  42. private _wxiad: string[] = [];
  43. @property({ visible: false })
  44. private _wxtmpid: string[] = [];
  45. @property({ visible: false })
  46. private _serverIP: string[] = [];
  47. @property({ visible: false })
  48. private _pid: pack = pack.id0;
  49. @property({ type: Enum(pack), displayName: '包id', group: { name: '选包', id: 'pack', displayOrder: 0 } })
  50. get pid() { return this._pid; }
  51. set pid(val) {
  52. this._pid = val;
  53. for (let i = 0; i <= this._pid; i++) {
  54. this._gname[i] ??= '';
  55. this._is_local[i] ??= this._is_local[i - 1] ?? true;
  56. this._gid[i] ??= this._gid[i - 1] ?? '0';
  57. this._server[i] ??= this._server[i - 1] ?? chsdk.serverType.online;
  58. this._report[i] ??= this._report[i - 1] ?? chsdk.reportType.off;
  59. this._log[i] ??= this._log[i - 1] ?? chsdk.loglevel.DEBUG;
  60. this._ttad[i] ??= '';
  61. this._ttiad[i] ??= '';
  62. this._tttmpid[i] ??= '';
  63. this._wxad[i] ??= '';
  64. this._wxiad[i] ??= '';
  65. this._wxtmpid[i] ??= '';
  66. this._serverIP[i] ??= 'http://192.168.1.120:8787/v1';
  67. }
  68. }
  69. @property({ displayName: '游戏名', group: { name: '选包', id: 'pack', displayOrder: 0 } })
  70. get gname() { return this._gname[this._pid] ?? ''; }
  71. set gname(val) { this._gname[this._pid] = val; }
  72. @property({ displayName: '游戏id', group: { name: '选包', id: 'pack', displayOrder: 0 } })
  73. get gid() { return this._gid[this._pid]; }
  74. set gid(val) { this._gid[this._pid] = val; }
  75. //
  76. @property({ displayName: '是否单机', group: { name: 'ch.sdk模块', id: 'sdk', displayOrder: 0 } })
  77. get is_local() { return this._is_local[this._pid] }
  78. set is_local(val) { this._is_local[this._pid] = val; }
  79. //
  80. @property({ type: Enum(chsdk.serverType), displayName: '服务器地址', visible: function () { return !this.is_local; }, group: { name: 'ch.sdk模块', id: 'sdk', displayOrder: 0 }, tooltip: "local 局域网 \n dev 测试服 \n online 正式服" })
  81. get server() { return this._server[this._pid]; }
  82. set server(val) { this._server[this._pid] = val; }
  83. @property({ displayName: '本地服测试ip', visible: function () { return this.server == chsdk.serverType.test }, group: { name: 'ch.sdk模块', id: 'sdk', displayOrder: 0 } })
  84. get serverIP() { return this._serverIP[this._pid]; }
  85. set serverIP(val) { this._serverIP[this._pid] = val; }
  86. @property({ type: Enum(chsdk.reportType), displayName: '上报类型', group: { name: '基本参数', id: 'sdk', displayOrder: 0 }, tooltip: "off 不上报 \n ch 使用ch服务器(需要在ch后台配置事件) \n platform 使用微信/抖音平台(需要在平台后台设置事件)\nch__platflorm所有平台自定义事件都上报" })
  87. get report() { return this._report[this._pid]; }
  88. set report(val) { this._report[this._pid] = val; }
  89. @property({ type: Enum(chsdk.loglevel), displayName: '日志等级', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
  90. get log() { return this._log[this._pid]; }
  91. set log(val) { this._log[this._pid] = val; }
  92. //
  93. @property({ displayName: '抖音奖励广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
  94. get ttad() { return this._ttad[this._pid]; }
  95. set ttad(val) { this._ttad[this._pid] = val; }
  96. @property({ displayName: '抖音插屏广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
  97. get ttiad() { return this._ttiad[this._pid]; }
  98. set ttiad(val) { this._ttiad[this._pid] = val; }
  99. @property({ displayName: '抖音订阅id(,隔开)', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
  100. get tttmpid() { return this._tttmpid[this._pid]; }
  101. set tttmpid(val) { this._tttmpid[this._pid] = val; }
  102. @property({ displayName: '微信奖励广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
  103. get wxad() { return this._wxad[this._pid]; }
  104. set wxad(val) { this._wxad[this._pid] = val; }
  105. @property({ displayName: '微信插屏广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
  106. get wxiad() { return this._wxiad[this._pid]; }
  107. set wxiad(val) { this._wxiad[this._pid] = val; }
  108. @property({ displayName: '微信订阅id(,隔开)', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
  109. get wxtmpid() { return this._wxtmpid[this._pid]; }
  110. set wxtmpid(val) { this._wxtmpid[this._pid] = val; }
  111. //
  112. @property({ visible: false })
  113. private _use_ch_audio: boolean = false;
  114. @property({ type: Enum(loadType), visible: false })
  115. private _ch_audio_type: loadType = loadType.bundle;
  116. @property({ displayName: '是否使用ch.audio模块', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 } })
  117. get use_ch_audio() { return this._use_ch_audio; }
  118. set use_ch_audio(val) { this._use_ch_audio = val; }
  119. @property({ type: Enum(loadType), displayName: '音频资源加载模式', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 }, visible: function () { return this._use_ch_audio; } })
  120. get sount_load_type() { return this._ch_audio_type; }
  121. set sount_load_type(val) { this._ch_audio_type = val; }
  122. @property({ displayName: '音频资源默认bundle名', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.bundle } })
  123. private sound_bundle: string = 'res';
  124. @property({ displayName: '音频资源远程地址', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.remote } })
  125. private sound_url: string = '';
  126. protected onLoad(): void {
  127. console.log('包Id:' + this.pid, '游戏名:' + this.gname, '游戏Id:' + this.gid, '单机:' + this.is_local, '服务器:' + chsdk.serverType[this.server]);
  128. }
  129. /**初始化*/
  130. async init(): Promise<boolean> {
  131. chsdk.setConf(chsdk.pf.tt, { adUnitId: this.ttad, multiton: false, inster_unitId: this.ttiad, tmplIds: this.tttmpid ? this.tttmpid.split(',') : null });
  132. chsdk.setConf(chsdk.pf.wx, { adUnitId: this.wxad, multiton: false, inster_unitId: this.wxiad, tmplIds: this.wxtmpid ? this.wxtmpid.split(',') : null });
  133. let ret = await chsdk.init_inside(this.gid, this.log, this.server == chsdk.serverType.test ? this.serverIP : this.server, this.is_local, this.report);
  134. if (ret.code == chsdk.code.success) {
  135. if (this.use_ch_audio) {
  136. ch.audio.init(this.sount_load_type, this.sound_bundle, this.sound_url);
  137. }
  138. if (chsdk.get_pf() === chsdk.pf.web) {
  139. const sdk = new Node();
  140. sdk.name = '__ch_sdk__';
  141. sdk.addComponent(ch_sdk_comp);
  142. director.getScene().addChild(sdk);
  143. director.addPersistRootNode(sdk);
  144. }
  145. return true;
  146. } else {
  147. chsdk.log.error(ret.err);
  148. return false;
  149. }
  150. }
  151. /**初化始并在遇到问题时以单机模式进入
  152. * @param try_count 重试次数
  153. * @param try_interval 重试间隔时间(MS)
  154. */
  155. async try_init(try_count: number = 5, try_interval: number = 2000): Promise<void> {
  156. let ret = await this.init();
  157. if (ret) return;
  158. try_count--;
  159. if (try_count < 0) {
  160. this.is_local = true;
  161. await this.init();
  162. return;
  163. } else {
  164. await new Promise(resolve => setTimeout(resolve, try_interval));
  165. return this.try_init(try_count, try_interval);
  166. }
  167. }
  168. }