Game.js 15 KB

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