Files
ZYZ/game-server/app/services/actionPointService.ts
2020-10-15 15:36:47 +08:00

46 lines
1.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 { ActionPointModel } from '../db/ActionPoint';
import { ACTION_POIN } from '../consts/consts';
export async function getAp(now: number, roleId: string) {
let dataAp = await ActionPointModel.getAp(roleId);
const maxAp = ACTION_POIN.MAX; // 最大体力值
const per = ACTION_POIN.PER; // 恢复时间ms)
let {ap, refTime} = dataAp;
if(ap >= maxAp) {
// 体力溢出不需要做时间的处理
return { ap, maxAp, refTime: now, apRemainTime:0, isOver: true }
} else {
if(refTime > now) refTime = now; // refTime每次记录时候最近的一个整的时间点绝对不会大于now
let n = Math.floor((now - refTime)/per); // 上次记录到现在可以增长多少体力
ap += n; // 增加上
if(ap >= maxAp) { // 加上后溢出了
ap = maxAp;
return { ap, maxAp, refTime: now, apRemainTime:0, isOver: true }
} else {
refTime += n * per; // 更新refTime到离现在最近的一个整的时间点
let apRemainTime = Math.floor((refTime + per - now)/1000); // 恢复下一点需要多少时间
return { ap, maxAp, refTime, apRemainTime, isOver: false }
}
}
}
export async function setAp(now: number, roleId: string, changeAp: number) {
let ApResult = await getAp(now, roleId);
let { ap, maxAp, refTime, apRemainTime, isOver } = ApResult; // 更新ap
ap += changeAp;
if(ap >= maxAp) { // 溢出
refTime = now;
apRemainTime = 0;
}
if(ap < 0) return null // 体力不足
await ActionPointModel.saveAp(roleId, ap, refTime);
if(isOver && ap < maxAp) apRemainTime = Math.floor(ACTION_POIN.PER / 1000); // 特殊处理
return {ap, maxAp, refTime, apRemainTime}
}