GoodsManager.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Framework } from "../../framework/Framework";
  2. import { ItemEnum } from "../common/InterfaceAddEnum";
  3. import { MaterialConf } from "../config/MaterialConf";
  4. import { GameEvent } from "../data/GameEvent";
  5. import { GoodsData } from "../data/GoodsData";
  6. export class GoodsManager {
  7. static getBagGoods() {
  8. let _goodsData = GoodsData.getAllGoods();
  9. let bagList = [];
  10. for (const key in _goodsData) {
  11. if (Object.prototype.hasOwnProperty.call(_goodsData, key)) {
  12. const element = _goodsData[key];
  13. if (element.count > 0 && Number(element.conf['Showable'] == 1)) {
  14. bagList.push(element);
  15. }
  16. }
  17. }
  18. bagList.sort((a, b) => {
  19. return Number(a.id) > Number(b.id) ? -1 : 1;
  20. });
  21. return bagList;
  22. }
  23. static getGoodsById(id: string) {
  24. let _goodsData = GoodsData.getAllGoods();
  25. if (_goodsData[id]) {
  26. return _goodsData[id];
  27. } else {
  28. let mConf = MaterialConf.data[id];
  29. if (mConf) {
  30. let item = {
  31. id: id,
  32. count: 0,
  33. type: ItemEnum.material,
  34. conf: mConf,
  35. }
  36. return item;
  37. }
  38. }
  39. return null;
  40. }
  41. static addGoodsById(id: string, num: number) {
  42. let _goodsData = GoodsData.getAllGoods();
  43. let mConf = MaterialConf.data[id];
  44. if (mConf) {
  45. if (_goodsData[id]) {
  46. let sumCount = _goodsData[id].count + num;
  47. if (mConf.MaxStoreLimit) {
  48. if (sumCount > mConf.MaxStoreLimit) {
  49. _goodsData[id].count = mConf.MaxStoreLimit
  50. } else {
  51. _goodsData[id].count = sumCount
  52. }
  53. } else {
  54. _goodsData[id].count = sumCount
  55. }
  56. } else {
  57. let item = {
  58. id: id,
  59. count: num,
  60. type: ItemEnum.material,
  61. conf: mConf,
  62. }
  63. _goodsData[id] = item;
  64. }
  65. Framework.event.fireEvent(GameEvent.MaterialChange, id);
  66. //後期可能有紅點邏輯
  67. }
  68. }
  69. static getGoodsLocalInfo(id: string, num: number) {
  70. let _goodsData = GoodsData.getAllGoods();
  71. let mConf = MaterialConf.data[id];
  72. if (mConf) {
  73. let item = {
  74. id: id,
  75. count: num,
  76. type: ItemEnum.material,
  77. conf: mConf,
  78. }
  79. return item;
  80. }
  81. return null;
  82. }
  83. static parseServerAwards(data) {
  84. for (const element of data) {
  85. if (element[0] == 'material') {
  86. this.addGoodsById(element[1], element[2]);
  87. } else if (element[0] == 'equip') {
  88. }
  89. }
  90. }
  91. // //消息-讀郵件
  92. // static sendReadMailMsg(args: { id: string }, callback) {
  93. // LoginMgr.sendPost('user', 'read_mail', (data) => {
  94. // console.log(data);
  95. // // MailData.setMailReadById(args.id)
  96. // }, args)
  97. // }
  98. }