2143bcf1-8f80-4035-a6ce-c3ec84d386bd.js 3.8 KB
"use strict";
cc._RF.push(module, '2143bzxj4BANabOw+yE04a9', 'GameCenter');
// script/cooperation/script/GameCenter.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const simba_sdk_1 = require("simba-sdk");
const GameConfig_1 = require("../../GameConfig");
const EventCenter_1 = require("../event/EventCenter");
const EventKit_1 = require("../event/EventKit");
const CooperationDataCenter_1 = require("./CooperationDataCenter");
/**游戏中心 */
let GameCenter = /** @class */ (() => {
    class GameCenter {
        constructor() {
            /**是否处于正式环境[控制访问oss地址] */
            this._isRelease = false;
            this._disposable = null;
            this._quickStartGameAppID = "";
            /**响应拉取游戏中心配置文件事件监听 */
            this.onRequestCooperationData = (release) => {
                this._isRelease = release;
                CooperationDataCenter_1.default.getInstance().requestCooperationDataFromOSS(this._isRelease);
            };
            /**快速开始小游戏 */
            this.onQuickStartSubGame = (isRandom) => {
                let list = CooperationDataCenter_1.default.getInstance().getCooperationItemDataModelList();
                if (!list || list.length === 0) {
                    return;
                }
                let appId = "";
                if (isRandom) {
                    appId = list[Math.floor(Math.random() * list.length)].appId;
                }
                else {
                    appId = "" === this._quickStartGameAppID ? list[0].appId : this._quickStartGameAppID;
                }
                this._quickStartGameAppID = appId;
                this.openSubProgram(this._quickStartGameAppID);
            };
            this.onOpenSubGameByAppId = (appId) => {
                this._quickStartGameAppID = appId;
                // this.openSubProgram(appId);
                simba_sdk_1.SDK.openProgramV2(appId);
            };
        }
        /**游戏中心单例对象 */
        static getInstance() {
            if (!GameCenter._instance) {
                GameCenter._instance = new GameCenter();
            }
            return GameCenter._instance;
        }
        /**初始化 */
        init() {
            this.registerEventListener();
        }
        /**注册事件监听 */
        registerEventListener() {
            if (this._disposable) {
                this._disposable.dispose();
            }
            this._disposable = new EventKit_1.CompositeDisposable;
            this._disposable.add(EventCenter_1.EventCenter.REQUEST_COOPERATION_DATA_FROM_OSS.on(this.onRequestCooperationData));
            this._disposable.add(EventCenter_1.EventCenter.QUICK_START_SUB_GAME.on(this.onQuickStartSubGame));
            this._disposable.add(EventCenter_1.EventCenter.OPEN_SUBGAME_BY_APPID.on(this.onOpenSubGameByAppId));
        }
        /**获取是否是正式环境 */
        getIsRelease() {
            return this._isRelease;
        }
        /**打开微信小游戏 */
        openSubProgram(appId, extraData) {
            return new Promise((resolve, reject) => {
                if (GameConfig_1.channel != "dummy") {
                    wx.navigateToMiniProgram({
                        appId, extraData,
                        envVersion: 'release',
                        success: () => {
                            resolve();
                        },
                        fail: (res) => {
                            reject(res);
                        }
                    });
                }
                else {
                    console.log('Open other MiniProgram.');
                }
            });
        }
    }
    /**单例对象 */
    GameCenter._instance = null;
    return GameCenter;
})();
exports.default = GameCenter;

cc._RF.pop();