From 26849affc973404c97f7fe0739c51882df81cfda Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 20 Sep 2017 18:10:37 -0400 Subject: [PATCH] Initial setup for multiple decks --- script.js | 6 +-- server.js | 133 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 90 insertions(+), 49 deletions(-) diff --git a/script.js b/script.js index 7fc0804..d6f769a 100644 --- a/script.js +++ b/script.js @@ -2,7 +2,6 @@ let deckName, deckJSON, cardCount, deckWidth, deckHeight, piles = {'deck': [], discard: []}; window.addEventListener('load', () => { - deckName = document.querySelector('#card-container').getAttribute("data-deckName"); let xhr = new XMLHttpRequest(); xhr.addEventListener("load", () => { deckJSON = JSON.parse(xhr.responseText); @@ -13,7 +12,8 @@ window.addEventListener('load', () => { deckHeight = deckJSON.ObjectStates[0].CustomDeck["1"].NumHeight; console.log(deckName); }); - xhr.open("GET", "/" + deckName + ".json"); + deckName = document.querySelector('#card-container').getAttribute("data-deckName"); + xhr.open("GET", "/deck/" + deckName + "/deck.json"); xhr.send(); }); @@ -85,7 +85,7 @@ interact('.card-pile') newCard.style.backgroundPosition = ` ${(cardNum % deckWidth) * parseInt(style.getPropertyValue("width"))}px ${Math.floor(cardNum/deckHeight) * parseInt(style.getPropertyValue("height"))}px`; - newCard.style.backgroundImage = `url('${deckName}.png')`; + newCard.style.backgroundImage = "url('deck.png')"; newCard.style.backgroundSize = `${deckWidth * 100}% ${deckHeight * 100}%`; // insert the card to the page diff --git a/server.js b/server.js index b9fcf48..450db2d 100644 --- a/server.js +++ b/server.js @@ -3,70 +3,95 @@ "use strict"; const http = require('http'), - qs = require('querystring'), fs = require('fs'), url = require('url'), - https = require('https'), port = 8080; -const deckName = "the_Unholy_Priest_update_2"; - const server = http.createServer((req, res) => { const uri = url.parse(req.url); - if (req.method === 'POST') { - handlePost(res, req, uri); - } - else { - switch( uri.pathname ) { - case '/': - case '/index.html': - sendIndex(res, ""); - break; - case '/style.css': - sendFile(res, 'style.css', 'text/css'); - break; - case '/script.js': - sendFile(res, 'script.js', 'application/javascript'); - break; - case '/interact.js': - sendFile(res, 'interact.js', 'application/javascript'); - break; - case '/' + deckName + '.png': - sendFile(res, deckName + '.png', 'image/png'); - break; - case '/' + deckName + '.json': - sendFile(res, deckName + '.json', 'application/json'); - break; - default: - res.writeHead(404, {'Content-type': "text/html; charset=utf-8"}); - const html = ` - - 404 Not Found - - - -

Error 404: Path ${uri.pathname} not found

- You seem to have gone to the wrong place, would you like to go - back to the main page? - `; - res.end(html, 'utf-8'); + 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; + case 'script.js': + sendFile(res, 'script.js', 'application/javascript'); + break; + case 'interact.js': + sendFile(res, 'interact.js', 'application/javascript'); + break; + case 'deck': + if (pathParts.length === 3) { + let deckName = pathParts[2]; + sendDeckIndex(res, deckName); } + else if (pathParts.length === 4) { + let deckName = pathParts[2]; + switch (pathParts[3]) { + case 'play': + sendPlayfield(res, deckName); + break; + case 'deck.png': + sendFile(res, deckName + '.png', 'image/png'); + break; + case 'deck.json': + sendFile(res, deckName + '.json', 'application/json'); + break; + default: + send404(res, uri); + } + } + break; + default: + send404(res, uri); } - }); server.listen(process.env.PORT || port); console.log('listening on 8080'); function sendIndex(res) { - let cards = []; + const html = ` + + + + + + + + `; + res.writeHead(200, {'contentType': 'text/html; charset=utf-8'}); + res.end(html, 'utf-8'); +} + +function sendDeckIndex(res, deckName) { + const html = ` + + + + + + Play! + + `; + res.writeHead(200, {'contentType': 'text/html; charset=utf-8'}); + res.end(html, 'utf-8'); +} + +function sendPlayfield(res, deckName) { const html = ` - - - + + +
@@ -80,6 +105,22 @@ function sendIndex(res) { res.end(html, 'utf-8'); } + +function send404(res, uri) { + res.writeHead(404, {'Content-type': "text/html; charset=utf-8"}); + const html = ` + + 404 Not Found + + + +

Error 404: Path ${uri.pathname} not found

+ You seem to have gone to the wrong place, would you like to go + back to the main page? + `; + res.end(html, 'utf-8'); +} + function sendFile(res, filename, contentType='text/html; charset=utf-8') { fs.readFile(filename, (error, content) => { res.writeHead(200, {'Content-type': contentType});