const HomeApi = require("../net/HomeApi"); const InviteApi = require("../net/InviteApi"); const AlertManager = require('../utils/AlertManager'); const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey; const ShareAction = require('../utils/ShareAction'); const GameModule = require("../utils/GameModule"); const WsManager = require('../net/Ws'); const GameRedDot = require('../utils/GameEnum').GameRedDot; const TapTapTool = require("../utils/TapTapTool"); // import DwSdk from "../duowansdk/DwSdk"; // let debug = false; // var objSdk = new DwSdk("点点大明星", debug); // // 注册sdk的update事件 // cc.director.on(cc.Director.EVENT_AFTER_DRAW, objSdk.update); cc.Class({ extends: cc.Component, properties: { levelHomePrefab: cc.Prefab, myInfoTop: cc.Node, sidebar: cc.Node, luckyBagButton: cc.Button, loginRewardButton: cc.Button, clickAddMoney: cc.Prefab, additionTipsNode: cc.Node, homeGuide: cc.Node, storeNode: cc.Node, drawNode: cc.Node, inviteNode: cc.Node, messageListNode: cc.Node, taskRedNode: cc.Node, storeRedNode: cc.Node, drawRedNode: cc.Node, inviteFriendRedNode: cc.Node, }, // LIFE-CYCLE CALLBACKS: onLoad () { this.isShowBar = false; this._isTimer = false; this.winSize = cc.view.getVisibleSize(); GameGlobal.winSize = this.winSize; // 创建一个事件监听实例, 用来实现跨节点监听事件 GameEvent.init(); this._setEventListener(); this._initSocketMng(); this._getInformation(); if (CC_WECHATGAME) { GameGlobal.wechatScoreKey = GameGlobal.debug ? 'buildingLevel_test' : 'buildingLevel'; window.wx.postMessage({ messageType: 5, key1: GameGlobal.debug }); window.wx.postMessage({ messageType: 6, winSize: GameGlobal.winSize }); } else if (CC_QQPLAY) { if (window.GameStatusInfo) { GameStatusInfo.startMs = ((new Date()).getTime()).toString(); } } // 引导界面 this.homeGuide.zIndex = 200; GameModule.homeGuide = this.homeGuide; this.homeGuide = this.homeGuide.getComponent('HomeGuide'); this.homeGuide.init(); cc.debug.setDisplayStats(false); //建筑展示 let levelHome = cc.instantiate(this.levelHomePrefab); levelHome = levelHome.getComponent('LevelHome'); levelHome.init(GameGlobal.user.uid); this.levelHome = levelHome; this.levelHome.node.active = true; this.XHeight = 1624; console.log('game ======================= ' + this.winSize); if (this.winSize.height >= this.XHeight) { this.myInfoTop.height = 200; this.sidebar.getComponent(cc.Widget).top = 180; this.messageListNode.getComponent(cc.Widget).top = 250 + 30; } this.additionTipsNode = this.additionTipsNode.getComponent('AdditionTips'); if (GameGlobal.shareType == ShareAction.SHOW_GROUP_RANK && GameGlobal.shareTicket.length > 0) { if (this.homeGuide.guideState.state1.pass) { GameEvent.fire(GameNotificationKey.GameShowGroupRank); } } if (GameGlobal.isCheck) { this.storeNode.active = false; this.drawNode.active = false; } if (GameGlobal.messageNotice !== undefined && GameGlobal.messageNotice.length > 0) { AlertManager.showNoticeAlert(GameGlobal.messageNotice); } if (GameGlobal.shareUid != -1) { this.processShareAction(); } }, start () { if (!this.homeGuide.guideState.state1.pass) { // 触发引导系统state1状态 GameModule.homeGuide.getComponent('HomeGuide').handleState('state1'); } GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state4', 'state5'); GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state6', 'state7'); // GameModule.homeGuide.getComponent('HomeGuide').handleState('state31'); GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state29', 'state31'); GameModule.homeGuide.on('Fire_state29', this.finishState29, this); GameModule.homeGuide.on('Fire_state31', this.handleQuestPopup, this); if (this.homeGuide.guideState.state7.pass || this.homeGuide.guideState.state11.pass) { GameModule.homeGuide.getComponent('HomeGuide').handleState('state13'); GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false); if (this.homeGuide.guideState.state13.pass) { GameModule.homeGuide.getComponent('HomeGuide').handleState('state15'); GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false); } } this._showSidebarUI(); }, update (dt) { if (GameModule.skill != null && GameModule.skill != undefined && GameModule.skill.isUsingSkill3) { GameEvent.fire(GameNotificationKey.PlaySuccessAnimation); } }, _setEventListener() { GameEvent.on(GameNotificationKey.GameRedDotUpdate, this, this.handelShowRedDot); GameEvent.on(GameNotificationKey.GameShowAdditionTips, this, this.showAdditionTips); GameEvent.on(GameNotificationKey.GameShowGroupRank, this, this.showGroupRank); //// 重连socket GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, () => { // debugger; GameGlobal._wxSocket._reconnectTimes = 0; GameGlobal._wxSocket.connect(); // if (Global._wxSocket.readyState != 'open') { // } // if (Global._wxSocket.readyState == 'open') { // } // this._initSocketMng(); }); /// 通过分享添加好友 GameEvent.on(GameNotificationKey.ProcessShareAction, this, this.processShareAction); //获取我的小程序奖励 GameEvent.on('Gain_My_Applet', this, this._hideLuckyBag); //7天登录奖励 GameEvent.on('Gain_Login_Reward', this, this._hideLoginReward); }, _initSocketMng() { let testUrl = 'wss://test-message-taptap.duowan.com/connect'; let pUrl = 'wss://message-taptap.duowan.com/connect'; let qqUrl = 'wss://qq-message-taptap.duowan.com'; var socketUrl = GameGlobal.debug ? testUrl : pUrl; // let socketUrl = 'ws://172.16.15.196:9099/connect'; if (CC_QQPLAY) { socketUrl = GameGlobal.debug ? testUrl : qqUrl; } let ws = new WsManager(socketUrl, { binaryType: 'arraybuffer', autoConnect: true, // 自动连接 reconnection: true, // 断开自动重连 reconnectionDelay: 3000, // 重连间隔时间,单位毫秒 reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity }); GameGlobal._wxSocket = ws; ws.on('open', (res) => { console.log('socket 打开'); }); ws.on('message', (data) => { let typeStr = typeof data; if (typeStr !== 'string' || data.indexOf('type') == -1) { return; } data = JSON.parse(data); if (data.type != undefined) { // Global._redTypes = data.type; /// 商品购买的通知 第一次的socket不管的啦因为接口已经处理了 if (data.type.indexOf(1) != -1 && data.userShops.length > 0 && GameGlobal._socketCount > 1) { let userShops = data.userShops; for (let i = 0; i < data.userShops.length; ++ i) { let shopData = userShops[i]; //// 处理商品信息流重复的问题 this.handelSocketShop(shopData); GameModule.shop.handelShop(shopData); GameModule.shop.updateTimeSchedule(); /// 处理信息流信息 GameModule.shop.handleShpDataToMessageList(shopData); /// 增加商品钻石 if (shopData.diamond !== undefined && shopData.diamond > 0) { GameModule.userInfo.diamond += shopData.diamond; } //// 购买的是礼包 if (shopData.type == 4) { //// 更新购买明星数量 GameEvent.fire(GameNotificationKey.GameUpdateStarContentBuyGold); if (shopData.starId != undefined && shopData.roomId != undefined) { GameEvent.fire(GameNotificationKey.StarEnterRoom, shopData.starId, shopData.roomId); } } } return; } //// 下面这段代码只是去重,不要重复的更新 let isContain = false; for (let i = 0; i < data.type.length; ++ i) { /// 如果包含它就什么都不管 let type = data.type[i]; if (GameGlobal._redTypes.indexOf(type) != -1) { isContain = true; } else { GameGlobal._redTypes.push(type); isContain = false; } } if (isContain == false) { GameEvent.fire(GameNotificationKey.GameRedDotUpdate); // this.handelShowRedDot(); } } }); ws.on('close', () => { console.log('socket 关闭'); }); ws.on('error', () => { console.log('sockete 错误'); }); }, /// 处理socket推过来的商品是否跟已经拥有的商品重复,如果重复,那么直接更新cd时间 /// 需要更新的时间包括 handelSocketShop(shopData) { /// 更新信息流时间 let informationsLength = Global._timeInformations.length; if (informationsLength > 0) { for(let i = 0; i < informationsLength; ++i) { let information = Global._timeInformations[i]; /// 如果是商品 并且已经拥有过的话 if (information.type == 1 && information.sId == shopData.shopId) { information.cdTime = shopData.cdTime; break; } } } /// 更新商品使用时间 }, /// 获取信息流 _getInformation() { GameGlobal._redTypes = []; GameGlobal._timeInformations = []; GameGlobal._fixInformations = []; GameGlobal._gold10 = {'n': 0, 'e': 0}; GameGlobal._buyStarGold = {'n': 0, 'e': 0}; this.getInformation((respondData) => { console.log(respondData); GameGlobal._gold10 = respondData.gold10; GameGlobal._buyStarGold = respondData.buyStarGold; GameGlobal._isSkillReset = respondData.isSkillReset; if (respondData.types !== undefined) { GameGlobal._redTypes = respondData.types; } if (TapTapTool.compare(GameGlobal.userData.goldObj, respondData.buyStarGold)) { GameGlobal._redTypes.push(GameRedDot.star); } GameEvent.fire(GameNotificationKey.GameRedDotUpdate); // this.handelShowRedDot(); //// 更新信息流数据 if (respondData.informations !== undefined) { GameGlobal._timeInformations = respondData.informations; GameEvent.fire(GameNotificationKey.GameUpdateMessageList, GameGlobal._timeInformations.length); } }, ({code, msg}) => { console.log(code, msg); }); }, /// 网络请求 getInformation(success, fail) { // 获取目标用户的建筑 HomeApi.getInformation(success, fail); }, onDestroy() { GameEvent.off(GameNotificationKey.GameShowAdditionTips, this); GameEvent.off(GameNotificationKey.GameShowGroupRank, this); }, _showSidebarUI() { let isOk = false; //教程31还没有完成不显示 if (!this.homeGuide.guideState.state31.pass) { // 1. 总部大楼大于25级 // 2. 已拥有1个明星 let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25; let unLockStatus2 = GameModule.userInfo.buyStarCount > 0; if (unLockStatus1 && unLockStatus2) { isOk = true } } else { isOk = true; } if (!isOk) { return; } this.isShowBar = true; let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0)); this.sidebar.runAction(action); this.handelShowRedDot(); //是否已经领取过神秘礼物 if (!GameGlobal.appletAward && CC_WECHATGAME) { this.luckyBagButton.node.active = true; var offset = 0; if (this.winSize.height >= this.XHeight) { offset = 30; } let action2 = cc.moveBy(0.4, cc.v2(0, -this.luckyBagButton.node.height - offset)); this.luckyBagButton.node.runAction(action2); } else { this.luckyBagButton.node.active = false; } }, _hideLuckyBag() { this.luckyBagButton.node.active = false; }, _hideLoginReward() { }, finishState29() { if (this.isShowBar) { return; } this.isShowBar = true; let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0)); this.sidebar.runAction(action); this.handelShowRedDot(); }, handelShowRedDot() { if (GameGlobal._redTypes == null || GameGlobal._redTypes == undefined || GameGlobal._redTypes.length == 0) { this.taskRedNode.active = false; this.drawRedNode.active = false; this.storeRedNode.active = false; this.inviteFriendRedNode.active = false; return; } let redTypes = GameGlobal._redTypes; this.taskRedNode.active = (redTypes.indexOf(GameRedDot.mainTask) != -1 || redTypes.indexOf(GameRedDot.dayTask) != -1); this.drawRedNode.active = redTypes.indexOf(GameRedDot.draw) != -1; this.inviteFriendRedNode.active = redTypes.indexOf(GameRedDot.inviteFriend) != -1; if (this.isShowBar) { let isStoreRed = (redTypes.indexOf(GameRedDot.storeDiamond) != -1 || redTypes.indexOf(GameRedDot.storeCoin) != -1); this.storeRedNode.active = isStoreRed; if (isStoreRed) { if (this._isTimer === false) { this._isTimer = true; this._storeTimeCount = 0; this.storeRedNode.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.2, 3, 0), cc.moveBy(0.2, -3, 0)))); this.schedule(this.storeTimeAction, 6); } } else { if (this._isTimer == true) { this.unschedule(this.storeTimeAction, this); this._isTimer = false; } } } else { if (this._isTimer === true) { this.unschedule(this.storeTimeAction, this); this._isTimer = false; } this.storeRedNode.active = false; } }, /// 商城未读消息的显示 storeTimeAction() { this._storeTimeCount += 1; /// 第一个6秒的时候 if (this._storeTimeCount === 1) { this.storeRedNode.active = false; } else if (this._storeTimeCount === 6) { this.storeRedNode.active = true; this._storeTimeCount = 0; // this.storeRedNode.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.2, 3, 0), cc.moveBy(0.2, -3, 0)))); } }, //处理邀请好友 processShareAction() { if (GameGlobal.shareType == ShareAction.INVITE_FRIEND) { let shareUid = GameGlobal.shareUid; InviteApi.postInviteFriend(shareUid, () => { }); } }, // handleQuestPopup: _.debounce((event) => { AlertManager.showQuestPopup(); }, 1000, true), //显示抽奖界面 handleShowDraw: _.debounce((event) => { AlertManager.showDrawAlert(); }, 1000, true), //显示商城界面 handleShowStore: _.debounce((event) => { AlertManager.showStoreAlert(); }, 1000, true), //显示排行榜界面 handleShowRank: _.debounce((event) => { AlertManager.showRankAlert(); }, 1000, true), //显示红包特殊礼包奖励 handleShowApplet: _.debounce((event) => { AlertManager.showAppletAlert(); }, 1000, true), //显示邀请好友任务 handleShowInvite: _.debounce((event) => { AlertManager.showInviteAlert(); }, 1000, true), //显示邀请好友任务 handleShowLoginReward: _.debounce((event) => { AlertManager.showLoginRewardAlert(); }, 1000, true), showGroupRank() { AlertManager.showRankAlert(2); }, showAdditionTips(text, type) { if (this.node.active) { this.additionTipsNode.show(text, type); } }, // update (dt) {}, });