7th-saga/web/server.js

126 lines
2.9 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', true);
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([ '/', '/index.html' ], (req, res) => {
render(res, 'enemies', {
context: 'enemies',
enemies,
});
});
app.get('/spells.html', (req, res) => {
render(res, 'spells', {
context: 'spells',
spells: spells.spells.sort((a, b) => a.name.localeCompare(b.name)),
});
});
app.get('/exp.html', (req, res) => {
render(res, 'exp', {
context: 'exp',
exp: exp.exp,
});
});
app.get('/weapons.html', (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.html', (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.html', (req, res) => {
render(res, 'accessories', {
context: 'accessories',
accessories: items.accessories,
});
});
app.get('/items.html', (req, res) => {
render(res, 'items', {
context: 'items',
items: items.items,
});
});
app.get('/levels.html', (req, res) => {
render(res, 'levels', {
context: 'levels',
});
});
const port = process.env.PORT || 18000;
const server = app.listen(port, () => {
const addr = server.address();
console.log(`listening on ${addr.address}:${addr.port}`);
});