Created
October 19, 2016 15:21
-
-
Save Kattoor/18818c5e7a2712e53f3d67942cda4c13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var fs = require('fs'); | |
var http = require('http'); | |
var container = {}; | |
var server = http.createServer(handleRequest); | |
function handleRequest(request, response) { | |
if (request.url === '/') | |
serveCards(request, response); | |
} | |
function serveCards(request, response) { | |
let deck = getDeck(); | |
var blackCard = getRandomCards(deck, 'black')[0]; | |
let whiteCards = getRandomCards(deck, 'white', blackCard['pick']); | |
let html = getHtml(); | |
container.blackText = blackCard.text; | |
container.whiteText = ''; | |
for (let i = 0; i < whiteCards.length; i++) | |
container.whiteText += whiteCards[i] + '<br>'; | |
html = html.replace(/\$\{(\w*)}/g, (full, part) => container[part]); | |
response.end(html); | |
} | |
function getHtml() { | |
return fs.readFileSync('index.html', 'utf8'); | |
} | |
function getDeck() { | |
return JSON.parse(fs.readFileSync('cards.json', 'utf8')); | |
} | |
function getRandomCards(deck, color, amount) { | |
let cards = []; | |
for (let i = 0; i < (amount || 1); i++) | |
cards.push(deck[color + 'Cards'][Math.floor(Math.random() * deck[color + 'Cards'].length)]); | |
return cards; | |
} | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment