From d09036e66c5bfe59e482cb21309114717e3080b0 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 20 Mar 2019 17:13:06 +0100 Subject: [PATCH] Working --- server.js | 41 ++++++++++++++++++++++++++++++++++++++++- views/base.pug | 4 ++-- views/index.pug | 18 +++++++++++------- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index b233070..638a687 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,45 @@ const pug = require('pug'); const app = express(); const port = 8000; +function parse(data) { + let content = JSON.parse(data); + let parsed = {ais: [], battles: {}}; + for (let ai of content.ais) { + parsed.ais.push({ + name: ai, + victories: 0, + defeats: 0, + }); + } + + for (let key in content.battles) { + let battlers = key.split('/'); + let ai1 = battlers[0]; + let ai2 = battlers[1]; + parsed.battles[ai1 + '/' + ai2] = Math.floor(100 * content.battles[key]); + parsed.battles[ai2 + '/' + ai1] = 100 - parsed.battles[ai1 + '/' + ai2]; + } + parsed.sortedAis = parsed.ais.slice(0); + + parsed.sortedAis.sort((a, b) => { + if (a.victories < b.victories) { + return -1; + } else if (a.victories > b.victories) { + return 1; + } else { + if (a.defeats > b.defeats) { + return -1; + } else if (a.defeats < b.defeats) { + return 1; + } else { + return 0; + } + } + }); + + return parsed; +} + app.set('view engine', 'pug'); app.get('/', (req, res) => { @@ -11,7 +50,7 @@ app.get('/', (req, res) => { if (err != null) { console.log("Error: " + e); } - let parsed = JSON.parse(data); + let parsed = parse(data); res.render('index', parsed); }); }); diff --git a/views/base.pug b/views/base.pug index e760bab..d78b0a1 100644 --- a/views/base.pug +++ b/views/base.pug @@ -18,8 +18,8 @@ html(lang='en') span(aria-hidden='true') #navbarBasicExample.navbar-menu .navbar-end - a.navbar-item.is-size-6(href='/button') - strong Button + a.navbar-item.is-size-6(href='/upload') + strong Upload your AI block content script. document.addEventListener('DOMContentLoaded', () => { diff --git a/views/index.pug b/views/index.pug index 6b00edc..f915cf1 100644 --- a/views/index.pug +++ b/views/index.pug @@ -9,17 +9,21 @@ block content tr th for ai in ais - th.has-text-centered= ai + th.has-text-centered= ai.name th.has-text-centered.has-text-success ✓ th.has-text-centered.has-text-danger ✗ for ai1 in ais tr - th.has-text-centered= ai1 + th.has-text-centered= ai1.name for ai2 in ais - if ai1 == ai2 + if ai1.name == ai2.name td + else if battles[ai1.name + "/" + ai2.name] > 50 + td.has-text-success= battles[ai1.name + "/" + ai2.name] + else if battles[ai1.name + "/" + ai2.name] < 50 + td.has-text-danger= battles[ai1.name + "/" + ai2.name] else - td.has-text-success= battles[ai1 + "/" + ai2] + td= battles[ai1.name + "/" + ai2.name] td.has-text-centered strong= 0 td.has-text-centered @@ -29,12 +33,12 @@ block content tr th.has-text-centered AI th.has-text-centered Score - for ai in ais + for ai in sortedAis tr td.has-text-centered - strong= ai + strong= ai.name td.has-text-centered - strong 0 + strong= ai.victories - ai.defeats