refacto: Changing click cooldown to daily actions for users and teams
This commit is contained in:
@@ -106,6 +106,13 @@ export async function initGameSchema() {
|
||||
END $$;
|
||||
ALTER TABLE grid_cells ALTER COLUMN discovered_by SET NOT NULL;
|
||||
`);
|
||||
await pool.query(`
|
||||
CREATE TABLE IF NOT EXISTS team_action_quota (
|
||||
team TEXT PRIMARY KEY CHECK (team IN ('blue', 'red')),
|
||||
actions_remaining INTEGER NOT NULL DEFAULT 0,
|
||||
quota_reset_at TIMESTAMPTZ NOT NULL DEFAULT '1970-01-01 00:00:00+00'
|
||||
);
|
||||
`);
|
||||
}
|
||||
|
||||
// ── World-seed epoch ──────────────────────────────────────────────────────────
|
||||
@@ -355,3 +362,39 @@ export async function setTileOwner(worldSeed, x, y, team) {
|
||||
[team, worldSeed, x, y]
|
||||
);
|
||||
}
|
||||
|
||||
// ── Team action quota (daily, independent of world seed) ─────────────────────
|
||||
|
||||
export async function getTeamActionsRow(team) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT actions_remaining, quota_reset_at FROM team_action_quota WHERE team = $1`,
|
||||
[team]
|
||||
);
|
||||
return rows[0] ?? null;
|
||||
}
|
||||
|
||||
export async function resetTeamActions(team, actionsRemaining, quotaResetAt) {
|
||||
await pool.query(
|
||||
`INSERT INTO team_action_quota (team, actions_remaining, quota_reset_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (team) DO UPDATE
|
||||
SET actions_remaining = $2,
|
||||
quota_reset_at = $3`,
|
||||
[team, actionsRemaining, quotaResetAt]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically decrements team actions_remaining by 1 if > 0.
|
||||
* Returns the updated row, or null if already 0.
|
||||
*/
|
||||
export async function decrementTeamActions(team) {
|
||||
const { rows } = await pool.query(
|
||||
`UPDATE team_action_quota
|
||||
SET actions_remaining = actions_remaining - 1
|
||||
WHERE team = $1 AND actions_remaining > 0
|
||||
RETURNING actions_remaining`,
|
||||
[team]
|
||||
);
|
||||
return rows[0] ?? null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user