99 lines
4.1 KiB
TypeScript
99 lines
4.1 KiB
TypeScript
import { Application, BackendSession } from 'pinus';
|
|
import { DailyRecordModel } from '../../../db/DailyRecord';
|
|
import { getGamedata } from '../../../pubUtils/gamedata';
|
|
import { GOLD_COST_RATIO } from '../../../consts/consts';
|
|
import { STATUS } from '../../../consts/statusCode';
|
|
import { resResult, calculateNum } from '../../../pubUtils/util';
|
|
import { RoleModel } from '../../../db/Role';
|
|
import { getDailyNum } from '../../../services/dailyBattleService';
|
|
|
|
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);
|
|
checkDailyResult['count'] = checkDailyResult.battleCount;
|
|
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 < 0) return resResult(STATUS.WRONG_PARMS);
|
|
const roleId = session.get('roleId');
|
|
|
|
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 RoleModel.costGold(roleId, buyCost);
|
|
let newDailyRecord = await DailyRecordModel.increseBuyCount(roleId, type, count);
|
|
|
|
let checkDailyResult = await getDailyNum(newDailyRecord, timesPerDay, timesCanBuy);
|
|
checkDailyResult['count'] = checkDailyResult.battleCount;
|
|
return resResult(STATUS.SUCCESS, { type, ...checkDailyResult, buyCost});
|
|
}
|
|
} |