From 2eef36ddcb7c255bb56007e58c3b5c9f663753f3 Mon Sep 17 00:00:00 2001 From: GeekOfWires Date: Sun, 20 Sep 2020 05:15:17 -0400 Subject: [PATCH 1/6] Added basic SQL query --- api/db.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/api/db.js b/api/db.js index 8c8ec4f..98fe162 100644 --- a/api/db.js +++ b/api/db.js @@ -295,6 +295,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]) From 10114899f375c22cdfd321507e03df7a7e882576 Mon Sep 17 00:00:00 2001 From: GeekOfWires Date: Sun, 20 Sep 2020 05:16:16 -0400 Subject: [PATCH 2/6] Added Object.freeze for Avatar --- api/db.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/db.js b/api/db.js index 98fe162..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"), From 623bd5a0d2895000c91ba151f64684582682a8c9 Mon Sep 17 00:00:00 2001 From: GeekOfWires Date: Sun, 20 Sep 2020 05:17:08 -0400 Subject: [PATCH 3/6] Added basic API for character stats --- api/stats.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 api/stats.js diff --git a/api/stats.js b/api/stats.js new file mode 100644 index 0000000..6984992 --- /dev/null +++ b/api/stats.js @@ -0,0 +1,25 @@ +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' }); + } +}); + +export default api; \ No newline at end of file From 45de4d3ecf927eeb491bd9585675cb7c6a27f09a Mon Sep 17 00:00:00 2001 From: GeekOfWires Date: Sun, 20 Sep 2020 05:17:47 -0400 Subject: [PATCH 4/6] Added API get for character sorted by BEP stats --- api/stats.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/api/stats.js b/api/stats.js index 6984992..4605658 100644 --- a/api/stats.js +++ b/api/stats.js @@ -22,4 +22,22 @@ api.get('/char_stats/:batch', async (req, res, next) => { } }); +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' }); + } +}); + export default api; \ No newline at end of file From 4b314a147d7138a8437bcc5a504c824819a657dc Mon Sep 17 00:00:00 2001 From: GeekOfWires Date: Sun, 20 Sep 2020 05:18:09 -0400 Subject: [PATCH 5/6] Added API get for character sorted by CEP stats --- api/stats.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/api/stats.js b/api/stats.js index 4605658..594f748 100644 --- a/api/stats.js +++ b/api/stats.js @@ -40,4 +40,22 @@ api.get('/char_stats_bep/:batch', async (req, res, next) => { } }); +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 From bc8a49fbe946afa9d85748567f328e20a696f849 Mon Sep 17 00:00:00 2001 From: GeekOfWires Date: Sun, 20 Sep 2020 05:19:54 -0400 Subject: [PATCH 6/6] Finally, tell Express to include the stats API route for stats --- api/index.js | 2 ++ 1 file changed, 2 insertions(+) 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)