Files
ZYZ/game-server/app/servers/battle/handler/dailyBattleHandler.ts
2021-01-27 16:00:49 +08:00

101 lines
4.1 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 { DailyRecordModel } from '../../../db/DailyRecord';
import { getGamedata } from '../../../pubUtils/gamedata';
import { GOLD_COST_RATIO } from '../../../consts';
import { STATUS } from '../../../consts/statusCode';
import { resResult, calculateNum } from '../../../pubUtils/util';
import { RoleModel } from '../../../db/Role';
import { getDailyNum } from '../../../services/dailyBattleService';
import { handleCost } from '../../../services/rewardService';
import { getGoldObject } from '../../../pubUtils/itemUtils';
export default function(app: Application) {
return new DailyBattleHandler(app);
}
export class DailyBattleHandler {
constructor(private app: Application) {
}
// 获取关卡列表
async getData(msg: { }, session: BackendSession) {
let roleId = session.get('roleId');
let {warStar} = await RoleModel.findByRoleId(roleId);
let dicDaily = getGamedata('dic_zyz_daily');
let dicDailyWar = getGamedata('dic_zyz_gk_daily');
let result = new Array();
for(let {dailyType: type, name, timesPerDay, timesCanBuy } of dicDaily) {
let refreshResult = await DailyRecordModel.refreshRecord(roleId, type);
let wars = new Array();
for(let {war_id, dailyType, cost, gk_name, previousGk } of dicDailyWar) {
if(dailyType == type) {
let status = 0, star = 0;
let curBattle = warStar.find(cur => cur.id == war_id);
if(curBattle) {
status = 2;
star = curBattle.star;
} else {
if (previousGk) {
let preBattleRecord = warStar.find(cur => cur.id == previousGk);
if(preBattleRecord) {
status = 1;
} else {
status = 0;
}
} else {
status = 1;
}
}
wars.push({
battleId: war_id, cost, star, status, name: gk_name
});
}
}
let checkDailyResult = await getDailyNum(refreshResult, timesPerDay, timesCanBuy);
result.push({
type, name, ...checkDailyResult,
wars
});
}
return resResult(STATUS.SUCCESS, { list: result });
}
// 购买每日次数
async buyNum(msg: { type: number, count: number }, session: BackendSession) {
let {type, count} = msg;
if( !count || count < 0) return resResult(STATUS.WRONG_PARMS);
const roleId = session.get('roleId');
const sid = session.get('sid');
const dicDaily = getGamedata('dic_zyz_daily');
const curDaily = dicDaily.find(cur => cur.dailyType == type);
if(!curDaily) return resResult(STATUS.DAILY_TYPE_NOT_FOUND);
let { timesPerDay, timesCanBuy } = curDaily;
let { buyCount = 0 } = await DailyRecordModel.refreshRecord(roleId, type);
let { vLv } = await RoleModel.findByRoleId(roleId);
if(vLv > 0) {
timesCanBuy += 0; // 暂留用于处理vip等级增加最大购买次数
}
if(buyCount + count > timesCanBuy ) {
return resResult(STATUS.DAILY_REFRESH_TIMES_LACK)
}
let buyCost = 0;
for(let i = 1; i <= count; i++) {
let num = buyCount + i;
buyCost += calculateNum(GOLD_COST_RATIO.DAILY_REF_NUM, {num}, 50);
}
let {gold} = await RoleModel.findByRoleId(roleId);
if(buyCost > gold) {
return resResult(STATUS.DAILY_REFRESH_GOLD_LACK);
}
await handleCost(roleId, sid, [getGoldObject(buyCost)]);
let newDailyRecord = await DailyRecordModel.increseBuyCount(roleId, type, count);
let checkDailyResult = await getDailyNum(newDailyRecord, timesPerDay, timesCanBuy);
return resResult(STATUS.SUCCESS, { type, ...checkDailyResult, buyCost});
}
}