添加一个物品消耗通用方法
This commit is contained in:
@@ -646,7 +646,7 @@ export class ComBattleHandler {
|
||||
// 战斗胜利队长扣减藏宝图
|
||||
if (result && teamStatus.capId != 'robot') {
|
||||
let res = await ItemModel.decreaseItems(teamStatus.capId, [{id: teamStatus.blueprtId, count: 1}]);
|
||||
if (!res) return resResult(STATUS.COM_BATTLE_BLUEPRT_NOT_ENOUGH);
|
||||
if (res.hasError) return resResult(STATUS.COM_BATTLE_BLUEPRT_NOT_ENOUGH);
|
||||
}
|
||||
channel.pushMessage('onTeamComplete', {teamCode, result});
|
||||
this.teamMap.delete(teamCode);
|
||||
@@ -810,7 +810,7 @@ export class ComBattleHandler {
|
||||
});
|
||||
// 消耗藏宝图和寻宝币
|
||||
const rec = await ItemModel.decreaseItems(roleId, original);
|
||||
if(!rec) {
|
||||
if(rec.hasError) {
|
||||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||
}
|
||||
|
||||
|
||||
31
game-server/app/servers/role/handler/heroHandler.ts
Normal file
31
game-server/app/servers/role/handler/heroHandler.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import {Application, BackendSession, createTcpMailBox, ChannelService} from 'pinus';
|
||||
import { handleCost } from '../../../services/rewardService';
|
||||
import { resResult } from '../../../pubUtils/util';
|
||||
import { STATUS } from '../../../consts/statusCode';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new HeroHandler(app);
|
||||
}
|
||||
|
||||
export class HeroHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
private channelService: ChannelService = this.app.get('channelService');
|
||||
|
||||
public async test(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');
|
||||
|
||||
let {id, count} = msg;
|
||||
|
||||
let result = await handleCost(this.channelService, roleId, sid, [{id, count}] );
|
||||
if(!result) {
|
||||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||
}
|
||||
return resResult(STATUS.SUCCESS);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -51,4 +51,12 @@ export enum ABI_TYPE{
|
||||
/**属性固定值加成(百分比) */
|
||||
TYPE102 = 102,
|
||||
}
|
||||
|
||||
|
||||
export enum ABI_STAGE {
|
||||
HP = 1,
|
||||
ATK = 2,
|
||||
DEF = 3,
|
||||
MDEF = 4,
|
||||
AGI = 5,
|
||||
LUK = 6
|
||||
}
|
||||
@@ -49,10 +49,11 @@ export default class Item extends BaseModel {
|
||||
}
|
||||
|
||||
public static async decreaseItems(roleId: string, items: Array<{id: number, count: number}>, lean = true) {
|
||||
let updateItems = new Array<{id: number, count: number}>(), hasError: boolean = false;
|
||||
let updateItems = new Array<{id: number, count: number}>(), hasError: boolean = false, result = new Array();
|
||||
for(let {id, count} of items) {
|
||||
const rec = await ItemModel.findOneAndUpdate({roleId, id, count: {$gte: count} },{ $inc: { count: -1 * count }}, {new: true}).lean(lean);
|
||||
if(rec) {
|
||||
result.push(rec);
|
||||
updateItems.push({id, count});
|
||||
} else {
|
||||
hasError = true; break;
|
||||
@@ -62,9 +63,9 @@ export default class Item extends BaseModel {
|
||||
for(let {id, count} of updateItems) {
|
||||
await ItemModel.findOneAndUpdate({roleId, id },{ $inc: { count }}, {new: true}).lean(lean);
|
||||
}
|
||||
return false
|
||||
return {hasError: true}
|
||||
} else {
|
||||
return true
|
||||
return {hasError: false, result}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -242,6 +242,11 @@ export default class Role extends BaseModel {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async costCoin(roleId: string, cnt: number, lean = true) {
|
||||
let result = await RoleModel.findOneAndUpdate({roleId, coin: {$gte: cnt}}, { $inc: { coin: -cnt } }, { "new": true}).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async costGold(roleId: string, cnt: number, lean = true) {
|
||||
let result = await RoleModel.findOneAndUpdate({roleId, gold: {$gte: cnt}}, { $inc: { gold: -cnt, totalCost: cnt } }, { "new": true}).lean(lean);
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user