Private
Public Access
1
0

feat: Adding the name of capturers of planets + displaying usernames for each team

This commit is contained in:
gauvainboiche
2026-04-01 17:46:48 +02:00
parent 362aa07f5a
commit e28a2d6e9c
8 changed files with 217 additions and 10 deletions

View File

@@ -140,6 +140,10 @@ export async function initGameSchema() {
ALTER TABLE grid_cells ADD CONSTRAINT grid_cells_discovered_by_check
CHECK (discovered_by IS NULL OR discovered_by IN ('blue', 'red'));
`);
// ── Store the username of whoever last captured a tile ────────────────────
await pool.query(`
ALTER TABLE grid_cells ADD COLUMN IF NOT EXISTS captured_by TEXT;
`);
}
// ── World-seed epoch ──────────────────────────────────────────────────────────
@@ -203,7 +207,7 @@ export async function ensureSeedEpoch() {
export async function getGridCells(worldSeed) {
const { rows } = await pool.query(
`SELECT x, y, exploitable, has_planet, planet_json, discovered_by
`SELECT x, y, exploitable, has_planet, planet_json, discovered_by, captured_by
FROM grid_cells WHERE world_seed = $1`,
[worldSeed]
);
@@ -246,7 +250,7 @@ export async function insertTeamVisibility(worldSeed, team, x, y) {
/** Returns all grid cells that are visible to a given team. */
export async function getTeamVisibleCells(worldSeed, team) {
const { rows } = await pool.query(
`SELECT gc.x, gc.y, gc.exploitable, gc.has_planet, gc.planet_json, gc.discovered_by
`SELECT gc.x, gc.y, gc.exploitable, gc.has_planet, gc.planet_json, gc.discovered_by, gc.captured_by
FROM grid_cells gc
JOIN team_cell_visibility tcv
ON tcv.world_seed = gc.world_seed AND tcv.x = gc.x AND tcv.y = gc.y
@@ -258,7 +262,7 @@ export async function getTeamVisibleCells(worldSeed, team) {
export async function getExistingCell(seed, x, y) {
const { rows } = await pool.query(
`SELECT x, y, exploitable, has_planet, planet_json, discovered_by
`SELECT x, y, exploitable, has_planet, planet_json, discovered_by, captured_by
FROM grid_cells WHERE world_seed = $1 AND x = $2 AND y = $3`,
[seed, x, y]
);
@@ -431,13 +435,26 @@ export async function getCellAttackCount(worldSeed, x, y) {
return rows[0]?.cnt ?? 0;
}
export async function setTileOwner(worldSeed, x, y, team) {
export async function setTileOwner(worldSeed, x, y, team, capturedBy = null) {
await pool.query(
`UPDATE grid_cells SET discovered_by = $1 WHERE world_seed = $2 AND x = $3 AND y = $4`,
[team, worldSeed, x, y]
`UPDATE grid_cells SET discovered_by = $1, captured_by = $2 WHERE world_seed = $3 AND x = $4 AND y = $5`,
[team, capturedBy, worldSeed, x, y]
);
}
/** Returns per-team lists of user IDs who have been active this seed epoch. */
export async function getActivePlayerIds(worldSeed) {
const { rows } = await pool.query(
`SELECT team, user_id FROM user_cooldowns WHERE world_seed = $1`,
[worldSeed]
);
const result = { blue: [], red: [] };
for (const row of rows) {
if (result[row.team]) result[row.team].push(row.user_id);
}
return result;
}
// ── Team action quota (daily, independent of world seed) ─────────────────────
export async function getTeamActionsRow(team) {

View File

@@ -75,6 +75,16 @@ export async function getTeamPlayerCounts() {
return result;
}
/** Returns username and team for each of the given user IDs. */
export async function getUsersByIds(ids) {
if (!ids.length) return [];
const { rows } = await usersPool.query(
`SELECT id, username, team FROM users WHERE id = ANY($1::int[])`,
[ids]
);
return rows;
}
// ── User action quota ─────────────────────────────────────────────────────────
/** Returns the current quota row for a user, or null if it doesn't exist. */