card_info_svr/src/logic/ItemCtrl.ts
2021-01-21 20:01:41 +08:00

161 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { DropItemCfg } from "cfg/parsers/DropItemCfg";
import { ItemCardCfg } from "cfg/parsers/ItemCardCfg";
import { ZError } from "common/ZError";
import { BaseConst } from "constants/BaseConst";
import { CommonItem, COMMON_ITEM_TYPE, ItemInfo } from "./ItemDef";
let ItemMan = {
// 物品按品质分
_item_grades: new Map(),
// 物品按类型分
_item_types: new Map(),
// 卡牌按子类型分
_card_types: new Map(),
_items: new Map(),
useItem(itemid: number, itemcount: number = 1) {
let obj: DropItemCfg = global.$cfg.get(BaseConst.DROPITEM).get(itemid);
if(!obj){
throw new ZError(1001, 'not find item!');
}
return [{ itemid: 10001, itemnum: 1, itemtype: 1 }];
},
/**
* useItem使用物品主要指礼包类物品
* @param itemid : 物品卡id
* @param itemcount :物品卡数量
*/
useItemPackage(itemid: number, itemcount: number = 1) {
let obj: DropItemCfg = global.$cfg.get(BaseConst.DROPITEM).get(itemid);
if(!obj){
throw new ZError(1001, 'not find item!');
}
return [{ itemid: 10001, itemnum: 1 }];
},
/**
* decomposeItemCard分解卡片
* @param itemid : 物品卡id
* @param itemcount :物品卡数量
*/
decomposeItemCard(itemid: number, itemcount: number = 1){
},
/**
* 获取单个物品价格
* @param itemid : 物品id
* @returns
*/
getItemPrice(itemid: number): ItemInfo{
let item: CommonItem = this._items.get(itemid);
return item? item._priceinfo: new ItemInfo();
},
initItems(){
return;
this._items.clear();
// 默认id都唯一
this.initDropItems();
this.initCardItems();
this.handleItems();
},
initDropItems(){
let map: Map<number, DropItemCfg> = global.$cfg.get(BaseConst.DROPITEM);
map.forEach((v: DropItemCfg, k: number) => {
if(k != 0){
let item = new CommonItem();
item.loadDropItem(v);
this._items.set(k, item);
}
});
},
initCardItems(){
let map: Map<number, ItemCardCfg> = global.$cfg.get(BaseConst.ITEMCARD);
map.forEach((v: ItemCardCfg, k: number) => {
if(k != 0){
let item = new CommonItem();
item.loadCardItem(v);
this._items.set(k, item);
}
});
},
handleItems(){
this.handCaches();
this.handRandWeights();
},
handCaches(){
this._item_grades.clear();
this._card_types.clear();
this._items.forEach((v: CommonItem, k: number) =>{
let lst = this._item_grades.get(v._grade);
if(!lst){
lst = [v];
this._item_grades.set(v._grade, lst);
}else{
lst.push(v);
}
lst = this._item_types.get(v._type);
if(!lst){
lst = [v];
this._item_types.set(v._type, lst);
}else{
lst.push(v);
}
if(v._type == COMMON_ITEM_TYPE.CARD){
lst = this._card_types.get(v._subtype);
if(!lst){
lst = [v];
this._card_types.set(v._subtype, lst);
}else{
lst.push(v);
}
}
v._randexitems && v._randexitems.forEach((item: ItemInfo) =>{
let obj: CommonItem = this._items.get(item.id);
obj && (item.weight = obj._weight);
});
});
},
handRandWeights(){
this._items.forEach((v: CommonItem, k: number) =>{
if(v._randexitems || v._randsubgrade || v._randsubtype){
if(!v._randsubitems){
v._randsubitems = [];
}
let lst = [];
if(v._randsubtype == COMMON_ITEM_TYPE.CARD && v._randsubgrade){
lst = this._card_types.get(v._randsubgrade);
}else{
lst = this._item_types.get(v._randsubtype);
}
lst.forEach((element: CommonItem) => {
});
}
});
}
};
export default ItemMan;