mirror of
https://github.com/psforever/PSFPortal.git
synced 2026-01-19 18:14:45 +00:00
Make EmpireStats show current players
This commit is contained in:
parent
482b499fec
commit
96fcbba812
28
api/db.js
28
api/db.js
|
|
@ -15,14 +15,14 @@ let pg_error_inv = objectFlip(pg_error)
|
||||||
|
|
||||||
export let pool;
|
export let pool;
|
||||||
|
|
||||||
const FACTION_MAP = {
|
export const FACTION_MAP = {
|
||||||
0 : ["Terran Republic", "TR"],
|
0 : ["Terran Republic", "TR"],
|
||||||
1 : ["New Conglomerate", "NC"],
|
1 : ["New Conglomerate", "NC"],
|
||||||
2 : ["Vanu Sovereignty", "VS"],
|
2 : ["Vanu Sovereignty", "VS"],
|
||||||
3 : ["Neutral", "NL"],
|
3 : ["Neutral", "NL"],
|
||||||
}
|
}
|
||||||
|
|
||||||
const FACTION_MAP_INV = objectFlip(FACTION_MAP)
|
export const FACTION_MAP_INV = objectFlip(FACTION_MAP)
|
||||||
const BCRYPT_ROUNDS = 4;
|
const BCRYPT_ROUNDS = 4;
|
||||||
|
|
||||||
export const SQL_ORDER = Object.freeze({
|
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() {
|
export async function get_stats() {
|
||||||
try {
|
try {
|
||||||
const account_count = await get_row_count(ACCOUNT.THIS);
|
const account_count = await get_row_count(ACCOUNT.THIS);
|
||||||
const character_count = await get_row_count(CHARACTER.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 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 = {}
|
const stats = {}
|
||||||
|
|
||||||
stats.accounts = account_count;
|
stats.accounts = account_count;
|
||||||
stats.characters = character_count;
|
stats.characters = character_count;
|
||||||
stats.last = {};
|
stats.last = {};
|
||||||
stats.last.character = last_character.rows[0];
|
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;
|
return stats;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code)
|
if (e.code)
|
||||||
|
|
|
||||||
12
api/info.js
12
api/info.js
|
|
@ -7,17 +7,23 @@ const api = express.Router();
|
||||||
api.get('/stats', async (req, res, next) => {
|
api.get('/stats', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const stats = await db.get_stats();
|
const stats = await db.get_stats();
|
||||||
|
|
||||||
|
stats.empires = { "TR" : 0, "NC" : 0, "VS" : 0 }
|
||||||
|
|
||||||
const info = get_server_info();
|
const info = get_server_info();
|
||||||
let player_info = []
|
let player_info = []
|
||||||
let players = info.players;
|
let players = info.players;
|
||||||
for (let i = 0; i < players.length; i++) {
|
for (let i = 0; i < players.length; i++) {
|
||||||
const char = await db.get_character_by_name(players[i]);
|
const char = await db.get_character_by_name(players[i]);
|
||||||
|
|
||||||
if (char)
|
if (char) {
|
||||||
player_info = player_info.concat(char)
|
player_info = player_info.concat(char)
|
||||||
else
|
stats.empires[db.FACTION_MAP[char.faction_id][1]] += 1
|
||||||
console.log("WARNING: cannot find player info " + players[i])
|
} else
|
||||||
|
console.log("WARNING: cannot find player info '" + players[i] + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(stats)
|
||||||
info.players = player_info
|
info.players = player_info
|
||||||
res.status(200).json({ ...stats, ...info });
|
res.status(200).json({ ...stats, ...info });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue