mirror of
https://github.com/psforever/PSFPortal.git
synced 2026-07-16 00:44:40 +00:00
Merge pull request #1 from GeekOfWires/api-stats-avatar
API for Avatar Stats
This commit is contained in:
commit
7e054a33f3
3 changed files with 90 additions and 0 deletions
27
api/db.js
27
api/db.js
|
|
@ -59,6 +59,17 @@ export const CHARACTER = Object.freeze({
|
||||||
DELETED: Symbol("deleted"),
|
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({
|
export const LOGIN = Object.freeze({
|
||||||
THIS: Symbol("login"),
|
THIS: Symbol("login"),
|
||||||
ID: Symbol("id"),
|
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) {
|
export async function get_characters_by_account(account_id) {
|
||||||
try {
|
try {
|
||||||
const characters = await pool.query('SELECT * FROM avatar WHERE account_id=$1 AND deleted=false', [account_id])
|
const characters = await pool.query('SELECT * FROM avatar WHERE account_id=$1 AND deleted=false', [account_id])
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import api_auth from './authentication.js'
|
||||||
import api_user from './user.js'
|
import api_user from './user.js'
|
||||||
import api_info from './info.js'
|
import api_info from './info.js'
|
||||||
import api_admin from './admin.js'
|
import api_admin from './admin.js'
|
||||||
|
import api_stats from './stats.js'
|
||||||
|
|
||||||
const VERSION = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
|
const VERSION = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
|
||||||
const api = express.Router();
|
const api = express.Router();
|
||||||
|
|
@ -23,6 +24,7 @@ api.use(bodyParser.urlencoded({ extended: true }));
|
||||||
|
|
||||||
api.use(api_auth)
|
api.use(api_auth)
|
||||||
api.use(api_info)
|
api.use(api_info)
|
||||||
|
api.use(api_stats)
|
||||||
|
|
||||||
// These calls are gated within their respective routers
|
// These calls are gated within their respective routers
|
||||||
api.use(api_user)
|
api.use(api_user)
|
||||||
|
|
|
||||||
61
api/stats.js
Normal file
61
api/stats.js
Normal file
|
|
@ -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;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue