添加一个物品消耗通用方法
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import { GOOD_TYPE, ITID, CURRENCY, CURRENCY_TYPE, COUNTER } from './../consts/consts';
|
||||
import { EquipModel } from './../db/Equip';
|
||||
import { CounterModel } from './../db/Counter';
|
||||
import { decodeStr } from '../pubUtils/util';
|
||||
import { decodeStr, resResult } from '../pubUtils/util';
|
||||
import { getGoodById } from '../pubUtils/gamedata';
|
||||
import { RoleModel } from '../db/Role';
|
||||
import Role, { RoleModel } from '../db/Role';
|
||||
import { setAp } from './actionPointService';
|
||||
import { ItemModel } from '../db/Item';
|
||||
import { ChannelService } from 'pinus';
|
||||
import { isRegExp } from 'util';
|
||||
import { STATUS } from '../consts/statusCode';
|
||||
|
||||
export async function handleFixedReward(roleId: string, roleName: string, rewardStr: string, multi: number) {
|
||||
let reward = decodeStr('fixReward', rewardStr);
|
||||
@@ -119,4 +122,58 @@ async function rewardCurrency (roleId: string, dicGood: any, data: {id:number,cn
|
||||
currencyType: dicCurrency.type
|
||||
});
|
||||
return goods;
|
||||
}
|
||||
|
||||
export async function handleCost(channelService: ChannelService, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(let {id, count} of items) {
|
||||
let item = await ItemModel.findbyRoleAndGidAndCount(roleId, id, count);
|
||||
if(!item) return false
|
||||
}
|
||||
|
||||
// 推送参数
|
||||
let gold = 0, coin = 0, resultGoods = [];
|
||||
if(costGold > 0) {
|
||||
role = await RoleModel.costGold(roleId, costGold);
|
||||
if(role) gold = role.gold;
|
||||
}
|
||||
if(costCoin > 0) {
|
||||
role = await RoleModel.costCoin(roleId, costCoin);
|
||||
if(role) coin = role.coin;
|
||||
}
|
||||
if(items.length > 0) {
|
||||
let {hasError, result} = await ItemModel.decreaseItems(roleId, items);
|
||||
if(hasError) return;
|
||||
resultGoods = result;
|
||||
}
|
||||
|
||||
let uids = [{uid: roleId, sid}];
|
||||
channelService.pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: resultGoods, gold, coin} ), uids);
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user