Game.js 18 KB

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