Game.js 16 KB

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