Game.js 16 KB

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