counters: make decay accurate over time, even if it's sometimes rounded; don't apply decay of 0
This commit is contained in:
parent
39448a4213
commit
a93db7f417
1 changed files with 10 additions and 3 deletions
|
@ -184,8 +184,6 @@ export class GuildCounters extends BaseGuildRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
async decay(id: number, decayPeriodMs: number, decayAmount: number) {
|
async decay(id: number, decayPeriodMs: number, decayAmount: number) {
|
||||||
const now = moment.utc().format(DBDateFormat);
|
|
||||||
|
|
||||||
const counter = (await this.counters.findOne({
|
const counter = (await this.counters.findOne({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
|
@ -198,6 +196,15 @@ export class GuildCounters extends BaseGuildRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
const decayAmountToApply = Math.round((diffFromLastDecayMs / decayPeriodMs) * decayAmount);
|
const decayAmountToApply = Math.round((diffFromLastDecayMs / decayPeriodMs) * decayAmount);
|
||||||
|
if (decayAmountToApply === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate new last_decay_at based on the rounded decay amount we applied. This makes it so that over time, the decayed amount will stay accurate, even if we round some here.
|
||||||
|
const newLastDecayDate = moment
|
||||||
|
.utc(counter.last_decay_at)
|
||||||
|
.add((decayAmountToApply / decayAmount) * decayPeriodMs, "ms")
|
||||||
|
.format(DBDateFormat);
|
||||||
|
|
||||||
const rawUpdate =
|
const rawUpdate =
|
||||||
decayAmountToApply >= 0
|
decayAmountToApply >= 0
|
||||||
|
@ -218,7 +225,7 @@ export class GuildCounters extends BaseGuildRepository {
|
||||||
id,
|
id,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
last_decay_at: now,
|
last_decay_at: newLastDecayDate,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue