LevelHome.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. const HomeApi = require("../net/HomeApi");
  2. const GameModule = require("../utils/GameModule");
  3. const { GameNotificationKey } = require("../utils/GameEnum")
  4. const AlertManager = require('../utils/AlertManager')
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. scrollView: cc.ScrollView,
  9. levelHomeItem: cc.Prefab,
  10. levelHomeTop: cc.Prefab,
  11. levelHomeBottom: cc.Prefab,
  12. minContentPosition: -150,
  13. _unlockBuilding: [],
  14. showOffLineUI: true,
  15. },
  16. /**
  17. * home 初始化方法, 所有的初始化操作在这里操作, 必须在加入父节点之前调用
  18. * */
  19. init(uid, cityId) {
  20. this.uid = uid;
  21. this.cityId = cityId;
  22. this.buildingInfos = [];
  23. this.node.parent = cc.find("Canvas/game");
  24. this.scrollView.scrollToBottom();
  25. this.refreshTheme();
  26. this.getNetworkData();
  27. },
  28. refreshTheme() {
  29. if (this.topScript) {
  30. this.topScript.init(this.cityId);
  31. }
  32. if (this.buildings) {
  33. for (let i = 0; i < this.buildings.length; i++) {
  34. let itemScript = this.buildings[i];
  35. itemScript.init(this.cityId, i + 1);
  36. }
  37. }
  38. if (this.bottomScript) {
  39. this.bottomScript.init(this.cityId);
  40. }
  41. },
  42. // LIFE-CYCLE CALLBACKS:
  43. onLoad() {
  44. this.buildings = [];
  45. this.unlockBuilding = [];
  46. this.matchScreenSize();
  47. let topNode = cc.instantiate(this.levelHomeTop);
  48. this.topScript = topNode.getComponent('LevelHomeTop');
  49. this.topScript.init(this.cityId);
  50. this.scrollView.content.addChild(topNode);
  51. for (let i = 0; i < 5; i++) {
  52. let item = cc.instantiate(this.levelHomeItem);
  53. let itemScript = item.getComponent('LevelHomeItem');
  54. itemScript.init(this.cityId, i + 1);
  55. this.scrollView.content.addChild(item);
  56. this.buildings.push(itemScript);
  57. }
  58. let bottomNode = cc.instantiate(this.levelHomeBottom);
  59. this.bottomScript = bottomNode.getComponent('LevelHomeBottom'); this.bottomScript.init(this.cityId);
  60. this.scrollView.content.addChild(bottomNode);
  61. this.setEventLisenter();
  62. // 监听小游戏隐藏到后台事件
  63. if (CC_WECHATGAME) {
  64. wx.onHide(() => {
  65. this.wxHide = true
  66. // 离线收益计算重置
  67. this.showOffLineUI = true
  68. let lastTime = new Date().getTime()
  69. cc.sys.localStorage.setItem('offlineLastTime', lastTime);
  70. // 退出前立刻调用一次上报
  71. GameModule.userInfo.doReport()
  72. })
  73. wx.onShow(() => {
  74. if (this.wxHide) {
  75. this.refreshTheme();
  76. this.getNetworkData();
  77. this.wxHide = false
  78. }
  79. })
  80. }
  81. },
  82. setEventLisenter() {
  83. this.scrollView.node.on("scrolling", (event) => {
  84. if (this.scrollView._isOutOfBoundary()) {
  85. if (this.scrollView._outOfBoundaryAmount.y > 0) { // 超出上面的界限
  86. this.scrollView._outOfBoundaryAmount.y = 0;
  87. this.scrollView._adjustContentOutOfBoundary();
  88. } else { // 超出下面的界限
  89. if (this.scrollView._outOfBoundaryAmount.y < this.minContentPosition) {
  90. if (this.recordScrollViewPosition) {
  91. this.scrollView.content.setPosition(this.recordScrollViewPosition);
  92. return;
  93. } else {
  94. this.recordScrollViewPosition = this.scrollView.getContentPosition();
  95. }
  96. } else {
  97. this.recordScrollViewPosition = null;
  98. }
  99. }
  100. }
  101. }, this);
  102. GameEvent.on(GameNotificationKey.showCatFlyAnimation, this, () => {
  103. this.scrollView.scrollToTop(0.2);
  104. });
  105. GameEvent.on(GameNotificationKey.ReloadLevelHomeData, this, () => {
  106. this.refreshTheme();
  107. this.getNetworkData();
  108. });
  109. GameEvent.on(GameNotificationKey.ResetLevelHomePaddingBottom, this, () => {
  110. this.resetPaddingBottom();
  111. });
  112. //房间解锁通知,用来触发是否显示入驻艺人通知(因为通知需要随机出现在没有艺人的房间,所以需要在LevelHome这一层来处理)
  113. GameEvent.on(GameNotificationKey.LevelHomeItemUnlock, this, this.randomResidentTip);
  114. GameEvent.on(GameNotificationKey.ReceiveLevelHomeItemPropUpdate, this, (props) => {
  115. for (const key in props) {
  116. if (temp.hasOwnProperty(key)) {
  117. const element = props[key];
  118. console.log(element);
  119. }
  120. }
  121. });
  122. },
  123. /**
  124. * 适配不同高度的屏幕尺寸
  125. */
  126. matchScreenSize() {
  127. let initHeight = 1624;
  128. let vsize = cc.view.getVisibleSize()
  129. let paddingY = (initHeight - vsize.height) / 2;
  130. if (paddingY < 0) {
  131. paddingY = 0;
  132. }
  133. this.scrollView.content.getComponent(cc.Layout).paddingTop = paddingY;
  134. this.scrollView.elastic = false;
  135. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  136. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  137. },
  138. resetPaddingBottom() {
  139. let initHeight = 1624;
  140. let vsize = cc.view.getVisibleSize()
  141. let paddingY = (initHeight - vsize.height) / 2;
  142. if (GameModule.userInfo.stars >= 20) {
  143. this.scrollView.elastic = true;
  144. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  145. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  146. } else {
  147. this.scrollView.elastic = false;
  148. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  149. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 275;
  150. }
  151. this.scrollView.scrollToBottom(0);
  152. },
  153. // 用来访问自己家园时, 重置scrollView位置
  154. start() {
  155. this.scrollView.scrollToBottom(0.0);
  156. },
  157. getNetworkData(callback) {
  158. this.getUserBuildings()
  159. .then((responseData) => {
  160. // 清空数据
  161. this.buildingInfos = [];
  162. // 满级后去别的城市就置零
  163. GameModule.userInfo.levelHomeItemFullCount = 0;
  164. let sortArray = responseData.buildings.sort((a, b) => {
  165. return a.buildingId < b.buildingId;
  166. });
  167. // 离线收益金币数量
  168. let offlineGrossIncome = 0;
  169. sortArray.map((value, index, array) => {
  170. let model = Global.BuildingManager.getBuildingInfo(this.cityId, value.buildingId, value.level)
  171. this._unlockBuilding[index] = model.isUnlocked ? 1 : 0;
  172. if (model.isFull() && this.cityId === Global.devCityId) {
  173. try {
  174. GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
  175. } catch (error) {
  176. console.log(error);
  177. }
  178. }
  179. model.coinCount = value.coinCount;
  180. model.artists = value.artists || [];
  181. if (model.artists.length > 0) { // 有艺人入驻的情况
  182. let addition = 0;
  183. for (let i = 0; i < model.artists.length; i++) {
  184. let artist = model.artists[i];
  185. addition += artist.stationJobLevel + 1;
  186. }
  187. offlineGrossIncome += (model.coinCount * (model.rate * addition));
  188. } else { // 无艺人入驻时
  189. offlineGrossIncome += (model.coinCount * model.rate);
  190. }
  191. this.buildingInfos.push(model);
  192. });
  193. this._unlockBuilding = this._unlockBuilding.reverse();
  194. // GameModule.userInfo.setUserInfo(responseData.user);
  195. // this.resetPaddingBottom();
  196. callback && callback();
  197. // 开始设置建筑
  198. this.configBuildings();
  199. // 离线收益处理
  200. this.configOffIncome(sortArray, offlineGrossIncome)
  201. return this.getBuildingItems();
  202. })
  203. .then((buildingItems) => {
  204. for (const buildingId in buildingItems) {
  205. let itemScript;
  206. let filterList = this.buildings.filter( item => item.buildingInfo.buildingId === parseInt(buildingId) ) || [];
  207. if (filterList.length > 0) { itemScript = filterList[0]; }
  208. if (buildingItems.hasOwnProperty(buildingId)) {
  209. let prop = buildingItems[buildingId];
  210. itemScript.configProp(prop);
  211. }
  212. }
  213. })
  214. .catch((err) => {
  215. console.log(err);
  216. });
  217. },
  218. getUserBuildings() {
  219. return new Promise((resolve, reject) => {
  220. // 获取目标用户的建筑
  221. HomeApi.getUserBuildings(this.uid, this.cityId, (responseData) => {
  222. resolve(responseData);
  223. }, (error) => {
  224. reject(error);
  225. });
  226. })
  227. },
  228. getBuildingItems() {
  229. return new Promise((resolve, reject) => {
  230. HomeApi.getBuildingItems((buildingItems) => {
  231. resolve(buildingItems);
  232. }, (err) => {
  233. reject(err);
  234. })
  235. });
  236. },
  237. configBuildings() {
  238. for (let i = 0; i < this.buildings.length; i++) {
  239. let itemScript = this.buildings[i];
  240. itemScript.config(this.buildingInfos[i], this.uid, this._unlockBuilding, (newBuildingInfo) => {
  241. this.buildingInfos[i] = newBuildingInfo;
  242. });
  243. }
  244. this.randomResidentTip();
  245. },
  246. /**
  247. * 离线收益处理
  248. * @param {Array} sortArray 用户当前城市buildingInfos数组
  249. * @param {Number} offlineGrossIncome 离线收益金币数量
  250. */
  251. configOffIncome(sortArray, offlineGrossIncome) {
  252. let lastTime = cc.sys.localStorage.getItem('offlineLastTime');
  253. if (!lastTime) {
  254. lastTime = new Date().getTime()
  255. cc.sys.localStorage.setItem('offlineLastTime', lastTime);
  256. } else {
  257. let curTime = new Date().getTime()
  258. cc.sys.localStorage.setItem('offlineLastTime', curTime);
  259. // 显示离线收益的条件:
  260. // 1. 离上一次显示超过5分钟
  261. // 2. 用户解锁星数大于20
  262. // 3. 当前城市已解锁建筑数大于5
  263. let unLockStatus1 = curTime - lastTime > 300 * 1000
  264. let unLockStatus2 = GameModule.userInfo.stars > 20
  265. let unLockStatus3 = (() => {
  266. let flag = true
  267. sortArray.forEach(n => {
  268. if (n.level == 0) {
  269. flag = false
  270. }
  271. })
  272. return flag
  273. })()
  274. // showOffLineUI: 用户每次进入游戏离线收益只会显示1次
  275. if (this.showOffLineUI && unLockStatus1 && unLockStatus2 && unLockStatus3) {
  276. this.showOffLineUI = false;
  277. AlertManager.showOfflineGrossIncome(offlineGrossIncome);
  278. }
  279. }
  280. },
  281. randomResidentTip() {
  282. if (this.buildings && this.buildings.length > 0) {
  283. let noArtistBuildings = [];
  284. for (let i = 0; i < this.buildings.length; i++) {
  285. let itemScript = this.buildings[i];
  286. let buildingInfo = this.buildingInfos[i];
  287. if (buildingInfo.level == 0) {
  288. itemScript.setResidentTip(false);
  289. continue;
  290. }
  291. if (buildingInfo.artists && buildingInfo.artists.length > 0) {
  292. itemScript.setResidentTip(false);
  293. } else {
  294. noArtistBuildings.push(itemScript);
  295. }
  296. }
  297. if (noArtistBuildings.length > 0) {
  298. let index = Math.floor(Math.random() * noArtistBuildings.length);
  299. for (let i = 0; i < noArtistBuildings.length; i++) {
  300. noArtistBuildings[i].setResidentTip(i == index);
  301. }
  302. clearInterval(this.residentTipHideTimer);
  303. clearInterval(this.residentTipShowTimer);
  304. this.residentTipHideTimer = setInterval(() => {
  305. noArtistBuildings[index].setResidentTip(false);
  306. }, 5000);
  307. this.residentTipShowTimer = setInterval(() => {
  308. this.randomResidentTip(false);
  309. }, 15000);
  310. }
  311. }
  312. },
  313. onDestroy() {
  314. if (this.residentTipHideTimer) {
  315. clearInterval(this.residentTipHideTimer);
  316. clearInterval(this.residentTipShowTimer);
  317. }
  318. GameEvent.off(GameNotificationKey.showCatFlyAnimation, this);
  319. GameEvent.off(GameNotificationKey.ReloadLevelHomeData, this);
  320. GameEvent.off(GameNotificationKey.ResetLevelHomePaddingBottom, this);
  321. GameEvent.off(GameNotificationKey.ReceiveLevelHomeItemPropUpdate, this);
  322. GameEvent.off(GameNotificationKey.LevelHomeItemUnlock, this);
  323. },
  324. });