From 2b9139dd66743315082f6b12678d9f5d1952dfb3 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 2 Oct 2017 03:29:53 -0400 Subject: [PATCH] Load more stuff from deck JSON actually uses deck number and FaceURL preperation for arbitrary decks (from TTS mods) also changes to send only the deck part of the JSON --- js/playfield.js | 19 ++++++++++--------- server.js | 14 +++++++++++++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/js/playfield.js b/js/playfield.js index d0fccfc..68f9d7a 100644 --- a/js/playfield.js +++ b/js/playfield.js @@ -1,4 +1,4 @@ -let deckName, deckJSON, cardCount, deckWidth, deckHeight, +let deckName, deckNum, deckJSON, cardCount, deckWidth, deckHeight, piles = {'deck': [], discard: []}; interact.dynamicDrop(true); @@ -6,16 +6,17 @@ interact.dynamicDrop(true); window.addEventListener('load', () => { let xhr = new XMLHttpRequest(); xhr.addEventListener("load", () => { - deckJSON = JSON.parse(xhr.responseText).ObjectStates[0]; - piles.deck = deckJSON.DeckIDs.map(c => c - 100); + deckJSON = JSON.parse(xhr.responseText); + deckNum = Object.keys(deckJSON.CustomDeck)[0]; + piles.deck = deckJSON.DeckIDs.map(c => c - deckNum * 100); cardCount = piles.deck.length; shuffle(piles.deck); - deckWidth = deckJSON.CustomDeck["1"].NumWidth; - deckHeight = deckJSON.CustomDeck["1"].NumHeight; + deckWidth = deckJSON.CustomDeck[deckNum].NumWidth; + deckHeight = deckJSON.CustomDeck[deckNum].NumHeight; console.log(deckName); }); deckName = document.querySelector('#card-container').getAttribute("data-deckName"); - xhr.open("GET", "/deck/" + deckName + "/deck.json"); + xhr.open("GET", "deck.json"); xhr.send(); window.addEventListener("contextmenu", event => event.preventDefault()); @@ -182,10 +183,10 @@ interact('.card-pile') searchBox.setAttribute('type', 'search'); searchBox.setAttribute('placeholder', 'Filter'); searchBox.addEventListener('input', event => { + let input = event.target.value; Array.from(cardList.children).forEach(card => { - let input = event.target.value; let cardNum = parseInt(card.getAttribute('data-num')); - let cardData = deckJSON.ContainedObjects.find(c => c.CardID === (cardNum + 100)); + let cardData = deckJSON.ContainedObjects.find(c => c.CardID === (cardNum + deckNum * 100)); card.style.display = (cardData.Nickname.toLowerCase().includes(input.toLowerCase()) || cardData.Description.toLowerCase().includes(input.toLowerCase())) ? @@ -224,8 +225,8 @@ function makeCard(cardNum) { card.style.backgroundPositionX = -(cardNum % deckWidth) * parseInt(style.getPropertyValue("width")) + "px"; card.style.backgroundPositionY = - card.style.backgroundImage = "url('deck.png')"; -Math.floor(cardNum/deckWidth) * parseInt(style.getPropertyValue("height")) + "px"; + card.style.backgroundImage = `url(${deckJSON.CustomDeck[deckNum].FaceURL})`; card.style.backgroundSize = `${deckWidth * 100}% ${deckHeight * 100}%`; document.body.removeChild(card); diff --git a/server.js b/server.js index f24bc30..bd8b316 100644 --- a/server.js +++ b/server.js @@ -76,7 +76,8 @@ const server = http.createServer((req, res) => { sendFile(res, deckName + '.png', 'image/png'); break; case 'deck.json': - sendFile(res, deckName + '.json', 'application/json'); + sendFileJSON(res, deckName); + break; case 'upload': handleUpload(res, req, deckName); break; @@ -169,6 +170,17 @@ function sendPlayfield(res, deckName) { } +function sendFileJSON(res, deckName) { + fs.readFile(deckName + '.json', (error, content) => { + console.log(JSON.parse(content)); + res.writeHead(200, {'Content-type': 'application/json; charset=utf-8'}); + res.end(JSON.stringify(JSON.parse(content).ObjectStates[0]), 'utf-8'); + if (error) { + console.error(error); + } + }); +} + function handleUpload(res, req) { let body = '';