Game.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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://message-taptap.duowan.com/connect';
  125. let qqUrl = 'wss://qq-message-taptap.duowan.com';
  126. var socketUrl = GameGlobal.debug ? testUrl : pUrl;
  127. // let socketUrl = 'ws://172.16.15.196:9099/connect';
  128. if (CC_QQPLAY) {
  129. socketUrl = GameGlobal.debug ? testUrl : qqUrl;
  130. }
  131. let ws = new WsManager(socketUrl, {
  132. binaryType: 'arraybuffer',
  133. autoConnect: true, // 自动连接
  134. reconnection: true, // 断开自动重连
  135. reconnectionDelay: 3000, // 重连间隔时间,单位毫秒
  136. reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity
  137. });
  138. GameGlobal._wxSocket = ws;
  139. ws.on('open', (res) => {
  140. console.log('socket 打开');
  141. });
  142. ws.on('message', (data) => {
  143. let typeStr = typeof data;
  144. if (typeStr !== 'string' || data.indexOf('type') == -1) {
  145. return;
  146. }
  147. data = JSON.parse(data);
  148. if (data.type != undefined) {
  149. // GameGlobal._redTypes = data.type;
  150. /// 商品购买的通知
  151. if (data.type.indexOf(1) != -1 && data.userShops.length > 0) {
  152. let userShops = data.userShops;
  153. for (let i = 0; i < data.userShops.length; ++ i) {
  154. let shopData = userShops[i];
  155. GameModule.userInfo.shop.handelShop(shopData);
  156. GameModule.userInfo.shop.updateTimeSchedule();
  157. /// 处理信息流信息
  158. GameModule.userInfo.shop.handleShpDataToMessageList(shopData);
  159. /// 说明购买了金币加速器,那么需要发通知告知商城的金币页面重新拉去数据
  160. if (shopData.shopId == 13 || shopData.shopId == 14 || shopData.shopId == 15) {
  161. GameEvent.fire('storeCoin_need_update');
  162. }
  163. /// 增加商品钻石
  164. if (shopData.diamond !== undefined && shopData.diamond > 0) {
  165. GameModule.userInfo.diamond += shopData.diamond;
  166. }
  167. //// 购买的是礼包
  168. if (shopData.type == 4) {
  169. //// 更新购买明星数量
  170. GameEvent.fire(GameNotificationKey.GameUpdateStarContentBuyGold);
  171. if (shopData.starId != undefined && shopData.roomId != undefined) {
  172. GameEvent.fire(GameNotificationKey.StarEnterRoom, shopData.starId, shopData.roomId);
  173. }
  174. }
  175. }
  176. return;
  177. }
  178. if (TapTapTool.compare(GameGlobal._buyStarGold, GameGlobal.userData.goldObj)) {
  179. GameGlobal._redTypes.push(GameRedDot.star);
  180. }
  181. //// 下面这段代码只是去重,不要重复的更新
  182. let isContain = false;
  183. for (let i = 0; i < data.type.length; ++ i) {
  184. /// 如果包含它就什么都不管
  185. let type = data.type[i];
  186. if (GameGlobal._redTypes.indexOf(type) != -1) {
  187. isContain = true;
  188. } else {
  189. GameGlobal._redTypes.push(type);
  190. isContain = false;
  191. }
  192. }
  193. if (isContain == false) {
  194. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  195. // this.handelShowRedDot();
  196. }
  197. }
  198. });
  199. ws.on('close', () => {
  200. console.log('socket 关闭');
  201. });
  202. ws.on('error', () => {
  203. console.log('sockete 错误');
  204. });
  205. },
  206. /// 获取信息流
  207. _getInformation() {
  208. GameGlobal._redTypes = [];
  209. GameGlobal._timeInformations = [];
  210. GameGlobal._fixInformations = [];
  211. GameGlobal._gold10 = {'n': 0, 'e': 0};
  212. GameGlobal._buyStarGold = {'n': 0, 'e': 0};
  213. this.getInformation((respondData) => {
  214. console.log(respondData);
  215. GameGlobal._gold10 = respondData.gold10;
  216. GameGlobal._buyStarGold = respondData.buyStarGold;
  217. GameGlobal._isSkillReset = respondData.isSkillReset;
  218. if (respondData.types !== undefined) {
  219. GameGlobal._redTypes = respondData.types;
  220. }
  221. if (TapTapTool.compare(GameGlobal.userData.goldObj, respondData.buyStarGold)) {
  222. GameGlobal._redTypes.push(GameRedDot.star);
  223. }
  224. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  225. // this.handelShowRedDot();
  226. //// 更新信息流数据
  227. if (respondData.informations !== undefined) {
  228. GameGlobal._timeInformations = respondData.informations;
  229. /// 如果是技能可以重置
  230. // if (respondData.isSkillReset) {
  231. // /// 添加技能可重置的信息流
  232. // let objct = {'cdTime': 0, 'desc': '重置技能可使用', 'name': '重置技能', 'icon': 900012, 'sId': 4, 'type': 2, 'skillStatus': 0};
  233. // GameGlobal._timeInformations.push(objct);
  234. // }
  235. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, GameGlobal._timeInformations.length);
  236. }
  237. }, ({code, msg}) => {
  238. console.log(code, msg);
  239. });
  240. },
  241. /// 网络请求
  242. getInformation(success, fail) {
  243. // 获取目标用户的建筑
  244. HomeApi.getInformation(success, fail);
  245. },
  246. onDestroy() {
  247. GameEvent.off(GameNotificationKey.GameShowAdditionTips, this);
  248. GameEvent.off(GameNotificationKey.GameShowGroupRank, this);
  249. },
  250. _showSidebarUI() {
  251. let isOk = false;
  252. //教程31还没有完成不显示
  253. if (!this.homeGuide.guideState.state31.pass) {
  254. // 1. 总部大楼大于25级
  255. // 2. 已拥有1个明星
  256. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  257. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  258. if (unLockStatus1 && unLockStatus2) {
  259. isOk = true
  260. }
  261. } else {
  262. isOk = true;
  263. }
  264. if (!isOk) {
  265. return;
  266. }
  267. this.isShowBar = true;
  268. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  269. this.sidebar.runAction(action);
  270. this.handelShowRedDot();
  271. },
  272. finishState29() {
  273. if (this.isShowBar) {
  274. return;
  275. }
  276. this.isShowBar = true;
  277. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  278. this.sidebar.runAction(action);
  279. this.handelShowRedDot();
  280. },
  281. handelShowRedDot() {
  282. if (GameGlobal._redTypes == null || GameGlobal._redTypes == undefined || GameGlobal._redTypes.length == 0) {
  283. this.taskRedNode.active = false;
  284. this.drawRedNode.active = false;
  285. this.storeRedNode.active = false;
  286. return;
  287. }
  288. let redTypes = GameGlobal._redTypes;
  289. this.taskRedNode.active = (redTypes.indexOf(GameRedDot.mainTask) != -1 || redTypes.indexOf(GameRedDot.dayTask) != -1);
  290. this.drawRedNode.active = redTypes.indexOf(GameRedDot.draw) != -1;
  291. if (this.isShowBar) {
  292. let isStoreRed = (redTypes.indexOf(GameRedDot.storeDiamond) != -1 || redTypes.indexOf(GameRedDot.storeCoin) != -1);
  293. this.storeRedNode.active = isStoreRed;
  294. if (isStoreRed) {
  295. if (this._isTimer === false) {
  296. this._isTimer = true;
  297. this._storeTimeCount = 0;
  298. this.storeRedNode.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.2, 3, 0), cc.moveBy(0.2, -3, 0))));
  299. this.schedule(this.storeTimeAction, 6);
  300. }
  301. } else {
  302. if (this._isTimer == true) {
  303. this.unschedule(this.storeTimeAction, this);
  304. this._isTimer = false;
  305. }
  306. }
  307. } else {
  308. if (this._isTimer === true) {
  309. this.unschedule(this.storeTimeAction, this);
  310. this._isTimer = false;
  311. }
  312. this.storeRedNode.active = false;
  313. }
  314. },
  315. /// 商城未读消息的显示
  316. storeTimeAction() {
  317. this._storeTimeCount += 1;
  318. /// 第一个6秒的时候
  319. if (this._storeTimeCount === 1) {
  320. this.storeRedNode.active = false;
  321. } else if (this._storeTimeCount === 6) {
  322. this.storeRedNode.active = true;
  323. this._storeTimeCount = 0;
  324. // this.storeRedNode.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.2, 3, 0), cc.moveBy(0.2, -3, 0))));
  325. }
  326. },
  327. //
  328. handleQuestPopup: _.debounce((event) => {
  329. AlertManager.showQuestPopup();
  330. // if (event) {
  331. // event.target.getChildByName("notice_point").active = false;
  332. // }
  333. }, 1000, true),
  334. //显示抽奖界面
  335. handleShowDraw: _.debounce((event) => {
  336. AlertManager.showDrawAlert();
  337. }, 1000, true),
  338. //显示商城界面
  339. handleShowStore: _.debounce((event) => {
  340. AlertManager.showStoreAlert();
  341. // GameEvent.fire(GameNotificationKey.StarLeaveRoom, 1, 1);
  342. // GameEvent.fire(GameNotificationKey.AllStarLeaveRoom);
  343. }, 1000, true),
  344. //显示排行榜界面
  345. handleShowRank: _.debounce((event) => {
  346. AlertManager.showRankAlert();
  347. }, 1000, true),
  348. showGroupRank() {
  349. AlertManager.showRankAlert(2);
  350. },
  351. showAdditionTips(text, type) {
  352. if (this.node.active) {
  353. this.additionTipsNode.show(text, type);
  354. }
  355. },
  356. // update (dt) {},
  357. });