From e647cb79eac12253d2ac4c2e0d304f6075f58d63 Mon Sep 17 00:00:00 2001 From: luying Date: Sun, 26 Dec 2021 15:08:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E6=98=BE=E7=A4=BA=E4=BD=93?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/services/actionPointService.ts | 19 ++++++++++--------- shared/db/ActionPoint.ts | 6 ++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/game-server/app/services/actionPointService.ts b/game-server/app/services/actionPointService.ts index 865a47563..22924add5 100644 --- a/game-server/app/services/actionPointService.ts +++ b/game-server/app/services/actionPointService.ts @@ -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 diff --git a/shared/db/ActionPoint.ts b/shared/db/ActionPoint.ts index 514ed9621..984fd8cbc 100644 --- a/shared/db/ActionPoint.ts +++ b/shared/db/ActionPoint.ts @@ -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; }