diff --git a/api/db.js b/api/db.js index 8c8ec4f..d5ee2cb 100644 --- a/api/db.js +++ b/api/db.js @@ -59,6 +59,17 @@ export const CHARACTER = Object.freeze({ DELETED: Symbol("deleted"), }); +// Added for Avatar +// Only utilizing columns believed to be "safe" +// Should be reviewed at a later date +export const AVATAR = Object.freeze({ + THIS: Symbol("experience"), + ID: Symbol("id"), + NAME: Symbol("name"), + BEP: Symbol("bep"), + CEP: Symbol("cep"), +}); + export const LOGIN = Object.freeze({ THIS: Symbol("login"), ID: Symbol("id"), @@ -295,6 +306,22 @@ export async function get_characters(pagination, sort, order) { } } +// Database action added for the sake of reporting avatar information out to a publicly exposed API route for leader-boards and other components. +export async function get_character_batch_for_stats(batch, sort, order) { + const values = [batch]; + + try { + const char_count = await get_row_count(CHARACTER.THIS); + const chars = await pool.query(`SELECT id, name, faction_id, bep, cep FROM avatar ORDER BY ${to_sql(sort)} ${to_sql(order)} OFFSET $1*1000 LIMIT 1000`, values); + + return chars.rows; + } catch (e) { + if (e.code) + e.code = pg_error_inv[e.code] + throw e; + } +} + export async function get_characters_by_account(account_id) { try { const characters = await pool.query('SELECT * FROM avatar WHERE account_id=$1 AND deleted=false', [account_id]) diff --git a/api/index.js b/api/index.js index 9693d29..f01402e 100644 --- a/api/index.js +++ b/api/index.js @@ -5,6 +5,7 @@ import api_auth from './authentication.js' import api_user from './user.js' import api_info from './info.js' import api_admin from './admin.js' +import api_stats from './stats.js' const VERSION = JSON.parse(fs.readFileSync('package.json', 'utf8')).version; const api = express.Router(); @@ -23,6 +24,7 @@ api.use(bodyParser.urlencoded({ extended: true })); api.use(api_auth) api.use(api_info) +api.use(api_stats) // These calls are gated within their respective routers api.use(api_user) diff --git a/api/stats.js b/api/stats.js new file mode 100644 index 0000000..594f748 --- /dev/null +++ b/api/stats.js @@ -0,0 +1,61 @@ +import express from 'express' +import * as db from './db.js' +import {AVATAR, SQL_ORDER} from "./db.js"; + +const api = express.Router(); + +api.get('/char_stats/:batch', async (req, res, next) => { + try { + const stats = await db.get_stats(); + + stats.empires = { "TR": 0, "NC": 0, "VS": 0 } + + var batch = req.params.batch; + + const avatars = await db.get_character_batch_for_stats(batch, AVATAR.ID, SQL_ORDER.ASCENDING); + + res.status(200).json({ players: avatars }); + + } catch (e) { + console.log(e); + res.status(500).json({ message: 'error' }); + } +}); + +api.get('/char_stats_bep/:batch', async (req, res, next) => { + try { + const stats = await db.get_stats(); + + stats.empires = { "TR": 0, "NC": 0, "VS": 0 } + + var batch = req.params.batch; + + const avatars = await db.get_character_batch_for_stats(batch, AVATAR.BEP, SQL_ORDER.DESCENDING); + + res.status(200).json({ players: avatars }); + + } catch (e) { + console.log(e); + res.status(500).json({ message: 'error' }); + } +}); + +api.get('/char_stats_cep/:batch', async (req, res, next) => { + try { + const stats = await db.get_stats(); + + stats.empires = { "TR": 0, "NC": 0, "VS": 0 } + + var batch = req.params.batch; + + const avatars = await db.get_character_batch_for_stats(batch, AVATAR.CEP, SQL_ORDER.DESCENDING); + + res.status(200).json({ players: avatars }); + + } catch (e) { + console.log(e); + res.status(500).json({ message: 'error' }); + } +}); + +export default api; \ No newline at end of file