LevelHome.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. // 获取目标用户的建筑
  133. // HomeApi.getUserBuildings((responseData) => {
  134. // resolve(responseData);
  135. // }, (error) => {
  136. // reject(error);
  137. // });
  138. return new Promise((resolve, reject) => {
  139. if (GameGlobal.BuildingManager.networkRooms && GameGlobal.BuildingManager.networkRooms.length > 0) {
  140. resolve(GameGlobal.BuildingManager.networkRooms);
  141. } else {
  142. reject('error');
  143. }
  144. })
  145. // if (GameGlobal.BuildingManager.networkRooms && GameGlobal.BuildingManager.networkRooms.length > 0) {
  146. // success(GameGlobal.BuildingManager.networkRooms);
  147. // } else {
  148. // fail('error');
  149. // }
  150. },
  151. configBuildings() {
  152. this.refreshAllbuildingGoldRate();
  153. let sortLockRoom = 0;
  154. this.buildingInfos.forEach(n => {
  155. if (n.isUnlocked == 0) {
  156. sortLockRoom += 1;
  157. }
  158. });
  159. if (sortLockRoom == 0) {
  160. this.scrollViewMng.configBuildings(this.buildingInfos);
  161. } else {
  162. let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length);
  163. this.scrollViewMng.configBuildings(sortArray);
  164. }
  165. GameModule.roomInfo = this.buildingInfos;
  166. },
  167. unlockLevelHome(buildingModel) {
  168. this.buildingInfos.forEach((item, index) => {
  169. if (item.roomId == buildingModel.roomId) {
  170. this.buildingInfos[index] = buildingModel;
  171. }
  172. });
  173. let sortLockRoom = 0;
  174. this.buildingInfos.forEach(n => {
  175. if (n.isUnlocked == 0) {
  176. sortLockRoom += 1;
  177. }
  178. });
  179. // 等于0为所有建筑已开锁
  180. if (sortLockRoom != 0) {
  181. let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length);
  182. this.scrollViewMng.configBuildings(sortArray, true);
  183. }
  184. GameModule.roomInfo = this.buildingInfos;
  185. },
  186. /**
  187. * 离线收益处理
  188. * @param {Array} sortArray 用户当前城市buildingInfos数组
  189. * @param {Number} offlineGrossIncome 离线收益金币数量
  190. */
  191. configOffIncome() {
  192. let offlineGold = GameGlobal.offlineGold;
  193. // AlertManager.showOfflineGrossIncome(offlineGold);
  194. // return;
  195. let lastTime = cc.sys.localStorage.getItem('offlineLastTime');
  196. if (!lastTime) {
  197. //缓存被删除后离线收益也要弹出
  198. lastTime = new Date().getTime();
  199. cc.sys.localStorage.setItem('offlineLastTime', lastTime);
  200. if (offlineGold.n <= 0 ) {
  201. return;
  202. }
  203. // 显示离线收益的条件:
  204. // 2. 总部大楼大于25级
  205. // 3. 已拥有1个明星
  206. // 4. 已签到过一次
  207. let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25;
  208. let unLockStatus3 = GameModule.userInfo.buyStarCount > 0;
  209. let unLockStatus4 = GameGlobal.signCount > 0;
  210. // showOffLineUI: 用户每次进入游戏离线收益只会显示1次
  211. if (this.showOffLineUI && unLockStatus2 && unLockStatus3 && unLockStatus4) {
  212. this.showOffLineUI = false;
  213. AlertManager.showOfflineGrossIncome(offlineGold);
  214. }
  215. } else {
  216. if (offlineGold.n <= 0 ) {
  217. return;
  218. }
  219. let curTime = new Date().getTime();
  220. cc.sys.localStorage.setItem('offlineLastTime', curTime);
  221. // 显示离线收益的条件:
  222. // 1. 离上一次显示超过5分钟
  223. // 2. 总部大楼大于25级
  224. // 3. 已拥有1个明星
  225. // 4. 已签到过一次
  226. let unLockStatus1 = curTime - lastTime > 300 * 1000;
  227. let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25;
  228. let unLockStatus3 = GameModule.userInfo.buyStarCount > 0;
  229. let unLockStatus4 = GameGlobal.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 (!GameGlobal.isSignAward && unLockStatus1 && unLockStatus2) {
  243. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state31', 'state34');
  244. AlertManager.showSignInAlert();
  245. }
  246. },
  247. //刷新建筑每秒生产金币
  248. refreshAllbuildingGoldRate(buildingModel) {
  249. if (buildingModel) {
  250. this.buildingInfos.forEach((item, index) => {
  251. if (item.roomId == buildingModel.roomId) {
  252. this.buildingInfos[index] = buildingModel;
  253. }
  254. });
  255. this.scrollViewMng.refreshRoomData(buildingModel);
  256. }
  257. var totalRate = {"n": 0, 'e': 0};
  258. this.buildingInfos.forEach(n => {
  259. if (n.isUnlocked) {
  260. let gold1 = TapTapTool.goldStrToClass(n.gold1);
  261. let gold2 = TapTapTool.goldStrToClass(n.gold2);
  262. let roomMt = TapTapTool.goldStrToClass(n.roomMt);
  263. var roomRate = TapTapTool.multiple(gold1, n.level);
  264. roomRate = TapTapTool.add(roomRate, gold2);
  265. roomRate = TapTapTool.multiple(roomRate,roomMt);
  266. totalRate = TapTapTool.add(totalRate, roomRate);
  267. }
  268. });
  269. GameModule.userInfo.rateGold = totalRate;
  270. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  271. },
  272. //获取房间里程碑
  273. getRoomAward(responseData) {
  274. this.buildingInfos.forEach((item, index) => {
  275. if (item.roomId == responseData.roomId) {
  276. item.awardCount = responseData.awardCount;
  277. item.roomMt = responseData.roomMt;
  278. }
  279. });
  280. this.refreshAllbuildingGoldRate();
  281. },
  282. // update (dt) {},
  283. });