Game.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. moreNode: cc.Node,
  23. luckyBagButton: 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. bottomTabbar: cc.Node,
  31. commonNode: cc.Node,
  32. sliderNode: cc.Node,
  33. },
  34. // LIFE-CYCLE CALLBACKS:
  35. onLoad () {
  36. this.isShowBar = false;
  37. this._isTimer = false;
  38. this.winSize = cc.view.getVisibleSize();
  39. Global.winSize = this.winSize;
  40. // 创建一个事件监听实例, 用来实现跨节点监听事件
  41. GameEvent.init();
  42. this._setEventListener();
  43. /// 初始化广告W6Lcux7ArPGCBpnWuAyEVK48PrrYX7xWBA
  44. if (CC_WECHATGAME) {
  45. Global._adVideo = new ADVideo();
  46. Global._adVideo.wxCreateADVideo();
  47. }
  48. this._initSocketMng();
  49. this._getInformation();
  50. if (window.wx != undefined) {
  51. Global.wechatScoreKey = Global.debug ? 'buildingLevel_test' : 'buildingLevel';
  52. window.wx.postMessage({
  53. messageType: 5,
  54. key1: Global.debug
  55. });
  56. window.wx.postMessage({
  57. messageType: 6,
  58. winSize: Global.winSize
  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(Global.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 (Global.shareType == ShareAction.SHOW_GROUP_RANK && Global.shareTicket.length > 0) {
  82. if (this.homeGuide.guideState.state1.pass) {
  83. GameEvent.fire(GameNotificationKey.GameShowGroupRank);
  84. }
  85. }
  86. if (Global.messageNotice !== undefined && Global.messageNotice.length > 0) {
  87. if (Global.noticeDiamond !== undefined && Global.noticeDiamond > 0) {
  88. let showText = `钻石 x ${Global.noticeDiamond}`;
  89. AlertManager.showActGiftAlert('diamond', showText);
  90. }
  91. AlertManager.showNoticeAlert(Global.messageNotice);
  92. }
  93. if (Global.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.GameShowAdditionTips, this, this.showAdditionTips);
  125. GameEvent.on(GameNotificationKey.GameShowGroupRank, this, this.showGroupRank);
  126. //// 重连socket
  127. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, () => {
  128. // debugger;
  129. Global._wxSocket._reconnectTimes = 0;
  130. Global._wxSocket.connect();
  131. // if (Global._wxSocket.readyState != 'open') {
  132. // }
  133. // if (Global._wxSocket.readyState == 'open') {
  134. // }
  135. // this._initSocketMng();
  136. });
  137. /// 通过分享添加好友
  138. GameEvent.on(GameNotificationKey.ProcessShareAction, this, this.processShareAction);
  139. //获取我的小程序奖励领取后隐藏按钮
  140. GameEvent.on('Gain_My_Applet', this, this._hideLuckyBag);
  141. },
  142. _initSocketMng() {
  143. let testUrl = 'wss://test-message-taptap.duowan.com/connect';
  144. let pUrl = 'wss://message-taptap.duowan.com/connect';
  145. // let testUrl = 'wss://new-message-taptap.duowan.com/connect';
  146. let socketUrl = Global.debug ? testUrl : pUrl;
  147. // let socketUrl = 'ws://172.16.15.196:9099/connect';
  148. let ws = new WsManager(socketUrl, {
  149. binaryType: 'arraybuffer',
  150. autoConnect: true, // 自动连接
  151. reconnection: true, // 断开自动重连
  152. reconnectionDelay: 3000, // 重连间隔时间,单位毫秒
  153. reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity
  154. });
  155. Global._wxSocket = ws;
  156. ws.on('open', (res) => {
  157. console.log('socket 打开');
  158. });
  159. ws.on('message', (data) => {
  160. let typeStr = typeof data;
  161. if (typeStr !== 'string' || (data.indexOf('type') === -1 && data.indexOf('cancelType') === -1)) {
  162. return;
  163. }
  164. data = JSON.parse(data);
  165. if (data.type != undefined) {
  166. // Global._redTypes = data.type;
  167. /// 商品购买的通知 第一次的socket不管的啦因为接口已经处理了
  168. if (data.type.indexOf(1) != -1 && data.userShops.length > 0 && Global._socketCount > 1) {
  169. let userShops = data.userShops;
  170. for (let i = 0; i < data.userShops.length; ++ i) {
  171. let shopData = userShops[i];
  172. //// 处理商品信息流重复的问题
  173. this.handelSocketShop(shopData);
  174. GameModule.shop.handelShop(shopData);
  175. GameModule.shop.updateTimeSchedule();
  176. /// 处理信息流信息
  177. GameModule.shop.handleShpDataToMessageList(shopData);
  178. /// 增加商品钻石
  179. if (shopData.diamond !== undefined && shopData.diamond > 0) {
  180. GameModule.userInfo.diamond += shopData.diamond;
  181. }
  182. //// 购买的是礼包
  183. if (shopData.type == 4) {
  184. //// 更新购买明星数量
  185. GameEvent.fire(GameNotificationKey.GameUpdateStarContentBuyGold);
  186. if (shopData.starId != undefined && shopData.roomId != undefined) {
  187. GameEvent.fire(GameNotificationKey.StarEnterRoom, shopData.starId, shopData.roomId);
  188. }
  189. }
  190. }
  191. return;
  192. }
  193. //// 下面这段代码只是去重,不要重复的更新
  194. let isContain = false;
  195. for (let i = 0; i < data.type.length; ++ i) {
  196. /// 如果包含它就什么都不管
  197. let type = data.type[i];
  198. if (Global._redTypes.indexOf(type) != -1) {
  199. isContain = true;
  200. } else {
  201. Global._redTypes.push(type);
  202. isContain = false;
  203. }
  204. }
  205. if (isContain == false) {
  206. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  207. // this.handelShowRedDot();
  208. }
  209. }
  210. //// 如果是取消红点的类型
  211. if (data.cancelType !== undefined && data.cancelType > 0) {
  212. TapTapTool.removeRedDot(data.cancelType);
  213. }
  214. });
  215. ws.on('close', () => {
  216. console.log('socket 关闭');
  217. });
  218. ws.on('error', () => {
  219. console.log('sockete 错误');
  220. });
  221. },
  222. /// 处理socket推过来的商品是否跟已经拥有的商品重复,如果重复,那么直接更新cd时间
  223. /// 需要更新的时间包括
  224. handelSocketShop(shopData) {
  225. /// 更新信息流时间
  226. let informationsLength = Global._timeInformations.length;
  227. if (informationsLength > 0) {
  228. for(let i = 0; i < informationsLength; ++i) {
  229. let information = Global._timeInformations[i];
  230. /// 如果是商品 并且已经拥有过的话
  231. if (information.type == 1 && information.sId == shopData.shopId) {
  232. information.cdTime = shopData.cdTime;
  233. break;
  234. }
  235. }
  236. }
  237. /// 更新商品使用时间
  238. },
  239. /// 获取信息流
  240. _getInformation() {
  241. Global._redTypes = [];
  242. Global._timeInformations = [];
  243. Global._fixInformations = [];
  244. Global._gold10 = {'n': 0, 'e': 0};
  245. Global._buyStarGold = {'n': 0, 'e': 0};
  246. this.getInformation().then((respondData) => {
  247. console.log(respondData);
  248. Global._gold10 = respondData.gold10;
  249. Global._buyStarGold = respondData.buyStarGold;
  250. Global._isSkillReset = respondData.isSkillReset;
  251. Global._upBuildingInfo = respondData.upBuildingInfo;
  252. Global._upRoomInfo = respondData.upRoomInfo;
  253. Global._buyStarInfo = respondData.buyStarInfo;
  254. if (respondData.types !== undefined) {
  255. Global._redTypes = respondData.types;
  256. /// 这个是直接返回的 没有推送 七天登录的红点
  257. }
  258. if (TapTapTool.compare(Global.userData.goldObj, respondData.buyStarGold)) {
  259. Global._redTypes.push(GameRedDot.star);
  260. }
  261. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  262. // this.handelShowRedDot();
  263. //// 更新信息流数据
  264. if (respondData.informations !== undefined) {
  265. Global._timeInformations = respondData.informations;
  266. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, Global._timeInformations.length);
  267. }
  268. }).catch(({code, msg}) => {
  269. console.log(code, msg);
  270. });
  271. },
  272. /// 网络请求
  273. getInformation() {
  274. return new Promise((resolve, reject) => {
  275. // 获取目标用户的建筑
  276. HomeApi.getInformation((respondData) => {
  277. resolve(respondData);
  278. }, (code, msg) => {
  279. reject({code, msg});
  280. });
  281. })
  282. },
  283. onDestroy() {
  284. GameEvent.off(GameNotificationKey.GameShowAdditionTips, this);
  285. GameEvent.off(GameNotificationKey.GameShowGroupRank, this);
  286. },
  287. _showSidebarUI() {
  288. let isOk = false;
  289. //教程31还没有完成不显示
  290. if (!this.homeGuide.guideState.state31.pass) {
  291. // 1. 总部大楼大于25级
  292. // 2. 已拥有1个明星
  293. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  294. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  295. if (unLockStatus1 && unLockStatus2) {
  296. isOk = true
  297. }
  298. } else {
  299. isOk = true;
  300. }
  301. if (!isOk) {
  302. return;
  303. }
  304. this.isShowBar = true;
  305. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  306. this.sidebar.runAction(action);
  307. this.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot();
  308. //是否已经领取过神秘礼物
  309. if (!Global.appletAward && CC_WECHATGAME) {
  310. this.luckyBagButton.node.active = true;
  311. var offset = 0;
  312. if (this.winSize.height >= this.XHeight) {
  313. offset = 30;
  314. }
  315. let action2 = cc.moveBy(0.4, cc.v2(0, -this.luckyBagButton.node.height - offset));
  316. this.luckyBagButton.node.runAction(action2);
  317. } else {
  318. this.luckyBagButton.node.active = false;
  319. }
  320. },
  321. _hideLuckyBag() {
  322. this.luckyBagButton.node.active = false;
  323. },
  324. finishState29() {
  325. if (this.isShowBar) {
  326. return;
  327. }
  328. this.isShowBar = true;
  329. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  330. this.sidebar.runAction(action);
  331. this.sliderNode.getComponent('HomeSideCtrl').handelShowRedDot();
  332. },
  333. //处理邀请好友
  334. processShareAction() {
  335. //邀请好友和好友助力的分享链接进入
  336. if (Global.shareType == ShareAction.INVITE_FRIEND) {
  337. let shareUid = Global.shareUid;
  338. InviteApi.postInviteFriend(shareUid, () => {
  339. });
  340. InviteApi.postFriendRewardAdd(shareUid, () => {
  341. });
  342. }
  343. },
  344. //显示红包特殊礼包奖励
  345. handleShowApplet: _.debounce((event) => {
  346. AlertManager.showAppletAlert();
  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. });