refacto: Cooldown is now user-set
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import express from "express";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { getConfig } from "../configLoader.js";
|
||||
import { computeWorldSeedState } from "../worldSeed.js";
|
||||
import { authMiddleware, JWT_SECRET } from "../middleware/auth.js";
|
||||
import {
|
||||
ensureSeedEpoch,
|
||||
getGridCells,
|
||||
insertCell,
|
||||
getExistingCell,
|
||||
getTeamCooldown,
|
||||
upsertTeamCooldown,
|
||||
getUserCooldown,
|
||||
upsertUserCooldown,
|
||||
getScores,
|
||||
getEconScores,
|
||||
addEconScore,
|
||||
@@ -34,11 +37,26 @@ router.get("/config", async (req, res) => {
|
||||
const bonus = await getElementBonus(worldSeed);
|
||||
const teamBonus = bonus[team] ?? 0;
|
||||
const effectiveCooldown = cfg.clickCooldownSeconds / (1 + teamBonus / 100);
|
||||
const row = await getTeamCooldown(worldSeed, team);
|
||||
if (row) {
|
||||
const secondsSince = (Date.now() - new Date(row.last_reveal).getTime()) / 1000;
|
||||
if (secondsSince < effectiveCooldown) {
|
||||
teamCooldownRemaining = Math.ceil(effectiveCooldown - secondsSince);
|
||||
// Use per-user cooldown if auth token provided, else fall back to team-wide
|
||||
const authHeader = req.headers["authorization"];
|
||||
if (authHeader && authHeader.startsWith("Bearer ")) {
|
||||
try {
|
||||
const payload = jwt.verify(authHeader.slice(7), JWT_SECRET);
|
||||
const row = await getUserCooldown(worldSeed, payload.userId);
|
||||
if (row) {
|
||||
const secondsSince = (Date.now() - new Date(row.last_reveal).getTime()) / 1000;
|
||||
if (secondsSince < effectiveCooldown) {
|
||||
teamCooldownRemaining = Math.ceil(effectiveCooldown - secondsSince);
|
||||
}
|
||||
}
|
||||
} catch { /* invalid token — return 0 */ }
|
||||
} else {
|
||||
const row = await getTeamCooldown(worldSeed, team);
|
||||
if (row) {
|
||||
const secondsSince = (Date.now() - new Date(row.last_reveal).getTime()) / 1000;
|
||||
if (secondsSince < effectiveCooldown) {
|
||||
teamCooldownRemaining = Math.ceil(effectiveCooldown - secondsSince);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,18 +102,16 @@ router.get("/grid/:seed", async (req, res) => {
|
||||
});
|
||||
|
||||
// POST /api/cell/reveal
|
||||
router.post("/cell/reveal", async (req, res) => {
|
||||
router.post("/cell/reveal", authMiddleware, async (req, res) => {
|
||||
const seed = String(req.body?.seed ?? "");
|
||||
const team = String(req.body?.team ?? "");
|
||||
const team = req.user.team; // taken from verified JWT, not request body
|
||||
const userId = req.user.userId;
|
||||
const x = Number(req.body?.x);
|
||||
const y = Number(req.body?.y);
|
||||
|
||||
if (!seed || !Number.isInteger(x) || !Number.isInteger(y) || x < 0 || x >= 100 || y < 0 || y >= 100) {
|
||||
return res.status(400).json({ error: "invalid_body" });
|
||||
}
|
||||
if (team !== "blue" && team !== "red") {
|
||||
return res.status(400).json({ error: "invalid_team" });
|
||||
}
|
||||
|
||||
try {
|
||||
const worldSeed = await ensureSeedEpoch();
|
||||
@@ -108,7 +124,7 @@ router.post("/cell/reveal", async (req, res) => {
|
||||
const bonus = await getElementBonus(worldSeed);
|
||||
const teamBonus = bonus[team] ?? 0;
|
||||
const effectiveCooldown = cfg.clickCooldownSeconds / (1 + teamBonus / 100);
|
||||
const cooldownRow = await getTeamCooldown(worldSeed, team);
|
||||
const cooldownRow = await getUserCooldown(worldSeed, userId);
|
||||
if (cooldownRow) {
|
||||
const secondsSince = (Date.now() - new Date(cooldownRow.last_reveal).getTime()) / 1000;
|
||||
if (secondsSince < effectiveCooldown) {
|
||||
@@ -127,7 +143,7 @@ router.post("/cell/reveal", async (req, res) => {
|
||||
const inserted = await insertCell(seed, x, y, cell.exploitable, cell.hasPlanet, planetJson, team);
|
||||
|
||||
if (inserted) {
|
||||
await upsertTeamCooldown(worldSeed, team);
|
||||
await upsertUserCooldown(worldSeed, userId, team);
|
||||
return res.json(rowToCellPayload(inserted));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user