pytron-web/server.js

63 lines
1.6 KiB
JavaScript

const fs = require('fs');
const express = require('express');
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) => {
fs.readFile('assets/data.json', 'utf-8', (err, data) => {
if (err != null) {
console.log("Error: " + e);
}
let parsed = parse(data);
res.render('index', parsed);
});
});
app.use('/static', express.static('static'));
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`)
})