物品消耗
This commit is contained in:
@@ -20,7 +20,23 @@ export class HeroHandler {
|
||||
|
||||
private channelService: ChannelService = this.app.get('channelService');
|
||||
|
||||
public async test(msg: { id: number, count: number}, session: BackendSession) {
|
||||
public async test(msg: { id: number, count: number, seqId:number}, session: BackendSession) {
|
||||
let roleId: string = session.get('roleId');
|
||||
let roleName: string = session.get('roleName');
|
||||
let sid: string = session.get('sid');
|
||||
|
||||
let {id, count, seqId} = msg;
|
||||
|
||||
let result = await handleCost(roleId, sid, [{id, count, seqId}] );
|
||||
//let result = await addItems(roleId, roleName, sid, [{id, count}] );
|
||||
if(!result) {
|
||||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||
}
|
||||
return resResult(STATUS.SUCCESS,{result});
|
||||
|
||||
}
|
||||
|
||||
public async addItem(msg: { id: number, count: number}, session: BackendSession) {
|
||||
let roleId: string = session.get('roleId');
|
||||
let roleName: string = session.get('roleName');
|
||||
let sid: string = session.get('sid');
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GOOD_TYPE, ITID, CURRENCY, CURRENCY_TYPE, COUNTER, CONSUME_TYPE } from './../consts/consts';
|
||||
import { GOOD_TYPE, ITID, CURRENCY, CURRENCY_TYPE, COUNTER, CONSUME_TYPE, getCurNameById } from './../consts/consts';
|
||||
import { EquipModel } from './../db/Equip';
|
||||
import { CounterModel } from './../db/Counter';
|
||||
import { decodeStr, resResult } from '../pubUtils/util';
|
||||
@@ -125,72 +125,95 @@ async function rewardCurrency (roleId: string, dicGood: any, data: {id:number,cn
|
||||
});
|
||||
return goods;
|
||||
}
|
||||
|
||||
export async function handleCost(roleId: string, sid: string, goods: Array<{id: number, count: number}>) {
|
||||
// 检查道具数量
|
||||
let role, costGold = 0, costCoin = 0, items = new Array<{id: number, count: number}>(), ids = new Array<number>() ;
|
||||
for(let {id, count} of goods) {
|
||||
let goodInfo = getGoodById(id);
|
||||
if(goodInfo.goodType == GOOD_TYPE.CONSUMES|| goodInfo.goodType == GOOD_TYPE.SCRIPT) { // 消耗品
|
||||
let {isCurrency} = ITID.get(goodInfo.itid);
|
||||
if(isCurrency) { // 货币
|
||||
if(!role) role = await RoleModel.findByRoleId(roleId);
|
||||
let dicCurrency = CURRENCY.get(id);
|
||||
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 处理元宝
|
||||
if(role.gold < count + costGold) return false;
|
||||
costGold += count;
|
||||
} else if(dicCurrency.type == CURRENCY_TYPE.COIN) { // 处理铜币
|
||||
if(role.coin < count + costCoin) return false;
|
||||
costCoin += count;
|
||||
}
|
||||
} else {
|
||||
let findItem = items.find(cur => cur.id == id);
|
||||
if(findItem) {
|
||||
findItem.count += count;
|
||||
} else {
|
||||
items.push({id, count}); ids.push(id);
|
||||
}
|
||||
interface Item {id: number, count: number, seqId?: number};
|
||||
interface Equip {id: number, name: string, quality: number, type: number};
|
||||
interface Bag {id: number, itemName: string, count: number, type: number, hid:number};
|
||||
export async function handleCost(roleId: string, sid: string, goods: Array<Item>) {
|
||||
let currencysMap: any = {};
|
||||
let equips: Array<number> = [];
|
||||
let bags: Array<Item> = [];
|
||||
let uids = [{uid: roleId, sid}];
|
||||
if (!sortConsumes(goods, bags, currencysMap, equips))
|
||||
return false;
|
||||
// 检查货币是否充足
|
||||
if (!!Object.keys(currencysMap).length) {
|
||||
let role = await RoleModel.findByRoleId(roleId);
|
||||
for (let key in currencysMap) {
|
||||
currencysMap[key] = role[key] - currencysMap[key];
|
||||
if (currencysMap[key] < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(let {id, count} of items) {
|
||||
let item = await ItemModel.findbyRoleAndGidAndCount(roleId, id, count);
|
||||
if(!item) return false
|
||||
//检查装备是否存在
|
||||
if (!!equips.length) {
|
||||
let resEquips = await EquipModel.getEquips(roleId, equips);
|
||||
if (resEquips.length < equips.length)
|
||||
return false;
|
||||
}
|
||||
//检查并修改道具
|
||||
if(bags.length > 0) {
|
||||
let {hasError, result} = await ItemModel.decreaseItems(roleId, bags);
|
||||
if(hasError) return false;
|
||||
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: result} ), uids);
|
||||
}
|
||||
|
||||
// 推送参数
|
||||
let gold = 0, coin = 0, resultGoods = [];
|
||||
if(costGold > 0) {
|
||||
role = await RoleModel.costGold(roleId, costGold);
|
||||
if(role) gold = role.gold;
|
||||
//删除装备
|
||||
if (!!equips.length) {
|
||||
await EquipModel.deleteEquips(roleId, equips);
|
||||
pinus.app.get('channelService').pushMessageByUids('onEquipDel', resResult(STATUS.SUCCESS, equips), uids);
|
||||
}
|
||||
if(costCoin > 0) {
|
||||
role = await RoleModel.costCoin(roleId, costCoin);
|
||||
if(role) coin = role.coin;
|
||||
|
||||
//消耗玩家货币
|
||||
if (!!Object.keys(currencysMap).length) {
|
||||
await RoleModel.updateRoleInfo(roleId, currencysMap);
|
||||
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, currencysMap), uids);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function sortConsumes(goods: Array<Item>, bags: Array<Item>, currencysMap: any, equips: Array<number>) {
|
||||
for (let good of goods) {
|
||||
let goodInfo = getGoodById(good.id);
|
||||
if (goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
|
||||
if (!!good.seqId) {
|
||||
equips.push(good.seqId);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
let {type} = ITID.get(goodInfo.itid);
|
||||
let curname = getCurNameById(goodInfo.good_id);
|
||||
if (!!curname) {
|
||||
if (curname != 'ap') {
|
||||
currencysMap[curname] = (currencysMap[curname]||0) + good.count;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if (type != CONSUME_TYPE.SKIN){
|
||||
let index = _.indexOf(bags, {id: good.id});
|
||||
if (index > 0) {
|
||||
bags[index].count = bags[index].count + good.count;
|
||||
} else {
|
||||
bags.push(good);
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(items.length > 0) {
|
||||
let {hasError, result} = await ItemModel.decreaseItems(roleId, items);
|
||||
if(hasError) return;
|
||||
resultGoods = result;
|
||||
}
|
||||
|
||||
let uids = [{uid: roleId, sid}];
|
||||
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: resultGoods, gold, coin} ), uids);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
interface Item {id: number, count: number};
|
||||
interface Equip {id: number, name: string, quality: number, type: number};
|
||||
interface Bag {id: number, itemName: string, count: number, type: number, hid:number};
|
||||
export async function addItems(roleId: string, roleName: string, sid: string, goods: Array<Item>) {
|
||||
let showItems: Array<Item> = [];
|
||||
let currencys: Array<Item> = [];
|
||||
let currencysMap: any = {};
|
||||
let equips: Array<Equip> = [];
|
||||
let bags: Array<Bag> = [];
|
||||
let skins: Array<number> = [];
|
||||
let uids = [{uid: roleId, sid}];
|
||||
sortItems(goods, bags, skins, currencys, equips);
|
||||
sortItems(goods, bags, skins, currencysMap, equips, showItems);
|
||||
let equipInfos = [];
|
||||
for (let equip of equips) {
|
||||
let equipInfo = await addEquips(roleId, roleName, equip);
|
||||
@@ -199,17 +222,22 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
||||
}
|
||||
//装备推送
|
||||
if (!!equipInfos.length)
|
||||
pinus.app.get('channelService').pushMessageByUids('onEquipAdd', resResult(STATUS.SUCCESS, equipInfos), uids);
|
||||
|
||||
let data = {};
|
||||
for (let currency of currencys) {
|
||||
await addCurrency(roleId, currency, data);
|
||||
showItems.push(currency);
|
||||
}
|
||||
pinus.app.get('channelService').pushMessageByUids('onEquipAdd', resResult(STATUS.SUCCESS, equipInfos), uids);
|
||||
|
||||
//货币推送
|
||||
if (!!Object.keys(data).length)
|
||||
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, data), uids);
|
||||
if (!!Object.keys(currencysMap).length) {
|
||||
let role = await RoleModel.findByRoleId(roleId);
|
||||
for (let key in currencysMap) {
|
||||
if (key == 'ap') {
|
||||
let {ap} = await setAp(Date.now(), roleId, currencysMap[key]);
|
||||
currencysMap.ap = ap;
|
||||
} else {
|
||||
currencysMap[key] += role[key];
|
||||
}
|
||||
}
|
||||
RoleModel.updateRoleInfo(roleId, currencysMap);
|
||||
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, currencysMap), uids);
|
||||
}
|
||||
|
||||
let bagInfos = [];
|
||||
for (let item of bags) {
|
||||
@@ -235,7 +263,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
||||
return showItems;
|
||||
}
|
||||
|
||||
function sortItems (goods: Array<Item>, bags: Array<Bag>, skins: Array<number>, currencys: Array<Item>, equips: Array<Equip>) {
|
||||
function sortItems (goods: Array<Item>, bags: Array<Bag>, skins: Array<number>, currencysMap: any, equips: Array<Equip>, showItems: Array<Item>) {
|
||||
for (let good of goods) {
|
||||
let goodInfo = getGoodById(good.id);
|
||||
if (goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
|
||||
@@ -245,11 +273,15 @@ function sortItems (goods: Array<Item>, bags: Array<Bag>, skins: Array<number>,
|
||||
} else {
|
||||
let {type, isCurrency} = ITID.get(goodInfo.itid);
|
||||
if (!!isCurrency) {
|
||||
let index = _.indexOf(currencys, {id: good.id});
|
||||
if (index > 0) {
|
||||
currencys[index].count = currencys[index].count + good.count;
|
||||
} else {
|
||||
currencys.push(good);
|
||||
let curname = getCurNameById(goodInfo.good_id);
|
||||
if (!!curname) {
|
||||
let index = _.indexOf(showItems, {id: good.id});
|
||||
if (index > 0) {
|
||||
showItems[index].count = showItems[index].count + good.count;
|
||||
} else {
|
||||
showItems.push(good);
|
||||
}
|
||||
currencysMap[curname] = (currencysMap[curname]||0 )+ good.count;
|
||||
}
|
||||
} else if (type == CONSUME_TYPE.SKIN) {
|
||||
let index = _.indexOf(skins, good.id);
|
||||
@@ -257,7 +289,7 @@ function sortItems (goods: Array<Item>, bags: Array<Bag>, skins: Array<number>,
|
||||
skins.push(good.id);
|
||||
}
|
||||
} else {
|
||||
let index = _.indexOf(skins, {id: good.id});
|
||||
let index = _.indexOf(bags, {id: good.id});
|
||||
if (index > 0) {
|
||||
bags[index].count = bags[index].count + good.count;
|
||||
} else {
|
||||
@@ -294,19 +326,3 @@ async function addEquips (roleId: string, roleName: string, weapon: Equip) {
|
||||
return await EquipModel.createEquip(equip);
|
||||
}
|
||||
|
||||
async function addCurrency(roleId: string, currency: {id: number, count: number }, data: any) {
|
||||
let {id, count} = currency;
|
||||
let dicCurrency = CURRENCY.get(id);
|
||||
let result;
|
||||
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 处理元宝
|
||||
result = await RoleModel.addGoldFree(roleId, count);
|
||||
data.giftGold = result.giftGold;
|
||||
data.gold = result.gold;
|
||||
} else if(dicCurrency.type == CURRENCY_TYPE.COIN) { // 处理铜币
|
||||
result = await RoleModel.addCoin(roleId, count);
|
||||
data.coin = result.coin;
|
||||
} else if(dicCurrency.type == CURRENCY_TYPE.ACTION_POINT) { // 处理体力
|
||||
result = await setAp(Date.now(), roleId, count);
|
||||
data.ap = result.ap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +132,24 @@ for(let obj of currencyArr) {
|
||||
CURRENCY_BY_TYPE.set(obj.type, obj.gid);
|
||||
}
|
||||
|
||||
export function getCurNameById(gid) {
|
||||
let currency = CURRENCY.get(gid);
|
||||
if (!currency) {
|
||||
return;
|
||||
}
|
||||
if (currency.type == CURRENCY_TYPE.COIN) {
|
||||
return 'coin';
|
||||
} else if (currency.type == CURRENCY_TYPE.GOLD) {
|
||||
return 'gold';
|
||||
} else if (currency.type == CURRENCY_TYPE.ACTION_POINT) {
|
||||
return 'ap';
|
||||
} else if (currency.type == CURRENCY_TYPE.FRIEND_POINT) {
|
||||
return 'frdCnt';
|
||||
} else if (currency.type == CURRENCY_TYPE.EXPEDITION_POINT) {
|
||||
return 'expeditionPoint';
|
||||
}
|
||||
}
|
||||
|
||||
export const WAR_TYPE = {
|
||||
NORMAL: 1, // 主线本
|
||||
VESTIGE: 2, // 遗迹本
|
||||
|
||||
@@ -71,6 +71,15 @@ export default class Equip extends BaseModel {
|
||||
let result = await EquipModel.deleteMany({roleId});
|
||||
return result;
|
||||
}
|
||||
public static async deleteEquips(roleId: string, ids:Array<number>) {
|
||||
let result = await EquipModel.deleteMany({roleId, seqId:{$in: ids}});
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async getEquips(roleId: string, ids:Array<number>) {
|
||||
let result = await EquipModel.find({roleId, seqId:{$in: ids}});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const EquipModel = getModelForClass(Equip);
|
||||
|
||||
@@ -54,7 +54,7 @@ export default class Item extends BaseModel {
|
||||
for(let {id, count} of items) {
|
||||
const rec: ItemType = await ItemModel.findOneAndUpdate({roleId, id, count: {$gte: count} },{ $inc: { count: -1 * count }}, {new: true}).lean(lean);
|
||||
if(rec) {
|
||||
result.push(rec);
|
||||
result.push({id: rec.id, count: rec.count});
|
||||
updateItems.push({id, count});
|
||||
} else {
|
||||
hasError = true; break;
|
||||
@@ -74,6 +74,7 @@ export default class Item extends BaseModel {
|
||||
let result = await ItemModel.deleteMany({roleId});
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const ItemModel = getModelForClass(Item);
|
||||
|
||||
@@ -8,7 +8,12 @@ import { HeroModel } from './Hero';
|
||||
interface roleUpdate {
|
||||
ce?: number;
|
||||
_id?:number;
|
||||
coin?: number;
|
||||
gold?:number;
|
||||
frdCnt?: number;
|
||||
expeditionPoint?:number;
|
||||
}
|
||||
|
||||
class TopHero {
|
||||
@prop({ required: true })
|
||||
hid: number; // 武将id
|
||||
|
||||
Reference in New Issue
Block a user