Game.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. if (this.homeGuide.guideState.state7.pass || this.homeGuide.guideState.state11.pass) {
  120. GameModule.homeGuide.getComponent('HomeGuide').handleState('state13');
  121. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  122. if (this.homeGuide.guideState.state13.pass) {
  123. GameModule.homeGuide.getComponent('HomeGuide').handleState('state15');
  124. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  125. }
  126. }
  127. this._showSidebarUI();
  128. },
  129. update (dt) {
  130. if (GameModule.skill != null && GameModule.skill != undefined && GameModule.skill.isUsingSkill3) {
  131. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  132. }
  133. },
  134. _setEventListener() {
  135. GameEvent.on(GameNotificationKey.GameShowAdditionTips, this, this.showAdditionTips);
  136. GameEvent.on(GameNotificationKey.GameShowGroupRank, this, this.showGroupRank);
  137. //// 重连socket
  138. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, () => {
  139. // debugger;
  140. GameGlobal._wxSocket._reconnectTimes = 0;
  141. GameGlobal._wxSocket.connect();
  142. // if (Global._wxSocket.readyState != 'open') {
  143. // }
  144. // if (Global._wxSocket.readyState == 'open') {
  145. // }
  146. // this._initSocketMng();
  147. });
  148. /// 通过分享添加好友
  149. GameEvent.on(GameNotificationKey.ProcessShareAction, this, this.processShareAction);
  150. //获取我的小程序奖励领取后隐藏按钮
  151. GameEvent.on('Gain_My_Applet', this, this._hideLuckyBag);
  152. },
  153. _initSocketMng() {
  154. let testUrl = 'wss://test-message-taptap.duowan.com/connect';
  155. let pUrl = 'wss://message-taptap.duowan.com/connect';
  156. let qqUrl = 'wss://qq-message-taptap.duowan.com/connect';
  157. var socketUrl = GameGlobal.debug ? testUrl : pUrl;
  158. let ttUrl = 'wss://headlines-message-taptap.duowan.com/connect';
  159. // let socketUrl = 'ws://172.16.15.196:9099/connect';
  160. if (CC_QQPLAY) {
  161. socketUrl = GameGlobal.debug ? testUrl : qqUrl;
  162. } else if (window.tt != undefined) {
  163. socketUrl = GameGlobal.debug ? testUrl : ttUrl;
  164. }
  165. let ws = new WsManager(socketUrl, {
  166. binaryType: 'arraybuffer',
  167. autoConnect: true, // 自动连接
  168. reconnection: true, // 断开自动重连
  169. reconnectionDelay: 3000, // 重连间隔时间,单位毫秒
  170. // reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity
  171. });
  172. GameGlobal._wxSocket = ws;
  173. ws.on('open', (res) => {
  174. console.log('socket 打开');
  175. });
  176. ws.on('message', (data) => {
  177. let typeStr = typeof data;
  178. if (typeStr !== 'string' || (data.indexOf('type') === -1 && data.indexOf('cancelType') === -1)) {
  179. return;
  180. }
  181. data = JSON.parse(data);
  182. if (data.type != undefined) {
  183. // Global._redTypes = data.type;
  184. /// 商品购买的通知 第一次的socket不管的啦因为接口已经处理了
  185. if (data.type.indexOf(1) != -1 && data.userShops.length > 0 && GameGlobal._socketCount > 1) {
  186. let userShops = data.userShops;
  187. for (let i = 0; i < data.userShops.length; ++ i) {
  188. let shopData = userShops[i];
  189. //// 处理商品信息流重复的问题
  190. this.handelSocketShop(shopData);
  191. GameModule.shop.handelShop(shopData);
  192. GameModule.shop.updateTimeSchedule();
  193. /// 处理信息流信息
  194. GameModule.shop.handleShpDataToMessageList(shopData);
  195. /// 增加商品钻石
  196. if (shopData.diamond !== undefined && shopData.diamond > 0) {
  197. GameModule.userInfo.diamond += shopData.diamond;
  198. }
  199. //// 购买的是礼包
  200. if (shopData.type == 4) {
  201. //// 更新购买明星数量
  202. GameEvent.fire(GameNotificationKey.GameUpdateStarContentBuyGold);
  203. if (shopData.starId != undefined && shopData.roomId != undefined) {
  204. GameEvent.fire(GameNotificationKey.StarEnterRoom, shopData.starId, shopData.roomId);
  205. }
  206. }
  207. }
  208. return;
  209. }
  210. //// 下面这段代码只是去重,不要重复的更新
  211. let isContain = false;
  212. for (let i = 0; i < data.type.length; ++ i) {
  213. /// 如果包含它就什么都不管
  214. let type = data.type[i];
  215. if (GameGlobal._redTypes.indexOf(type) != -1) {
  216. isContain = true;
  217. } else {
  218. GameGlobal._redTypes.push(type);
  219. isContain = false;
  220. }
  221. }
  222. if (isContain == false) {
  223. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  224. // this.handelShowRedDot();
  225. }
  226. }
  227. //// 如果是取消红点的类型
  228. if (data.cancelType !== undefined && data.cancelType > 0) {
  229. TapTapTool.removeRedDot(data.cancelType);
  230. }
  231. });
  232. ws.on('close', () => {
  233. console.log('socket 关闭');
  234. });
  235. ws.on('error', () => {
  236. console.log('sockete 错误');
  237. });
  238. },
  239. /// 处理socket推过来的商品是否跟已经拥有的商品重复,如果重复,那么直接更新cd时间
  240. /// 需要更新的时间包括
  241. handelSocketShop(shopData) {
  242. /// 更新信息流时间
  243. let informationsLength = GameGlobal._timeInformations.length;
  244. if (informationsLength > 0) {
  245. for(let i = 0; i < informationsLength; ++i) {
  246. let information = GameGlobal._timeInformations[i];
  247. /// 如果是商品 并且已经拥有过的话
  248. if (information.type == 1 && information.sId == shopData.shopId) {
  249. information.cdTime = shopData.cdTime;
  250. break;
  251. }
  252. }
  253. }
  254. /// 更新商品使用时间
  255. },
  256. /// 获取信息流
  257. _getInformation() {
  258. GameGlobal._redTypes = [];
  259. GameGlobal._timeInformations = [];
  260. GameGlobal._fixInformations = [];
  261. GameGlobal._gold10 = {'n': 0, 'e': 0};
  262. GameGlobal._buyStarGold = {'n': 0, 'e': 0};
  263. this.getInformation((respondData) => {
  264. console.log(respondData);
  265. GameGlobal._gold10 = respondData.gold10;
  266. GameGlobal._buyStarGold = respondData.buyStarGold;
  267. GameGlobal._isSkillReset = respondData.isSkillReset;
  268. GameGlobal._upBuildingInfo = respondData.upBuildingInfo;
  269. GameGlobal._upRoomInfo = respondData.upRoomInfo;
  270. GameGlobal._buyStarInfo = respondData.buyStarInfo;
  271. if (respondData.types !== undefined) {
  272. GameGlobal._redTypes = respondData.types;
  273. /// 这个是直接返回的 没有推送 七天登录的红点
  274. }
  275. if (TapTapTool.compare(GameGlobal.userData.goldObj, respondData.buyStarGold)) {
  276. GameGlobal._redTypes.push(GameRedDot.star);
  277. }
  278. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  279. // this.handelShowRedDot();
  280. //// 更新信息流数据
  281. if (respondData.informations !== undefined) {
  282. GameGlobal._timeInformations = respondData.informations;
  283. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, GameGlobal._timeInformations.length);
  284. }
  285. }, ({code, msg}) => {
  286. console.log(code, msg);
  287. });
  288. },
  289. /// 网络请求
  290. getInformation(success, fail) {
  291. // 获取目标用户的建筑
  292. HomeApi.getInformation(success, fail);
  293. },
  294. onDestroy() {
  295. GameEvent.off(GameNotificationKey.GameShowAdditionTips, this);
  296. GameEvent.off(GameNotificationKey.GameShowGroupRank, this);
  297. },
  298. _showSidebarUI() {
  299. let isOk = false;
  300. //教程31还没有完成不显示
  301. if (!this.homeGuide.guideState.state31.pass) {
  302. // 1. 总部大楼大于25级
  303. // 2. 已拥有1个明星
  304. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  305. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  306. if (unLockStatus1 && unLockStatus2) {
  307. isOk = true
  308. }
  309. } else {
  310. isOk = true;
  311. }
  312. if (!isOk) {
  313. return;
  314. }
  315. GameGlobal.isShowBar = true;
  316. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  317. this.sidebar.runAction(action);
  318. this.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot();
  319. //是否已经领取过神秘礼物
  320. if (!GameGlobal.appletAward && CC_WECHATGAME && window.tt == undefined) {
  321. this.luckyBagButton.node.active = true;
  322. var offset = 0;
  323. if (this.winSize.height >= this.XHeight) {
  324. offset = 30;
  325. }
  326. let action2 = cc.moveBy(0.4, cc.v2(0, -this.luckyBagButton.node.height - offset));
  327. this.luckyBagButton.node.runAction(action2);
  328. } else {
  329. this.luckyBagButton.node.active = false;
  330. }
  331. },
  332. _hideLuckyBag() {
  333. this.luckyBagButton.node.active = false;
  334. },
  335. finishState29() {
  336. if (GameGlobal.isShowBar) {
  337. return;
  338. }
  339. GameGlobal.isShowBar = true;
  340. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  341. this.sidebar.runAction(action);
  342. this.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot();
  343. },
  344. //处理邀请好友
  345. processShareAction() {
  346. //邀请好友和好友助力的分享链接进入
  347. if (GameGlobal.shareType == ShareAction.INVITE_FRIEND) {
  348. let shareUid = GameGlobal.shareUid;
  349. InviteApi.postInviteFriend(shareUid, () => {
  350. });
  351. InviteApi.postFriendRewardAdd(shareUid, () => {
  352. });
  353. }
  354. },
  355. //显示红包特殊礼包奖励
  356. handleShowApplet: _.debounce((event) => {
  357. AlertManager.showAppletAlert();
  358. }, 1000, true),
  359. showGroupRank() {
  360. AlertManager.showRankAlert(2);
  361. },
  362. showAdditionTips(text, type) {
  363. if (this.node.active) {
  364. this.additionTipsNode.show(text, type);
  365. }
  366. },
  367. // update (dt) {},
  368. });