Game.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. const HomeApi = require("../net/HomeApi");
  2. const InviteApi = require("../net/InviteApi");
  3. const AlertManager = require('../utils/AlertManager');
  4. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  5. const ShareAction = require('../utils/ShareAction');
  6. const GameModule = require("../utils/GameModule");
  7. const WsManager = require('../net/Ws');
  8. const GameRedDot = require('../utils/GameEnum').GameRedDot;
  9. const TapTapTool = require("../utils/TapTapTool");
  10. const ADVideo = require('../utils/ADVideo');
  11. // import DwSdk from "../duowansdk/DwSdk";
  12. // let debug = false;
  13. // var objSdk = new DwSdk("点点大明星", debug);
  14. // // 注册sdk的update事件
  15. // cc.director.on(cc.Director.EVENT_AFTER_DRAW, objSdk.update);
  16. cc.Class({
  17. extends: cc.Component,
  18. properties: {
  19. levelHomePrefab: cc.Prefab,
  20. myInfoTop: cc.Node,
  21. sidebar: cc.Node,
  22. moreNode: cc.Node,
  23. luckyBagButton: cc.Button,
  24. clickAddMoney: cc.Prefab,
  25. additionTipsNode: cc.Node,
  26. homeGuide: cc.Node,
  27. // storeNode: cc.Node,
  28. // drawNode: cc.Node,
  29. inviteNode: cc.Node,
  30. messageListNode: cc.Node,
  31. bottomTabbar: cc.Node,
  32. commonNode: cc.Node,
  33. sliderNode: cc.Node,
  34. },
  35. // LIFE-CYCLE CALLBACKS:
  36. onLoad () {
  37. GameGlobal.isShowBar = false;
  38. this._isTimer = false;
  39. this.winSize = cc.view.getVisibleSize();
  40. GameGlobal.winSize = this.winSize;
  41. // 创建一个事件监听实例, 用来实现跨节点监听事件
  42. GameEvent.init();
  43. this._setEventListener();
  44. /// 初始化广告W6Lcux7ArPGCBpnWuAyEVK48PrrYX7xWBA
  45. if (window.tt != undefined) {
  46. GameGlobal._adVideoState = 1;
  47. } else if (CC_WECHATGAME) {
  48. GameGlobal._adVideo = new ADVideo();
  49. GameGlobal._adVideo.wxCreateADVideo();
  50. }
  51. this._initSocketMng();
  52. this._getInformation();
  53. if (window.tt != undefined) {
  54. } else if (CC_WECHATGAME) {
  55. GameGlobal.wechatScoreKey = GameGlobal.debug ? 'buildingLevel_test' : 'buildingLevel';
  56. window.wx.postMessage({
  57. messageType: 5,
  58. key1: GameGlobal.debug
  59. });
  60. window.wx.postMessage({
  61. messageType: 6,
  62. winSize: GameGlobal.winSize
  63. });
  64. } else if (CC_QQPLAY) {
  65. if (window.GameStatusInfo) {
  66. GameStatusInfo.startMs = ((new Date()).getTime()).toString();
  67. }
  68. }
  69. // 引导界面
  70. this.homeGuide.zIndex = 200;
  71. GameModule.homeGuide = this.homeGuide;
  72. this.homeGuide = this.homeGuide.getComponent('HomeGuide');
  73. this.homeGuide.init();
  74. cc.debug.setDisplayStats(false);
  75. //建筑展示
  76. let levelHome = cc.instantiate(this.levelHomePrefab);
  77. levelHome = levelHome.getComponent('LevelHome');
  78. levelHome.init(GameGlobal.user.uid);
  79. this.levelHome = levelHome;
  80. this.levelHome.node.active = true;
  81. this.XHeight = 1624;
  82. console.log('game ======================= ' + this.winSize);
  83. if (this.winSize.height >= this.XHeight) {
  84. this.myInfoTop.height = 200;
  85. this.sidebar.getComponent(cc.Widget).top = 180;
  86. this.messageListNode.getComponent(cc.Widget).top = 250 + 30;
  87. }
  88. this.additionTipsNode = this.additionTipsNode.getComponent('AdditionTips');
  89. if (GameGlobal.shareType == ShareAction.SHOW_GROUP_RANK && GameGlobal.shareTicket.length > 0) {
  90. if (this.homeGuide.guideState.state1.pass) {
  91. GameEvent.fire(GameNotificationKey.GameShowGroupRank);
  92. }
  93. }
  94. // if (GameGlobal.isCheck) {
  95. // this.storeNode.active = false;
  96. // this.drawNode.active = false;
  97. // }
  98. if (GameGlobal.messageNotice !== undefined && GameGlobal.messageNotice.length > 0) {
  99. AlertManager.showNoticeAlert(GameGlobal.messageNotice);
  100. if (GameGlobal.noticeDiamond !== undefined && GameGlobal.noticeDiamond > 0) {
  101. let showText = `钻石 x ${GameGlobal.noticeDiamond}`;
  102. AlertManager.showActGiftAlert('diamond', showText);
  103. }
  104. }
  105. if (GameGlobal.shareUid != -1) {
  106. this.processShareAction();
  107. }
  108. },
  109. start () {
  110. if (!this.homeGuide.guideState.state1.pass) {
  111. // 触发引导系统state1状态
  112. GameModule.homeGuide.getComponent('HomeGuide').handleState('state1');
  113. }
  114. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state4', 'state5');
  115. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state6', 'state7');
  116. // GameModule.homeGuide.getComponent('HomeGuide').handleState('state31');
  117. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state29', 'state31');
  118. GameModule.homeGuide.on('Fire_state29', this.finishState29, this);
  119. GameModule.homeGuide.on('Fire_state31', this.handleQuestPopup, this);
  120. if (this.homeGuide.guideState.state7.pass || this.homeGuide.guideState.state11.pass) {
  121. GameModule.homeGuide.getComponent('HomeGuide').handleState('state13');
  122. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  123. if (this.homeGuide.guideState.state13.pass) {
  124. GameModule.homeGuide.getComponent('HomeGuide').handleState('state15');
  125. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  126. }
  127. }
  128. this._showSidebarUI();
  129. },
  130. update (dt) {
  131. if (GameModule.skill != null && GameModule.skill != undefined && GameModule.skill.isUsingSkill3) {
  132. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  133. }
  134. },
  135. _setEventListener() {
  136. GameEvent.on(GameNotificationKey.GameShowAdditionTips, this, this.showAdditionTips);
  137. GameEvent.on(GameNotificationKey.GameShowGroupRank, this, this.showGroupRank);
  138. //// 重连socket
  139. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, () => {
  140. // debugger;
  141. GameGlobal._wxSocket._reconnectTimes = 0;
  142. GameGlobal._wxSocket.connect();
  143. // if (Global._wxSocket.readyState != 'open') {
  144. // }
  145. // if (Global._wxSocket.readyState == 'open') {
  146. // }
  147. // this._initSocketMng();
  148. });
  149. /// 通过分享添加好友
  150. GameEvent.on(GameNotificationKey.ProcessShareAction, this, this.processShareAction);
  151. //获取我的小程序奖励领取后隐藏按钮
  152. GameEvent.on('Gain_My_Applet', this, this._hideLuckyBag);
  153. },
  154. _initSocketMng() {
  155. let testUrl = 'wss://test-message-taptap.duowan.com/connect';
  156. let pUrl = 'wss://message-taptap.duowan.com/connect';
  157. let qqUrl = 'wss://qq-message-taptap.duowan.com';
  158. var socketUrl = GameGlobal.debug ? testUrl : pUrl;
  159. let ttUrl = 'wss://headlines-message-taptap.duowan.com';
  160. // let socketUrl = 'ws://172.16.15.196:9099/connect';
  161. if (CC_QQPLAY) {
  162. socketUrl = GameGlobal.debug ? testUrl : qqUrl;
  163. } else if (window.tt != undefined) {
  164. socketUrl = GameGlobal.debug ? testUrl : ttUrl;
  165. }
  166. let ws = new WsManager(socketUrl, {
  167. binaryType: 'arraybuffer',
  168. autoConnect: true, // 自动连接
  169. reconnection: true, // 断开自动重连
  170. reconnectionDelay: 3000, // 重连间隔时间,单位毫秒
  171. // reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity
  172. });
  173. GameGlobal._wxSocket = ws;
  174. ws.on('open', (res) => {
  175. console.log('socket 打开');
  176. });
  177. ws.on('message', (data) => {
  178. let typeStr = typeof data;
  179. if (typeStr !== 'string' || (data.indexOf('type') === -1 && data.indexOf('cancelType') === -1)) {
  180. return;
  181. }
  182. data = JSON.parse(data);
  183. if (data.type != undefined) {
  184. // Global._redTypes = data.type;
  185. /// 商品购买的通知 第一次的socket不管的啦因为接口已经处理了
  186. if (data.type.indexOf(1) != -1 && data.userShops.length > 0 && GameGlobal._socketCount > 1) {
  187. let userShops = data.userShops;
  188. for (let i = 0; i < data.userShops.length; ++ i) {
  189. let shopData = userShops[i];
  190. //// 处理商品信息流重复的问题
  191. this.handelSocketShop(shopData);
  192. GameModule.shop.handelShop(shopData);
  193. GameModule.shop.updateTimeSchedule();
  194. /// 处理信息流信息
  195. GameModule.shop.handleShpDataToMessageList(shopData);
  196. /// 增加商品钻石
  197. if (shopData.diamond !== undefined && shopData.diamond > 0) {
  198. GameModule.userInfo.diamond += shopData.diamond;
  199. }
  200. //// 购买的是礼包
  201. if (shopData.type == 4) {
  202. //// 更新购买明星数量
  203. GameEvent.fire(GameNotificationKey.GameUpdateStarContentBuyGold);
  204. if (shopData.starId != undefined && shopData.roomId != undefined) {
  205. GameEvent.fire(GameNotificationKey.StarEnterRoom, shopData.starId, shopData.roomId);
  206. }
  207. }
  208. }
  209. return;
  210. }
  211. //// 下面这段代码只是去重,不要重复的更新
  212. let isContain = false;
  213. for (let i = 0; i < data.type.length; ++ i) {
  214. /// 如果包含它就什么都不管
  215. let type = data.type[i];
  216. if (GameGlobal._redTypes.indexOf(type) != -1) {
  217. isContain = true;
  218. } else {
  219. GameGlobal._redTypes.push(type);
  220. isContain = false;
  221. }
  222. }
  223. if (isContain == false) {
  224. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  225. // this.handelShowRedDot();
  226. }
  227. }
  228. //// 如果是取消红点的类型
  229. if (data.cancelType !== undefined && data.cancelType > 0) {
  230. TapTapTool.removeRedDot(data.cancelType);
  231. }
  232. });
  233. ws.on('close', () => {
  234. console.log('socket 关闭');
  235. });
  236. ws.on('error', () => {
  237. console.log('sockete 错误');
  238. });
  239. },
  240. /// 处理socket推过来的商品是否跟已经拥有的商品重复,如果重复,那么直接更新cd时间
  241. /// 需要更新的时间包括
  242. handelSocketShop(shopData) {
  243. /// 更新信息流时间
  244. let informationsLength = GameGlobal._timeInformations.length;
  245. if (informationsLength > 0) {
  246. for(let i = 0; i < informationsLength; ++i) {
  247. let information = GameGlobal._timeInformations[i];
  248. /// 如果是商品 并且已经拥有过的话
  249. if (information.type == 1 && information.sId == shopData.shopId) {
  250. information.cdTime = shopData.cdTime;
  251. break;
  252. }
  253. }
  254. }
  255. /// 更新商品使用时间
  256. },
  257. /// 获取信息流
  258. _getInformation() {
  259. GameGlobal._redTypes = [];
  260. GameGlobal._timeInformations = [];
  261. GameGlobal._fixInformations = [];
  262. GameGlobal._gold10 = {'n': 0, 'e': 0};
  263. GameGlobal._buyStarGold = {'n': 0, 'e': 0};
  264. this.getInformation((respondData) => {
  265. console.log(respondData);
  266. GameGlobal._gold10 = respondData.gold10;
  267. GameGlobal._buyStarGold = respondData.buyStarGold;
  268. GameGlobal._isSkillReset = respondData.isSkillReset;
  269. GameGlobal._upBuildingInfo = respondData.upBuildingInfo;
  270. GameGlobal._upRoomInfo = respondData.upRoomInfo;
  271. GameGlobal._buyStarInfo = respondData.buyStarInfo;
  272. if (respondData.types !== undefined) {
  273. GameGlobal._redTypes = respondData.types;
  274. /// 这个是直接返回的 没有推送 七天登录的红点
  275. }
  276. if (TapTapTool.compare(GameGlobal.userData.goldObj, respondData.buyStarGold)) {
  277. GameGlobal._redTypes.push(GameRedDot.star);
  278. }
  279. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  280. // this.handelShowRedDot();
  281. //// 更新信息流数据
  282. if (respondData.informations !== undefined) {
  283. GameGlobal._timeInformations = respondData.informations;
  284. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, GameGlobal._timeInformations.length);
  285. }
  286. }, ({code, msg}) => {
  287. console.log(code, msg);
  288. });
  289. },
  290. /// 网络请求
  291. getInformation(success, fail) {
  292. // 获取目标用户的建筑
  293. HomeApi.getInformation(success, fail);
  294. },
  295. onDestroy() {
  296. GameEvent.off(GameNotificationKey.GameShowAdditionTips, this);
  297. GameEvent.off(GameNotificationKey.GameShowGroupRank, this);
  298. },
  299. _showSidebarUI() {
  300. let isOk = false;
  301. //教程31还没有完成不显示
  302. if (!this.homeGuide.guideState.state31.pass) {
  303. // 1. 总部大楼大于25级
  304. // 2. 已拥有1个明星
  305. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  306. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  307. if (unLockStatus1 && unLockStatus2) {
  308. isOk = true
  309. }
  310. } else {
  311. isOk = true;
  312. }
  313. if (!isOk) {
  314. return;
  315. }
  316. GameGlobal.isShowBar = true;
  317. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  318. this.sidebar.runAction(action);
  319. this.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot();
  320. //是否已经领取过神秘礼物
  321. if (!GameGlobal.appletAward && CC_WECHATGAME && window.tt == undefined) {
  322. this.luckyBagButton.node.active = true;
  323. var offset = 0;
  324. if (this.winSize.height >= this.XHeight) {
  325. offset = 30;
  326. }
  327. let action2 = cc.moveBy(0.4, cc.v2(0, -this.luckyBagButton.node.height - offset));
  328. this.luckyBagButton.node.runAction(action2);
  329. } else {
  330. this.luckyBagButton.node.active = false;
  331. }
  332. },
  333. _hideLuckyBag() {
  334. this.luckyBagButton.node.active = false;
  335. },
  336. finishState29() {
  337. if (GameGlobal.isShowBar) {
  338. return;
  339. }
  340. GameGlobal.isShowBar = true;
  341. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  342. this.sidebar.runAction(action);
  343. this.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot();
  344. },
  345. //处理邀请好友
  346. processShareAction() {
  347. //邀请好友和好友助力的分享链接进入
  348. if (GameGlobal.shareType == ShareAction.INVITE_FRIEND) {
  349. let shareUid = GameGlobal.shareUid;
  350. InviteApi.postInviteFriend(shareUid, () => {
  351. });
  352. InviteApi.postFriendRewardAdd(shareUid, () => {
  353. });
  354. }
  355. },
  356. //显示红包特殊礼包奖励
  357. handleShowApplet: _.debounce((event) => {
  358. AlertManager.showAppletAlert();
  359. }, 1000, true),
  360. showGroupRank() {
  361. AlertManager.showRankAlert(2);
  362. },
  363. showAdditionTips(text, type) {
  364. if (this.node.active) {
  365. this.additionTipsNode.show(text, type);
  366. }
  367. },
  368. // update (dt) {},
  369. });