Game.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. Global.winSize = this.winSize;
  36. // 创建一个事件监听实例, 用来实现跨节点监听事件
  37. GameEvent.init();
  38. this._setEventListener();
  39. this._initSocketMng();
  40. this._getInformation();
  41. if (window.wx != undefined) {
  42. Global.wechatScoreKey = Global.debug ? 'buildingLevel_test' : 'buildingLevel';
  43. window.wx.postMessage({
  44. messageType: 5,
  45. key1: Global.debug
  46. });
  47. window.wx.postMessage({
  48. messageType: 6,
  49. winSize: Global.winSize
  50. });
  51. }
  52. // 引导界面
  53. this.homeGuide.zIndex = 200;
  54. GameModule.homeGuide = this.homeGuide;
  55. this.homeGuide = this.homeGuide.getComponent('HomeGuide');
  56. this.homeGuide.init();
  57. cc.debug.setDisplayStats(false);
  58. //建筑展示
  59. let levelHome = cc.instantiate(this.levelHomePrefab);
  60. levelHome = levelHome.getComponent('LevelHome');
  61. levelHome.init(Global.user.uid);
  62. this.levelHome = levelHome;
  63. this.levelHome.node.active = true;
  64. let XHeight = 1624;
  65. console.log('game ======================= ' + this.winSize);
  66. if (this.winSize.height >= XHeight) {
  67. this.myInfoTop.height = 200;
  68. this.sidebar.getComponent(cc.Widget).top = 180;
  69. this.messageListNode.getComponent(cc.Widget).top = 250 + 30;
  70. }
  71. this.additionTipsNode = this.additionTipsNode.getComponent('AdditionTips');
  72. if (Global.shareType == ShareAction.SHOW_GROUP_RANK && Global.shareTicket.length > 0) {
  73. if (this.homeGuide.guideState.state1.pass) {
  74. GameEvent.fire(GameNotificationKey.GameShowGroupRank);
  75. }
  76. }
  77. if (Global.isCheck) {
  78. this.storeNode.active = false;
  79. this.drawNode.active = false;
  80. }
  81. },
  82. start () {
  83. if (!this.homeGuide.guideState.state1.pass) {
  84. // 触发引导系统state1状态
  85. GameModule.homeGuide.getComponent('HomeGuide').handleState('state1');
  86. }
  87. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state4', 'state5');
  88. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state6', 'state7');
  89. // GameModule.homeGuide.getComponent('HomeGuide').handleState('state31');
  90. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state29', 'state31');
  91. GameModule.homeGuide.on('Fire_state29', this.finishState29, this);
  92. GameModule.homeGuide.on('Fire_state31', this.handleQuestPopup, this);
  93. if (this.homeGuide.guideState.state7.pass || this.homeGuide.guideState.state11.pass) {
  94. GameModule.homeGuide.getComponent('HomeGuide').handleState('state13');
  95. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  96. if (this.homeGuide.guideState.state13.pass) {
  97. GameModule.homeGuide.getComponent('HomeGuide').handleState('state15');
  98. GameModule.homeGuide.getComponent('HomeGuide').changeGuideTask1315(false);
  99. }
  100. }
  101. this._showSidebarUI();
  102. },
  103. update (dt) {
  104. if (GameModule.skill != null && GameModule.skill != undefined && GameModule.skill.isUsingSkill3) {
  105. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  106. }
  107. },
  108. _setEventListener() {
  109. GameEvent.on(GameNotificationKey.GameRedDotUpdate, this, this.handelShowRedDot);
  110. GameEvent.on(GameNotificationKey.GameShowAdditionTips, this, this.showAdditionTips);
  111. GameEvent.on(GameNotificationKey.GameShowGroupRank, this, this.showGroupRank);
  112. //// 重连socket
  113. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, this._initSocketMng);
  114. },
  115. _initSocketMng() {
  116. let testUrl = 'wss://test-message-taptap.duowan.com/connect';
  117. let pUrl = 'wss://message-allstar.duowan.com/connect';
  118. let socketUrl = Global.debug ? testUrl : pUrl;
  119. // let socketUrl = 'ws://172.16.15.196:9099/connect';
  120. let ws = new WsManager(socketUrl, {
  121. binaryType: 'arraybuffer',
  122. autoConnect: true, // 自动连接
  123. reconnection: true, // 断开自动重连
  124. reconnectionDelay: 3000, // 重连间隔时间,单位毫秒
  125. reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity
  126. });
  127. Global._wxSocket = ws;
  128. ws.on('open', (res) => {
  129. console.log('socket 打开');
  130. });
  131. ws.on('message', (data) => {
  132. let typeStr = typeof data;
  133. if (typeStr !== 'string' || data.indexOf('type') == -1) {
  134. return;
  135. }
  136. data = JSON.parse(data);
  137. if (data.type != undefined) {
  138. // Global._redTypes = data.type;
  139. /// 商品购买的通知
  140. if (data.type.indexOf(1) != -1 && data.userShops.length > 0) {
  141. let userShops = data.userShops;
  142. for (let i = 0; i < data.userShops.length; ++ i) {
  143. let shopData = userShops[i];
  144. GameModule.userInfo.shop.handelShop(shopData);
  145. GameModule.userInfo.shop.updateTimeSchedule();
  146. /// 处理信息流信息
  147. GameModule.user.shop.handleShpDataToMessageList(shopData);
  148. /// 说明购买了金币加速器,那么需要发通知告知商城的金币页面重新拉去数据
  149. if (shopData.shopId == 13 || shopData.shopId == 14 || shopData.shopId == 15) {
  150. GameEvent.fire('storeCoin_need_update');
  151. }
  152. }
  153. return;
  154. }
  155. if (TapTapTool.compare(Global._buyStarGold, Global.userData.goldObj)) {
  156. Global._redTypes.push(GameRedDot.star);
  157. }
  158. //// 下面这段代码只是去重,不要重复的更新
  159. let isContain = false;
  160. for (let i = 0; i < data.type.length; ++ i) {
  161. /// 如果包含它就什么都不管
  162. let type = data.type[i];
  163. if (Global._redTypes.indexOf(type) != -1) {
  164. isContain = true;
  165. } else {
  166. Global._redTypes.push(type);
  167. isContain = false;
  168. }
  169. }
  170. if (isContain == false) {
  171. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  172. // this.handelShowRedDot();
  173. }
  174. }
  175. });
  176. ws.on('close', () => {
  177. console.log('socket 关闭');
  178. });
  179. ws.on('error', () => {
  180. console.log('sockete 错误');
  181. });
  182. },
  183. /// 获取信息流
  184. _getInformation() {
  185. Global._redTypes = [];
  186. Global._timeInformations = [];
  187. Global._fixInformations = [];
  188. Global._gold10 = {'n': 0, 'e': 0};
  189. Global._buyStarGold = {'n': 0, 'e': 0};
  190. this.getInformation().then((respondData) => {
  191. console.log(respondData);
  192. Global._gold10 = respondData.gold10;
  193. Global._buyStarGold = respondData.buyStarGold;
  194. Global._isSkillReset = respondData.isSkillReset;
  195. if (respondData.types !== undefined) {
  196. Global._redTypes = respondData.types;
  197. }
  198. if (TapTapTool.compare(Global.userData.goldObj, respondData.buyStarGold)) {
  199. Global._redTypes.push(GameRedDot.star);
  200. }
  201. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  202. // this.handelShowRedDot();
  203. //// 更新信息流数据
  204. if (respondData.informations !== undefined) {
  205. Global._timeInformations = respondData.informations;
  206. /// 如果是技能可以重置
  207. if (respondData.isSkillReset) {
  208. /// 添加技能可重置的信息流
  209. let objct = {'cdTime': 0, 'desc': '重置技能可使用', 'name': '重置技能', 'icon': 900012, 'sId': 4, 'type': 2, 'skillStatus': 0};
  210. Global._timeInformations.push(objct);
  211. }
  212. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, Global._timeInformations.length);
  213. }
  214. }).catch(({code, msg}) => {
  215. console.log(code, msg);
  216. });
  217. },
  218. /// 网络请求
  219. getInformation() {
  220. return new Promise((resolve, reject) => {
  221. // 获取目标用户的建筑
  222. HomeApi.getInformation((respondData) => {
  223. resolve(respondData);
  224. }, (code, msg) => {
  225. reject({code, msg});
  226. });
  227. })
  228. },
  229. onDestroy() {
  230. GameEvent.off(GameNotificationKey.GameShowAdditionTips, this);
  231. GameEvent.off(GameNotificationKey.GameShowGroupRank, this);
  232. },
  233. _showSidebarUI() {
  234. let isOk = false;
  235. //教程31还没有完成不显示
  236. if (!this.homeGuide.guideState.state31.pass) {
  237. // 1. 总部大楼大于25级
  238. // 2. 已拥有1个明星
  239. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  240. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  241. if (unLockStatus1 && unLockStatus2) {
  242. isOk = true
  243. }
  244. } else {
  245. isOk = true;
  246. }
  247. if (!isOk) {
  248. return;
  249. }
  250. this.isShowBar = true;
  251. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  252. this.sidebar.runAction(action);
  253. },
  254. finishState29() {
  255. if (this.isShowBar) {
  256. return;
  257. }
  258. this.isShowBar = true;
  259. let action = cc.moveBy(0.3, cc.v2(-this.sidebar.width, 0));
  260. this.sidebar.runAction(action);
  261. },
  262. handelShowRedDot() {
  263. if (Global._redTypes == null || Global._redTypes == undefined || Global._redTypes.length == 0) {
  264. this.taskRedNode.active = false;
  265. this.drawRedNode.active = false;
  266. this.storeRedNode.active = false;
  267. return;
  268. }
  269. let redTypes = Global._redTypes;
  270. this.taskRedNode.active = (redTypes.indexOf(GameRedDot.mainTask) != -1 || redTypes.indexOf(GameRedDot.dayTask) != -1);
  271. this.drawRedNode.active = redTypes.indexOf(GameRedDot.draw) != -1;
  272. let isStoreRed = (redTypes.indexOf(GameRedDot.storeDiamond) != -1 || redTypes.indexOf(GameRedDot.storeCoin) != -1);
  273. this.storeRedNode.active = isStoreRed;
  274. this._storeRedNode = isStoreRed;
  275. if (isStoreRed) {
  276. if (this._isTimer === false) {
  277. this._isTimer = true;
  278. this._storeTimeCount = 0;
  279. this.storeRedNode.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.2, 3, 0), cc.moveBy(0.2, -3, 0))));
  280. this.schedule(this.storeTimeAction, 6);
  281. }
  282. } else {
  283. if (this._isTimer == true) {
  284. this.unschedule(this.storeTimeAction, this);
  285. this._isTimer = false;
  286. }
  287. }
  288. },
  289. /// 商城未读消息的显示
  290. storeTimeAction() {
  291. this._storeTimeCount += 1;
  292. /// 第一个6秒的时候
  293. if (this._storeTimeCount === 1) {
  294. this.storeRedNode.active = false;
  295. } else if (this._storeTimeCount === 6) {
  296. this.storeRedNode.active = true;
  297. this._storeTimeCount = 0;
  298. this._isTimer = false;
  299. this.storeRedNode.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.2, 3, 0), cc.moveBy(0.2, -3, 0))));
  300. }
  301. },
  302. //
  303. handleQuestPopup: _.debounce((event) => {
  304. AlertManager.showQuestPopup();
  305. // if (event) {
  306. // event.target.getChildByName("notice_point").active = false;
  307. // }
  308. }, 1000, true),
  309. //显示抽奖界面
  310. handleShowDraw: _.debounce((event) => {
  311. AlertManager.showDrawAlert();
  312. }, 1000, true),
  313. //显示商城界面
  314. handleShowStore: _.debounce((event) => {
  315. AlertManager.showStoreAlert();
  316. // GameEvent.fire(GameNotificationKey.StarLeaveRoom, 1, 1);
  317. // GameEvent.fire(GameNotificationKey.AllStarLeaveRoom);
  318. }, 1000, true),
  319. //显示排行榜界面
  320. handleShowRank: _.debounce((event) => {
  321. AlertManager.showRankAlert();
  322. }, 1000, true),
  323. showGroupRank() {
  324. AlertManager.showRankAlert(2);
  325. },
  326. showAdditionTips(text, type) {
  327. if (this.node.active) {
  328. this.additionTipsNode.show(text, type);
  329. }
  330. },
  331. // update (dt) {},
  332. });