Files
ZYZ/game-server/app/services/dungeonService.ts
2020-12-15 16:02:59 +08:00

55 lines
2.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 { resResult, shouldRefresh } from '../pubUtils/util';
import { STATUS } from '../consts/statusCode';
import { RoleModel } from '../db/Role';
import { DUNGEON_CONST } from '../consts';
// 检查秘境本次数checkBattle使用
export async function checkDungeonNum(roleId: string, inc: number) {
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;
}
if(dungeonCnt + inc > DUNGEON_CONST.MAX_CNT + dungeonBuyCnt) {
return {status: -1, resResult: resResult(STATUS.DUNGEON_TIMES_LACK)}
}
await RoleModel.increaseDungeonCnt(roleId, needRefresh, 0, curTime);
return { status: 0, data: {
battleCount: DUNGEON_CONST.MAX_CNT + dungeonBuyCnt - dungeonCnt,
buyCount: DUNGEON_CONST.MAX_BUY_CNT - dungeonBuyCnt
}};
}
/**
* 检查秘境本次数并添加warEnd和warSweep使用
* @param roleId
* @param inc 增加的次数
* @param isRef 在计算之前是否检查每日的刷新
*/
export async function checkDungeonAndIncrease(roleId: string, inc: number, isRef: boolean) {
let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime } = await RoleModel.findByRoleId(roleId);
let curTime = new Date();
let needRefresh = isRef && shouldRefresh(dungeonRefTime, curTime, DUNGEON_CONST.REFRESH_TIME);
if(needRefresh) {
dungeonCnt = 0; dungeonBuyCnt = 0;
}
if(dungeonCnt + inc > DUNGEON_CONST.MAX_CNT + dungeonBuyCnt ) {
return { status: -1, resResult: resResult(STATUS.DUNGEON_TIMES_LACK) }
}
console.log('needRefresh', needRefresh);
await RoleModel.increaseDungeonCnt(roleId, needRefresh, inc, curTime);
return {status: 1, data: {
battleCount: DUNGEON_CONST.MAX_CNT + dungeonBuyCnt - dungeonCnt - inc,
buyCount: DUNGEON_CONST.MAX_BUY_CNT - dungeonBuyCnt
}};
}