From 2e9be63962e7bd69159ca1ad79bb7b0f44a92be1 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 18 Oct 2024 21:41:35 +0100 Subject: [PATCH] Pass server deckBuilt to client before client load The cards/decks built in server now are loaded before the game client starts setting up the game. Should allow for using the cards/decks stored in DB in the game instead of file --- public/board.js | 16 ++++++++-------- public/main.js | 4 ++++ server.js | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/public/board.js b/public/board.js index 0aa57d8..1144aa9 100644 --- a/public/board.js +++ b/public/board.js @@ -805,13 +805,14 @@ let board = new Board; // Board is now async (initialisation) // Everything beyond initialisation shouldn't be required so no need to wait // At least I think, for now. May need more stuff in future -async function loadBoard() { - // TODO: Figure out how to wait for requestDeck to responnd - // Have tried wrapping the function in a newPromise doodad, but that didn't - // change anything TODO TODO !IMPORTANT - console.log(await requestDeck()); - //console.log(await requestGetCards(1, 2)); // Deck 1 for Player 2 - //const dbLoaded = await loadDBElements(); + +// Request the deck(s) from server/db +requestDeck(); +// This will trigger an emit to and on from the server will trigger loadBoard(); +// public/main.js +// socket.on('responseGetDeck', function (data) { + +function loadBoard() { // Fill each players deck with their cards createDecks(); @@ -828,7 +829,6 @@ async function loadBoard() { board.drawBoard(true); }; } -loadBoard(); // Right Click, Rightclick, rightclick, right click canvas.addEventListener('contextmenu', function(event) { diff --git a/public/main.js b/public/main.js index 0e54888..527211d 100644 --- a/public/main.js +++ b/public/main.js @@ -173,10 +173,14 @@ function responseGetDeck(data){ console.log(data); if(!data.success){ alert(data.message); + return false; } + return data; } socket.on('responseGetDeck', function (data) { console.log('<< responseGetDeck'); responseGetDeck(data); + console.log('Load board?'); + loadBoard(data); }); diff --git a/server.js b/server.js index 19524b7..e46005d 100644 --- a/server.js +++ b/server.js @@ -204,8 +204,11 @@ function getCardColourRequirement(){ // decks will likely be changed around // https://www.geeksforgeeks.org/how-to-wait-for-multiple-promises-in-javascript/ +// https://medium.com/@nikolozz/using-socket-io-with-async-await-13fa8c2dc9d9 // using last example -async function requestDeck(socket, playerId, deckId){ +function requestDeck(socket, playerId, deckId){ + return new Promise((resolve, reject) => { + (async () => { // Get the deck(s) requested. // Not 100% on how to do two differening atm @@ -307,11 +310,11 @@ async function requestDeck(socket, playerId, deckId){ }); //console.log(cardData); + return resolve(cardData); - - let response = {'success':true, 'message':'Nothing happened'}; - response.message = cardData; // Add the cardData generated to the response - io.to(socket.id).emit('responseGetDeck', response); + // Promise stuff testing + })() + }); // TODO: In future the item inc. all the decks, cards, locations, and other attributes // will come from here @@ -323,6 +326,27 @@ async function requestDeck(socket, playerId, deckId){ // point to see. For now DB, and generating is ok, as still working on it } +function requestDeckStart(socket){ + // For future: + // Don't try to use promises between server/client + // It's painful and doesn't appear to work. Instead, let client have a loading + // screen, or wait for a while before doing something that requires server data + // like loading the decks... Then have the socket.on clientside trigger + // whatever functions, and stuff needs to happen + // Wasted so much time trying to do async server-client stuff. Don't bother + response = {success: false, message: 'Failed requestDeckStart() server.js'}; + requestDeck().then(data => { + response.success = true; + response.message = data; + io.to(socket.id).emit('responseGetDeck', response); + + }) + .catch(err => { + response.message = err; + io.to(socket.id).emit('responseGetDeck', err); + }); +} + function buildCards(cards, cardClasses, cardColourRequirements){ const dPromise = new Promise((resolve, reject) => { @@ -396,7 +420,10 @@ function onConnection(socket){ // TODO: request specific deckId/playerId (and multiples, i.e. get 6 decks at same // time, based on deckId/playerId combo. Maybe pass as array [deckId, playerId],[deck socket.on('requestDeck', function() { - requestDeck(socket); + requestDeckStart(socket); + }); + socket.on("exampleEvent", (data) => { + io.emit("exampleEvent", "hello from server"); }); socket.on('requestRooms', function(filter) {