7th-saga/web/server.js

126 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2021-02-15 17:18:01 +00:00
const express = require('express');
const path = require('path');
const enemies = require('../data/enemies.json');
2021-02-22 04:37:37 +00:00
const spells = require('./static/spells');
2021-02-15 17:18:01 +00:00
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'));
2021-03-02 05:17:41 +00:00
app.set('cache views', true);
2021-02-15 17:18:01 +00:00
app.use(express.static(path.join(__dirname)));
2021-02-22 04:37:37 +00:00
const render = (res, view, params) => {
res.render(view, {
...params,
charSpells: spells.spells.concat([]).sort((a, b) => {
2021-02-22 04:41:27 +00:00
if (a.power && !b.power) {
2021-02-22 04:37:37 +00:00
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);
}),
});
}
2021-08-26 18:31:53 +00:00
app.get([ '/', '/index.html' ], (req, res) => {
2021-02-22 04:37:37 +00:00
render(res, 'enemies', {
2021-02-15 17:18:01 +00:00
context: 'enemies',
enemies,
});
});
2021-08-26 18:31:53 +00:00
app.get('/spells.html', (req, res) => {
2021-02-22 04:37:37 +00:00
render(res, 'spells', {
2021-02-15 17:18:01 +00:00
context: 'spells',
spells: spells.spells.sort((a, b) => a.name.localeCompare(b.name)),
2021-02-15 17:18:01 +00:00
});
});
2021-08-26 18:31:53 +00:00
app.get('/exp.html', (req, res) => {
2021-02-22 04:37:37 +00:00
render(res, 'exp', {
2021-02-15 17:18:01 +00:00
context: 'exp',
exp: exp.exp,
});
});
2021-08-26 18:31:53 +00:00
app.get('/weapons.html', (req, res) => {
2021-02-22 04:37:37 +00:00
render(res, 'weapons', {
2021-02-16 17:03:42 +00:00
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 );
})
2021-02-16 17:03:42 +00:00
});
});
2021-08-26 18:31:53 +00:00
app.get('/armor.html', (req, res) => {
2021-02-22 04:37:37 +00:00
render(res, 'armor', {
2021-02-16 17:03:42 +00:00
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)
},),
2021-02-16 17:03:42 +00:00
});
});
2021-08-26 18:31:53 +00:00
app.get('/accessories.html', (req, res) => {
2021-02-22 04:37:37 +00:00
render(res, 'accessories', {
2021-02-16 17:03:42 +00:00
context: 'accessories',
accessories: items.accessories,
});
});
2021-08-26 18:31:53 +00:00
app.get('/items.html', (req, res) => {
2021-02-22 04:37:37 +00:00
render(res, 'items', {
2021-02-16 17:03:42 +00:00
context: 'items',
items: items.items,
});
});
2021-08-26 18:31:53 +00:00
app.get('/levels.html', (req, res) => {
2021-03-16 20:41:50 +00:00
render(res, 'levels', {
context: 'levels',
});
});
2021-02-15 17:18:01 +00:00
const port = process.env.PORT || 18000;
const server = app.listen(port, () => {
const addr = server.address();
console.log(`listening on ${addr.address}:${addr.port}`);
});