PSFPortal/api/info.js

36 lines
928 B
JavaScript
Raw Normal View History

2019-12-30 14:27:49 +00:00
import express from 'express'
import * as db from './db.js'
import { get_server_info } from './psadmin.js'
2019-12-30 14:27:49 +00:00
const api = express.Router();
api.get('/stats', async (req, res, next) => {
try {
const stats = await db.get_stats();
2020-05-13 19:00:38 +00:00
stats.empires = { "TR": 0, "NC": 0, "VS": 0 }
2020-05-13 19:00:38 +00:00
2020-05-12 21:06:22 +00:00
const info = get_server_info();
let player_info = []
let players = info.players;
2020-05-14 17:52:00 +00:00
2020-05-12 21:06:22 +00:00
for (let i = 0; i < players.length; i++) {
const char = await db.get_character_by_name(players[i].name);
2020-05-12 21:06:22 +00:00
2020-05-13 19:00:38 +00:00
if (char) {
2020-05-12 21:06:22 +00:00
player_info = player_info.concat(char)
2020-05-13 19:00:38 +00:00
stats.empires[db.FACTION_MAP[char.faction_id][1]] += 1
2020-05-14 17:52:00 +00:00
} else {
console.log("WARNING: cannot find player info '" + players[i].name + "' (are you sure PSAdmin is configured right?)")
2020-05-14 17:52:00 +00:00
}
2020-05-12 21:06:22 +00:00
}
2020-05-13 19:00:38 +00:00
res.status(200).json({ status: info.status, players: player_info, ...stats });
2019-12-30 14:27:49 +00:00
} catch (e) {
console.log(e);
res.status(500).json({ message: 'error' });
2019-12-30 14:27:49 +00:00
}
});
export default api;