Game.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. const HomeApi = require("../net/HomeApi");
  2. const AlertManager = require('../utils/AlertManager');
  3. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  4. const ShareAction = require('../utils/ShareAction');
  5. const GameModule = require("../utils/GameModule");
  6. const WsManager = require('../net/Ws');
  7. const GameRedDot = require('../utils/GameEnum').GameRedDot;
  8. const TapTapTool = require("../utils/TapTapTool");
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. levelHomePrefab: cc.Prefab,
  13. myInfoTop: cc.Node,
  14. sidebar: cc.Node,
  15. clickAddMoney: cc.Prefab,
  16. additionTipsNode: cc.Node,
  17. homeGuide: cc.Node,
  18. storeNode: cc.Node,
  19. drawNode: cc.Node,
  20. messageListNode: cc.Node,
  21. taskRedNode: cc.Node,
  22. storeRedNode: cc.Node,
  23. drawRedNode: cc.Node,
  24. },
  25. // LIFE-CYCLE CALLBACKS:
  26. onLoad () {
  27. this.isShowBar = false;
  28. this._setEventListener();
  29. this._initSocketMng();
  30. this._getInformation();
  31. // 引导界面
  32. this.homeGuide.zIndex = 200;
  33. GameModule.homeGuide = this.homeGuide;
  34. this.homeGuide = this.homeGuide.getComponent('HomeGuide');
  35. this.homeGuide.init();
  36. // 创建一个事件监听实例, 用来实现跨节点监听事件
  37. GameEvent.init();
  38. cc.debug.setDisplayStats(false);
  39. //建筑展示
  40. let levelHome = cc.instantiate(this.levelHomePrefab);
  41. levelHome = levelHome.getComponent('LevelHome');
  42. levelHome.init(Global.user.uid);
  43. this.levelHome = levelHome;
  44. this.levelHome.node.active = true;
  45. let XHeight = 1624;
  46. this.winSize = cc.view.getVisibleSize();
  47. console.log('game ======================= ' + this.winSize);
  48. if (this.winSize.height >= XHeight) {
  49. this.myInfoTop.height = 200;
  50. this.sidebar.getComponent(cc.Widget).top = 180;
  51. this.messageListNode.getComponent(cc.Widget).top = 250 + 30;
  52. }
  53. this.additionTipsNode = this.additionTipsNode.getComponent('AdditionTips');
  54. if (Global.shareType == ShareAction.SHOW_GROUP_RANK && Global.shareTicket.length > 0) {
  55. if (this.homeGuide.guideState.state1.pass) {
  56. GameEvent.fire(GameNotificationKey.GameShowGroupRank);
  57. }
  58. }
  59. if (Global.isCheck) {
  60. this.storeNode.active = false;
  61. this.drawNode.active = false;
  62. }
  63. },
  64. start () {
  65. if (!this.homeGuide.guideState.state1.pass) {
  66. // 触发引导系统state1状态
  67. GameModule.homeGuide.getComponent('HomeGuide').handleState('state1');
  68. }
  69. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state4', 'state5');
  70. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state6', 'state7');
  71. // GameModule.homeGuide.getComponent('HomeGuide').handleState('state31');
  72. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state29', 'state31');
  73. GameModule.homeGuide.on('Fire_state29', this.finishState29, this);
  74. GameModule.homeGuide.on('Fire_state31', this.handleQuestPopup, this);
  75. if (this.homeGuide.guideState.state7.pass || this.homeGuide.guideState.state11.pass) {
  76. GameModule.homeGuide.getComponent('HomeGuide').handleState('state13');
  77. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  78. if (this.homeGuide.guideState.state13.pass) {
  79. GameModule.homeGuide.getComponent('HomeGuide').handleState('state15');
  80. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  81. }
  82. }
  83. this._showSidebarUI();
  84. },
  85. update (dt) {
  86. if (GameModule.skill != null && GameModule.skill != undefined && GameModule.skill.isUsingSkill3) {
  87. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  88. }
  89. },
  90. _setEventListener() {
  91. GameEvent.on(GameNotificationKey.GameShowAdditionTips, this, this.showAdditionTips);
  92. GameEvent.on(GameNotificationKey.GameShowGroupRank, this, this.showGroupRank);
  93. GameEvent.on(GameNotificationKey.GameRedDotUpdate, this, this.handelShowRedDot);
  94. },
  95. _initSocketMng() {
  96. let testUrl = 'wss://test-message-taptap.duowan.com/connect';
  97. let pUrl = 'wss://message-allstar.duowan.com/connect';
  98. let socketUrl = Global.debug ? testUrl : pUrl;
  99. // let socketUrl = 'ws://172.16.15.196:9099/connect';
  100. let ws = new WsManager(socketUrl, {
  101. binaryType: 'arraybuffer',
  102. autoConnect: true, // 自动连接
  103. reconnection: true, // 断开自动重连
  104. reconnectionDelay: 3000, // 重连间隔时间,单位毫秒
  105. reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity
  106. });
  107. ws.on('open', (res) => {
  108. console.log('socket 打开');
  109. });
  110. ws.on('message', (data) => {
  111. if (data.type != undefined) {
  112. // Global._redTypes = data.type;
  113. /// 商品购买的通知
  114. if (data.type == 1 && data.userShops.length > 0) {
  115. let userShops = data.userShops;
  116. for (let i = 0; i < data.userShops.length; ++ i) {
  117. GameModule.userInfo.shop.handelShop(userShops[i]);
  118. GameModule.userInfo.shop.updateTimeSchedule();
  119. /// 处理信息流信息
  120. GameModule.user.shop.handleShpDataToMessageList();
  121. }
  122. return;
  123. }
  124. if (TapTapTool.compare(Global._buyStarGold, Global.userData.goldObj)) {
  125. Global._redTypes.push(GameRedDot.star);
  126. }
  127. //// 下面这段代码只是去重,不要重复的更新
  128. let isContain = false;
  129. for (let i = 0; i < data.type.length; ++ i) {
  130. /// 如果包含它就什么都不管
  131. let type = data.type[i];
  132. if (Global._redTypes.indexOf(type) != -1) {
  133. isContain = true;
  134. } else {
  135. Global._redTypes.push(type);
  136. isContain = false;
  137. }
  138. }
  139. if (isContain == false) {
  140. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  141. this.handelShowRedDot();
  142. }
  143. }
  144. });
  145. ws.on('close', () => {
  146. console.log('socket 关闭');
  147. });
  148. ws.on('error', () => {
  149. console.log('sockete 错误');
  150. });
  151. },
  152. /// 获取信息流
  153. _getInformation() {
  154. Global._redTypes = [];
  155. Global._timeInformations = [];
  156. Global._fixInformations = [];
  157. Global._gold10 = {'n': 0, 'e': 0};
  158. Global._buyStarGold = {'n': 0, 'e': 0};
  159. this.getInformation().then((respondData) => {
  160. console.log(respondData);
  161. Global._gold10 = respondData.gold10;
  162. Global._buyStarGold = respondData.buyStarGold;
  163. if (respondData.types !== undefined) {
  164. Global._redTypes = respondData.types;
  165. }
  166. if (TapTapTool.compare(Global.userData.goldObj, respondData.buyStarGold)) {
  167. Global._redTypes.push(GameRedDot.star);
  168. }
  169. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  170. this.handelShowRedDot();
  171. //// 更新信息流数据
  172. if (respondData.informations !== undefined) {
  173. Global._timeInformations = respondData.informations;
  174. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, respondData.informations.length);
  175. }
  176. }).catch(({code, msg}) => {
  177. console.log(code, msg);
  178. });
  179. },
  180. /// 网络请求
  181. getInformation() {
  182. return new Promise((resolve, reject) => {
  183. // 获取目标用户的建筑
  184. HomeApi.getInformation((respondData) => {
  185. resolve(respondData);
  186. }, (code, msg) => {
  187. reject({code, msg});
  188. });
  189. })
  190. },
  191. onDestroy() {
  192. GameEvent.off(GameNotificationKey.GameShowAdditionTips, this);
  193. GameEvent.off(GameNotificationKey.GameShowGroupRank, this);
  194. },
  195. _showSidebarUI() {
  196. let isOk = false;
  197. //教程31还没有完成不显示
  198. if (!this.homeGuide.guideState.state31.pass) {
  199. // 1. 总部大楼大于25级
  200. // 2. 已拥有1个明星
  201. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  202. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  203. if (unLockStatus1 && unLockStatus2) {
  204. isOk = true
  205. }
  206. } else {
  207. isOk = true;
  208. }
  209. if (!isOk) {
  210. return;
  211. }
  212. this.isShowBar = true;
  213. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  214. this.sidebar.runAction(action);
  215. },
  216. finishState29() {
  217. if (this.isShowBar) {
  218. return;
  219. }
  220. this.isShowBar = true;
  221. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  222. this.sidebar.runAction(action);
  223. },
  224. handelShowRedDot() {
  225. if (Global._redTypes == null || Global._redTypes == undefined || Global._redTypes.length == 0) {
  226. this.taskRedNode.active = false;
  227. this.drawRedNode.active = false;
  228. this.storeRedNode.active = false;
  229. return;
  230. }
  231. let redTypes = Global._redTypes;
  232. this.taskRedNode.active = (redTypes.indexOf(GameRedDot.mainTask) != -1 || redTypes.indexOf(GameRedDot.dayTask) != -1);
  233. this.drawRedNode.active = redTypes.indexOf(GameRedDot.draw) != -1;
  234. let isStoreRed = (redTypes.indexOf(GameRedDot.storeDiamond) != -1 || redTypes.indexOf(GameRedDot.storeCoin) != -1);
  235. this.storeRedNode.active = isStoreRed;
  236. this._storeRedNode = isStoreRed;
  237. if (isStoreRed) {
  238. this._isTimer = true;
  239. this._storeTimeCount = 0;
  240. this.storeRedNode.runAction( cc.repeatForever(cc.sequence(cc.rotateTo(0.2,3), cc.rotateTo(0.2,-3))));
  241. this.schedule(this.storeTimeAction, 6);
  242. } else {
  243. if (this._isTimer == true) {
  244. this.unschedule(this.storeTimeAction, this);
  245. this._isTimer = false;
  246. }
  247. }
  248. },
  249. /// 商城未读消息的显示
  250. storeTimeAction() {
  251. this._storeTimeCount += 1;
  252. /// 第一个6秒的时候
  253. if (this._storeTimeCount === 1) {
  254. this.storeRedNode.active = false;
  255. } else if (this._storeTimeCount === 6) {
  256. this.storeRedNode.active = true;
  257. this._storeTimeCount = 0;
  258. this.storeRedNode.runAction( cc.repeatForever(cc.sequence(cc.rotateTo(0.2,3), cc.rotateTo(0.2,-3))));
  259. }
  260. },
  261. //
  262. handleQuestPopup: _.debounce((event) => {
  263. AlertManager.showQuestPopup();
  264. // if (event) {
  265. // event.target.getChildByName("notice_point").active = false;
  266. // }
  267. }, 1000, true),
  268. //显示抽奖界面
  269. handleShowDraw: _.debounce((event) => {
  270. AlertManager.showDrawAlert();
  271. }, 1000, true),
  272. //显示商城界面
  273. handleShowStore: _.debounce((event) => {
  274. AlertManager.showStoreAlert();
  275. }, 1000, true),
  276. //显示排行榜界面
  277. handleShowRank: _.debounce((event) => {
  278. AlertManager.showRankAlert();
  279. }, 1000, true),
  280. showGroupRank() {
  281. AlertManager.showRankAlert(2);
  282. },
  283. showAdditionTips(text, type) {
  284. if (this.node.active) {
  285. this.additionTipsNode.show(text, type);
  286. }
  287. },
  288. // update (dt) {},
  289. });