@ -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 ) {