GoodsData.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Framework } from "../../framework/Framework";
  2. import { ItemEnum } from "../common/InterfaceAddEnum";
  3. import { MaterialConf } from "../config/MaterialConf";
  4. import { GameEvent } from "./GameEvent";
  5. interface BaseItem {
  6. conf: {};
  7. id: string;
  8. count: number;
  9. type: ItemEnum;
  10. }
  11. class Data {
  12. private _goodsData: { [id: string]: BaseItem } = {};
  13. init(): void {
  14. this.reset();
  15. }
  16. reset(): void {
  17. this._goodsData = {};
  18. }
  19. purge(): void {
  20. this.reset();
  21. }
  22. setAllGoods(data) {
  23. this._goodsData = {};
  24. let conf = MaterialConf.data;
  25. for (const key in data) {
  26. if (Object.prototype.hasOwnProperty.call(data, key)) {
  27. const element = data[key];
  28. let mConf = conf[key];
  29. if (mConf) {
  30. let item = {
  31. id: key,
  32. count: Number(element),
  33. type: ItemEnum.material,
  34. conf: mConf,
  35. }
  36. this._goodsData[key] = item;
  37. }
  38. }
  39. }
  40. }
  41. getAllGoods() {
  42. return this._goodsData;
  43. }
  44. getBagGoods() {
  45. let bagList = [];
  46. for (const key in this._goodsData) {
  47. if (Object.prototype.hasOwnProperty.call(this._goodsData, key)) {
  48. const element = this._goodsData[key];
  49. if (element.count > 0 && Number(element.conf['Showable'] == 1)) {
  50. bagList.push(element);
  51. }
  52. }
  53. }
  54. bagList.sort((a, b) => {
  55. return Number(a.id) > Number(b.id) ? -1 : 1;
  56. });
  57. return bagList;
  58. }
  59. getGoodsById(id: string) {
  60. if (this._goodsData[id]) {
  61. return this._goodsData[id];
  62. } else {
  63. let mConf = MaterialConf.data[id];
  64. if (mConf) {
  65. let item = {
  66. id: id,
  67. count: 0,
  68. type: ItemEnum.material,
  69. conf: mConf,
  70. }
  71. return item;
  72. }
  73. }
  74. return null;
  75. }
  76. addGoodsById(id: string, num: number) {
  77. let mConf = MaterialConf.data[id];
  78. if (mConf) {
  79. if (this._goodsData[id]) {
  80. let sumCount = this._goodsData[id].count + num;
  81. if (mConf.MaxStoreLimit) {
  82. if (sumCount > mConf.MaxStoreLimit) {
  83. this._goodsData[id].count = mConf.MaxStoreLimit
  84. } else {
  85. this._goodsData[id].count = sumCount
  86. }
  87. } else {
  88. this._goodsData[id].count = sumCount
  89. }
  90. } else {
  91. let item = {
  92. id: id,
  93. count: num,
  94. type: ItemEnum.material,
  95. conf: mConf,
  96. }
  97. this._goodsData[id] = item;
  98. }
  99. Framework.event.fireEvent(GameEvent.MaterialChange, id);
  100. //後期可能有紅點邏輯
  101. }
  102. }
  103. getGoodsLocalInfo(id: string, num: number) {
  104. let mConf = MaterialConf.data[id];
  105. if (mConf) {
  106. let item = {
  107. id: id,
  108. count: 0,
  109. type: num,
  110. conf: mConf,
  111. }
  112. return item;
  113. }
  114. return null;
  115. }
  116. }
  117. export let GoodsData = new Data;