import { _decorator, CCInteger, CCString, Component, director, Enum, Node } from 'cc'; import { ch } from '../ch'; import { loadType } from '../audio/audio'; import { ch_sdk_comp } from './ch_sdk_comp'; const { ccclass, property } = _decorator; /**包id*/ export enum pack { id0 = 0, id1 = 1, id2 = 2, id3 = 3, id4 = 4, id5 = 5, id6 = 6, id7 = 7, id8 = 8, id9 = 9, id10 = 10, id11 = 11, id12 = 12, id13 = 13, id14 = 14, id16 = 16, id17 = 17, id18 = 18, id19 = 19, id20 = 20, } @ccclass('BannerAdConf') class BannerAdConf { @property({ displayName: "广告位id" }) adUnitId: string = ''; @property({ type: CCInteger, displayName: "刷新的间隔(S)", visible: function () { return this.adUnitId; } }) adIntervals: number = 120; @property({ type: CCInteger, displayName: "左上角横坐标", visible: function () { return this.adUnitId; } }) left: number = 10; @property({ type: CCInteger, displayName: "左上角纵坐标", visible: function () { return this.adUnitId; } }) top: number = 80; @property({ type: CCInteger, displayName: "宽度", visible: function () { return this.adUnitId; } }) width: number = 500; @property({ type: CCInteger, displayName: "高度", visible: function () { return this.adUnitId; } }) height: number = 60; } @ccclass('PfConf') class PfConf { @property({ displayName: "奖励广告位id" }) r_adUnitId: string = ''; @property({ displayName: "插屏广告位id" }) i_adUnitId: string = ''; @property({ type: [BannerAdConf], displayName: "Banner广告位" }) banners: BannerAdConf[] = []; @property({ type: [CCString], displayName: "订阅id" }) subscriptionIds: string[] = []; } @ccclass('PackConf') class PackConf { // @property({ displayName: '游戏名', group: { name: '基本信息', id: 'base', displayOrder: 0 } }) gname: string = ''; @property({ displayName: '游戏id', group: { name: '基本信息', id: 'base', displayOrder: 0 } }) gid: string = ''; @property({ type: Enum(chsdk.reportType), displayName: '上报类型', group: { name: '基础配置', id: 'sdk', displayOrder: 0 }, tooltip: "off 不上报 \n ch 使用ch服务器(需要在ch后台配置事件) \n platform 使用微信/抖音平台(需要在平台后台设置事件)\nch__platflorm所有平台自定义事件都上报" }) report: chsdk.reportType = chsdk.reportType.off; @property({ type: Enum(chsdk.loglevel), displayName: '日志等级', group: { name: '基础配置', id: 'sdk', displayOrder: 0 } }) log: chsdk.loglevel = chsdk.loglevel.ALL; // @property({ type: PfConf, displayName: '抖音相关配置', group: { name: '平台配置', id: 'sdk', displayOrder: 0 } }) tt_conf: PfConf = new PfConf(); @property({ type: PfConf, displayName: '微信相关配置', group: { name: '平台配置', id: 'sdk', displayOrder: 0 } }) wx_conf: PfConf = new PfConf(); // @property({ displayName: '是否单机', group: { name: '网络配置', id: 'web', displayOrder: 1 } }) is_local: boolean = false; @property({ type: Enum(chsdk.serverType), displayName: '服务器内置地址类型', visible: function () { return !this.is_local }, tooltip: "local 局域网 \n dev 测试服 \n online 正式服" }) server: chsdk.serverType = chsdk.serverType.dev; @property({ displayName: '配置本地测试服务器地址(,隔开)', tooltip: ",隔开,前面的为sdk基础地址(带上V1),后面的为sdk上报地址\n为空使用sdk内置地址没", visible: function () { return !this.is_local && this.server == chsdk.serverType.test } }) serverIP: string = 'http://192.168.1.120:8787/v1'; @property({ displayName: '配置测试服务器地址(,隔开)', tooltip: ",隔开,前面的为sdk基础地址(带上V1),后面的为sdk上报地址\n为空使用sdk内置地址", visible: function () { return !this.is_local && this.server == chsdk.serverType.dev } }) serverIP1: string = ''; @property({ displayName: '配置正试服务器地址(,隔开)', tooltip: ",隔开,前面的为sdk基础地址(带上V1),后面的为sdk上报地址\n为空使用sdk内置地址", visible: function () { return !this.is_local && this.server == chsdk.serverType.online } }) serverIP2: string = ''; } @ccclass('ch_start') export class ch_start extends Component { @property({ type: [PackConf], visible: false }) private _confs: PackConf[] = []; @property({ visible: false }) private _pid: pack = pack.id0; @property({ type: Enum(pack), displayName: '包id', group: { name: '选包', id: 'pack', displayOrder: 0 } }) get pid() { return this._pid; } set pid(val) { this._pid = val; } @property({ displayName: '包配置', group: { name: '选包', id: 'pack', displayOrder: 0 } }) get conf() { return this._confs[this._pid] ?? new PackConf(); } set conf(val) { this._confs[this._pid] = val; } //联机游戏模块 @property({ visible: false }) private _use_ch_net: boolean = false; @property({ displayName: '是否使用ch.net模块', tooltip: "联机游戏基于chsdk的登录,如果sdk选单机将无法使用,不同的包共享此配置", visible: function () { return !this.conf.is_local }, group: { name: 'ch.net模块', id: 'net', displayOrder: 1 } }) get use_ch_net() { return this._use_ch_net; } set use_ch_net(val) { this._use_ch_net = val; } @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 } }) wsIP: string = ''; @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 } }) wsIP1: string = ''; @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 } }) wsIP2: string = ''; //音频模块 @property({ visible: false }) private _use_ch_audio: boolean = false; @property({ type: Enum(loadType), visible: false }) private _ch_audio_type: loadType = loadType.bundle; @property({ displayName: '是否使用ch.audio模块', tooltip: "不同的包共享此配置", group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 } }) get use_ch_audio() { return this._use_ch_audio; } set use_ch_audio(val) { this._use_ch_audio = val; } @property({ type: Enum(loadType), displayName: '音频资源加载模式', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 }, visible: function () { return this._use_ch_audio; } }) get sount_load_type() { return this._ch_audio_type; } set sount_load_type(val) { this._ch_audio_type = val; } @property({ displayName: '音频资源默认bundle名', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.bundle } }) private sound_bundle: string = 'res'; @property({ displayName: '音频资源远程地址', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 2 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.remote } }) private sound_url: string = ''; //------------------------------------------- private _real_server: string = ''; private _real_ws: string = ''; private _ready: boolean = false; protected onLoad(): void { } private ready(): void { if (this.conf.server === chsdk.serverType.test) { this._real_server = this.conf.serverIP; this._real_ws = this.wsIP; } else if (this.conf.server === chsdk.serverType.dev) { this._real_server = this.conf.serverIP1; this._real_ws = this.wsIP1; } else if (this.conf.server === chsdk.serverType.online) { this._real_server = this.conf.serverIP2; this._real_ws = this.wsIP2; } console.log('包Id:' + this.pid, '游戏名:' + this.conf.gname, '游戏Id:' + this.conf.gid, '单机:' + this.conf.is_local, '服务器:' + chsdk.serverType[this.conf.server] + '-- ' + this._real_server + '-- ' + this._real_ws ); } private getPfConf(conf: PfConf): chsdk.pf_conf { const c = { adUnitId: conf.r_adUnitId, multiton: false, inster_unitId: conf.i_adUnitId, tmplIds: conf.subscriptionIds.length > 0 ? conf.subscriptionIds : null, bannerAds: [] } for (let i = 0; i < conf.banners.length; i++) { const banner = conf.banners[i]; c.bannerAds.push({ adUnitId: banner.adUnitId, adIntervals: banner.adIntervals, style: { left: banner.left, top: banner.top, width: banner.width, height: banner.height, } }); } return c; } /**初始化*/ async init(): Promise { if (!this._ready) { this._ready = true; this.ready(); } chsdk.setConf(chsdk.pf.tt, this.getPfConf(this.conf.tt_conf)); chsdk.setConf(chsdk.pf.wx, this.getPfConf(this.conf.wx_conf)); 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); if (ret.code == chsdk.code.success) { if (this.use_ch_audio) { ch.audio.init(this.sount_load_type, this.sound_bundle, this.sound_url); } if (this.use_ch_net) { if (!this._real_ws) { const base = chsdk.getUrl('/handle'); if (this.conf.server === chsdk.serverType.test) { this._real_ws = base; } else { const url = base.replace(base.split(':')[0], 'wss'); this._real_ws = url; } } ch.wsurl = this._real_ws; } if (chsdk.get_pf() === chsdk.pf.web) { const sdk = new Node(); sdk.name = '__ch_sdk__'; sdk.addComponent(ch_sdk_comp); director.getScene().addChild(sdk); director.addPersistRootNode(sdk); } return true; } else { chsdk.log.error(ret.err); return false; } } /**初化始并在遇到问题时以单机模式进入 * @param try_count 重试次数 * @param try_interval 重试间隔时间(MS) */ async try_init(try_count: number = 5, try_interval: number = 2000): Promise { let ret = await this.init(); if (ret) return; try_count--; if (try_count < 0) { this.conf.is_local = true; await this.init(); return; } else { await new Promise(resolve => setTimeout(resolve, try_interval)); return this.try_init(try_count, try_interval); } } }