summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2021-07-18 16:22:56 +0200
committerTor Andersson <tor@ccxvii.net>2021-07-18 16:22:56 +0200
commit974834ff11b5420920cb9727a201878dc748ee4f (patch)
treecf180618fc36239bdf1514c20b51d49a82057b34
parent4b39b1e0023d1470dd7abe2eb8eda54b41369516 (diff)
downloadserver-974834ff11b5420920cb9727a201878dc748ee4f.tar.gz
Improve stats page.
-rw-r--r--server.js46
-rw-r--r--views/stats.ejs55
2 files changed, 55 insertions, 46 deletions
diff --git a/server.js b/server.js
index 264ef6e..4b909b8 100644
--- a/server.js
+++ b/server.js
@@ -542,20 +542,6 @@ app.post('/change_mail', must_be_logged_in, function (req, res) {
* GAME LOBBY
*/
-let RULES = {};
-for (let title_id of db.prepare("SELECT * FROM titles").pluck().all()) {
- if (fs.existsSync(__dirname + "/public/" + title_id + "/rules.js")) {
- console.log("Loading rules for " + title_id);
- try {
- RULES[title_id] = require("./public/" + title_id + "/rules.js");
- } catch (err) {
- console.log(err);
- }
- } else {
- console.log("Cannot find rules for " + title_id);
- }
-}
-
const QUERY_LIST_GAMES_OF_TITLE = db.prepare(`
SELECT *,
EXISTS (
@@ -640,6 +626,22 @@ const QUERY_REMATCH_CREATE = db.prepare(`
)
`);
+let RULES = {};
+let ROLES = {};
+for (let title_id of db.prepare("SELECT * FROM titles").pluck().all()) {
+ if (fs.existsSync(__dirname + "/public/" + title_id + "/rules.js")) {
+ console.log("Loading rules for " + title_id);
+ try {
+ RULES[title_id] = require("./public/" + title_id + "/rules.js");
+ ROLES[title_id] = QUERY_ROLES.all(title_id);
+ } catch (err) {
+ console.log(err);
+ }
+ } else {
+ console.log("Cannot find rules for " + title_id);
+ }
+}
+
app.get('/', function (req, res) {
res.render('index.ejs', { user: req.user, message: req.flash('message') });
});
@@ -1389,18 +1391,24 @@ io.on('connection', (socket) => {
// EXTRAS
+const QUERY_TITLES = db.prepare("SELECT * FROM titles ORDER BY title_name");
const QUERY_STATS = db.prepare(`
- SELECT title_name, scenario, result, count(*) AS count
- FROM games
- JOIN titles ON games.title_id=titles.title_id
- WHERE status=2 AND private=0
+ SELECT title_id, scenario, result, count(*) AS count
+ FROM game_view
+ WHERE status=2 AND is_solo=0
GROUP BY title_name, scenario, result
`);
app.get('/stats', function (req, res) {
LOG(req, "GET /stats");
let stats = QUERY_STATS.all();
- res.render('stats.ejs', { user: req.user, message: req.flash('message'), stats: stats });
+ let titles = Object.fromEntries(QUERY_TITLES.all().map(t => [t.title_id, t.title_name]));
+ res.render('stats.ejs', {
+ user: req.user,
+ message: req.flash('message'),
+ stats: stats,
+ title_role_map: ROLES, title_name_map: titles, title_rule_map: RULES
+ });
});
app.get('/users', function (req, res) {
diff --git a/views/stats.ejs b/views/stats.ejs
index db497e8..64a6303 100644
--- a/views/stats.ejs
+++ b/views/stats.ejs
@@ -2,35 +2,36 @@
<style>tr.blank{height:2rem;border:none;}</style>
<table class="wide">
<%
- let total = {};
- let results = {};
- for (let { title_name, scenario, result, count } of stats) {
- if (total[title_name] == undefined)
- total[title_name] = {};
- if (total[title_name][scenario] == undefined)
- total[title_name][scenario] = 0;
- total[title_name][scenario] += count;
- if (results[title_name] == undefined)
- results[title_name] = [];
- if (!results[title_name].includes(result))
- results[title_name].push(result);
+ function total(t, s) {
+ return stats
+ .filter(entry => entry.title_id === t && entry.scenario === s)
+ .reduce((acc, entry) => acc + entry.count, 0);
}
- let last_title = null;
- let last_scenario = null;
- for (let { title_name, scenario, result, count } of stats) {
- if (title_name != last_title) {
- if (last_title != null) {
- %><tr class="blank"><%
+ function result(t, s, r) {
+ let info = stats.find(entry => {
+ return entry.title_id === t &&
+ entry.scenario === s &&
+ entry.result === r});
+ return info ? info.count : 0;
+ }
+ for (let title_id in title_name_map) {
+ let scenarios = title_rule_map[title_id].scenarios;
+ let roles = title_role_map[title_id].concat(['Draw']);
+ %><tr><th><%= title_name_map[title_id] %><%
+ roles.forEach(role => {
+ %><th><%= role %><%
+ });
+ scenarios.forEach(scenario => {
+ let t = total(title_id, scenario);
+ if (t > 0) {
+ %><tr><td><%= scenario %> (<%= t %>)<%
+ roles.forEach(role => {
+ let r = result(title_id, scenario, role);
+ %><td><%= Math.round(r * 100 / t) %>%<%
+ });
}
- %><tr><th><%= title_name %><%
- results[title_name].forEach(item => { %><th><%= item %><% });
- }
- if (scenario != last_scenario) {
- %><tr><td><%= scenario %> (<%= total[title_name][scenario] %>)<%
- }
- %><td><%= Math.round(count * 100 / total[title_name][scenario]) %>%<%
- last_title = title_name;
- last_scenario = scenario;
+ });
+ %><tr class="blank"><%
}
%>
</table>