refacto: Displaying number of players for each team + adding logo at registration
This commit is contained in:
@@ -273,3 +273,17 @@ export async function getScores(worldSeed) {
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
// ── Active player counts (players who have played in the current epoch) ───────
|
||||
|
||||
export async function getActivePlayerCounts(worldSeed) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT team, COUNT(DISTINCT user_id)::int AS count
|
||||
FROM user_cooldowns WHERE world_seed = $1
|
||||
GROUP BY team`,
|
||||
[worldSeed]
|
||||
);
|
||||
const result = { blue: 0, red: 0 };
|
||||
for (const row of rows) result[row.team] = row.count;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -42,4 +42,13 @@ export async function getUserById(id) {
|
||||
[id]
|
||||
);
|
||||
return rows[0] ?? null;
|
||||
}
|
||||
|
||||
export async function getTeamPlayerCounts() {
|
||||
const { rows } = await usersPool.query(
|
||||
`SELECT team, COUNT(*)::int AS count FROM users GROUP BY team`
|
||||
);
|
||||
const result = { blue: 0, red: 0 };
|
||||
for (const row of rows) result[row.team] = row.count;
|
||||
return result;
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import express from "express";
|
||||
import bcrypt from "bcryptjs";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { JWT_SECRET, authMiddleware } from "../middleware/auth.js";
|
||||
import { createUser, getUserByUsername, getUserById } from "../db/usersDb.js";
|
||||
import { createUser, getUserByUsername, getUserById, getTeamPlayerCounts } from "../db/usersDb.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -67,6 +67,17 @@ router.post("/login", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/auth/player-counts
|
||||
router.get("/player-counts", async (_req, res) => {
|
||||
try {
|
||||
const counts = await getTeamPlayerCounts();
|
||||
return res.json(counts);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return res.status(500).json({ error: "database_error" });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/auth/me
|
||||
router.get("/me", authMiddleware, async (req, res) => {
|
||||
try {
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
setElementBonus,
|
||||
getDbCreatedAt,
|
||||
getVictoryPoints,
|
||||
getActivePlayerCounts,
|
||||
} from "../db/gameDb.js";
|
||||
import { computeCell, rowToCellPayload } from "../helpers/cell.js";
|
||||
|
||||
@@ -246,6 +247,18 @@ router.get("/victory-points", async (_req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/active-players
|
||||
router.get("/active-players", async (_req, res) => {
|
||||
try {
|
||||
const worldSeed = await ensureSeedEpoch();
|
||||
const counts = await getActivePlayerCounts(worldSeed);
|
||||
res.json(counts);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
res.status(500).json({ error: "database_error" });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/scores
|
||||
router.get("/scores", async (_req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user