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"); const ADVideo = require('../utils/ADVideo'); 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, moreNode: cc.Node, luckyBagButton: cc.Button, clickAddMoney: cc.Prefab, additionTipsNode: cc.Node, homeGuide: cc.Node, // storeNode: cc.Node, // drawNode: cc.Node, messageListNode: cc.Node, bottomTabbar: cc.Node, commonNode: cc.Node, sliderNode: cc.Node, }, // LIFE-CYCLE CALLBACKS: onLoad () { this.isShowBar = false; this._isTimer = false; this.winSize = cc.view.getVisibleSize(); Global.winSize = this.winSize; // 创建一个事件监听实例, 用来实现跨节点监听事件 GameEvent.init(); this._setEventListener(); /// 初始化广告W6Lcux7ArPGCBpnWuAyEVK48PrrYX7xWBA if (CC_WECHATGAME) { Global._adVideo = new ADVideo(); Global._adVideo.wxCreateADVideo(); } this._initSocketMng(); this._getInformation(); if (window.wx != undefined) { Global.wechatScoreKey = Global.debug ? 'buildingLevel_test' : 'buildingLevel'; window.wx.postMessage({ messageType: 5, key1: Global.debug }); window.wx.postMessage({ messageType: 6, winSize: Global.winSize }); } // 引导界面 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(Global.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 (Global.shareType == ShareAction.SHOW_GROUP_RANK && Global.shareTicket.length > 0) { if (this.homeGuide.guideState.state1.pass) { GameEvent.fire(GameNotificationKey.GameShowGroupRank); } } if (Global.messageNotice !== undefined && Global.messageNotice.length > 0) { if (Global.noticeDiamond !== undefined && Global.noticeDiamond > 0) { let showText = `钻石 x ${Global.noticeDiamond}`; AlertManager.showActGiftAlert('diamond', showText); } AlertManager.showNoticeAlert(Global.messageNotice); } if (Global.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.GameShowAdditionTips, this, this.showAdditionTips); GameEvent.on(GameNotificationKey.GameShowGroupRank, this, this.showGroupRank); //// 重连socket GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, () => { // debugger; Global._wxSocket._reconnectTimes = 0; Global._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); }, _initSocketMng() { let testUrl = 'wss://test-message-taptap.duowan.com/connect'; let pUrl = 'wss://message-taptap.duowan.com/connect'; // let testUrl = 'wss://new-message-taptap.duowan.com/connect'; let socketUrl = Global.debug ? testUrl : pUrl; // let socketUrl = 'ws://172.16.15.196:9099/connect'; let ws = new WsManager(socketUrl, { binaryType: 'arraybuffer', autoConnect: true, // 自动连接 reconnection: true, // 断开自动重连 reconnectionDelay: 3000, // 重连间隔时间,单位毫秒 reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity }); Global._wxSocket = ws; ws.on('open', (res) => { console.log('socket 打开'); }); ws.on('message', (data) => { let typeStr = typeof data; if (typeStr !== 'string' || (data.indexOf('type') === -1 && data.indexOf('cancelType') === -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 && Global._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 (Global._redTypes.indexOf(type) != -1) { isContain = true; } else { Global._redTypes.push(type); isContain = false; } } if (isContain == false) { GameEvent.fire(GameNotificationKey.GameRedDotUpdate); // this.handelShowRedDot(); } } //// 如果是取消红点的类型 if (data.cancelType !== undefined && data.cancelType > 0) { TapTapTool.removeRedDot(data.cancelType); } }); 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() { Global._redTypes = []; Global._timeInformations = []; Global._fixInformations = []; Global._gold10 = {'n': 0, 'e': 0}; Global._buyStarGold = {'n': 0, 'e': 0}; this.getInformation().then((respondData) => { console.log(respondData); Global._gold10 = respondData.gold10; Global._buyStarGold = respondData.buyStarGold; Global._isSkillReset = respondData.isSkillReset; Global._upBuildingInfo = respondData.upBuildingInfo; Global._upRoomInfo = respondData.upRoomInfo; Global._buyStarInfo = respondData.buyStarInfo; if (respondData.types !== undefined) { Global._redTypes = respondData.types; /// 这个是直接返回的 没有推送 七天登录的红点 } if (TapTapTool.compare(Global.userData.goldObj, respondData.buyStarGold)) { Global._redTypes.push(GameRedDot.star); } GameEvent.fire(GameNotificationKey.GameRedDotUpdate); // this.handelShowRedDot(); //// 更新信息流数据 if (respondData.informations !== undefined) { Global._timeInformations = respondData.informations; GameEvent.fire(GameNotificationKey.GameUpdateMessageList, Global._timeInformations.length); } }).catch(({code, msg}) => { console.log(code, msg); }); }, /// 网络请求 getInformation() { return new Promise((resolve, reject) => { // 获取目标用户的建筑 HomeApi.getInformation((respondData) => { resolve(respondData); }, (code, msg) => { reject({code, msg}); }); }) }, 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.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot(); //是否已经领取过神秘礼物 if (!Global.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; }, 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.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot(); }, //处理邀请好友 processShareAction() { //邀请好友和好友助力的分享链接进入 if (Global.shareType == ShareAction.INVITE_FRIEND) { let shareUid = Global.shareUid; InviteApi.postInviteFriend(shareUid, () => { }); InviteApi.postFriendRewardAdd(shareUid, () => { }); } }, //显示红包特殊礼包奖励 handleShowApplet: _.debounce((event) => { AlertManager.showAppletAlert(); }, 1000, true), showGroupRank() { AlertManager.showRankAlert(2); }, showAdditionTips(text, type) { if (this.node.active) { this.additionTipsNode.show(text, type); } }, // update (dt) {}, });