deckard/index.html

108 lines
2.9 KiB
HTML

<html>
<head>
<title>Deckard</title>
<link href="css.css" rel="stylesheet">
<script src="https://labs.murkfall.net/bluesaxman/blue.js/raw/master/libs/bluecore.js"></script>
</head>
<body>
<h1>Welcome to Deckard<h1>
</body>
<script>
window.deck = [];
window.hand = [];
function updateEvent() {
window.UI.deck.innerHTML = "";
window.UI.hand.innerHTML = "";
var theDeck = elementPlace("#deck","deckDisp","card","div");
theDeck.innerHTML = "<div>"+window.deck.length+"</div>";
window.hand.forEach(function (card,index) {
if (index < 10) {
var currentCard = elementPlace("#hand",null,"card","li");
currentCard.innerHTML = card;
}
});
}
function generateDeck(DDF) {
var cards = [""];
DDF.forEach( function (dataBlock) {
var tempcards = [];
cards.forEach( function (current) {
dataBlock.data.forEach( function (value) {
tempcards.push(current+value);
} );
} );
cards = tempcards.slice();
});
return cards;
}
function shuffleDeck(deck) {
deck.forEach(function (card,index) {
var swapCardIndex = Math.floor( Math.random() * deck.length );
var swapCard = deck[swapCardIndex];
deck[swapCardIndex] = card;
deck[index] = swapCard;
});
}
function deckFromJSON(ourFile) {
if ( Array.isArray(ourFile[0]) ) {
ourFile.forEach(function (deck) {
window.deck = window.deck.concat(generateDeck(deck));
});
} else {
window.deck = window.deck.concat(generateDeck(ourFile));
}
}
elementPlace("body","menu",null,"div");
buttonAdd("#menu","upload","Upload Deck", function () {
popupDialog("deckLoader","Select Deck definition File",true,inputDialog,{"inputType":"file"},function (f) {
var myFile = new FileReader();
myFile.onload = function (file) {
// Probably validate the file somehow befor eating it
var ourFile = JSON.parse(file.target.result);
window.deck = [];
deckFromJSON(ourFile);
window.hand = [];
updateEvent();
}
myFile.readAsText(f[0]);
});
},null,"div");
buttonAdd("#menu","add","Add Deck", function () {
popupDialog("deckLoader","Select Deck definition to add",true,inputDialog,{"inputType":"file"},function (f) {
var myFile = new FileReader();
myFile.onload = function (file) {
var ourFile = JSON.parse(file.target.result);
deckFromJSON(ourFile);
updateEvent();
}
myFile.readAsText(f[0]);
});
}, null, "div");
buttonAdd("#menu","shuffle", "Shuffle Deck", function () {
shuffleDeck(window.deck);
}, null, "div");
buttonAdd("#menu","shuffleAll", "Shuffle Whole Deck", function () {
window.deck = window.deck.concat(window.hand);
window.hand = [];
shuffleDeck(window.deck);
updateEvent();
}, null, "div");
buttonAdd("#menu","draw", "Draw Card", function () {
if (window.deck.length > 0) {
window.hand.unshift(window.deck.shift());
updateEvent();
}
}, null, "div");
elementPlace("body","display",null,"div");
window.UI = {};
window.UI.deck = elementPlace("#display","deck",null,"div");
window.UI.hand = elementPlace("#display","hand",null,"ol");
</script>
</html>