From c31633a49773012504d43f172466e88002e29622 Mon Sep 17 00:00:00 2001 From: "y.x" Date: Fri, 22 Jan 2021 02:59:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/GConfig.ts | 2 +- src/controllers/AccountController.ts | 6 +- src/logic/ItemCtrl.ts | 146 +++++++++++++++++++++------ src/logic/ItemDef.ts | 36 +++++-- src/logic/LogicUtil.ts | 21 ++++ 5 files changed, 172 insertions(+), 39 deletions(-) create mode 100644 src/logic/LogicUtil.ts diff --git a/src/common/GConfig.ts b/src/common/GConfig.ts index cda2338..d9d2998 100644 --- a/src/common/GConfig.ts +++ b/src/common/GConfig.ts @@ -7,7 +7,7 @@ import { DropItemCfg } from '../cfg/parsers/DropItemCfg' import { ItemCardCfg } from '../cfg/parsers/ItemCardCfg' import { MatchCfg } from '../cfg/parsers/MatchCfg' import { ItemFuncCfg } from '../cfg/parsers/ItemFuncCfg' -import ItemCtrl from "logic/ItemCtrl"; +import ItemCtrl from "../logic/ItemCtrl"; export function initData() { diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 8cfa547..041fd19 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -130,10 +130,10 @@ export default class AccountController extends BaseController { for (let obj of data) { let item = (await BagItem.findOrCreate({ accountid, - itemid: obj.itemid, - itemtype: ItemType.UNKNOW + itemid: obj.id, + itemtype: obj.type })).doc - item.count += obj.itemnum + item.count += obj.count await item.save() } await record.save() diff --git a/src/logic/ItemCtrl.ts b/src/logic/ItemCtrl.ts index 5656c4b..b3ec048 100644 --- a/src/logic/ItemCtrl.ts +++ b/src/logic/ItemCtrl.ts @@ -1,8 +1,9 @@ -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"; +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 = { // 物品按品质分 @@ -14,31 +15,29 @@ let ItemMan = { _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 :物品卡数量 + * 使用物品 + * @param itemid :物品id + * @param itemcount :物品数量 */ - useItemPackage(itemid: number, itemcount: number = 1) { - let obj: DropItemCfg = global.$cfg.get(BaseConst.DROPITEM).get(itemid); + useItem(itemid: number, itemcount: number = 1) { + let obj: CommonItem = this.findItem(itemid); if(!obj){ - throw new ZError(1001, 'not find item!'); + throw new ZError(1001, 'not find item cfg!'); } - + let lst: CIC[] = []; + let reslst:ItemInfo[] = []; - return [{ itemid: 10001, itemnum: 1 }]; + return reslst; + + this._useItem(obj, itemcount, false, lst); + + lst.forEach((item: CIC) => { + reslst.push(item.info); + }); + + return reslst; }, /** @@ -47,7 +46,25 @@ let ItemMan = { * @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; }, /** @@ -60,8 +77,58 @@ let ItemMan = { 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(){ - return; this._items.clear(); // 默认id都唯一 @@ -142,16 +209,37 @@ let ItemMan = { 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){ - lst = this._card_types.get(v._randsubgrade); - }else{ + 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.forEach((element: CommonItem) => { - + 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; + } }); } }); diff --git a/src/logic/ItemDef.ts b/src/logic/ItemDef.ts index 562a921..949291d 100644 --- a/src/logic/ItemDef.ts +++ b/src/logic/ItemDef.ts @@ -1,5 +1,7 @@ import { DropItemCfg } from "cfg/parsers/DropItemCfg"; import { ItemCardCfg } from "cfg/parsers/ItemCardCfg"; +import { count } from "console"; +import { timeBeforeDay } from "utils/time.util"; /** * 1.杂物 @@ -18,6 +20,7 @@ export const enum COMMON_ITEM_TYPE{ CARD_PKG = 4, SKILL = 5, SKILL_PKG = 6, + MONEY = 7, }; /** @@ -41,11 +44,12 @@ export const enum COMMON_ITEM_GRADE{ export class ItemInfo{ id: number; count: number; - weight: number = 0; - constructor(itemid?: number, itemcount?: number, itemweight?: number){ + weight: number; + type: number = 0; + constructor(itemid?: number, itemcount: number = 1, itemweight: number = 0){ this.id = itemid? itemid: 0; - this.count = itemcount? itemcount: 0; - this.weight = itemweight? itemweight: 0; + this.count = itemcount; + this.weight = itemweight; }; loadData(itemid: number, itemcount: number){ @@ -68,6 +72,8 @@ export class CommonItem{ public _randsubtype: number = 0; public _randsubgrade: number = 0; + public _randsubtotalwh: number = 0; + public _solidsubitems: ItemInfo[]; public _randexitems: ItemInfo[]; @@ -133,7 +139,7 @@ export class CommonItem{ lst = aitem.additionalcandidates.split('|'); lst.forEach((v: string) => { - let item = new ItemInfo(parseInt(v), 1); + let item = new ItemInfo(parseInt(v)); if(!this._randexitems){ this._randexitems = [item]; }else{ @@ -142,4 +148,22 @@ export class CommonItem{ }); }; -}; \ No newline at end of file + isPackage(){ + return this._type == COMMON_ITEM_TYPE.CARD_PKG || + this._type == COMMON_ITEM_TYPE.NORMAL_PKG || + this._type == COMMON_ITEM_TYPE.SKILL_PKG; + }; + + isCard(){ + return this._type == COMMON_ITEM_TYPE.CARD; + } +}; + +export class CIC{ + public item: CommonItem; + public info: ItemInfo; + constructor(aitem: CommonItem, acount: number = 1){ + this.item = aitem; + this.info = new ItemInfo(aitem? aitem._id: 0, acount, aitem? aitem._weight: 0); + }; +} \ No newline at end of file diff --git a/src/logic/LogicUtil.ts b/src/logic/LogicUtil.ts new file mode 100644 index 0000000..0f962c8 --- /dev/null +++ b/src/logic/LogicUtil.ts @@ -0,0 +1,21 @@ + //[{weight}] + /** + * [计算权重] + * @param {[array]} obj_vec [对象数组,对象中要有权重变量weight] + * @return {[number]} [obj_vec数组索引] + */ + export function calcWeight(obj_vec: any[], all_weight: number) { + if (all_weight <= 0) { + return 0; + } + + let ret = 0; + let rand = Math.random() * all_weight; + for (let i = 0; i < obj_vec.length; i++) { + if (rand < obj_vec[i].weight) { + ret = i; + break; + } + } + return ret; + }; \ No newline at end of file