BuildingManager.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var BuildingModel = require('./BuildingModel');
  2. // var BuildingInfoJson = require('../utils/buildingInfo');
  3. const bInfo = require('../data/buildingInfo');
  4. const bLevel = require('../data/buildingLevel');
  5. export default class BuildingManager {
  6. static _instance;
  7. _allBuildData = {};
  8. constructor() {
  9. // this._allBuildData = BuildingInfoJson.BuildingInfo;
  10. this._allBuildData = this.formatBuildingInfo();
  11. console.log(this._allBuildData);
  12. };
  13. static get instance() {
  14. if (!BuildingManager._instance) {
  15. BuildingManager._instance = new BuildingManager();
  16. }
  17. return BuildingManager._instance;
  18. };
  19. formatBuildingInfo() {
  20. let bInfoMap = {}
  21. bInfo.forEach(n => {
  22. bInfoMap[n.id] = n
  23. })
  24. // let bLevelMap = {}
  25. // bLevel.forEach(n => {
  26. // if(bLevelMap[n.buildingId] && bLevelMap[n.buildingId].length > 0) {
  27. // bLevelMap[n.buildingId].push(n)
  28. // } else {
  29. // bLevelMap[n.buildingId] = []
  30. // bLevelMap[n.buildingId].push(n)
  31. // }
  32. // })
  33. for (let i in bLevel) {
  34. let curLevel = bLevel[i]
  35. curLevel.forEach((item, index) => {
  36. for (var j in item) {
  37. item[j] = parseInt(item[j])
  38. }
  39. let nextObj = {
  40. nextRate: 0,
  41. nextRateUnit: 0,
  42. nextUpScore: 0,
  43. hasNext: 0
  44. };
  45. let staticObj = {
  46. cityId: bInfoMap[item.buildingId].cityId,
  47. type: bInfoMap[item.buildingId].type,
  48. name: bInfoMap[item.buildingId].name,
  49. jobId: bInfoMap[item.buildingId].jobId,
  50. };
  51. if (curLevel[index + 1] && curLevel[index + 1].buildingId == item.buildingId) {
  52. nextObj = {
  53. nextRate: parseInt(curLevel[index + 1].rate),
  54. nextRateUnit: parseInt(curLevel[index + 1].rateUnit),
  55. nextUpScore: parseInt(curLevel[index + 1].upScore),
  56. hasNext: 1,
  57. }
  58. staticObj['unlockScore'] = 0
  59. }
  60. item = Object.assign(item, staticObj, nextObj)
  61. })
  62. }
  63. for (let i in bLevel) {
  64. let tarObj = Object.assign({}, bLevel[i][0]);
  65. tarObj['unlockScore'] = bInfoMap[tarObj.buildingId].unlockScore;
  66. tarObj['level'] = 0;
  67. bLevel[i].unshift(tarObj);
  68. }
  69. return bLevel;
  70. }
  71. /**
  72. * 获取建筑的下一级信息
  73. * @param {ing} cityId
  74. * @param {int} buildingId
  75. * @param {int} level
  76. */
  77. getBuildingInfo(cityId, buildingId, level) {
  78. let building = this._allBuildData[buildingId];
  79. let buildObj = building.find(n => {
  80. return n.level == level;
  81. })
  82. let buildModel = new BuildingModel(buildObj);
  83. return buildModel;
  84. };
  85. /**
  86. * 获取对应建筑的最高等级
  87. * @param {*} buildingId
  88. */
  89. getLevelCount(buildingId) {
  90. let building = this._allBuildData[buildingId];
  91. return building.length - 1;
  92. };
  93. }