Game.js 18 KB

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