deckard-and-company/UI/index.html

132 lines
4.2 KiB
HTML
Raw Normal View History

2020-06-09 13:48:19 -06:00
<html>
<head>
2020-09-02 11:48:56 -06:00
<title>Deckard + Company</title>
2020-06-09 13:48:19 -06:00
<link href="css.css" rel="stylesheet">
<script src="https://labs.murkfall.net/bluesaxman/blue.js/raw/master/libs/bluecore.js"></script>
</head>
<body>
2020-09-02 11:48:56 -06:00
<h1>Welcome to Deckard + Company<h1>
2020-06-09 13:48:19 -06:00
</body>
<script>
2020-09-02 11:48:56 -06:00
window.gameState = {};
window.gameState.deck = [];
window.gameState,pool = [];
2020-06-09 13:48:19 -06:00
window.hand = [];
function updateEvent() {
window.UI.deck.innerHTML = "";
window.UI.hand.innerHTML = "";
var theDeck = elementPlace("#deck","deckDisp","card","div");
2020-09-02 11:48:56 -06:00
theDeck.innerHTML = "<div>"+window.gameState.deck.length+"</div>";
2020-06-09 13:48:19 -06:00
window.hand.forEach(function (card,index) {
if (index < 10) {
var currentCard = elementPlace("#hand",null,"card","li");
currentCard.innerHTML = card;
}
});
}
function generateDeck(DDF) {
var cards = [""];
for (var attribute in DDF) {
var tempcards = [];
cards.forEach( function (current) {
DDF[attribute].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) ) {
ourFile.forEach(function (deck) {
2020-09-02 11:48:56 -06:00
window.gameState.deck = window.gameState.deck.concat(generateDeck(deck));
2020-06-09 13:48:19 -06:00
});
} else {
2020-09-02 11:48:56 -06:00
window.gameState.deck = window.gameState.deck.concat(generateDeck(ourFile));
2020-06-09 13:48:19 -06:00
}
}
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);
2020-09-02 11:48:56 -06:00
window.gameState.deck = [];
2020-06-09 13:48:19 -06:00
deckFromJSON(ourFile);
window.hand = [];
2020-09-02 11:48:56 -06:00
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
2020-06-09 13:48:19 -06:00
}
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);
2020-09-02 11:48:56 -06:00
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
2020-06-09 13:48:19 -06:00
}
myFile.readAsText(f[0]);
});
}, null, "div");
buttonAdd("#menu","shuffle", "Shuffle Deck", function () {
2020-09-02 11:48:56 -06:00
shuffleDeck(window.gameState.deck);
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
2020-06-09 13:48:19 -06:00
}, null, "div");
2020-09-02 11:48:56 -06:00
buttonAdd("#menu","shuffleAll", "Shuffle Hand to Deck", function () {
window.gameState.deck = window.gameState.deck.concat(window.hand);
2020-06-09 13:48:19 -06:00
window.hand = [];
2020-09-02 11:48:56 -06:00
shuffleDeck(window.gameState.deck);
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
2020-06-09 13:48:19 -06:00
}, null, "div");
buttonAdd("#menu","draw", "Draw Card", function () {
2020-09-02 11:48:56 -06:00
if (window.gameState.decks[0].length > 0) {
window.hand.unshift(window.gameState.decks[0].shift());
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
2020-06-09 13:48:19 -06:00
}
}, 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");
2020-09-02 11:48:56 -06:00
popupDialog("gettingStarted","Welcome to Deckard and company", false, inputDialog,{"content":"Please enter a game ID or leave blank to start a new game","inputType":"number"},
function (value) {
if (Number.isInteger(Number.parseInt(value))) {
window.gameID = Number.parseInt(value);
} else {
var tempses = new WebSocket(server);
tempses.onmessage = function (event) {
var message = JSON.parse(event.data);
if (message.id) { gameID = message.id; }
};
}
window.gameSession = new WebSocket(server+gameID);
gameSession.onmessage = function (event) {
var message = JSON.parse(event.data);
if (message.state) { gameState = message.state; updateEvent(); }
if (message.message) { console.log(message.message); }
if (message.command) {}
};
});
2020-06-09 13:48:19 -06:00
</script>
</html>