Files
ZYZ/game-server/app/servers/battle/handler/dungeonBattleHandler.ts
2020-12-15 16:02:59 +08:00

73 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Application, BackendSession } from 'pinus';
import { GOLD_COST_RATIO, DUNGEON_CONST } from '../../../consts';
import { STATUS } from '../../../consts/statusCode';
import { resResult, calculateNum, shouldRefresh } from '../../../pubUtils/util';
import { RoleModel } from '../../../db/Role';
export default function(app: Application) {
return new DungeonBattleHandler(app);
}
export class DungeonBattleHandler {
constructor(private app: Application) {
}
// 获取关卡列表
async getData(msg: { }, session: BackendSession) {
let roleId = session.get('roleId');
let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime, dungeonHeroes=[] } = await RoleModel.findByRoleId(roleId);
let curTime = new Date();
if(shouldRefresh(dungeonRefTime, curTime, DUNGEON_CONST.REFRESH_TIME)) {
dungeonCnt = 0; dungeonBuyCnt = 0;
}
let nextCostGold = calculateNum(GOLD_COST_RATIO.DUNGRON_BUY_NUM, {num: dungeonBuyCnt + 1 }, 50);
return resResult(STATUS.SUCCESS, {
nextCostGold,
dungeonHeroes,
battleCount: DUNGEON_CONST.MAX_CNT + dungeonBuyCnt - dungeonCnt,
buyCount: DUNGEON_CONST.MAX_BUY_CNT - dungeonBuyCnt
});
}
// 购买每日次数
async buyNum(msg: { count: number }, session: BackendSession) {
let { count } = msg;
if(count < 0) return resResult(STATUS.WRONG_PARMS);
const roleId = session.get('roleId');
let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime } = await RoleModel.findByRoleId(roleId);
let curTime = new Date();
let needRefresh = shouldRefresh(dungeonRefTime, curTime, DUNGEON_CONST.REFRESH_TIME);
if(needRefresh) {
dungeonCnt = 0; dungeonBuyCnt = 0;
}
let timesCanBuy = DUNGEON_CONST.MAX_BUY_CNT;
let { vLv } = await RoleModel.findByRoleId(roleId);
if(vLv > 0) {
timesCanBuy += 0; // 暂留用于处理vip等级增加最大购买次数
}
if(dungeonBuyCnt + count > timesCanBuy ) {
return resResult(STATUS.DUNGEON_REFRESH_TIMES_LACK)
}
let buyCost = 0;
for(let i = 1; i <= count; i++) {
let num = dungeonBuyCnt + i;
buyCost += calculateNum(GOLD_COST_RATIO.DUNGRON_BUY_NUM, {num}, 50);
}
let {gold} = await RoleModel.findByRoleId(roleId);
if(buyCost > gold) {
return resResult(STATUS.DAILY_REFRESH_GOLD_LACK);
}
await RoleModel.costGold(roleId, buyCost);
await RoleModel.buyCnt(roleId, needRefresh, count, curTime);
let nextCostGold = calculateNum(GOLD_COST_RATIO.DUNGRON_BUY_NUM, {num: dungeonBuyCnt + count + 1 }, 50);
return resResult(STATUS.SUCCESS, {
costGold: buyCost,
nextCostGold,
battleCount: DUNGEON_CONST.MAX_CNT + dungeonBuyCnt + count - dungeonCnt,
buyCount: DUNGEON_CONST.MAX_BUY_CNT - dungeonBuyCnt - count
});
}
}