Make EmpireStats show current players

This commit is contained in:
Chord 2020-05-13 21:00:38 +02:00
parent 482b499fec
commit 96fcbba812
2 changed files with 30 additions and 10 deletions

View file

@ -15,14 +15,14 @@ let pg_error_inv = objectFlip(pg_error)
export let pool;
const FACTION_MAP = {
export const FACTION_MAP = {
0 : ["Terran Republic", "TR"],
1 : ["New Conglomerate", "NC"],
2 : ["Vanu Sovereignty", "VS"],
3 : ["Neutral", "NL"],
}
const FACTION_MAP_INV = objectFlip(FACTION_MAP)
export const FACTION_MAP_INV = objectFlip(FACTION_MAP)
const BCRYPT_ROUNDS = 4;
export const SQL_ORDER = Object.freeze({
@ -358,23 +358,37 @@ export async function update_account(account_id, fields) {
}
}
export async function get_empire_stats() {
try {
const query = await pool.query('SELECT faction_id, COUNT(*) FROM characters GROUP BY faction_id');
const empires = {};
query.rows.forEach((r) => {
empires[FACTION_MAP[r.faction_id][1]] = parseInt(r.count);
});
return empires;
} catch (e) {
if (e.code)
e.code = pg_error_inv[e.code]
throw e;
}
return stats;
}
export async function get_stats() {
try {
const account_count = await get_row_count(ACCOUNT.THIS);
const character_count = await get_row_count(CHARACTER.THIS);
const last_character = await pool.query('SELECT id, account_id, name, faction_id, created FROM characters ORDER BY id DESC LIMIT 1');
const empires = await pool.query('SELECT faction_id, COUNT(*) FROM characters GROUP BY faction_id');
const stats = {}
stats.accounts = account_count;
stats.characters = character_count;
stats.last = {};
stats.last.character = last_character.rows[0];
stats.empires = {};
empires.rows.forEach((r) =>
stats.empires[FACTION_MAP[r.faction_id][1]] = parseInt(r.count)
);
return stats;
} catch (e) {
if (e.code)

View file

@ -7,17 +7,23 @@ const api = express.Router();
api.get('/stats', async (req, res, next) => {
try {
const stats = await db.get_stats();
stats.empires = { "TR" : 0, "NC" : 0, "VS" : 0 }
const info = get_server_info();
let player_info = []
let players = info.players;
for (let i = 0; i < players.length; i++) {
const char = await db.get_character_by_name(players[i]);
if (char)
if (char) {
player_info = player_info.concat(char)
else
console.log("WARNING: cannot find player info " + players[i])
stats.empires[db.FACTION_MAP[char.faction_id][1]] += 1
} else
console.log("WARNING: cannot find player info '" + players[i] + "'")
}
console.log(stats)
info.players = player_info
res.status(200).json({ ...stats, ...info });
} catch (e) {