LevelHome.js 11 KB

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