From 758f1dad017e20c9974e1d8361aad91ab194fa42 Mon Sep 17 00:00:00 2001 From: luying Date: Tue, 8 Dec 2020 19:36:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA=E7=89=A9?= =?UTF-8?q?=E5=93=81=E6=B6=88=E8=80=97=E9=80=9A=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../battle/handler/comBattleHandler.ts | 4 +- .../app/servers/role/handler/heroHandler.ts | 31 ++++++++++ game-server/app/services/rewardService.ts | 61 ++++++++++++++++++- shared/consts/abilityConst.ts | 10 ++- shared/db/Item.ts | 7 ++- shared/db/Role.ts | 5 ++ 6 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 game-server/app/servers/role/handler/heroHandler.ts diff --git a/game-server/app/servers/battle/handler/comBattleHandler.ts b/game-server/app/servers/battle/handler/comBattleHandler.ts index 1c9e5e6ad..1151a653c 100644 --- a/game-server/app/servers/battle/handler/comBattleHandler.ts +++ b/game-server/app/servers/battle/handler/comBattleHandler.ts @@ -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); } diff --git a/game-server/app/servers/role/handler/heroHandler.ts b/game-server/app/servers/role/handler/heroHandler.ts new file mode 100644 index 000000000..b3e2ccf84 --- /dev/null +++ b/game-server/app/servers/role/handler/heroHandler.ts @@ -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); + + } + +} diff --git a/game-server/app/services/rewardService.ts b/game-server/app/services/rewardService.ts index c91d68522..db12d3dcf 100644 --- a/game-server/app/services/rewardService.ts +++ b/game-server/app/services/rewardService.ts @@ -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() ; + 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 } \ No newline at end of file diff --git a/shared/consts/abilityConst.ts b/shared/consts/abilityConst.ts index 211ba420c..7aaf44fbf 100644 --- a/shared/consts/abilityConst.ts +++ b/shared/consts/abilityConst.ts @@ -51,4 +51,12 @@ export enum ABI_TYPE{ /**属性固定值加成(百分比) */ TYPE102 = 102, } - \ No newline at end of file + + export enum ABI_STAGE { + HP = 1, + ATK = 2, + DEF = 3, + MDEF = 4, + AGI = 5, + LUK = 6 + } \ No newline at end of file diff --git a/shared/db/Item.ts b/shared/db/Item.ts index 88217788a..8f24bd364 100644 --- a/shared/db/Item.ts +++ b/shared/db/Item.ts @@ -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} } } diff --git a/shared/db/Role.ts b/shared/db/Role.ts index 02c7cb467..e5f882a73 100644 --- a/shared/db/Role.ts +++ b/shared/db/Role.ts @@ -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;