7th-saga/web/server.js
2021-02-21 20:37:37 -08:00

120 lines
2.7 KiB
JavaScript

const express = require('express');
const path = require('path');
const enemies = require('../data/enemies.json');
const spells = require('./static/spells');
const items = require('../data/items');
const exp = require('../data/exp');
const app = express();
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.set('cache views', false);
app.use(express.static(path.join(__dirname)));
const render = (res, view, params) => {
res.render(view, {
...params,
charSpells: spells.spells.concat([]).sort((a, b) => {
if (a.power && b.power) {
return -1;
}
if (!a.power && b.power) {
return 1;
}
if (a.power === b.power) {
return a.name.localeCompare(b.name);
}
return a.power < b.power ? -1 : 1;
}),
charWeapons: items.weapons.concat([]).sort((a, b) => {
if (a.attack === b.attack) {
return a.name.localeCompare(b.name);
}
return a.attack === b.attack ? 0 : (a.attack < b.attack ? -1 : 1);
}),
charArmor: items.armor.concat([]).sort((a, b) => {
if (a.defense === b.defense) {
return a.name.localeCompare(b.name);
}
return a.defense === b.defense ? 0 : (a.defense < b.defense ? -1 : 1);
}),
charAccessories: items.accessories.concat([]).sort((a, b) => {
if (a.defense === b.defense) {
return a.name.localeCompare(b.name);
}
return a.defense === b.defense ? 0 : (a.defense < b.defense ? -1 : 1);
}),
});
}
app.get([ '/', '/enemies' ], (req, res) => {
render(res, 'enemies', {
context: 'enemies',
enemies,
});
});
app.get('/spells', (req, res) => {
render(res, 'spells', {
context: 'spells',
spells: spells.spells.sort((a, b) => a.name.localeCompare(b.name)),
});
});
app.get('/exp', (req, res) => {
render(res, 'exp', {
context: 'exp',
exp: exp.exp,
});
});
app.get('/weapons', (req, res) => {
render(res, 'weapons', {
context: 'weapons',
weapons: items.weapons.sort((a, b) => {
if (a.attack === b.attack) {
return a.name.localeCompare(b.name);
}
return a.attack === b.attack ? 0 : (a.attack < b.attack ? -1 : 1 );
})
});
});
app.get('/armor', (req, res) => {
render(res, 'armor', {
context: 'armor',
armor: items.armor.sort((a, b) => {
if (a.defense === b.defense) {
return a.name.localeCompare(b.name);
}
return a.defense === b.defense ? 0 : (a.defense < b.defense ? -1 : 1)
},),
});
});
app.get('/accessories', (req, res) => {
render(res, 'accessories', {
context: 'accessories',
accessories: items.accessories,
});
});
app.get('/items', (req, res) => {
render(res, 'items', {
context: 'items',
items: items.items,
});
});
const port = process.env.PORT || 18000;
const server = app.listen(port, () => {
const addr = server.address();
console.log(`listening on ${addr.address}:${addr.port}`);
});