BuildingManager.js 3.3 KB

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