Game.js 18 KB

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