升级显示体力

This commit is contained in:
luying
2021-12-26 15:10:32 +08:00
parent 0c3fedabef
commit e647cb79ea
2 changed files with 14 additions and 11 deletions
+10 -9
View File
@@ -29,7 +29,7 @@ function getApWithDataAp(roleId: string, ip: string, lv: number, dataAp: ActionP
const dicAp = getDicApByLv(lv); const dicAp = getDicApByLv(lv);
const maxAp = dicAp.maxAp; // 最大体力值 const maxAp = dicAp.maxAp; // 最大体力值
const per = AP.PERAPRESTORETIME * 1000; // 恢复时间(ms) 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; let oldAp = ap;
if(shouldRefresh(new Date(buyRefTime), new Date())) { if(shouldRefresh(new Date(buyRefTime), new Date())) {
@@ -39,26 +39,26 @@ function getApWithDataAp(roleId: string, ip: string, lv: number, dataAp: ActionP
let result; let result;
if (ap >= maxAp) { 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 { } else {
if (refTime > now) refTime = now; // refTime:每次记录时候最近的一个整的时间点,绝对不会大于now if (refTime > now) refTime = now; // refTime:每次记录时候最近的一个整的时间点,绝对不会大于now
let n = Math.floor((now - refTime) / per); // 上次记录到现在可以增长多少体力 let n = Math.floor((now - refTime) / per); // 上次记录到现在可以增长多少体力
ap += n; // 增加上 ap += n; // 增加上
if (ap >= maxAp) { // 加上后溢出了 if (ap >= maxAp) { // 加上后溢出了
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 { } else {
refTime += n * per; // 更新refTime到离现在最近的一个整的时间点 refTime += n * per; // 更新refTime到离现在最近的一个整的时间点
let apRemainTime = Math.floor((refTime + per - now) / 1000); // 恢复下一点需要多少时间 let apRemainTime = Math.floor((refTime + per - now) / 1000); // 恢复下一点需要多少时间
let apMaxRemainTime = (maxAp - ap - 1) * AP.PERAPRESTORETIME + apRemainTime; 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) { 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 }) 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 * @param sid
*/ */
export async function setAp(roleId: string, ip: string, lv: number, changeAp: number, sid: string, reason: ITEM_CHANGE_REASON) { 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 now = Date.now();
const dicAp = getDicApByLv(lv); const dicAp = getDicApByLv(lv);
let ApResult = await getAp(roleId, ip, lv, true); let ApResult = await getAp(roleId, ip, lv, true);
let { ap, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver, buyTimes } = ApResult; // 更新ap let { ap: apBefore, maxAp, refTime, apRemainTime, apMaxRemainTime, isOver, buyTimes } = ApResult; // 更新ap
ap += changeAp; let ap = apBefore + changeAp;
if (ap >= maxAp) { // 溢出 if (ap >= maxAp) { // 溢出
refTime = now; refTime = now;
apRemainTime = 0; apRemainTime = 0;
apMaxRemainTime = 0; apMaxRemainTime = 0;
} }
if (ap < 0) return null // 体力不足 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) { // 特殊处理,溢出之后做减少体力的操作时 if (isOver && ap < maxAp) { // 特殊处理,溢出之后做减少体力的操作时
apRemainTime = AP.PERAPRESTORETIME; apRemainTime = AP.PERAPRESTORETIME;
@@ -99,7 +100,7 @@ export async function setAp(roleId: string, ip: string, lv: number, changeAp: nu
if (changeAp != 0) { if (changeAp != 0) {
let uids = [{ uid: roleId, sid }]; 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 return apJson
+4 -2
View File
@@ -14,6 +14,8 @@ export default class ActionPoint extends BaseModel {
refTime: number; // 刷新时间,时间戳 refTime: number; // 刷新时间,时间戳
@prop({ required: true }) @prop({ required: true })
ap: number; // 当前体力 ap: number; // 当前体力
@prop({ required: true })
apBefore: number; // 变化之前体力
@prop({ required: true }) @prop({ required: true })
buyRefTime: number; // 购买刷新时间,时间戳 buyRefTime: number; // 购买刷新时间,时间戳
@@ -28,8 +30,8 @@ export default class ActionPoint extends BaseModel {
return result; return result;
} }
public static async saveAp(roleId: string, ap: number, refTime: number, lean = true) { public static async saveAp(roleId: string, ap: number, apBefore: 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); let result: ActionPointType = await ActionPointModel.findOneAndUpdate({roleId}, { $set: {ap, refTime, apBefore }, $setOnInsert: {buyRefTime: 0, buyTimes: 0} }, {upsert: true, new: true}).lean(lean);
return result; return result;
} }