升级显示体力

This commit is contained in:
luying
2021-12-26 15:08:46 +08:00
parent 0c3fedabef
commit e647cb79ea
2 changed files with 14 additions and 11 deletions

View File

@@ -29,7 +29,7 @@ function getApWithDataAp(roleId: string, ip: string, lv: number, dataAp: ActionP
const dicAp = getDicApByLv(lv);
const maxAp = dicAp.maxAp; // 最大体力值
const per = AP.PERAPRESTORETIME * 1000; // 恢复时间ms)
let { ap, refTime, buyRefTime = 0, buyTimes = 0 } = dataAp;
let { ap, refTime, buyRefTime = 0, buyTimes = 0, apBefore } = dataAp;
let oldAp = ap;
if(shouldRefresh(new Date(buyRefTime), new Date())) {
@@ -39,26 +39,26 @@ function getApWithDataAp(roleId: string, ip: string, lv: number, dataAp: ActionP
let result;
if (ap >= maxAp) {
// 体力溢出不需要做时间的处理
result = { ap, maxAp, refTime: now, apRemainTime: 0, apMaxRemainTime: 0, isOver: true, buyTimes, buyRefTime };
result = { ap, maxAp, refTime: now, apRemainTime: 0, apMaxRemainTime: 0, isOver: true, buyTimes, buyRefTime, apBefore };
} else {
if (refTime > now) refTime = now; // refTime每次记录时候最近的一个整的时间点绝对不会大于now
let n = Math.floor((now - refTime) / per); // 上次记录到现在可以增长多少体力
ap += n; // 增加上
if (ap >= maxAp) { // 加上后溢出了
ap = maxAp;
result = { ap, maxAp, refTime: now, apRemainTime: 0, apMaxRemainTime: 0, isOver: true, buyTimes, buyRefTime };
result = { ap, maxAp, refTime: now, apRemainTime: 0, apMaxRemainTime: 0, isOver: true, buyTimes, buyRefTime, apBefore };
} else {
refTime += n * per; // 更新refTime到离现在最近的一个整的时间点
let apRemainTime = Math.floor((refTime + per - now) / 1000); // 恢复下一点需要多少时间
let apMaxRemainTime = (maxAp - ap - 1) * AP.PERAPRESTORETIME + apRemainTime;
result = { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver: false, buyTimes, buyRefTime };
result = { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver: false, buyTimes, buyRefTime, apBefore };
}
}
if(result.ap > oldAp) {
reportTAEvent(roleId, TA_EVENT.AP_GET, { change_count: oldAp - result.ap, change_after: result.ap, change_reason: ITEM_CHANGE_REASON.AP_RECOVERY })
}
return fromGetAp?result: pick(result, ['ap', 'maxAp', 'apRemainTime', 'apMaxRemainTime', 'buyTimes']);
return fromGetAp?result: pick(result, ['ap', 'maxAp', 'apRemainTime', 'apMaxRemainTime', 'buyTimes', 'apBefore']);
}
/**
@@ -69,18 +69,19 @@ function getApWithDataAp(roleId: string, ip: string, lv: number, dataAp: ActionP
* @param sid
*/
export async function setAp(roleId: string, ip: string, lv: number, changeAp: number, sid: string, reason: ITEM_CHANGE_REASON) {
console.log('***** setAp', roleId, ip, lv, changeAp)
const now = Date.now();
const dicAp = getDicApByLv(lv);
let ApResult = await getAp(roleId, ip, lv, true);
let { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver, buyTimes } = ApResult; // 更新ap
ap += changeAp;
let { ap: apBefore, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver, buyTimes } = ApResult; // 更新ap
let ap = apBefore + changeAp;
if (ap >= maxAp) { // 溢出
refTime = now;
apRemainTime = 0;
apMaxRemainTime = 0;
}
if (ap < 0) return null // 体力不足
let dataAp = await ActionPointModel.saveAp(roleId, ap, refTime);
let dataAp = await ActionPointModel.saveAp(roleId, ap, apBefore, refTime);
if (isOver && ap < maxAp) { // 特殊处理,溢出之后做减少体力的操作时
apRemainTime = AP.PERAPRESTORETIME;
@@ -99,7 +100,7 @@ export async function setAp(roleId: string, ip: string, lv: number, changeAp: nu
if (changeAp != 0) {
let uids = [{ uid: roleId, sid }];
pinus.app.get('channelService').pushMessageByUids('onApUpdate', resResult(STATUS.SUCCESS, { apJson }), uids);
pinus.app.get('channelService').pushMessageByUids('onApUpdate', resResult(STATUS.SUCCESS, { ...apJson }), uids);
}
return apJson

View File

@@ -14,6 +14,8 @@ export default class ActionPoint extends BaseModel {
refTime: number; // 刷新时间,时间戳
@prop({ required: true })
ap: number; // 当前体力
@prop({ required: true })
apBefore: number; // 变化之前体力
@prop({ required: true })
buyRefTime: number; // 购买刷新时间,时间戳
@@ -28,8 +30,8 @@ export default class ActionPoint extends BaseModel {
return result;
}
public static async saveAp(roleId: string, ap: number, refTime: number, lean = true) {
let result: ActionPointType = await ActionPointModel.findOneAndUpdate({roleId}, { $set: {ap, refTime}, $setOnInsert: {buyRefTime: 0, buyTimes: 0} }, {upsert: true, new: true}).lean(lean);
public static async saveAp(roleId: string, ap: number, apBefore: number, refTime: number, lean = true) {
let result: ActionPointType = await ActionPointModel.findOneAndUpdate({roleId}, { $set: {ap, refTime, apBefore }, $setOnInsert: {buyRefTime: 0, buyTimes: 0} }, {upsert: true, new: true}).lean(lean);
return result;
}