LevelHome.js 11 KB

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