物品消耗
This commit is contained in:
@@ -20,7 +20,23 @@ export class HeroHandler {
|
|||||||
|
|
||||||
private channelService: ChannelService = this.app.get('channelService');
|
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 roleId: string = session.get('roleId');
|
||||||
let roleName: string = session.get('roleName');
|
let roleName: string = session.get('roleName');
|
||||||
let sid: string = session.get('sid');
|
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 { EquipModel } from './../db/Equip';
|
||||||
import { CounterModel } from './../db/Counter';
|
import { CounterModel } from './../db/Counter';
|
||||||
import { decodeStr, resResult } from '../pubUtils/util';
|
import { decodeStr, resResult } from '../pubUtils/util';
|
||||||
@@ -125,72 +125,95 @@ async function rewardCurrency (roleId: string, dicGood: any, data: {id:number,cn
|
|||||||
});
|
});
|
||||||
return goods;
|
return goods;
|
||||||
}
|
}
|
||||||
|
interface Item {id: number, count: number, seqId?: number};
|
||||||
export async function handleCost(roleId: string, sid: string, goods: Array<{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};
|
||||||
let role, costGold = 0, costCoin = 0, items = new Array<{id: number, count: number}>(), ids = new Array<number>() ;
|
export async function handleCost(roleId: string, sid: string, goods: Array<Item>) {
|
||||||
for(let {id, count} of goods) {
|
let currencysMap: any = {};
|
||||||
let goodInfo = getGoodById(id);
|
let equips: Array<number> = [];
|
||||||
if(goodInfo.goodType == GOOD_TYPE.CONSUMES|| goodInfo.goodType == GOOD_TYPE.SCRIPT) { // 消耗品
|
let bags: Array<Item> = [];
|
||||||
let {isCurrency} = ITID.get(goodInfo.itid);
|
let uids = [{uid: roleId, sid}];
|
||||||
if(isCurrency) { // 货币
|
if (!sortConsumes(goods, bags, currencysMap, equips))
|
||||||
if(!role) role = await RoleModel.findByRoleId(roleId);
|
return false;
|
||||||
let dicCurrency = CURRENCY.get(id);
|
// 检查货币是否充足
|
||||||
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 处理元宝
|
if (!!Object.keys(currencysMap).length) {
|
||||||
if(role.gold < count + costGold) return false;
|
let role = await RoleModel.findByRoleId(roleId);
|
||||||
costGold += count;
|
for (let key in currencysMap) {
|
||||||
} else if(dicCurrency.type == CURRENCY_TYPE.COIN) { // 处理铜币
|
currencysMap[key] = role[key] - currencysMap[key];
|
||||||
if(role.coin < count + costCoin) return false;
|
if (currencysMap[key] < 0) {
|
||||||
costCoin += count;
|
return false;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let findItem = items.find(cur => cur.id == id);
|
|
||||||
if(findItem) {
|
|
||||||
findItem.count += count;
|
|
||||||
} else {
|
|
||||||
items.push({id, count}); ids.push(id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(let {id, count} of items) {
|
//检查装备是否存在
|
||||||
let item = await ItemModel.findbyRoleAndGidAndCount(roleId, id, count);
|
if (!!equips.length) {
|
||||||
if(!item) return false
|
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 (!!equips.length) {
|
||||||
if(costGold > 0) {
|
await EquipModel.deleteEquips(roleId, equips);
|
||||||
role = await RoleModel.costGold(roleId, costGold);
|
pinus.app.get('channelService').pushMessageByUids('onEquipDel', resResult(STATUS.SUCCESS, equips), uids);
|
||||||
if(role) gold = role.gold;
|
|
||||||
}
|
}
|
||||||
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;
|
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>) {
|
export async function addItems(roleId: string, roleName: string, sid: string, goods: Array<Item>) {
|
||||||
let showItems: Array<Item> = [];
|
let showItems: Array<Item> = [];
|
||||||
let currencys: Array<Item> = [];
|
let currencysMap: any = {};
|
||||||
let equips: Array<Equip> = [];
|
let equips: Array<Equip> = [];
|
||||||
let bags: Array<Bag> = [];
|
let bags: Array<Bag> = [];
|
||||||
let skins: Array<number> = [];
|
let skins: Array<number> = [];
|
||||||
let uids = [{uid: roleId, sid}];
|
let uids = [{uid: roleId, sid}];
|
||||||
sortItems(goods, bags, skins, currencys, equips);
|
sortItems(goods, bags, skins, currencysMap, equips, showItems);
|
||||||
let equipInfos = [];
|
let equipInfos = [];
|
||||||
for (let equip of equips) {
|
for (let equip of equips) {
|
||||||
let equipInfo = await addEquips(roleId, roleName, equip);
|
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)
|
if (!!equipInfos.length)
|
||||||
pinus.app.get('channelService').pushMessageByUids('onEquipAdd', resResult(STATUS.SUCCESS, equipInfos), uids);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
//货币推送
|
//货币推送
|
||||||
if (!!Object.keys(data).length)
|
if (!!Object.keys(currencysMap).length) {
|
||||||
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, data), uids);
|
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 = [];
|
let bagInfos = [];
|
||||||
for (let item of bags) {
|
for (let item of bags) {
|
||||||
@@ -235,7 +263,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
|
|||||||
return showItems;
|
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) {
|
for (let good of goods) {
|
||||||
let goodInfo = getGoodById(good.id);
|
let goodInfo = getGoodById(good.id);
|
||||||
if (goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
|
if (goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
|
||||||
@@ -245,11 +273,15 @@ function sortItems (goods: Array<Item>, bags: Array<Bag>, skins: Array<number>,
|
|||||||
} else {
|
} else {
|
||||||
let {type, isCurrency} = ITID.get(goodInfo.itid);
|
let {type, isCurrency} = ITID.get(goodInfo.itid);
|
||||||
if (!!isCurrency) {
|
if (!!isCurrency) {
|
||||||
let index = _.indexOf(currencys, {id: good.id});
|
let curname = getCurNameById(goodInfo.good_id);
|
||||||
if (index > 0) {
|
if (!!curname) {
|
||||||
currencys[index].count = currencys[index].count + good.count;
|
let index = _.indexOf(showItems, {id: good.id});
|
||||||
} else {
|
if (index > 0) {
|
||||||
currencys.push(good);
|
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) {
|
} else if (type == CONSUME_TYPE.SKIN) {
|
||||||
let index = _.indexOf(skins, good.id);
|
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);
|
skins.push(good.id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let index = _.indexOf(skins, {id: good.id});
|
let index = _.indexOf(bags, {id: good.id});
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
bags[index].count = bags[index].count + good.count;
|
bags[index].count = bags[index].count + good.count;
|
||||||
} else {
|
} else {
|
||||||
@@ -294,19 +326,3 @@ async function addEquips (roleId: string, roleName: string, weapon: Equip) {
|
|||||||
return await EquipModel.createEquip(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);
|
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 = {
|
export const WAR_TYPE = {
|
||||||
NORMAL: 1, // 主线本
|
NORMAL: 1, // 主线本
|
||||||
VESTIGE: 2, // 遗迹本
|
VESTIGE: 2, // 遗迹本
|
||||||
|
|||||||
@@ -71,6 +71,15 @@ export default class Equip extends BaseModel {
|
|||||||
let result = await EquipModel.deleteMany({roleId});
|
let result = await EquipModel.deleteMany({roleId});
|
||||||
return result;
|
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);
|
export const EquipModel = getModelForClass(Equip);
|
||||||
|
|||||||
+2
-1
@@ -54,7 +54,7 @@ export default class Item extends BaseModel {
|
|||||||
for(let {id, count} of items) {
|
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);
|
const rec: ItemType = await ItemModel.findOneAndUpdate({roleId, id, count: {$gte: count} },{ $inc: { count: -1 * count }}, {new: true}).lean(lean);
|
||||||
if(rec) {
|
if(rec) {
|
||||||
result.push(rec);
|
result.push({id: rec.id, count: rec.count});
|
||||||
updateItems.push({id, count});
|
updateItems.push({id, count});
|
||||||
} else {
|
} else {
|
||||||
hasError = true; break;
|
hasError = true; break;
|
||||||
@@ -74,6 +74,7 @@ export default class Item extends BaseModel {
|
|||||||
let result = await ItemModel.deleteMany({roleId});
|
let result = await ItemModel.deleteMany({roleId});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ItemModel = getModelForClass(Item);
|
export const ItemModel = getModelForClass(Item);
|
||||||
|
|||||||
@@ -8,7 +8,12 @@ import { HeroModel } from './Hero';
|
|||||||
interface roleUpdate {
|
interface roleUpdate {
|
||||||
ce?: number;
|
ce?: number;
|
||||||
_id?:number;
|
_id?:number;
|
||||||
|
coin?: number;
|
||||||
|
gold?:number;
|
||||||
|
frdCnt?: number;
|
||||||
|
expeditionPoint?:number;
|
||||||
}
|
}
|
||||||
|
|
||||||
class TopHero {
|
class TopHero {
|
||||||
@prop({ required: true })
|
@prop({ required: true })
|
||||||
hid: number; // 武将id
|
hid: number; // 武将id
|
||||||
|
|||||||
Reference in New Issue
Block a user