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
develop
Nathan Steel 1 year ago
parent 4b0b4495e4
commit 2e9be63962

@ -805,13 +805,14 @@ let board = new Board;
// Board is now async (initialisation) // Board is now async (initialisation)
// Everything beyond initialisation shouldn't be required so no need to wait // Everything beyond initialisation shouldn't be required so no need to wait
// At least I think, for now. May need more stuff in future // 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 // Request the deck(s) from server/db
// Have tried wrapping the function in a newPromise doodad, but that didn't requestDeck();
// change anything TODO TODO !IMPORTANT // This will trigger an emit to and on from the server will trigger loadBoard();
console.log(await requestDeck()); // public/main.js
//console.log(await requestGetCards(1, 2)); // Deck 1 for Player 2 // socket.on('responseGetDeck', function (data) {
//const dbLoaded = await loadDBElements();
function loadBoard() {
// Fill each players deck with their cards // Fill each players deck with their cards
createDecks(); createDecks();
@ -828,7 +829,6 @@ async function loadBoard() {
board.drawBoard(true); board.drawBoard(true);
}; };
} }
loadBoard();
// Right Click, Rightclick, rightclick, right click // Right Click, Rightclick, rightclick, right click
canvas.addEventListener('contextmenu', function(event) { canvas.addEventListener('contextmenu', function(event) {

@ -173,10 +173,14 @@ function responseGetDeck(data){
console.log(data); console.log(data);
if(!data.success){ if(!data.success){
alert(data.message); alert(data.message);
return false;
} }
return data;
} }
socket.on('responseGetDeck', function (data) { socket.on('responseGetDeck', function (data) {
console.log('<< responseGetDeck'); console.log('<< responseGetDeck');
responseGetDeck(data); responseGetDeck(data);
console.log('Load board?');
loadBoard(data);
}); });

@ -204,8 +204,11 @@ function getCardColourRequirement(){
// decks will likely be changed around // decks will likely be changed around
// https://www.geeksforgeeks.org/how-to-wait-for-multiple-promises-in-javascript/ // 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 // 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. // Get the deck(s) requested.
// Not 100% on how to do two differening atm // Not 100% on how to do two differening atm
@ -307,11 +310,11 @@ async function requestDeck(socket, playerId, deckId){
}); });
//console.log(cardData); //console.log(cardData);
return resolve(cardData);
// Promise stuff testing
let response = {'success':true, 'message':'Nothing happened'}; })()
response.message = cardData; // Add the cardData generated to the response });
io.to(socket.id).emit('responseGetDeck', response);
// TODO: In future the item inc. all the decks, cards, locations, and other attributes // TODO: In future the item inc. all the decks, cards, locations, and other attributes
// will come from here // 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 // 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){ function buildCards(cards, cardClasses, cardColourRequirements){
const dPromise = new Promise((resolve, reject) => { 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 // 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 // time, based on deckId/playerId combo. Maybe pass as array [deckId, playerId],[deck
socket.on('requestDeck', function() { socket.on('requestDeck', function() {
requestDeck(socket); requestDeckStart(socket);
});
socket.on("exampleEvent", (data) => {
io.emit("exampleEvent", "hello from server");
}); });
socket.on('requestRooms', function(filter) { socket.on('requestRooms', function(filter) {

Loading…
Cancel
Save