Files
ZYZ/game-server/app/services/dungeonService.ts
2022-03-30 14:22:46 +08:00

106 lines
3.8 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, RoleType } from '../db/Role';
import * as dicParam from '../pubUtils/dicParam';
import { RankParam } from '../domain/rank';
import { BattleRecordType } from '../db/BattleRecord';
import { gameData } from '../pubUtils/data';
import { DungeonFirstModel } from '../db/DungeonFirst';
import { nowSeconds } from '../pubUtils/timeUtil';
import { getVipDungeonCnt } from './activity/monthlyTicketService';
/**
* 获取秘境本数据
* @param role
*/
export async function getDungeonData(role: RoleType) {
let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime, dungeonHeroes=[] } = role;
let curTime = new Date();
if(shouldRefresh(dungeonRefTime, curTime)) {
dungeonCnt = 0; dungeonBuyCnt = 0;
}
let nextCostGold = getDungeonBuyCountCost(dungeonBuyCnt + 1);
let freeCount = getVipDungeonCnt(role.vipStartTime);
return {
nextCostGold,
dungeonHeroes,
battleCount: freeCount + dungeonBuyCnt - dungeonCnt,
buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - dungeonBuyCnt
}
}
// 检查秘境本次数checkBattle使用
export async function checkDungeonNum(roleId: string, inc: number) {
let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime, vipStartTime } = await RoleModel.findByRoleId(roleId);
let curTime = new Date();
let needRefresh = shouldRefresh(dungeonRefTime, curTime);
if(needRefresh) {
dungeonCnt = 0; dungeonBuyCnt = 0;
}
let freeCount = getVipDungeonCnt(vipStartTime);
if(dungeonCnt + inc > freeCount + dungeonBuyCnt) {
return {status: -1, resResult: resResult(STATUS.DUNGEON_TIMES_LACK)}
}
await RoleModel.increaseDungeonCnt(roleId, needRefresh, 0, curTime);
return { status: 0, data: {
battleCount: freeCount + dungeonBuyCnt - dungeonCnt,
buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - 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, vipStartTime } = await RoleModel.findByRoleId(roleId);
let curTime = new Date();
let needRefresh = isRef && shouldRefresh(dungeonRefTime, curTime);
if(needRefresh) {
dungeonCnt = 0; dungeonBuyCnt = 0;
}
let freeCount = getVipDungeonCnt(vipStartTime);
if(dungeonCnt + inc > freeCount + 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: freeCount + dungeonBuyCnt - dungeonCnt - inc,
buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - dungeonBuyCnt
}};
}
/**
* 秘境购买次数的花费
* @param num 如果没有买过次数那么这里算第一次传1
* @returns
*/
export function getDungeonBuyCountCost(num:number) {
let cost = (num - 1) * dicParam.DUNGEON_CONST.DUNGEON_CONST_UP + dicParam.DUNGEON_CONST.DUNGEON_CONST_BASE;
if(cost > dicParam.DUNGEON_CONST.DUNGEON_CONST_TOP) {
cost = dicParam.DUNGEON_CONST.DUNGEON_CONST_TOP;
}
return cost;
}
export async function saveDungeonFirst(role: RoleType, warId: number, battleRecord: BattleRecordType) {
let userInfo = new RankParam(role);
let lineup = battleRecord.record.lineup;
let dicWar = gameData.war.get(warId);
await DungeonFirstModel.createDungeonFirst(role.serverId, dicWar.movePoint, warId, {
time: nowSeconds(), roleId: role.roleId, userInfo, lineup
})
}