Simplify server switch/case and check if deck in decks list

This commit is contained in:
Adam Goldsmith 2017-10-02 14:37:56 -04:00
parent 4cedb447dd
commit 617176ed14
2 changed files with 33 additions and 31 deletions

View File

@ -9,6 +9,8 @@ const http = require('http'),
phantom = require('phantom'), phantom = require('phantom'),
port = 8080; port = 8080;
const decks = ["the_Unholy_Priest_update_2", "NZoths_Invasion_1.2", "Puffer_Fish_input_1.3"];
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
const uri = url.parse(req.url); const uri = url.parse(req.url);
@ -24,13 +26,9 @@ const server = http.createServer((req, res) => {
case 'js': case 'js':
switch (pathParts[2]) { switch (pathParts[2]) {
case 'playfield.js': case 'playfield.js':
sendFile(res, 'js/playfield.js', 'application/javascript');
break;
case 'editor.js': case 'editor.js':
sendFile(res, 'js/editor.js', 'application/javascript');
break;
case 'interact.js': case 'interact.js':
sendFile(res, 'interact.js', 'application/javascript'); sendFile(res, 'js/' + pathParts[2], 'application/javascript');
break; break;
default: default:
send404(res, uri); send404(res, uri);
@ -59,13 +57,19 @@ const server = http.createServer((req, res) => {
} }
break; break;
case 'deck': case 'deck':
if (pathParts.length === 3) { if (pathParts.length < 3 || pathParts[2] === '') {
let deckName = decodeURI(pathParts[2]); sendIndex(res);
sendDeckIndex(res, deckName); break;
} }
else if (pathParts.length === 4) {
let deckName = decodeURI(pathParts[2]); let deckName = decodeURI(pathParts[2]);
switch (pathParts[3]) { if (!decks.includes(deckName)) {
send404(res, uri);
break;
}
switch (pathParts[3] || '') {
case '':
sendDeckIndex(res, deckName);
break;
case 'play': case 'play':
sendFile(res, 'html/playfield.html'); sendFile(res, 'html/playfield.html');
break; break;
@ -84,7 +88,6 @@ const server = http.createServer((req, res) => {
default: default:
send404(res, uri); send404(res, uri);
} }
}
break; break;
default: default:
send404(res, uri); send404(res, uri);
@ -95,7 +98,6 @@ server.listen(process.env.PORT || port);
console.log('listening on 8080'); console.log('listening on 8080');
function sendIndex(res) { function sendIndex(res) {
let decks = ["the_Unholy_Priest_update_2", "NZoths_Invasion_1.1"];
const html = ` const html = `
<html> <html>
<head> <head>
@ -104,7 +106,7 @@ function sendIndex(res) {
</head> </head>
<body> <body>
<ul> <ul>
${(decks.map(d => `<li><a href="deck/${d}">${d}</a></li>`).join(' '))} ${(decks.map(d => `<li><a href="/deck/${d}">${d}</a></li>`).join(' '))}
</ul> </ul>
</body> </body>
</html>`; </html>`;