2017-09-18 12:04:55 -04:00
|
|
|
// jshint node:true
|
|
|
|
// jshint esversion:6
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const http = require('http'),
|
|
|
|
fs = require('fs'),
|
|
|
|
url = require('url'),
|
|
|
|
port = 8080;
|
|
|
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
const uri = url.parse(req.url);
|
|
|
|
|
2017-09-20 18:10:37 -04:00
|
|
|
let pathParts = uri.pathname.split("/");
|
|
|
|
switch (pathParts[1]) {
|
|
|
|
case '':
|
|
|
|
case 'index.html':
|
|
|
|
sendIndex(res);
|
|
|
|
break;
|
|
|
|
case 'style.css':
|
|
|
|
sendFile(res, 'style.css', 'text/css');
|
|
|
|
break;
|
2017-09-23 21:31:06 -04:00
|
|
|
case 'js':
|
2017-09-28 12:58:28 -04:00
|
|
|
switch (pathParts[2]) {
|
2017-09-23 21:31:06 -04:00
|
|
|
case 'playfield.js':
|
|
|
|
sendFile(res, 'js/playfield.js', 'application/javascript');
|
|
|
|
break;
|
2017-09-24 16:45:51 -04:00
|
|
|
case 'editor.js':
|
|
|
|
sendFile(res, 'js/editor.js', 'application/javascript');
|
|
|
|
break;
|
2017-09-23 21:31:06 -04:00
|
|
|
case 'interact.js':
|
|
|
|
sendFile(res, 'interact.js', 'application/javascript');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
send404(res, uri);
|
|
|
|
}
|
2017-09-20 18:10:37 -04:00
|
|
|
break;
|
2017-09-24 16:45:51 -04:00
|
|
|
case 'template':
|
|
|
|
pathParts.splice(0, 2); // remove first two elements
|
|
|
|
let item = pathParts.join("/");
|
|
|
|
console.log("template/" + item);
|
|
|
|
switch (item) {
|
|
|
|
case "card.json":
|
|
|
|
case "deck.json":
|
|
|
|
sendFile(res, "template/" + item, 'application/json');
|
|
|
|
break;
|
|
|
|
case "environment/card.svg":
|
|
|
|
case "hero/card.svg":
|
|
|
|
case "hero/charBack.svg":
|
|
|
|
case "hero/charFront.svg":
|
|
|
|
case "villain/card.svg":
|
|
|
|
case "villain/character.svg":
|
|
|
|
case "villain/instructions.svg":
|
|
|
|
sendFile(res, "template/" + item, 'image/svg+xml');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
send404(res, uri);
|
|
|
|
}
|
|
|
|
break;
|
2017-09-20 18:10:37 -04:00
|
|
|
case 'deck':
|
|
|
|
if (pathParts.length === 3) {
|
2017-10-01 00:30:07 -04:00
|
|
|
let deckName = decodeURI(pathParts[2]);
|
2017-09-20 18:10:37 -04:00
|
|
|
sendDeckIndex(res, deckName);
|
|
|
|
}
|
|
|
|
else if (pathParts.length === 4) {
|
2017-10-01 00:30:07 -04:00
|
|
|
let deckName = decodeURI(pathParts[2]);
|
2017-09-20 18:10:37 -04:00
|
|
|
switch (pathParts[3]) {
|
|
|
|
case 'play':
|
|
|
|
sendPlayfield(res, deckName);
|
|
|
|
break;
|
2017-09-24 16:45:51 -04:00
|
|
|
case 'editor':
|
|
|
|
sendEditor(res, deckName);
|
|
|
|
break;
|
2017-09-20 18:10:37 -04:00
|
|
|
case 'deck.png':
|
|
|
|
sendFile(res, deckName + '.png', 'image/png');
|
|
|
|
break;
|
|
|
|
case 'deck.json':
|
|
|
|
sendFile(res, deckName + '.json', 'application/json');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
send404(res, uri);
|
|
|
|
}
|
2017-09-18 12:04:55 -04:00
|
|
|
}
|
2017-09-20 18:10:37 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
send404(res, uri);
|
2017-09-18 12:04:55 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(process.env.PORT || port);
|
|
|
|
console.log('listening on 8080');
|
|
|
|
|
|
|
|
function sendIndex(res) {
|
2017-09-28 12:59:30 -04:00
|
|
|
let decks = ["the_Unholy_Priest_update_2", "NZoths_Invasion_1.1"];
|
2017-09-28 12:58:28 -04:00
|
|
|
const html = `
|
2017-09-20 18:10:37 -04:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/style.css">
|
2017-09-28 14:01:22 -04:00
|
|
|
<title>Index</title>
|
2017-09-20 18:10:37 -04:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<ul>
|
2017-09-28 12:59:30 -04:00
|
|
|
${(decks.map(d => `<li><a href="deck/${d}">${d}</a></li>`).join(' '))}
|
2017-09-20 18:10:37 -04:00
|
|
|
</ul>
|
|
|
|
</body>
|
|
|
|
</html>`;
|
2017-09-20 22:57:15 -04:00
|
|
|
res.writeHead(200, {'Content-type': 'text/html; charset=utf-8'});
|
2017-09-20 18:10:37 -04:00
|
|
|
res.end(html, 'utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendDeckIndex(res, deckName) {
|
|
|
|
const html = `
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/style.css">
|
2017-09-28 14:01:22 -04:00
|
|
|
<title>${deckName}</title>
|
2017-09-20 18:10:37 -04:00
|
|
|
</head>
|
|
|
|
<body>
|
2017-09-24 16:45:51 -04:00
|
|
|
<ul>
|
|
|
|
<li><a href="${deckName}/play">Play!</a></li>
|
|
|
|
<li><a href="${deckName}/editor">Editor</a></li>
|
|
|
|
</ul>
|
2017-09-20 18:10:37 -04:00
|
|
|
</body>
|
|
|
|
</html>`;
|
2017-09-20 22:57:15 -04:00
|
|
|
res.writeHead(200, {'Content-type': 'text/html; charset=utf-8'});
|
2017-09-20 18:10:37 -04:00
|
|
|
res.end(html, 'utf-8');
|
|
|
|
}
|
|
|
|
|
2017-09-24 16:45:51 -04:00
|
|
|
function sendEditor(res, deckName) {
|
|
|
|
const html = `
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<script src="/js/editor.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/style.css">
|
2017-09-28 14:01:22 -04:00
|
|
|
<title>${deckName} - Editor</title>
|
2017-09-24 16:45:51 -04:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<input id="jsonUpload" type="file"><br>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
res.writeHead(200, {'Content-type': 'text/html; charset=utf-8'});
|
|
|
|
res.end(html, 'utf-8');
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:10:37 -04:00
|
|
|
function sendPlayfield(res, deckName) {
|
2017-09-18 12:04:55 -04:00
|
|
|
const html = `
|
|
|
|
<html>
|
|
|
|
<head>
|
2017-09-23 21:31:06 -04:00
|
|
|
<script src="/js/interact.js"></script>
|
|
|
|
<script src="/js/playfield.js"></script>
|
2017-09-20 18:10:37 -04:00
|
|
|
<link rel="stylesheet" type="text/css" href="/style.css">
|
2017-09-28 14:01:22 -04:00
|
|
|
<title>${deckName} - Playfield</title>
|
2017-09-18 12:04:55 -04:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="card-container" data-deckName="${deckName}">
|
|
|
|
<div class="card-pile deck" data-pile="deck">DECK</div>
|
2017-10-01 00:30:18 -04:00
|
|
|
<div class="card-pile discard" data-pile="discard">DISCARD</div>
|
2017-09-18 12:04:55 -04:00
|
|
|
<div id="hand"></div>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>`;
|
2017-09-20 22:57:15 -04:00
|
|
|
res.writeHead(200, {'Content-type': 'text/html; charset=utf-8'});
|
2017-09-18 12:04:55 -04:00
|
|
|
res.end(html, 'utf-8');
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:10:37 -04:00
|
|
|
|
|
|
|
function send404(res, uri) {
|
|
|
|
res.writeHead(404, {'Content-type': "text/html; charset=utf-8"});
|
|
|
|
const html = `
|
|
|
|
<head>
|
|
|
|
<title>404 Not Found</title>
|
|
|
|
<link rel="stylesheet" href="/style.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Error 404: Path ${uri.pathname} not found</h1>
|
|
|
|
You seem to have gone to the wrong place, would you like to go
|
|
|
|
back to the <a href="/">main page</a>?
|
|
|
|
</body>`;
|
|
|
|
res.end(html, 'utf-8');
|
|
|
|
}
|
|
|
|
|
2017-09-18 12:04:55 -04:00
|
|
|
function sendFile(res, filename, contentType='text/html; charset=utf-8') {
|
|
|
|
fs.readFile(filename, (error, content) => {
|
|
|
|
res.writeHead(200, {'Content-type': contentType});
|
|
|
|
res.end(content, 'utf-8');
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|