体力:添加购买体力功能
This commit is contained in:
@@ -7,39 +7,47 @@ import { TASK_TYPE, STATUS } from '../consts';
|
||||
import { checkActivityTask, checkTask } from './taskService';
|
||||
import { getDicApByLv } from '../pubUtils/data';
|
||||
import { pinus } from 'pinus';
|
||||
import { resResult } from '../pubUtils/util';
|
||||
import { resResult, shouldRefresh } from '../pubUtils/util';
|
||||
import { RoleModel } from '../db/Role';
|
||||
import { pick } from 'underscore';
|
||||
import { AP } from '../pubUtils/dicParam';
|
||||
|
||||
/**
|
||||
* 获取当前体力
|
||||
* @param roleId 玩家id
|
||||
* @param lv 玩家等级
|
||||
*/
|
||||
export async function getAp(roleId: string, lv: number) {
|
||||
export async function getAp(roleId: string, lv: number, fromGetAp = false) {
|
||||
const now = Date.now();
|
||||
const dicAp = getDicApByLv(lv);
|
||||
let dataAp = await ActionPointModel.getAp(roleId);
|
||||
const maxAp = dicAp.maxAp; // 最大体力值
|
||||
const per = dicAp.perApRestoreTime * 1000; // 恢复时间(ms)
|
||||
let { ap, refTime } = dataAp;
|
||||
const per = AP.PERAPRESTORETIME * 1000; // 恢复时间(ms)
|
||||
let { ap, refTime, buyRefTime = 0, buyTimes = 0 } = dataAp;
|
||||
|
||||
if(shouldRefresh(new Date(buyRefTime), new Date())) {
|
||||
buyTimes = 0;
|
||||
}
|
||||
|
||||
let result;
|
||||
if (ap >= maxAp) {
|
||||
// 体力溢出不需要做时间的处理
|
||||
return { ap, maxAp, refTime: now, apRemainTime: 0, apMaxRemainTime: 0, isOver: true }
|
||||
result = { ap, maxAp, refTime: now, apRemainTime: 0, apMaxRemainTime: 0, isOver: true, buyTimes, buyRefTime };
|
||||
} 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, apMaxRemainTime: 0, isOver: true }
|
||||
result = { ap, maxAp, refTime: now, apRemainTime: 0, apMaxRemainTime: 0, isOver: true, buyTimes, buyRefTime };
|
||||
} else {
|
||||
refTime += n * per; // 更新refTime到离现在最近的一个整的时间点
|
||||
let apRemainTime = Math.floor((refTime + per - now) / 1000); // 恢复下一点需要多少时间
|
||||
let apMaxRemainTime = (maxAp - ap - 1) * dicAp.perApRestoreTime + apRemainTime;
|
||||
return { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver: false }
|
||||
let apMaxRemainTime = (maxAp - ap - 1) * AP.PERAPRESTORETIME + apRemainTime;
|
||||
result = { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver: false, buyTimes, buyRefTime };
|
||||
}
|
||||
}
|
||||
return fromGetAp?result: pick(result, ['ap', 'maxAp', 'apRemainTime', 'apMaxRemainTime', 'buyTimes']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,8 +61,8 @@ export async function getAp(roleId: string, lv: number) {
|
||||
export async function setAp(roleId: string, lv: number, changeAp: number, sid: string, funcs: number[]) {
|
||||
const now = Date.now();
|
||||
const dicAp = getDicApByLv(lv);
|
||||
let ApResult = await getAp(roleId, lv);
|
||||
let { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver } = ApResult; // 更新ap
|
||||
let ApResult = await getAp(roleId, lv, true);
|
||||
let { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver, buyTimes } = ApResult; // 更新ap
|
||||
ap += changeAp;
|
||||
if (ap >= maxAp) { // 溢出
|
||||
refTime = now;
|
||||
@@ -65,8 +73,8 @@ export async function setAp(roleId: string, lv: number, changeAp: number, sid: s
|
||||
await ActionPointModel.saveAp(roleId, ap, refTime);
|
||||
|
||||
if (isOver && ap < maxAp) { // 特殊处理,溢出之后做减少体力的操作时
|
||||
apRemainTime = dicAp.perApRestoreTime;
|
||||
apMaxRemainTime = (maxAp - ap) * dicAp.perApRestoreTime;
|
||||
apRemainTime = AP.PERAPRESTORETIME;
|
||||
apMaxRemainTime = (maxAp - ap) * AP.PERAPRESTORETIME;
|
||||
}
|
||||
|
||||
if (changeAp < 0) {
|
||||
@@ -74,7 +82,7 @@ export async function setAp(roleId: string, lv: number, changeAp: number, sid: s
|
||||
let { serverId } = await RoleModel.findByRoleId(roleId);
|
||||
await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.BATTLE_COST_AP, -1 * changeAp,);
|
||||
}
|
||||
let apJson = { ap, maxAp, refTime, apMaxRemainTime, apRemainTime };
|
||||
let apJson = { ap, maxAp, refTime, apMaxRemainTime, apRemainTime, buyTimes };
|
||||
if (changeAp != 0) {
|
||||
let uids = [{ uid: roleId, sid }];
|
||||
pinus.app.get('channelService').pushMessageByUids('onApUpdate', resResult(STATUS.SUCCESS, { apJson }), uids);
|
||||
@@ -82,3 +90,24 @@ export async function setAp(roleId: string, lv: number, changeAp: number, sid: s
|
||||
|
||||
return apJson
|
||||
}
|
||||
|
||||
export async function setApBuyTimes(roleId: string, lv: number, sid: string, incTimes: number) {
|
||||
if(incTimes <= 0) return false;
|
||||
|
||||
let ApResult = await getAp(roleId, lv, true);
|
||||
let { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, buyTimes, buyRefTime } = ApResult; // 更新ap
|
||||
|
||||
if(shouldRefresh(new Date(buyRefTime), new Date())) {
|
||||
buyTimes = 0;
|
||||
buyRefTime = new Date();
|
||||
}
|
||||
buyTimes += incTimes;
|
||||
await ActionPointModel.saveBuyTimes(roleId, buyTimes, buyRefTime);
|
||||
|
||||
let apJson = { ap, maxAp, refTime, apMaxRemainTime, apRemainTime, buyTimes };
|
||||
if(incTimes != 0) {
|
||||
let uids = [{ uid: roleId, sid }];
|
||||
pinus.app.get('channelService').pushMessageByUids('onApUpdate', resResult(STATUS.SUCCESS, { apJson }), uids);
|
||||
}
|
||||
return apJson;
|
||||
}
|
||||
Reference in New Issue
Block a user