ch_start.ts 11 KB

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