1
0

LevelHome.js 12 KB

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