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, inviteNode: cc.Node, messageListNode: cc.Node, bottomTabbar: cc.Node, commonNode: cc.Node, sliderNode: cc.Node, }, // LIFE-CYCLE CALLBACKS: onLoad () { GameGlobal.isShowBar = false; this._isTimer = false; this.winSize = cc.view.getVisibleSize(); GameGlobal.winSize = this.winSize; // 创建一个事件监听实例, 用来实现跨节点监听事件 GameEvent.init(); this._setEventListener(); /// 初始化广告W6Lcux7ArPGCBpnWuAyEVK48PrrYX7xWBA if (window.tt != undefined) { GameGlobal._adVideoState = 1; } else if (CC_WECHATGAME) { GameGlobal._adVideo = new ADVideo(); GameGlobal._adVideo.wxCreateADVideo(); } this._initSocketMng(); this._getInformation(); if (window.tt != undefined) { } else 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.noticeDiamond !== undefined && GameGlobal.noticeDiamond > 0) { let showText = `钻石 x ${GameGlobal.noticeDiamond}`; AlertManager.showActGiftAlert('diamond', showText); } } 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); 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; 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); }, _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/connect'; var socketUrl = GameGlobal.debug ? testUrl : pUrl; let ttUrl = 'wss://headlines-message-taptap.duowan.com/connect'; // let socketUrl = 'ws://172.16.15.196:9099/connect'; if (CC_QQPLAY) { socketUrl = GameGlobal.debug ? testUrl : qqUrl; } else if (window.tt != undefined) { socketUrl = GameGlobal.debug ? testUrl : ttUrl; } 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 && 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 && 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(); } } //// 如果是取消红点的类型 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 = GameGlobal._timeInformations.length; if (informationsLength > 0) { for(let i = 0; i < informationsLength; ++i) { let information = GameGlobal._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; GameGlobal._upBuildingInfo = respondData.upBuildingInfo; GameGlobal._upRoomInfo = respondData.upRoomInfo; GameGlobal._buyStarInfo = respondData.buyStarInfo; 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; } GameGlobal.isShowBar = true; let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0)); this.sidebar.runAction(action); this.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot(); //是否已经领取过神秘礼物 if (!GameGlobal.appletAward && CC_WECHATGAME && window.tt == undefined) { 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 (GameGlobal.isShowBar) { return; } GameGlobal.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 (GameGlobal.shareType == ShareAction.INVITE_FRIEND) { let shareUid = GameGlobal.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) {}, });