added initial hooks to websocket

This commit is contained in:
bluesaxman 2020-09-02 11:48:56 -06:00
parent 628f8c3430
commit 0bc7705a25

View File

@ -1,22 +1,24 @@
<html> <html>
<head> <head>
<title>Deckard</title> <title>Deckard + Company</title>
<link href="css.css" rel="stylesheet"> <link href="css.css" rel="stylesheet">
<script src="https://labs.murkfall.net/bluesaxman/blue.js/raw/master/libs/bluecore.js"></script> <script src="https://labs.murkfall.net/bluesaxman/blue.js/raw/master/libs/bluecore.js"></script>
</head> </head>
<body> <body>
<h1>Welcome to Deckard<h1> <h1>Welcome to Deckard + Company<h1>
</body> </body>
<script> <script>
window.deck = []; window.gameState = {};
window.gameState.deck = [];
window.gameState,pool = [];
window.hand = []; window.hand = [];
function updateEvent() { function updateEvent() {
window.UI.deck.innerHTML = ""; window.UI.deck.innerHTML = "";
window.UI.hand.innerHTML = ""; window.UI.hand.innerHTML = "";
var theDeck = elementPlace("#deck","deckDisp","card","div"); var theDeck = elementPlace("#deck","deckDisp","card","div");
theDeck.innerHTML = "<div>"+window.deck.length+"</div>"; theDeck.innerHTML = "<div>"+window.gameState.deck.length+"</div>";
window.hand.forEach(function (card,index) { window.hand.forEach(function (card,index) {
if (index < 10) { if (index < 10) {
var currentCard = elementPlace("#hand",null,"card","li"); var currentCard = elementPlace("#hand",null,"card","li");
@ -51,10 +53,10 @@ function shuffleDeck(deck) {
function deckFromJSON(ourFile) { function deckFromJSON(ourFile) {
if ( Array.isArray(ourFile) ) { if ( Array.isArray(ourFile) ) {
ourFile.forEach(function (deck) { ourFile.forEach(function (deck) {
window.deck = window.deck.concat(generateDeck(deck)); window.gameState.deck = window.gameState.deck.concat(generateDeck(deck));
}); });
} else { } else {
window.deck = window.deck.concat(generateDeck(ourFile)); window.gameState.deck = window.gameState.deck.concat(generateDeck(ourFile));
} }
} }
@ -65,10 +67,10 @@ buttonAdd("#menu","upload","Upload Deck", function () {
myFile.onload = function (file) { myFile.onload = function (file) {
// Probably validate the file somehow befor eating it // Probably validate the file somehow befor eating it
var ourFile = JSON.parse(file.target.result); var ourFile = JSON.parse(file.target.result);
window.deck = []; window.gameState.deck = [];
deckFromJSON(ourFile); deckFromJSON(ourFile);
window.hand = []; window.hand = [];
updateEvent(); gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
} }
myFile.readAsText(f[0]); myFile.readAsText(f[0]);
}); });
@ -79,29 +81,51 @@ buttonAdd("#menu","add","Add Deck", function () {
myFile.onload = function (file) { myFile.onload = function (file) {
var ourFile = JSON.parse(file.target.result); var ourFile = JSON.parse(file.target.result);
deckFromJSON(ourFile); deckFromJSON(ourFile);
updateEvent(); gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
} }
myFile.readAsText(f[0]); myFile.readAsText(f[0]);
}); });
}, null, "div"); }, null, "div");
buttonAdd("#menu","shuffle", "Shuffle Deck", function () { buttonAdd("#menu","shuffle", "Shuffle Deck", function () {
shuffleDeck(window.deck); shuffleDeck(window.gameState.deck);
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
}, null, "div"); }, null, "div");
buttonAdd("#menu","shuffleAll", "Shuffle Whole Deck", function () { buttonAdd("#menu","shuffleAll", "Shuffle Hand to Deck", function () {
window.deck = window.deck.concat(window.hand); window.gameState.deck = window.gameState.deck.concat(window.hand);
window.hand = []; window.hand = [];
shuffleDeck(window.deck); shuffleDeck(window.gameState.deck);
updateEvent(); gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
}, null, "div"); }, null, "div");
buttonAdd("#menu","draw", "Draw Card", function () { buttonAdd("#menu","draw", "Draw Card", function () {
if (window.deck.length > 0) { if (window.gameState.decks[0].length > 0) {
window.hand.unshift(window.deck.shift()); window.hand.unshift(window.gameState.decks[0].shift());
updateEvent(); gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
} }
}, null, "div"); }, null, "div");
elementPlace("body","display",null,"div"); elementPlace("body","display",null,"div");
window.UI = {}; window.UI = {};
window.UI.deck = elementPlace("#display","deck",null,"div"); window.UI.deck = elementPlace("#display","deck",null,"div");
window.UI.hand = elementPlace("#display","hand",null,"ol"); window.UI.hand = elementPlace("#display","hand",null,"ol");
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) {}
};
});
</script> </script>
</html> </html>