LevelHome.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. const GameModule = require("../utils/GameModule");
  2. const {GameNotificationKey, LevelHomeNotificationKey, WechatShareType} = require('../utils/GameEnum');
  3. const AlertManager = require('../utils/AlertManager');
  4. const TapTapTool = require("../utils/TapTapTool");
  5. const ADVideo = require('../utils/ADVideo');
  6. const WeChat = require('../net/WeChat');
  7. const HomeApi = require('../net/HomeApi');
  8. const DWTool = require('../utils/DWTool');
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. scrollView: cc.ScrollView,
  13. clickAddMoneyPrefab: cc.Prefab,
  14. _unlockBuilding: [],
  15. showOffLineUI: true,
  16. },
  17. // LIFE-CYCLE CALLBACKS:
  18. onLoad () {
  19. this.minContentPosition = 0;
  20. this.buildings = [];
  21. this.unlockBuilding = [];
  22. this.setEventListener();
  23. this.addMoneyPool = new cc.NodePool();
  24. this.scrollViewMng = this.scrollView.getComponent('LevelHomeListAdapter');
  25. // this._adState = 0;
  26. // this.initRoomAd();
  27. },
  28. setEventListener() {
  29. GameEvent.on(LevelHomeNotificationKey.LevelHomePlayVideoAd, this, (roomId) => {
  30. this.levelHomeWatchVide(roomId);
  31. });
  32. GameEvent.on(LevelHomeNotificationKey.LevelHomeShareUpdate, this, (roomId) => {
  33. this.levelHomeShare(roomId);
  34. });
  35. /// cd时间到了
  36. // GameEvent.on(GameNotificationKey.AdRoomStateUpdate, this, this.initRoomAd);
  37. GameEvent.on(GameNotificationKey.RefreshBuildingData, this, (buildingModel) => {
  38. this.refreshAllbuildingGoldRate(buildingModel);
  39. });
  40. GameEvent.on(GameNotificationKey.UnlockLevelHome, this, (buildingModel) => {
  41. this.unlockLevelHome(buildingModel);
  42. });
  43. GameEvent.on(GameNotificationKey.GetRoomAward, this, (responseData) => {
  44. this.getRoomAward(responseData);
  45. });
  46. GameModule.homeGuide.on('Fire_state33', this.configSignIn, this);
  47. },
  48. /**
  49. * home 初始化方法, 所有的初始化操作在这里操作, 必须在加入父节点之前调用
  50. * */
  51. init(uid) {
  52. this.uid = uid;
  53. this.node.setContentSize(cc.view.getVisibleSize());
  54. this.buildingInfos = [];
  55. this.node.parent = cc.find("Canvas/game");
  56. // this.refreshTheme();
  57. this.getNetworkData();
  58. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state1','state4');
  59. },
  60. //
  61. clickAddMoney (position) {
  62. let item = null;
  63. if (this.addMoneyPool.size() > 0) {
  64. item = this.addMoneyPool.get();
  65. } else {
  66. item = cc.instantiate(this.clickAddMoneyPrefab);
  67. }
  68. this.node.addChild(item);
  69. item.x = position.x;
  70. item.y = position.y;
  71. item.active = true;
  72. let itemManager = item.getComponent('ClickAddMoney');
  73. itemManager.showAddMoney( () => {
  74. this.addMoneyPool.put(item);
  75. });
  76. },
  77. start () {
  78. },
  79. sortNumber(a,b) {
  80. return a - b
  81. },
  82. //数据排序,roomId小的在前
  83. compareFunction (a, b) {
  84. if (a.roomId < b.roomId) {
  85. return 1; // a排在b的前面
  86. } else if (a.roomId > b.roomId) {
  87. return -1; // a排在b的后面
  88. } else {
  89. return 0; // a和b的位置保持不变
  90. }
  91. },
  92. getNetworkData(callback) {
  93. this.getUserBuildings()
  94. .then((userRooms) => {
  95. // 清空数据
  96. this.buildingInfos = [];
  97. let sortArray = userRooms.sort(this.compareFunction);
  98. // 离线收益金币数量
  99. // let offlineGrossIncome = 0;
  100. sortArray.map((value, index, array) => {
  101. let model = Global.BuildingManager.getBuildingInfo(value.roomId, value.roomLevel);
  102. this._unlockBuilding[index] = model.isUnlocked ? 1 : 0;
  103. if (value.roomStars == undefined) {
  104. model.roomStars = [];
  105. } else {
  106. model.roomStars = value.roomStars;
  107. }
  108. model.isUnlocked = value.isUnlocked;
  109. model.roomMt = value.roomMt;
  110. model.awardCount = value.awardCount;
  111. this.buildingInfos.push(model);
  112. });
  113. this._unlockBuilding = this._unlockBuilding.reverse();
  114. // GameModule.userInfo.setUserInfo(responseData.user);
  115. // this.resetPaddingBottom();
  116. callback && callback();
  117. // 开始设置建筑
  118. this.configBuildings();
  119. if (GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state1') && GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state4')) {
  120. // 离线收益处理
  121. this.configOffIncome();
  122. //每日签到奖励
  123. this.configSignIn();
  124. }
  125. //第一层楼是否已经解锁
  126. if (!Global.BuildingManager.getRoomIsUnlocked(1) || Global.BuildingManager.getRoomLevel(1) < 5) {
  127. let guide = GameModule.homeGuide.getComponent('HomeGuide');
  128. if (guide.isPassGuideState('state15') || guide.isPassGuideState('state16') || guide.isPassGuideState('state17') || guide.isPassGuideState('state20')) {
  129. GameModule.homeGuide.getComponent('HomeGuide').handleState('state21');
  130. }
  131. this.scrollViewMng.configGuide();
  132. } else if (Global.BuildingManager.getRoomLevel(1) < 25) {
  133. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state21', 'state24');
  134. this.scrollViewMng.configGuide();
  135. }
  136. })
  137. .then((buildingItems) => {
  138. for (const roomId in buildingItems) {
  139. let itemScript;
  140. let filterList = this.buildings.filter( item => item.buildingInfo.roomId === parseInt(roomId) ) || [];
  141. if (filterList.length > 0) { itemScript = filterList[0]; }
  142. if (buildingItems.hasOwnProperty(roomId)) {
  143. let prop = buildingItems[roomId];
  144. itemScript.configProp(prop);
  145. }
  146. }
  147. })
  148. .catch((err) => {
  149. console.log(err);
  150. });
  151. },
  152. getUserBuildings() {
  153. return new Promise((resolve, reject) => {
  154. // 返回用户的建筑等级
  155. if (Global.BuildingManager.networkRooms && Global.BuildingManager.networkRooms.length > 0) {
  156. resolve(Global.BuildingManager.networkRooms);
  157. } else {
  158. reject('error');
  159. }
  160. })
  161. },
  162. configBuildings() {
  163. this.refreshAllbuildingGoldRate();
  164. let sortLockRoom = 0;
  165. this.buildingInfos.forEach(n => {
  166. if (n.isUnlocked == 0) {
  167. sortLockRoom += 1;
  168. }
  169. });
  170. if (sortLockRoom == 0) {
  171. this.scrollViewMng.configBuildings(this.buildingInfos);
  172. } else {
  173. let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length);
  174. this.scrollViewMng.configBuildings(sortArray);
  175. }
  176. GameModule.roomInfo = this.buildingInfos;
  177. },
  178. unlockLevelHome(buildingModel) {
  179. this.buildingInfos.forEach((item, index) => {
  180. if (item.roomId == buildingModel.roomId) {
  181. this.buildingInfos[index] = buildingModel;
  182. }
  183. });
  184. let sortLockRoom = 0;
  185. this.buildingInfos.forEach(n => {
  186. if (n.isUnlocked == 0) {
  187. sortLockRoom += 1;
  188. }
  189. });
  190. // 等于0为所有建筑已开锁
  191. if (sortLockRoom != 0) {
  192. let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length);
  193. this.scrollViewMng.configBuildings(sortArray, true);
  194. }
  195. GameModule.roomInfo = this.buildingInfos;
  196. },
  197. /**
  198. * 离线收益处理
  199. * @param {Array} sortArray 用户当前城市buildingInfos数组
  200. * @param {Number} offlineGrossIncome 离线收益金币数量
  201. */
  202. configOffIncome() {
  203. let offlineGold = Global.offlineGold;
  204. // AlertManager.showOfflineGrossIncome(offlineGold);
  205. // return;
  206. let lastTime = cc.sys.localStorage.getItem('offlineLastTime');
  207. if (!lastTime) {
  208. //缓存被删除后离线收益也要弹出
  209. lastTime = new Date().getTime();
  210. cc.sys.localStorage.setItem('offlineLastTime', lastTime);
  211. if (offlineGold.n <= 0 ) {
  212. return;
  213. }
  214. // 显示离线收益的条件:
  215. // 2. 总部大楼大于25级
  216. // 3. 已拥有1个明星
  217. // 4. 已签到过一次
  218. let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25;
  219. let unLockStatus3 = GameModule.userInfo.buyStarCount > 0;
  220. let unLockStatus4 = Global.signCount > 0;
  221. // showOffLineUI: 用户每次进入游戏离线收益只会显示1次
  222. if (this.showOffLineUI && unLockStatus2 && unLockStatus3 && unLockStatus4) {
  223. this.showOffLineUI = false;
  224. AlertManager.showOfflineGrossIncome(offlineGold);
  225. }
  226. } else {
  227. if (offlineGold.n <= 0 ) {
  228. return;
  229. }
  230. let curTime = new Date().getTime();
  231. cc.sys.localStorage.setItem('offlineLastTime', curTime);
  232. // 显示离线收益的条件:
  233. // 1. 离上一次显示超过5分钟
  234. // 2. 总部大楼大于25级
  235. // 3. 已拥有1个明星
  236. // 4. 已签到过一次
  237. let unLockStatus1 = curTime - lastTime > 300 * 1000;
  238. // let unLockStatus1 = curTime - lastTime > 1 * 1000;
  239. let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25;
  240. let unLockStatus3 = GameModule.userInfo.buyStarCount > 0;
  241. let unLockStatus4 = Global.signCount > 0;
  242. // showOffLineUI: 用户每次进入游戏离线收益只会显示1次
  243. if (this.showOffLineUI && unLockStatus1 && unLockStatus2 && unLockStatus3 && unLockStatus4) {
  244. this.showOffLineUI = false;
  245. AlertManager.showOfflineGrossIncome(offlineGold);
  246. }
  247. }
  248. },
  249. configSignIn() {
  250. // 1. 总部大楼大于25级
  251. // 2. 已拥有1个明星
  252. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  253. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  254. if (!Global.isSignAward && unLockStatus1 && unLockStatus2) {
  255. if (Global.signCount == 0) {
  256. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state31', 'state34');
  257. }
  258. AlertManager.showSignInAlert();
  259. }
  260. },
  261. //刷新建筑每秒生产金币
  262. refreshAllbuildingGoldRate(buildingModel) {
  263. if (buildingModel) {
  264. this.buildingInfos.forEach((item, index) => {
  265. if (item.roomId == buildingModel.roomId) {
  266. this.buildingInfos[index] = buildingModel;
  267. }
  268. });
  269. this.scrollViewMng.refreshRoomData(buildingModel);
  270. }
  271. var totalRate = {"n": 0, 'e': 0};
  272. this.buildingInfos.forEach(n => {
  273. if (n.isUnlocked) {
  274. let gold1 = TapTapTool.goldStrToClass(n.gold1);
  275. let gold2 = TapTapTool.goldStrToClass(n.gold2);
  276. let roomMt = TapTapTool.goldStrToClass(n.roomMt);
  277. var roomRate = TapTapTool.multiple(gold1, n.level);
  278. roomRate = TapTapTool.add(roomRate, gold2);
  279. roomRate = TapTapTool.multiple(roomRate,roomMt);
  280. totalRate = TapTapTool.add(totalRate, roomRate);
  281. }
  282. });
  283. GameModule.userInfo.rateGold = totalRate;
  284. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  285. cc.sys.localStorage.setItem(`localRooms_${Global.user.uid}`, JSON.stringify(this.buildingInfos));
  286. },
  287. //获取房间里程碑
  288. getRoomAward(responseData) {
  289. this.buildingInfos.forEach((item, index) => {
  290. if (item.roomId == responseData.roomId) {
  291. item.awardCount = responseData.awardCount;
  292. item.roomMt = responseData.roomMt;
  293. }
  294. });
  295. this.refreshAllbuildingGoldRate();
  296. },
  297. //父层控制所有广告状态
  298. initRoomAd() {
  299. if (!CC_WECHATGAME) {
  300. return;
  301. }
  302. /// 如果已经达到当天最大的次数
  303. if (DWTool.checkIsOldUser() && Global._upRoomInfo != undefined && Global._upRoomInfo.count < Global._upRoomInfo.maxCount && Global._upRoomInfo.cdTime <= 0) {
  304. // if (DWTool.checkIsOldUser()) {
  305. if (this._video != undefined) {
  306. return;
  307. }
  308. let video = new ADVideo();
  309. video.wxCreateADVideo(ADVideo.roomAdId);
  310. this._video = video;
  311. /// 说明视频加载成功
  312. video.on('loadSuccess', (adId) => {
  313. if (adId === ADVideo.roomAdId) {
  314. this._adState = 2;
  315. this.refreshWatchVideoUI();
  316. }
  317. });
  318. video.on('adErr', () => {
  319. this._adState = 1;
  320. this.refreshWatchVideoUI();
  321. });
  322. video.on ('close', (isFinish) => {
  323. //判断是否完整看完视频
  324. if (isFinish) {
  325. this.finishVideoOrShare();
  326. } else {
  327. GameEvent.fire(LevelHomeNotificationKey.LevelHomeFreeUpdateError, this.adRoomId);
  328. }
  329. });
  330. } else {
  331. if (this._adState != 0) {
  332. this._adState = 0;
  333. this.refreshWatchVideoUI();
  334. }
  335. }
  336. },
  337. //刷新观看视频或者分享文案
  338. refreshWatchVideoUI() {
  339. GameEvent.fire(LevelHomeNotificationKey.RefreshLevelHomeAdType, this._adState);
  340. },
  341. finishVideoOrShare() {
  342. HomeApi.reportInformation(2, () => {
  343. Global._upRoomInfo.count += 1;
  344. /// 时间重置为5分钟
  345. Global._upRoomInfo.cdTime = 5 * 60 * 1000;
  346. if (this.adRoomId > 0) {
  347. GameEvent.fire(LevelHomeNotificationKey.LevelHomeVideoClose, this.adRoomId);
  348. this.adRoomId = 0;
  349. }
  350. this._adState = 0;
  351. this.refreshWatchVideoUI();
  352. }, (code, msg) => {
  353. GameEvent.fire(LevelHomeNotificationKey.LevelHomeFreeUpdateError, this.adRoomId);
  354. });
  355. },
  356. levelHomeWatchVide(roomId) {
  357. this.adRoomId = roomId;
  358. this._video.showVideoId(ADVideo.roomAdId);
  359. },
  360. levelHomeShare(roomId) {
  361. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
  362. if (type == WechatShareType.UpRoomLevel) {
  363. this.adRoomId = roomId;
  364. this.finishVideoOrShare();
  365. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  366. }
  367. });
  368. WeChat.shareAction(WechatShareType.UpRoomLevel, () => {
  369. }, () => {
  370. console.log('分享失败或取消');
  371. });
  372. },
  373. update() {
  374. // this.initRoomAd();
  375. }
  376. });