ch_start.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { _decorator, CCInteger, CCString, 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. id10 = 10,
  19. id11 = 11,
  20. id12 = 12,
  21. id13 = 13,
  22. id14 = 14,
  23. id16 = 16,
  24. id17 = 17,
  25. id18 = 18,
  26. id19 = 19,
  27. id20 = 20,
  28. }
  29. @ccclass('BannerAdConf')
  30. class BannerAdConf {
  31. @property({ displayName: "广告位id" })
  32. adUnitId: string = '';
  33. @property({ type: CCInteger, displayName: "刷新的间隔(S)", visible: function () { return this.adUnitId; } })
  34. adIntervals: number = 120;
  35. @property({ type: CCInteger, displayName: "左上角横坐标", visible: function () { return this.adUnitId; } })
  36. left: number = 10;
  37. @property({ type: CCInteger, displayName: "左上角纵坐标", visible: function () { return this.adUnitId; } })
  38. top: number = 80;
  39. @property({ type: CCInteger, displayName: "宽度", visible: function () { return this.adUnitId; } })
  40. width: number = 500;
  41. @property({ type: CCInteger, displayName: "高度", visible: function () { return this.adUnitId; } })
  42. height: number = 60;
  43. }
  44. @ccclass('PfConf')
  45. class PfConf {
  46. @property({ displayName: "奖励广告位id" })
  47. r_adUnitId: string = '';
  48. @property({ displayName: "插屏广告位id" })
  49. i_adUnitId: string = '';
  50. @property({ type: [BannerAdConf], displayName: "Banner广告位" })
  51. banners: BannerAdConf[] = [];
  52. @property({ type: [CCString], displayName: "订阅id" })
  53. subscriptionIds: string[] = [];
  54. }
  55. @ccclass('PackConf')
  56. class PackConf {
  57. //
  58. @property({ displayName: '游戏名', group: { name: '基本信息', id: 'base', displayOrder: 0 } })
  59. gname: string = '';
  60. @property({ displayName: '游戏id', group: { name: '基本信息', id: 'base', displayOrder: 0 } })
  61. gid: string = '';
  62. @property({ type: Enum(chsdk.reportType), displayName: '上报类型', group: { name: '基础配置', id: 'sdk', displayOrder: 0 }, tooltip: "off 不上报 \n ch 使用ch服务器(需要在ch后台配置事件) \n platform 使用微信/抖音平台(需要在平台后台设置事件)\nch__platflorm所有平台自定义事件都上报" })
  63. report: chsdk.reportType = chsdk.reportType.off;
  64. @property({ type: Enum(chsdk.loglevel), displayName: '日志等级', group: { name: '基础配置', id: 'sdk', displayOrder: 0 } })
  65. log: chsdk.loglevel = chsdk.loglevel.ALL;
  66. //
  67. @property({ type: PfConf, displayName: '抖音相关配置', group: { name: '平台配置', id: 'sdk', displayOrder: 0 } })
  68. tt_conf: PfConf = new PfConf();
  69. @property({ type: PfConf, displayName: '微信相关配置', group: { name: '平台配置', id: 'sdk', displayOrder: 0 } })
  70. wx_conf: PfConf = new PfConf();
  71. //
  72. @property({ displayName: '是否单机', group: { name: '网络配置', id: 'web', displayOrder: 1 } })
  73. is_local: boolean = false;
  74. @property({ type: Enum(chsdk.serverType), displayName: '服务器内置地址类型', visible: function () { return !this.is_local }, tooltip: "local 局域网 \n dev 测试服 \n online 正式服" })
  75. server: chsdk.serverType = chsdk.serverType.dev;
  76. @property({ displayName: '配置本地测试服务器地址(,隔开)', tooltip: ",隔开,前面的为sdk基础地址(带上V1),后面的为sdk上报地址\n为空使用sdk内置地址没", visible: function () { return !this.is_local && this.server == chsdk.serverType.test } })
  77. serverIP: string = 'http://192.168.1.120:8787/v1';
  78. @property({ displayName: '配置测试服务器地址(,隔开)', tooltip: ",隔开,前面的为sdk基础地址(带上V1),后面的为sdk上报地址\n为空使用sdk内置地址", visible: function () { return !this.is_local && this.server == chsdk.serverType.dev } })
  79. serverIP1: string = '';
  80. @property({ displayName: '配置正试服务器地址(,隔开)', tooltip: ",隔开,前面的为sdk基础地址(带上V1),后面的为sdk上报地址\n为空使用sdk内置地址", visible: function () { return !this.is_local && this.server == chsdk.serverType.online } })
  81. serverIP2: string = '';
  82. }
  83. @ccclass('ch_start')
  84. export class ch_start extends Component {
  85. @property({ type: [PackConf], visible: false })
  86. private _confs: PackConf[] = [];
  87. @property({ visible: false })
  88. private _pid: pack = pack.id0;
  89. @property({ type: Enum(pack), displayName: '包id', group: { name: '选包', id: 'pack', displayOrder: 0 } })
  90. get pid() { return this._pid; }
  91. set pid(val) { this._pid = val; }
  92. @property({ displayName: '包配置', group: { name: '选包', id: 'pack', displayOrder: 0 } })
  93. get conf() { return this._confs[this._pid] ?? new PackConf(); }
  94. set conf(val) { this._confs[this._pid] = val; }
  95. //联机游戏模块
  96. @property({ visible: false })
  97. private _use_ch_net: boolean = false;
  98. @property({ displayName: '是否使用ch.net模块', tooltip: "联机游戏基于chsdk的登录,如果sdk选单机将无法使用,不同的包共享此配置", visible: function () { return !this.conf.is_local }, group: { name: 'ch.net模块', id: 'net', displayOrder: 1 } })
  99. get use_ch_net() { return this._use_ch_net; }
  100. set use_ch_net(val) { this._use_ch_net = val; }
  101. @property({ displayName: '本地webscoket完整地址', tooltip: "为空使用配置测试地址转换", visible: function () { return !this.conf.is_local && this._use_ch_net && this.conf.server == chsdk.serverType.test; }, group: { name: 'ch.net模块', id: 'net', displayOrder: 1 } })
  102. wsIP: string = '';
  103. @property({ displayName: '测试服webscoket完整地址', tooltip: "为空使用sdk内置地址转换", visible: function () { return !this.conf.is_local && this._use_ch_net && this.conf.server == chsdk.serverType.dev; }, group: { name: 'ch.net模块', id: 'net', displayOrder: 1 } })
  104. wsIP1: string = '';
  105. @property({ displayName: '正式服webscoket完整地址', tooltip: "为空使用sdk内置地址转换", visible: function () { return !this.conf.is_local && this._use_ch_net && this.conf.server == chsdk.serverType.online; }, group: { name: 'ch.net模块', id: 'net', displayOrder: 1 } })
  106. wsIP2: string = '';
  107. //音频模块
  108. @property({ visible: false })
  109. private _use_ch_audio: boolean = false;
  110. @property({ type: Enum(loadType), visible: false })
  111. private _ch_audio_type: loadType = loadType.bundle;
  112. @property({ displayName: '是否使用ch.audio模块', tooltip: "不同的包共享此配置", group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 } })
  113. get use_ch_audio() { return this._use_ch_audio; }
  114. set use_ch_audio(val) { this._use_ch_audio = val; }
  115. @property({ type: Enum(loadType), displayName: '音频资源加载模式', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 }, visible: function () { return this._use_ch_audio; } })
  116. get sount_load_type() { return this._ch_audio_type; }
  117. set sount_load_type(val) { this._ch_audio_type = val; }
  118. @property({ displayName: '音频资源默认bundle名', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.bundle } })
  119. private sound_bundle: string = 'res';
  120. @property({ displayName: '音频资源远程地址', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.remote } })
  121. private sound_url: string = '';
  122. //-------------------------------------------
  123. private _real_server: string = '';
  124. private _real_ws: string = '';
  125. private _ready: boolean = false;
  126. protected onLoad(): void {
  127. }
  128. private ready(): void {
  129. if (this.conf.server === chsdk.serverType.test) {
  130. this._real_server = this.conf.serverIP;
  131. this._real_ws = this.wsIP;
  132. } else if (this.conf.server === chsdk.serverType.dev) {
  133. this._real_server = this.conf.serverIP1;
  134. this._real_ws = this.wsIP1;
  135. } else if (this.conf.server === chsdk.serverType.online) {
  136. this._real_server = this.conf.serverIP2;
  137. this._real_ws = this.wsIP2;
  138. }
  139. console.log('包Id:' + this.pid,
  140. '游戏名:' + this.conf.gname,
  141. '游戏Id:' + this.conf.gid,
  142. '单机:' + this.conf.is_local,
  143. '服务器:' + chsdk.serverType[this.conf.server] + '-- ' + this._real_server + '-- ' + this._real_ws
  144. );
  145. }
  146. private getPfConf(conf: PfConf): chsdk.pf_conf {
  147. const c = {
  148. adUnitId: conf.r_adUnitId, multiton: false,
  149. inster_unitId: conf.i_adUnitId,
  150. tmplIds: conf.subscriptionIds.length > 0 ? conf.subscriptionIds : null,
  151. bannerAds: []
  152. }
  153. for (let i = 0; i < conf.banners.length; i++) {
  154. const banner = conf.banners[i];
  155. c.bannerAds.push({
  156. adUnitId: banner.adUnitId,
  157. adIntervals: banner.adIntervals,
  158. style: {
  159. left: banner.left,
  160. top: banner.top,
  161. width: banner.width,
  162. height: banner.height,
  163. }
  164. });
  165. }
  166. return c;
  167. }
  168. /**初始化*/
  169. async init(): Promise<boolean> {
  170. if (!this._ready) {
  171. this._ready = true;
  172. this.ready();
  173. }
  174. chsdk.setConf(chsdk.pf.tt, this.getPfConf(this.conf.tt_conf));
  175. chsdk.setConf(chsdk.pf.wx, this.getPfConf(this.conf.wx_conf));
  176. let ret = await chsdk.init_inside(this.conf.gid, this.conf.log, this._real_server ? this._real_server : this.conf.server, this.conf.is_local, this.conf.report);
  177. if (ret.code == chsdk.code.success) {
  178. if (this.use_ch_audio) {
  179. ch.audio.init(this.sount_load_type, this.sound_bundle, this.sound_url);
  180. }
  181. if (this.use_ch_net) {
  182. if (!this._real_ws) {
  183. const base = chsdk.getUrl('/handle');
  184. if (this.conf.server === chsdk.serverType.test) {
  185. this._real_ws = base;
  186. } else {
  187. const url = base.replace(base.split(':')[0], 'wss');
  188. this._real_ws = url;
  189. }
  190. }
  191. ch.wsurl = this._real_ws;
  192. }
  193. if (chsdk.get_pf() === chsdk.pf.web) {
  194. const sdk = new Node();
  195. sdk.name = '__ch_sdk__';
  196. sdk.addComponent(ch_sdk_comp);
  197. director.getScene().addChild(sdk);
  198. director.addPersistRootNode(sdk);
  199. }
  200. return true;
  201. } else {
  202. chsdk.log.error(ret.err);
  203. return false;
  204. }
  205. }
  206. /**初化始并在遇到问题时以单机模式进入
  207. * @param try_count 重试次数
  208. * @param try_interval 重试间隔时间(MS)
  209. */
  210. async try_init(try_count: number = 5, try_interval: number = 2000): Promise<void> {
  211. let ret = await this.init();
  212. if (ret) return;
  213. try_count--;
  214. if (try_count < 0) {
  215. this.conf.is_local = true;
  216. await this.init();
  217. return;
  218. } else {
  219. await new Promise(resolve => setTimeout(resolve, try_interval));
  220. return this.try_init(try_count, try_interval);
  221. }
  222. }
  223. }