LevelHome.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. const GameModule = require("../utils/GameModule");
  2. const {GameNotificationKey} = require('../utils/GameEnum');
  3. const AlertManager = require('../utils/AlertManager');
  4. const TapTapTool = require("../utils/TapTapTool");
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. scrollView: cc.ScrollView,
  9. clickAddMoneyPrefab: cc.Prefab,
  10. _unlockBuilding: [],
  11. showOffLineUI: true,
  12. },
  13. // LIFE-CYCLE CALLBACKS:
  14. onLoad () {
  15. this.minContentPosition = 0;
  16. this.buildings = [];
  17. this.unlockBuilding = [];
  18. this.setEventListener();
  19. this.addMoneyPool = new cc.NodePool();
  20. this.scrollViewMng = this.scrollView.getComponent('LevelHomeListAdapter');
  21. // this._adState = 0;
  22. // this.initRoomAd();
  23. },
  24. setEventListener() {
  25. GameEvent.on(GameNotificationKey.RefreshBuildingData, this, (buildingModel) => {
  26. this.refreshAllbuildingGoldRate(buildingModel);
  27. });
  28. GameEvent.on(GameNotificationKey.UnlockLevelHome, this, (buildingModel) => {
  29. this.unlockLevelHome(buildingModel);
  30. });
  31. GameEvent.on(GameNotificationKey.GetRoomAward, this, (responseData) => {
  32. this.getRoomAward(responseData);
  33. });
  34. GameModule.homeGuide.on('Fire_state33', this.configSignIn, this);
  35. },
  36. /**
  37. * home 初始化方法, 所有的初始化操作在这里操作, 必须在加入父节点之前调用
  38. * */
  39. init(uid) {
  40. this.uid = uid;
  41. this.node.setContentSize(cc.view.getVisibleSize());
  42. this.buildingInfos = [];
  43. this.node.parent = cc.find("Canvas/game");
  44. // this.refreshTheme();
  45. this.getNetworkData();
  46. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state1','state4');
  47. },
  48. //
  49. clickAddMoney (position) {
  50. let item = null;
  51. if (this.addMoneyPool.size() > 0) {
  52. item = this.addMoneyPool.get();
  53. } else {
  54. item = cc.instantiate(this.clickAddMoneyPrefab);
  55. }
  56. this.node.addChild(item);
  57. item.x = position.x;
  58. item.y = position.y;
  59. item.active = true;
  60. let itemManager = item.getComponent('ClickAddMoney');
  61. itemManager.showAddMoney( () => {
  62. this.addMoneyPool.put(item);
  63. });
  64. },
  65. start () {
  66. },
  67. sortNumber(a,b) {
  68. return a - b
  69. },
  70. //数据排序,roomId小的在前
  71. compareFunction (a, b) {
  72. if (a.roomId < b.roomId) {
  73. return 1; // a排在b的前面
  74. } else if (a.roomId > b.roomId) {
  75. return -1; // a排在b的后面
  76. } else {
  77. return 0; // a和b的位置保持不变
  78. }
  79. },
  80. getNetworkData(callback) {
  81. this.getUserBuildings()
  82. .then((userRooms) => {
  83. // 清空数据
  84. this.buildingInfos = [];
  85. let sortArray = userRooms.sort(this.compareFunction);
  86. // 离线收益金币数量
  87. // let offlineGrossIncome = 0;
  88. sortArray.map((value, index, array) => {
  89. let model = Global.BuildingManager.getBuildingInfo(value.roomId, value.roomLevel);
  90. this._unlockBuilding[index] = model.isUnlocked ? 1 : 0;
  91. if (value.roomStars == undefined) {
  92. model.roomStars = [];
  93. } else {
  94. model.roomStars = value.roomStars;
  95. }
  96. model.isUnlocked = value.isUnlocked;
  97. model.roomMt = value.roomMt;
  98. model.awardCount = value.awardCount;
  99. this.buildingInfos.push(model);
  100. });
  101. this._unlockBuilding = this._unlockBuilding.reverse();
  102. // GameModule.userInfo.setUserInfo(responseData.user);
  103. // this.resetPaddingBottom();
  104. callback && callback();
  105. // 开始设置建筑
  106. this.configBuildings();
  107. if (GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state1') && GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state4')) {
  108. // 离线收益处理
  109. this.configOffIncome();
  110. //每日签到奖励
  111. this.configSignIn();
  112. }
  113. //第一层楼是否已经解锁
  114. if (!Global.BuildingManager.getRoomIsUnlocked(1) || Global.BuildingManager.getRoomLevel(1) < 5) {
  115. let guide = GameModule.homeGuide.getComponent('HomeGuide');
  116. if (guide.isPassGuideState('state15') || guide.isPassGuideState('state16') || guide.isPassGuideState('state17') || guide.isPassGuideState('state20')) {
  117. GameModule.homeGuide.getComponent('HomeGuide').handleState('state21');
  118. }
  119. this.scrollViewMng.configGuide();
  120. } else if (Global.BuildingManager.getRoomLevel(1) < 25) {
  121. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state21', 'state24');
  122. this.scrollViewMng.configGuide();
  123. }
  124. })
  125. .then((buildingItems) => {
  126. for (const roomId in buildingItems) {
  127. let itemScript;
  128. let filterList = this.buildings.filter( item => item.buildingInfo.roomId === parseInt(roomId) ) || [];
  129. if (filterList.length > 0) { itemScript = filterList[0]; }
  130. if (buildingItems.hasOwnProperty(roomId)) {
  131. let prop = buildingItems[roomId];
  132. itemScript.configProp(prop);
  133. }
  134. }
  135. })
  136. .catch((err) => {
  137. console.log(err);
  138. });
  139. },
  140. getUserBuildings() {
  141. return new Promise((resolve, reject) => {
  142. // 返回用户的建筑等级
  143. if (Global.BuildingManager.networkRooms && Global.BuildingManager.networkRooms.length > 0) {
  144. resolve(Global.BuildingManager.networkRooms);
  145. } else {
  146. reject('error');
  147. }
  148. })
  149. },
  150. configBuildings() {
  151. this.refreshAllbuildingGoldRate();
  152. let sortLockRoom = 0;
  153. this.buildingInfos.forEach(n => {
  154. if (n.isUnlocked == 0) {
  155. sortLockRoom += 1;
  156. }
  157. });
  158. if (sortLockRoom == 0) {
  159. this.scrollViewMng.configBuildings(this.buildingInfos);
  160. } else {
  161. let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length);
  162. this.scrollViewMng.configBuildings(sortArray);
  163. }
  164. GameModule.roomInfo = this.buildingInfos;
  165. },
  166. unlockLevelHome(buildingModel) {
  167. this.buildingInfos.forEach((item, index) => {
  168. if (item.roomId == buildingModel.roomId) {
  169. this.buildingInfos[index] = buildingModel;
  170. }
  171. });
  172. let sortLockRoom = 0;
  173. this.buildingInfos.forEach(n => {
  174. if (n.isUnlocked == 0) {
  175. sortLockRoom += 1;
  176. }
  177. });
  178. // 等于0为所有建筑已开锁
  179. if (sortLockRoom != 0) {
  180. let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length);
  181. this.scrollViewMng.configBuildings(sortArray, true);
  182. }
  183. GameModule.roomInfo = this.buildingInfos;
  184. },
  185. /**
  186. * 离线收益处理
  187. * @param {Array} sortArray 用户当前城市buildingInfos数组
  188. * @param {Number} offlineGrossIncome 离线收益金币数量
  189. */
  190. configOffIncome() {
  191. let offlineGold = Global.offlineGold;
  192. // AlertManager.showOfflineGrossIncome(offlineGold);
  193. // return;
  194. let lastTime = cc.sys.localStorage.getItem('offlineLastTime');
  195. if (!lastTime) {
  196. //缓存被删除后离线收益也要弹出
  197. lastTime = new Date().getTime();
  198. cc.sys.localStorage.setItem('offlineLastTime', lastTime);
  199. if (offlineGold.n <= 0 ) {
  200. return;
  201. }
  202. // 显示离线收益的条件:
  203. // 2. 总部大楼大于25级
  204. // 3. 已拥有1个明星
  205. // 4. 已签到过一次
  206. let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25;
  207. let unLockStatus3 = GameModule.userInfo.buyStarCount > 0;
  208. let unLockStatus4 = Global.signCount > 0;
  209. // showOffLineUI: 用户每次进入游戏离线收益只会显示1次
  210. if (this.showOffLineUI && unLockStatus2 && unLockStatus3 && unLockStatus4) {
  211. this.showOffLineUI = false;
  212. AlertManager.showOfflineGrossIncome(offlineGold);
  213. }
  214. } else {
  215. if (offlineGold.n <= 0 ) {
  216. return;
  217. }
  218. let curTime = new Date().getTime();
  219. cc.sys.localStorage.setItem('offlineLastTime', curTime);
  220. // 显示离线收益的条件:
  221. // 1. 离上一次显示超过5分钟
  222. // 2. 总部大楼大于25级
  223. // 3. 已拥有1个明星
  224. // 4. 已签到过一次
  225. let unLockStatus1 = curTime - lastTime > 300 * 1000;
  226. // let unLockStatus1 = curTime - lastTime > 1 * 1000;
  227. let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25;
  228. let unLockStatus3 = GameModule.userInfo.buyStarCount > 0;
  229. let unLockStatus4 = Global.signCount > 0;
  230. // showOffLineUI: 用户每次进入游戏离线收益只会显示1次
  231. if (this.showOffLineUI && unLockStatus1 && unLockStatus2 && unLockStatus3 && unLockStatus4) {
  232. this.showOffLineUI = false;
  233. AlertManager.showOfflineGrossIncome(offlineGold);
  234. }
  235. }
  236. },
  237. configSignIn() {
  238. // 1. 总部大楼大于25级
  239. // 2. 已拥有1个明星
  240. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  241. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  242. if (!Global.isSignAward && unLockStatus1 && unLockStatus2) {
  243. if (Global.signCount == 0) {
  244. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state31', 'state34');
  245. }
  246. AlertManager.showSignInAlert();
  247. }
  248. },
  249. //刷新建筑每秒生产金币
  250. refreshAllbuildingGoldRate(buildingModel) {
  251. if (buildingModel) {
  252. this.buildingInfos.forEach((item, index) => {
  253. if (item.roomId == buildingModel.roomId) {
  254. this.buildingInfos[index] = buildingModel;
  255. }
  256. });
  257. this.scrollViewMng.refreshRoomData(buildingModel);
  258. }
  259. var totalRate = {"n": 0, 'e': 0};
  260. this.buildingInfos.forEach(n => {
  261. if (n.isUnlocked) {
  262. let gold1 = TapTapTool.goldStrToClass(n.gold1);
  263. let gold2 = TapTapTool.goldStrToClass(n.gold2);
  264. let roomMt = TapTapTool.goldStrToClass(n.roomMt);
  265. var roomRate = TapTapTool.multiple(gold1, n.level);
  266. roomRate = TapTapTool.add(roomRate, gold2);
  267. roomRate = TapTapTool.multiple(roomRate,roomMt);
  268. totalRate = TapTapTool.add(totalRate, roomRate);
  269. }
  270. });
  271. GameModule.userInfo.rateGold = totalRate;
  272. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  273. cc.sys.localStorage.setItem(`localRooms_${Global.user.uid}`, JSON.stringify(this.buildingInfos));
  274. },
  275. //获取房间里程碑
  276. getRoomAward(responseData) {
  277. this.buildingInfos.forEach((item, index) => {
  278. if (item.roomId == responseData.roomId) {
  279. item.awardCount = responseData.awardCount;
  280. item.roomMt = responseData.roomMt;
  281. }
  282. });
  283. this.refreshAllbuildingGoldRate();
  284. }
  285. });