card_info_svr/src/logic/ItemCtrl.ts
2021-01-22 02:59:03 +08:00

249 lines
7.2 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 { CIC, CommonItem, COMMON_ITEM_TYPE, ItemInfo } from "./ItemDef";
import {calcWeight} from "./LogicUtil";
let ItemMan = {
// 物品按品质分
_item_grades: new Map(),
// 物品按类型分
_item_types: new Map(),
// 卡牌按子类型分
_card_types: new Map(),
_items: new Map(),
/**
* 使用物品
* @param itemid 物品id
* @param itemcount :物品数量
*/
useItem(itemid: number, itemcount: number = 1) {
let obj: CommonItem = this.findItem(itemid);
if(!obj){
throw new ZError(1001, 'not find item cfg!');
}
let lst: CIC[] = [];
let reslst:ItemInfo[] = [];
return reslst;
this._useItem(obj, itemcount, false, lst);
lst.forEach((item: CIC) => {
reslst.push(item.info);
});
return reslst;
},
/**
* decomposeItemCard分解卡片
* @param itemid : 物品卡id
* @param itemcount :物品卡数量
*/
decomposeItemCard(itemid: number, itemcount: number = 1){
let obj: CommonItem = this.findItem(itemid);
if(!obj){
throw new ZError(1001, 'not find item cfg!');
}
if(!obj.isCard()){
throw new ZError(1001, 'this item is not card!');
}
let lst: CIC[] = [];
this._useItem(obj, itemcount, true, lst);
let reslst:ItemInfo[] = [];
lst.forEach((item: CIC) => {
reslst.push(item.info);
});
return reslst;
},
/**
* 获取单个物品价格
* @param itemid : 物品id
* @returns
*/
getItemPrice(itemid: number): ItemInfo{
let item: CommonItem = this._items.get(itemid);
return item? item._priceinfo: new ItemInfo();
},
/**
* findItem
*/
findItem(itemid: number): CommonItem {
return this._items.get(itemid);
},
/**
* 是否是物品包
* @param itemid : 物品id
*/
isItemPackage(itemid: number): boolean{
let item = this.findItem(itemid);
return item? item.isPackage(): false;
},
_useItem(item: CommonItem, count: number, dpcard: boolean, reslst: CIC[]){
if(item.isPackage() && item._isautoopen){
this._openItemPkg(item, count, false, reslst);
}else if(dpcard && item.isCard()){
this._openItemPkg(item, count, true, reslst);
}else{
reslst.push(new CIC(item, count));
}
},
_openItemPkg(pkg: CommonItem, count: number, dpcard: boolean, reslst: CIC[]){
if(pkg._solidsubitems){
pkg._solidsubitems.forEach((item: ItemInfo) => {
let obj = this._items.get(item.id);
this._useItem(obj, item.count * count, false, reslst);
});
}
if(pkg._randsubitems){
for(let i = 0; i < count; i++){
let n = calcWeight(pkg._randsubitems, pkg._randsubtotalwh);
let info = pkg._randsubitems[n];
if(!info){
//todo:
continue;
}
let obj = this._items.get(info.id);
if(!obj){
//todo:
continue;
}
this._useItem(obj, info.count, false, reslst);
}
}
},
initItems(){
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 = [];
}else{
v._randsubitems.length = 0;
}
v._randsubtotalwh = 0;
let lst = [];
if(v._randsubtype == COMMON_ITEM_TYPE.CARD && v._randsubgrade){
console.log('is card random:', v._randsubtype, v._randsubgrade);
console.log([this._item_grades.keys()]);
lst = this._item_grades.get(v._randsubgrade);
}else if(v._randsubtype != COMMON_ITEM_TYPE.NONE){
console.log('is not card random:', v._randsubtype);
lst = this._item_types.get(v._randsubtype);
}
lst && lst.forEach((element: CommonItem) => {
v._randsubitems.push(new ItemInfo(element._id, 1, element._weight));
v._randsubtotalwh += element._weight;
});
v._randexitems && v._randexitems.forEach((item: ItemInfo) =>{
let obj = v._randsubitems.find((v: ItemInfo) => {
return v.id == item.id;
});
if(obj){
obj.weight += item.weight;
}else{
v._randsubitems.push(item);
v._randsubtotalwh += item.weight;
}
});
}
});
}
};
export default ItemMan;