123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { Framework } from "../../framework/Framework";
- import { ItemEnum } from "../common/InterfaceAddEnum";
- import { MaterialConf } from "../config/MaterialConf";
- import { GameEvent } from "./GameEvent";
- interface BaseItem {
- conf: {};
- id: string;
- count: number;
- type: ItemEnum;
- }
- class Data {
- private _goodsData: { [id: string]: BaseItem } = {};
- init(): void {
- this.reset();
- }
- reset(): void {
- this._goodsData = {};
- }
- purge(): void {
- this.reset();
- }
- setAllGoods(data) {
- this._goodsData = {};
- let conf = MaterialConf.data;
- for (const key in data) {
- if (Object.prototype.hasOwnProperty.call(data, key)) {
- const element = data[key];
- let mConf = conf[key];
- if (mConf) {
- let item = {
- id: key,
- count: Number(element),
- type: ItemEnum.material,
- conf: mConf,
- }
- this._goodsData[key] = item;
- }
- }
- }
- }
- getAllGoods() {
- return this._goodsData;
- }
- getBagGoods() {
- let bagList = [];
- for (const key in this._goodsData) {
- if (Object.prototype.hasOwnProperty.call(this._goodsData, key)) {
- const element = this._goodsData[key];
- if (element.count > 0 && Number(element.conf['Showable'] == 1)) {
- bagList.push(element);
- }
- }
- }
- bagList.sort((a, b) => {
- return Number(a.id) > Number(b.id) ? -1 : 1;
- });
- return bagList;
- }
- getGoodsById(id: string) {
- if (this._goodsData[id]) {
- return this._goodsData[id];
- } else {
- let mConf = MaterialConf.data[id];
- if (mConf) {
- let item = {
- id: id,
- count: 0,
- type: ItemEnum.material,
- conf: mConf,
- }
- return item;
- }
- }
- return null;
- }
- addGoodsById(id: string, num: number) {
- let mConf = MaterialConf.data[id];
- if (mConf) {
- if (this._goodsData[id]) {
- let sumCount = this._goodsData[id].count + num;
- if (mConf.MaxStoreLimit) {
- if (sumCount > mConf.MaxStoreLimit) {
- this._goodsData[id].count = mConf.MaxStoreLimit
- } else {
- this._goodsData[id].count = sumCount
- }
- } else {
- this._goodsData[id].count = sumCount
- }
- } else {
- let item = {
- id: id,
- count: num,
- type: ItemEnum.material,
- conf: mConf,
- }
- this._goodsData[id] = item;
- }
- Framework.event.fireEvent(GameEvent.MaterialChange, id);
- //後期可能有紅點邏輯
- }
- }
- getGoodsLocalInfo(id: string, num: number) {
- let mConf = MaterialConf.data[id];
- if (mConf) {
- let item = {
- id: id,
- count: 0,
- type: num,
- conf: mConf,
- }
- return item;
- }
- return null;
- }
- }
- export let GoodsData = new Data;
|