fix: Fixing the MP power bonus + seed maintenance

This commit is contained in:
gauvainboiche
2026-04-03 14:25:19 +02:00
parent b11446cf56
commit d345c025c0
11 changed files with 349 additions and 132 deletions
+24 -18
View File
@@ -29,28 +29,19 @@ export async function initUserActionQuotaSchema() {
/** Returns the next noon (12:00:00) UTC after the current moment. */
export function nextNoonUtc() {
return nextResetUtc(12);
return nextResetUtc(43200); // 12 hours = 43200 seconds
}
/**
* Returns the next reset timestamp UTC based on a repeating interval.
* @param {number} intervalHours - must be a divisor of 24 (1,2,3,4,6,8,12,24)
* Returns the next reset timestamp UTC based on a repeating interval relative to an epoch.
* @param {number} intervalSeconds - the interval length in seconds (>= 1)
* @param {number} epochSec - Unix seconds origin for slot calculation
*/
export function nextResetUtc(intervalHours) {
const now = new Date();
const utcHours = now.getUTCHours();
const slotsPassed = Math.floor(utcHours / intervalHours);
const nextSlotHour = (slotsPassed + 1) * intervalHours;
if (nextSlotHour < 24) {
return new Date(Date.UTC(
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
nextSlotHour, 0, 0, 0
));
}
return new Date(Date.UTC(
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1,
0, 0, 0, 0
));
export function nextResetUtc(intervalSeconds, epochSec = 0) {
const nowSec = Math.floor(Date.now() / 1000);
const elapsed = Math.max(0, nowSec - epochSec);
const slot = Math.floor(elapsed / intervalSeconds);
return new Date((epochSec + (slot + 1) * intervalSeconds) * 1000);
}
// ── Queries ───────────────────────────────────────────────────────────────────
@@ -153,6 +144,21 @@ export async function resetAllUserActions(actionsRemaining, quotaResetAt) {
);
}
/**
* Resets the quota for all users belonging to a specific team.
* Users who have no row yet get one inserted.
*/
export async function resetUserActionsByTeam(team, actionsRemaining, quotaResetAt) {
await usersPool.query(
`INSERT INTO user_action_quota (user_id, actions_remaining, quota_reset_at)
SELECT id, $1, $2 FROM users WHERE team = $3
ON CONFLICT (user_id) DO UPDATE
SET actions_remaining = $1,
quota_reset_at = $2`,
[actionsRemaining, quotaResetAt, team]
);
}
/** Removes all rows from user_action_quota (used on world-seed wipeout). */
export async function truncateUserActionQuota() {
await usersPool.query(`TRUNCATE user_action_quota`);