ch_start_pack.ts 8.3 KB

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