From c04b7f0b6c673d7681821be7415284d0bdc8227e Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 29 Oct 2024 21:24:41 +0000 Subject: [PATCH 1/8] Move all non-socket requests from server.js --- rooms.js | 226 ++++++++++++++++++++++++++++++++++++++++ server.js | 302 ++---------------------------------------------------- 2 files changed, 235 insertions(+), 293 deletions(-) create mode 100644 rooms.js diff --git a/rooms.js b/rooms.js new file mode 100644 index 0000000..1154b65 --- /dev/null +++ b/rooms.js @@ -0,0 +1,226 @@ +const roomMod = require('./roomMod'); + +// Data for rooms only atm +let data = {}; +let roomData = {}; + +// Variables for server overall +let numRooms = 0; +const maxRooms = 3; +const maxPlayersPerRoom = 1; +const maxSpectatorsPerRoom = 0; + +function requestRooms(socket, io, filter){ + console.log('+ requestRooms recieved'); + console.log('- filter: '+filter); + + let response = getRooms(filter, dump = true); + io.to(socket.id).emit('returnRooms', response); + + console.log(''); +} + +function getRooms(filter = 'all', dump = false){ + console.log('+ getRooms'); + let response = { + random: 'randomStuff', + roomData: roomData, + }; + + if(dump){ + console.log(response); + console.log(''); + } + + return response; +} + +function requestCreateRoom(socket, io, playerName){ + console.log('+ createRoom recieved'); + console.log('- requested by: '+playerName); + + response = createRoom(roomId = false, dump = true); + io.to(socket.id).emit('returnCreateRoom', response); + + if(response.success){ + let response = getRooms(filter = 'all', dump = true); + io.to(socket.id).emit('returnRooms', response); + } + + console.log(''); +} + +function createRoom(roomId = false, dump = true){ + let roomName = false; + if(roomId == false){ + roomId = numRooms + 1; + } + console.log(roomId); + + let response = { + success: false, + message: 'No idea bossman' + }; + + // Max room limit reached + console.log(numRooms); + console.log(maxRooms); + if(numRooms >= maxRooms){ + console.log('- Room limit reached'); + + response = { + success: false, + message: 'No space '+numRooms+' out of '+maxRooms+' created.' + }; + + // Create room + }else{ + console.log('- Creating room') + let room = {}; + room['id'] = roomId; + room['name'] = 'Room:'+room['id']; + roomName = room['name']; + room['password'] = ''; + room['timeout'] = {}; + room['timeout']['s'] = 10; + room['people'] = 0; + room['playerIds'] = {}; + + roomData[roomId] = room; + numRooms = numRooms + 1; + + response = { + success: true, + message: 'Room Created: '+roomName, + }; + } + + if(dump){ + console.log(response); + console.log(''); + } + return response; +} + +function requestJoinRoom(socket, io, playerName, roomId){ + + console.log('+ requestJoinRoom recieved'); + + let room = roomData[roomId]; + + // https://stackoverflow.com/a/18096649 + socket.playerId = playerName; + + if(room === undefined){ + console.log('>> Room does not exist'); + return 'error'; + } + + let roomName = 'Room_' + roomId; + let people = room['people']; + + if(isUserInRoom(playerName, roomId)){ + console.log('>> Already in room'); + return 'already in room'; + } + + if (people < maxPlayersPerRoom) { + + // Update people in room count + people = room['people'] += 1; + + // Add playerId to room (playerName for now while Ids don't exist TODO) + room['playerIds'][playerName] = playerName; + + // https://socket.io/docs/v4/rooms/ + // https://stackoverflow.com/a/25028953 + socket.join(roomId); + + console.log('>> User ' + playerName + + ' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); + + // Joined room (emit to the player that just joined) + io.to(socket.id).emit('responseRoom', response); + + if (people >= maxPlayersPerRoom) { + console.log('- starting game'); + // startGame for room + startGame(roomId, io); + } + + } + +} + +// Will need to be different to playerName in future (in case dupes) +// would use playerId TODO +function isUserInRoom(playerName, roomId){ + if(playerName in roomData[roomId]['playerIds']){ + return true; + } + return false; +} + +function startGame(roomId, io){ + + console.log('>> Room: ' + roomId + ': Requesting game...'); + let people = roomData[roomId].players; + /* + try { + //people = io.sockets.adapter.rooms.get(roomId).size; + } catch (e) { + console.log('>> Room: ' + roomId + ': No people here...'); + return; + } + */ + + // For now, if there's 2 people only. Will need changing for + // 3v1, 5v1, 2v2, etc... + + let response = {success: false, message: 'Failed requestStartGame() server.js'}; + if(people < maxPlayersPerRoom){ + console.log('Too few people'); + } + + console.log('>> Room: ' + roomId + ': Starting'); + + + // https://stackoverflow.com/a/25028953 + //console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true)) + let clients = io.sockets.adapter.rooms.get(roomId); + + // This should return the deck data, etc. for each client + // ideally only returning the items that the user can/should + // see i.e. shouldn't give them the inDeck card list just a counter + // shouldn't have opponent card data/their hand shouldn't be flipped + roomMod.roomGeneration().then(data => { + response.success = true; + response.message = data; + // Each player then gets the roomGeneration stuff + for (const clientId of clients) { + const clientSocket = io.sockets.sockets.get(clientId); + console.log('>> responseStartGame: '+clientSocket.playerId); + // Emit to client socket + io.to(clientSocket.id).emit('responseStartGame', response); + } + + }) + .catch(err => { + response.message = err; + // Each player then gets the error message + for (const clientId of clients) { + const clientSocket = io.sockets.sockets.get(clientId); + // Emit to client socket + io.to(clientSocket.id).emit('responseStartGame', err); + } + }); + +} + +// TODO: Need a 'leave room'/disconnect + +module.exports = { + requestRooms + ,requestJoinRoom + ,requestCreateRoom +}; diff --git a/server.js b/server.js index c29979d..c19a614 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,13 @@ - const express = require('express'); + const database = require('./database'); -const cardGen = require('./cardGen'); -const roomMod = require('./roomMod'); +const rooms = require('./rooms'); + const app = express(); const http = require('http').Server(app); const port = process.env.PORT || 3000; - const io = require('socket.io')(http); + // util is what nodejs uses for console.log, but has a depth of 2 set // so console.logs show [Array]/[Object] instead of useful info. // This can be overridden console.log(util.inspect(LOGDATA, true, 4, true)) @@ -21,309 +21,25 @@ database.connect(); io.on('connection', onConnection); -// Variables -let numRooms = 0; -let numRoomsToPreGen = 1; -const maxRooms = 3; -const maxPlayersPerRoom = 2; -const maxSpectatorsPerRoom = 0; - -// All the room -//let data = []; // Normal array -let data = {}; // Object array (this one for returning to player, and JSON stringify while keeping named ids) -let roomData = {}; -for (let roomId = 1; roomId <= numRoomsToPreGen; roomId++) { - // Never have more rooms than max rooms!!! - if(numRooms > maxRooms){ - break; - } - - createRoom(roomId); -} - function onConnection(socket){ console.log('+ User connected'); console.log(''); + // Rooms (joining, creating, etc) socket.on('requestRooms', function(filter) { - requestRooms(socket, filter); + rooms.requestRooms(socket, io, filter); }); socket.on('requestJoinRoom', function(playerName, roomId) { - requestJoinRoom(socket, playerName, roomId); + rooms.requestJoinRoom(socket, io, playerName, roomId); }); socket.on('requestCreateRoom', function(playerName) { - requestCreateRoom(socket, playerName); - }); - -} - -function requestRooms(socket, filter){ - console.log('+ requestRooms recieved'); - console.log('- filter: '+filter); - - let response = getRooms(filter, dump = true); - io.to(socket.id).emit('returnRooms', response); - - console.log(''); -} - -function getRooms(filter = 'all', dump = false){ - console.log('+ getRooms'); - let response = { - random: 'randomStuff', - roomData: roomData, - }; - - if(dump){ - console.log(response); - console.log(''); - } - - return response; -} - -function requestCreateRoom(socket, playerName){ - console.log('+ createRoom recieved'); - console.log('- requested by: '+playerName); - - response = createRoom(roomId = false, dump = true); - io.to(socket.id).emit('returnCreateRoom', response); - - if(response.success){ - let response = getRooms(filter = 'all', dump = true); - io.to(socket.id).emit('returnRooms', response); - } - - console.log(''); -} - -function createRoom(roomId = false, dump = true){ - let roomName = false; - if(roomId == false){ - roomId = numRooms + 1; - } - console.log(roomId); - - let response = { - success: false, - message: 'No idea bossman' - }; - - // Max room limit reached - console.log(numRooms); - console.log(maxRooms); - if(numRooms >= maxRooms){ - console.log('- Room limit reached'); - - response = { - success: false, - message: 'No space '+numRooms+' out of '+maxRooms+' created.' - }; - - // Create room - }else{ - console.log('- Creating room') - let room = {}; - room['id'] = roomId; - room['name'] = 'Room:'+room['id']; - roomName = room['name']; - room['password'] = ''; - room['timeout'] = {}; - room['timeout']['s'] = 10; - room['people'] = 0; - room['playerIds'] = {}; - - //room['deck'] = []; - //room['turn'] = 0; - - // Removed players for now, players may be seperate - // and back-end only with an assoc. to current room - - //let players = {}; - //for (let j = 0; j < maxPlayersPerRoom; j++) { - //let p = {}; - //p['id'] = 0; - //p['name'] = ""; - //p['hand'] = {}; - //players[j] = p; - //} - - //room['players'] = players; - roomData[roomId] = room; - numRooms = numRooms + 1; - - response = { - success: true, - message: 'Room Created: '+roomName, - }; - } - - if(dump){ - console.log(response); - console.log(''); - } - return response; -} - -// TODO: break into requestJoinRoom, and JoinRoom? -// Maybe not needed here? As won't be done via backend? -// TODO: Need a 'leave room'/disconnect -function requestJoinRoom(socket, playerName, roomId){ - - console.log('+ requestJoinRoom recieved'); - - let room = roomData[roomId]; - - // Add socket for playerName so that players in room get - // responses, etc. from the room (something like that) - - // Add player to socket object, so it can be referred to - // as each socket is individual to a player, but rooms need to - // emit to multiple players. These .player can be used in loops - - // https://socket.io/docs/v4/rooms/ - // https://stackoverflow.com/a/18096649 - socket.playerId = playerName; - - if(room === undefined){ - io.to(socket.id).emit('responseRoom', 'error'); - console.log('>> Room does not exist'); - } - - let roomName = 'Room_' + roomId; - let people = room['people']; - // people = io.sockets.adapter.rooms[roomId].length; // This gets the sockets in the room (i.e. the players) - - //console.log(util.inspect(io.sockets.adapter.rooms, true, 4, true)) - //console.log('- people(socket: '+io.sockets.adapter.rooms[player]+')'); - //console.log('- maxPlayersPerRoom: '+maxPlayersPerRoom); - - if(isUserInRoom(playerName, roomId)){ - console.log('Already in room'); - return false; - } - - if (people < maxPlayersPerRoom) { - - // Update people in room count - people = room['people'] += 1; - - // Add playerId to room (playerName for now while Ids don't exist TODO) - room['playerIds'][playerName] = playerName; - - // https://socket.io/docs/v4/rooms/ - // https://stackoverflow.com/a/25028953 - socket.join(roomId); - - // https://stackoverflow.com/a/25028953 - //console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true)) - /* - let clients = io.sockets.adapter.rooms.get(roomId); - let numClients = clients ? clients.size : 0; - for (const clientId of clients) { - //this is the socket of each client in the room. - const clientSocket = io.sockets.sockets.get(clientId); - console.log(clientSocket.playerId); // The playerId set beggining of func. - } - */ - - console.log('>> User ' + playerName + - ' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); - - // Joined room (emit to the player that just joined) - io.to(socket.id).emit('responseJoinRoom', roomName); - - if (people >= maxPlayersPerRoom) { - console.log('- starting game'); - // startGame for room - startGame(roomId); - } - - } - -} - -// Will need to be different to playerName in future (in case dupes) -// would use playerId TODO -function isUserInRoom(playerName, roomId){ - if(playerName in roomData[roomId]['playerIds']){ - return true; - } - return false; -} - -function startGame(roomId){ - - console.log('>> Room: ' + roomId + ': Requesting game...'); - let people = roomData[roomId].players; - /* - try { - //people = io.sockets.adapter.rooms.get(roomId).size; - } catch (e) { - console.log('>> Room: ' + roomId + ': No people here...'); - return; - } - */ - - // For now, if there's 2 people only. Will need changing for - // 3v1, 5v1, 2v2, etc... - - let response = {success: false, message: 'Failed requestStartGame() server.js'}; - if(people < maxPlayersPerRoom){ - console.log('Too few people'); - } - - console.log('>> Room: ' + roomId + ': Starting'); - - - // https://stackoverflow.com/a/25028953 - //console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true)) - let clients = io.sockets.adapter.rooms.get(roomId); - - /* - for (const clientId of clients) { - //this is the socket of each client in the room. - const clientSocket = io.sockets.sockets.get(clientId); - console.log(clientSocket.playerId); // The playerId set in requestJoin - - // Just so I know how to access stuff in future. - console.log(roomData[roomId].playerIds[clientSocket.playerId] + 'is in the game'); - } - */ - - // This should return the deck data, etc. for each client - // ideally only returning the items that the user can/should - // see i.e. shouldn't give them the inDeck card list just a counter - // shouldn't have opponent card data/their hand shouldn't be flipped - roomMod.roomGeneration().then(data => { - response.success = true; - response.message = data; - // Each player then gets the roomGeneration stuff - for (const clientId of clients) { - const clientSocket = io.sockets.sockets.get(clientId); - console.log('>> responseStartGame: '+clientSocket.playerId); - // Emit to client socket - io.to(clientSocket.id).emit('responseStartGame', response); - } - - }) - .catch(err => { - response.message = err; - // Each player then gets the error message - for (const clientId of clients) { - const clientSocket = io.sockets.sockets.get(clientId); - // Emit to client socket - io.to(clientSocket.id).emit('responseStartGame', err); - } + rooms.requestCreateRoom(socket, io, playerName); }); -} - -// Then do functions like this? -function shuffleDeck(roomId, playerId){ + // Game (actual things relating to the game) } From 06b3fe6da085379212e7882fc589632d4bee62ce Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 29 Oct 2024 21:55:40 +0000 Subject: [PATCH 2/8] Add 'components' mod + add empty comps. to roomGen Create a components module to be used as the overseeing list/build of components to be used through-out the code-base. Added the components in said module to roomGeneration, but they are not used at current --- components.js | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ roomMod.js | 30 +++++++--------- 2 files changed, 107 insertions(+), 18 deletions(-) create mode 100644 components.js diff --git a/components.js b/components.js new file mode 100644 index 0000000..c23847d --- /dev/null +++ b/components.js @@ -0,0 +1,95 @@ +// A seperate list for components, so they're easy to recall + +// Done as object, so it can be added to different rooms with components = NEW component? +const component = { + + // Entity Stuff + //item : [], + //itemCount : 0, + + // Card Stuff + cardData : {}, + cardFace : {}, + cardStatus : { + tapped : {}, + attacking : {}, + inspected : {}, + }, + cardAttack : {}, + cardColours : {}, // Replace with colour + colour : { + white : {}, // entityId, amountOfColour + blue : {}, + red : {}, + }, + cardManaColour : {}, + cardEffect : {}, // TODO: Split this into effect, trigger, step, targets. + cardCost : {}, + cardSprite : {}, // id, position in spritesheet [0,4] e.g. + //cardPlayer = {}, + + // Deck Stuff? + deckIn : {}, + deckData : {}, + + // UI (so users know what's been targetted) + selected : {}, + selectable : {}, + + // Effect (break it up?) + effect : {}, + effectTrigger : {}, + // etc, etc. + + // Board Elements + // loop component.shield for shield items + boardElement : {}, + // Replace with following + realDeck : {}, + inDeck : {}, + hand : {}, + board : {}, + shield : {}, + mana : {}, + grave : {}, + void : {}, + + // + listPosition : {}, + // position (clientside) + // size (clientside) + + // Passives + // component.passive.flight ? + passive : { + flight : {}, + reach : {}, + taunt : {}, + }, + + type : { + unit : {}, + spell : {}, + token : {}, + }, + + classes : { + orc : {}, + human : {}, + spirit : {}, + }, + + +}; + +// For front-end +// position, size + +// These should be used as such (Not 100% as yet) +// For onBoard()... for player()... for passive.flight()... +// Check the board, items belonging to playerX, for flight passive? + +module.exports = { + component +} + diff --git a/roomMod.js b/roomMod.js index 8223345..615fac7 100644 --- a/roomMod.js +++ b/roomMod.js @@ -1,41 +1,32 @@ // Build a room, fill will players, etc. const cardGen = require('./cardGen'); - +const components = require('./components'); // Room should, setPlayers, add them to correct team (TODO), build their decks, and first shuffle function startItemCount(){ + let item = []; let itemCount = 0; returns = {'item': item, 'itemCount': itemCount}; return(returns); } + function setPlayers(playerCount = 2, itemData){ - // Add new item attribute for 'players' - // TODO: Maybe check if exists, and add to in that case (for replacing people?) - // Doubt that would ever make it into the game, but could still be worth + itemData.player = {}; //let player = {}; // Player item belongs to itemData.players = []; // List of the players (an associated playerItem?) - //itemData. - - // Can be done with just referring to itemData[x], but less readable - // Will likely redefine vars each new function. For now will keep this as-is let playerNo = 0 + itemData['itemCount']; // Start loop from current itemCount playerCount = playerCount + itemData['itemCount']; // End at playerCount diff from itemCount for(playerNo; playerNo < playerCount; playerNo++){ - // REMOVED PLAYERS AS ITEM, BECAUSE THEY WERE BORKING - // TODO: Add back at some point, or don't bother - //itemData['item'].push(playerNo); - //itemData['player'][itemData['itemCount']] = playerNo; // The player belongs to itself - //itemData['itemCount']++; + itemData['players'].push(playerNo); // Add player no to array so can be looped + } - // Return related item and item attributes - //returns = {'item': item, 'itemCount': itemCount, 'player': player}; + return itemData; - //return([item, itemCount, player]); } // For future, when 2v2s, and 5v1 Raids, etc. @@ -48,10 +39,13 @@ function roomGeneration(playerCount, teamCount = null, playerTeams = null){ (async () => { let itemData = startItemCount(); - // Create 2 players for the room - itemData = setPlayers(2, itemData); + // Add players for the room + itemData = setPlayers(playerCount, itemData); // TODO: Get their selected decks (will need to pass somewhere) + + // Add all the empty components to the room itemData + itemData.component = components.component; // Generate the decks, and card within the deck cardLists [itemData] = await Promise.all([ cardGen.requestDeck(itemData) ]); From 8666ea05f40d13693315f777114920908f429d3d Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 29 Oct 2024 22:16:18 +0000 Subject: [PATCH 3/8] Change players[] to contain player/deck Ids --- roomMod.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/roomMod.js b/roomMod.js index 615fac7..2b26e17 100644 --- a/roomMod.js +++ b/roomMod.js @@ -13,16 +13,18 @@ function startItemCount(){ return(returns); } -function setPlayers(playerCount = 2, itemData){ +function setPlayers(playerData, itemData){ itemData.player = {}; //let player = {}; // Player item belongs to itemData.players = []; // List of the players (an associated playerItem?) let playerNo = 0 + itemData['itemCount']; // Start loop from current itemCount - playerCount = playerCount + itemData['itemCount']; // End at playerCount diff from itemCount + playerCount = playerData.length + itemData['itemCount']; // End at playerCount diff from itemCount + let i = 0; for(playerNo; playerNo < playerCount; playerNo++){ - itemData['players'].push(playerNo); // Add player no to array so can be looped + itemData['players'].push([playerNo, playerData[i]]); // Add player no to array so can be looped + i++; } @@ -37,12 +39,20 @@ function setTeams(){ function roomGeneration(playerCount, teamCount = null, playerTeams = null){ return new Promise((resolve, reject) => { (async () => { + let itemData = startItemCount(); + // This will be passed by the joinRoom for each player, then built + // on startGame) + let playerData = [ + {'playerId': 1, 'deck':{'playerId':1,'deckId':1}}, + {'playerId': 2, 'deck':{'playerId':2,'deckId':1}}, + ]; + // Add players for the room - itemData = setPlayers(playerCount, itemData); + itemData = setPlayers(playerData, itemData); - // TODO: Get their selected decks (will need to pass somewhere) + // TODO: Get their selected decks // Add all the empty components to the room itemData itemData.component = components.component; From 8df0969836061403cf9fd0d0cbef4097112c0792 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 16:10:46 +0000 Subject: [PATCH 4/8] Add DB clauses to return requested cards only --- cardGen.js | 123 +++++++++++++++++++++++++--------------------------- database.js | 49 ++++++++++++++++----- roomMod.js | 1 + 3 files changed, 96 insertions(+), 77 deletions(-) diff --git a/cardGen.js b/cardGen.js index 9adaf11..80ff642 100644 --- a/cardGen.js +++ b/cardGen.js @@ -4,22 +4,12 @@ const database = require('./database'); const util = require('util') -// cardClass, cardColourRequirement -// may want to be seperate too (as well as in cardItem), so that -// during match they can be altered by effects while keeping the OG card -// for inspecting (and compare against new stats,reqs,etc.) -// same with attack, cost, etc. things that will can be visually shown as -// changed in game - -// Just grabbing everything from DB for now, as it's quite small at current -// then will rejig when needed. -// Should all cards, effects, classes etc. be loaded in on server start -// then just load decks and decklists when needed? +// Get the decks requested function getDecks(deckIds = false){ // Await promise, once it's done get the data, if errors send err const dPromise = new Promise((resolve, reject) => { - database.dbGetDecks().then(data => { + database.dbGetDecks(deckIds).then(data => { let decks = []; @@ -48,10 +38,10 @@ function getDecks(deckIds = false){ return dPromise; } //getDecks(); -function getDeckList(){ +function getDeckList(deckIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetDeckList().then(data => { + database.dbGetDeckList(deckIds).then(data => { let deckList = []; @@ -70,11 +60,10 @@ function getDeckList(){ }); return dPromise; } -//getDeckList(); -function getCards(){ +function getCards(cardIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetCards().then(data => { + database.dbGetCards(cardIds).then(data => { let cards = []; @@ -97,10 +86,9 @@ function getCards(){ }); return dPromise; } -//getCards(); -function getCardClasses(){ +function getCardClasses(cardIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetCardClasses().then(data => { + database.dbGetCardClasses(cardIds).then(data => { let cardClasses = []; @@ -118,10 +106,9 @@ function getCardClasses(){ }); return dPromise; } -//getCardClasses(); -function getCardColourRequirement(){ +function getCardColourRequirement(cardIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetCardColourRequirement().then(data => { + database.dbGetCardColourRequirement(cardIds).then(data => { let colourRequirements = []; @@ -141,9 +128,9 @@ function getCardColourRequirement(){ }); return dPromise; } -function getCardManaColour(){ +function getCardManaColour(cardIds = false){ const cPromise = new Promise((resolve, reject) => { - database.dbGetCardManaColour().then(data => { + database.dbGetCardManaColour(cardIds).then(data => { let manaColours = []; @@ -274,25 +261,9 @@ function getCardPassive(){ return cPromise; } -//getCardColourRequirement(); -// Then effects which will have effects with parent triggers, and unit type checks -// colour checks, all sorts. So will be more difficult. Basic (flight, etc) -// shouldn't be too bad -// something like effect_basic, card_effect, effect_trigger, effect_requirement, effect_option -// effect_stage, effect_advanced, with advanced being an id with x triggers, triggers have -// x req, advanced as with x options, x stages to be able to fine-tune fancy stuff -// combining all other effects, units, cards, colours, etc. Will be a lot of though, -// but better than hard coding anything more than basic effects and effect check logic -// TODO: effect (as above) - -// request a deck in the format CURRENTLY used in the game -// 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 -// TODO: When this is functionally working, need to split up into other func/modules -// such as: playerMod, cardMod, deckMod, miscMod ? function requestDeck(itemData = null){ return new Promise((resolve, reject) => { (async () => { @@ -300,53 +271,73 @@ function requestDeck(itemData = null){ // Get the deck(s) requested. // Not 100% on how to do two differening atm // Besides all of playerId, and all of deckId. But 1,2/2,3 for instance + + // Build array of decks to get + let deckIds = []; + for(let i = 0; i < itemData.players.length; i++){ + let deckStuff = itemData.players[i][1].deck; + deckIds.push([deckStuff.playerId, deckStuff.deckId]); + // So should be array of [playerId, deckId] which is primary key in DB + } + + // Get said decks, and their deckLists const [decks, deckList] = await Promise.all([ - getDecks(), - getDeckList() + getDecks(deckIds), + getDeckList(deckIds) ]); - //console.log(decks); - //console.log(deckList); + /* + console.log('--- decks ---'); + console.log(decks); + console.log('= deckLists =') + console.log(deckList); + console.log('------'); + */ + // Now loop the deckList for the cardIds let deckCardIds = []; deckList.forEach((deckItem) => { deckCardIds.push(deckItem.cardId); - }); - //console.log(deckCardIds); // Next, get the cards in the deck by their ID // TODO: https://stackoverflow.com/a/65510676 - // Change SQL to accept for just the cards passed // Get each cards data, colourReqs, and classes const [cards, cardClasses, cardColourRequirements, cardManaColours, cardPassives] = await Promise.all([ - getCards(), - getCardClasses(), - getCardColourRequirement(), - getCardManaColour(), - getCardPassive(), + getCards(deckCardIds), + getCardClasses(deckCardIds), + getCardColourRequirement(deckCardIds), + getCardManaColour(deckCardIds), + getCardPassive(deckCardIds), ]); - // ^^^^ Classes async? Can pass the cardsIds, then loop classes, if the class cardId - // matches the cardId in cards[] then push the class to cards[x].classes + // Return all effect data from DB + const [effects] = + await Promise.all([ + database.dbGetEffect(deckCardIds), // Get effects + ]); - // TODO: Build the card so far async for it's done alongside getting effects? - // Then when both done add the effects to the existing cardObjects? Yep good idea me - //console.log(cards); - //console.info(cardClasses); - //console.log(cardColourRequirements); + // Loop the effects for their effectIds to then get the steps/triggers from DB + let effectIds = []; + await effects.forEach((effect) => { + effectIds.push(effect.effectId); + }); - // Return all effect data from DB - const [effects, effectSteps, effectTriggers] = + // Then pass the effectIds to get their steps/triggers + const [effectSteps, effectTriggers] = await Promise.all([ - database.dbGetEffect(), - database.dbGetEffectStep(), - database.dbGetEffectTrigger(), + database.dbGetEffectStep(effectIds), + database.dbGetEffectTrigger(effectIds), ]); + /* + console.log('--- Effects ---'); + console.log(effects); + */ + // Build Effects const [cardEffects] = await Promise.all([ @@ -460,6 +451,8 @@ function requestDeck(itemData = null){ // For each new card, loop to the cardCount (how many cards in deck) // and add to the deck + // TODO: Associate player to deck differently (both could be using same deck + // via some kind of 'try a deck' or 'use friends deck' option) for(let i = 0; i < deckListItem.cardCount; i++){ item.push(itemCount); // Add new item to add stuff for // ^ not using item.length incase anything in future gets deleted diff --git a/database.js b/database.js index 8f89c93..ece68f0 100644 --- a/database.js +++ b/database.js @@ -17,7 +17,7 @@ function disconnect(){ } // My DB stuffs -function dbGetDecks(){ +function dbGetDecks(deckIds = false){ const dPromise = new Promise((resolve, reject) => { let cards = []; let sql = `SELECT @@ -25,8 +25,16 @@ function dbGetDecks(){ ,playerId ,deckName FROM deck - LIMIT 10 - `; // TODO: Remove limit when happy/this accepts params + `; + + // TODO: Jank, need to unjank it + if(deckIds){ + for(let i = 0; i < deckIds.length; i++){ + if(i == 0){ sql += ' WHERE '; } + else{ sql += ' OR '; } + sql += '(deckId = '+deckIds[i][1]+' AND playerId = '+deckIds[i][0]+')'; + } + } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -35,7 +43,7 @@ function dbGetDecks(){ }); return dPromise; } -function dbGetDeckList(){ +function dbGetDeckList(deckIds = false){ const dPromise = new Promise((resolve, reject) => { let cards = []; let sql = `SELECT @@ -46,6 +54,15 @@ function dbGetDeckList(){ FROM deck_cards `; + // TODO: Jank, need to unjank it + if(deckIds){ + for(let i = 0; i < deckIds.length; i++){ + if(i == 0){ sql += ' WHERE '; } + else{ sql += ' OR '; } + sql += '(deckId = '+deckIds[i][1]+' AND playerId = '+deckIds[i][0]+')'; + } + } + con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } resolve(result); @@ -53,7 +70,7 @@ function dbGetDeckList(){ }); return dPromise; } -function dbGetCards(){ +function dbGetCards(cardIds = false){ // Start with basic stuff in card table const dPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -65,6 +82,7 @@ function dbGetCards(){ ,cardRarity FROM card `; + if(cardIds){ sql += 'WHERE card.id IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -73,7 +91,7 @@ function dbGetCards(){ }); return dPromise; } -function dbGetCardClasses(){ +function dbGetCardClasses(cardIds = false){ // Get the classes assoc. on each card const dPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -81,6 +99,7 @@ function dbGetCardClasses(){ ,classId FROM card_class `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -89,7 +108,7 @@ function dbGetCardClasses(){ }); return dPromise; } -function dbGetCardColourRequirement(){ +function dbGetCardColourRequirement(cardIds = false){ // Get the classes assoc. on each card const dPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -98,6 +117,7 @@ function dbGetCardColourRequirement(){ ,cost FROM card_colour_requirement `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -107,7 +127,7 @@ function dbGetCardColourRequirement(){ return dPromise; } -function dbGetCardManaColour(){ +function dbGetCardManaColour(cardIds = false){ // Get the classes assoc. on each card const cPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -115,6 +135,7 @@ function dbGetCardManaColour(){ ,colourId FROM card_mana_colour `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -125,7 +146,7 @@ function dbGetCardManaColour(){ } // Effect stuff -function dbGetEffect(){ +function dbGetEffect(cardIds = false){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT cardId @@ -136,6 +157,7 @@ function dbGetEffect(){ INNER JOIN effect ON effect.id = card_effect.effectId `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -144,7 +166,7 @@ function dbGetEffect(){ }); return ePromise; } -function dbGetEffectStep(){ +function dbGetEffectStep(effectIds = false){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT effectId, @@ -163,6 +185,7 @@ function dbGetEffectStep(){ effect_step_target ON effect_step_target.effectStep = effect_step.id `; + if(effectIds){ sql += 'WHERE effectId IN ('+effectIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -172,7 +195,7 @@ function dbGetEffectStep(){ return ePromise; } // Effect Trigger stuff -function dbGetEffectTrigger(){ +function dbGetEffectTrigger(effectIds = false){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT effect_trigger.id AS triggerId, @@ -196,6 +219,7 @@ function dbGetEffectTrigger(){ effect_trigger_target ON effect_trigger_target.effectTriggerId = effect_trigger.triggerTypeId `; + if(effectIds){ sql += 'WHERE effectId IN ('+effectIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -206,13 +230,14 @@ function dbGetEffectTrigger(){ } // Passive stuff -function dbGetPassive(){ +function dbGetPassive(cardIds = false){ const pPromise = new Promise((resolve, reject) => { let sql = `SELECT cardId ,passiveId FROM card_passive `; + if(cardIds){ sql += 'WHERE card.id IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } diff --git a/roomMod.js b/roomMod.js index 2b26e17..a305e51 100644 --- a/roomMod.js +++ b/roomMod.js @@ -44,6 +44,7 @@ function roomGeneration(playerCount, teamCount = null, playerTeams = null){ // This will be passed by the joinRoom for each player, then built // on startGame) + // Need to make it work if they're the same deck too let playerData = [ {'playerId': 1, 'deck':{'playerId':1,'deckId':1}}, {'playerId': 2, 'deck':{'playerId':2,'deckId':1}}, From 525e55a09202cf5420211e730e8e6bbfb315ee5f Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 18:48:20 +0000 Subject: [PATCH 5/8] Change io to global --- rooms.js | 30 +++++++++++++++--------------- server.js | 11 ++++++++--- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/rooms.js b/rooms.js index 1154b65..623d364 100644 --- a/rooms.js +++ b/rooms.js @@ -10,12 +10,12 @@ const maxRooms = 3; const maxPlayersPerRoom = 1; const maxSpectatorsPerRoom = 0; -function requestRooms(socket, io, filter){ +function requestRooms(socket, filter){ console.log('+ requestRooms recieved'); console.log('- filter: '+filter); let response = getRooms(filter, dump = true); - io.to(socket.id).emit('returnRooms', response); + global.io.to(socket.id).emit('returnRooms', response); console.log(''); } @@ -35,16 +35,16 @@ function getRooms(filter = 'all', dump = false){ return response; } -function requestCreateRoom(socket, io, playerName){ +function requestCreateRoom(socket, playerName){ console.log('+ createRoom recieved'); console.log('- requested by: '+playerName); response = createRoom(roomId = false, dump = true); - io.to(socket.id).emit('returnCreateRoom', response); + global.io.to(socket.id).emit('returnCreateRoom', response); if(response.success){ let response = getRooms(filter = 'all', dump = true); - io.to(socket.id).emit('returnRooms', response); + global.io.to(socket.id).emit('returnRooms', response); } console.log(''); @@ -102,7 +102,7 @@ function createRoom(roomId = false, dump = true){ return response; } -function requestJoinRoom(socket, io, playerName, roomId){ +function requestJoinRoom(socket, playerName, roomId){ console.log('+ requestJoinRoom recieved'); @@ -140,12 +140,12 @@ function requestJoinRoom(socket, io, playerName, roomId){ ' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); // Joined room (emit to the player that just joined) - io.to(socket.id).emit('responseRoom', response); + global.io.to(socket.id).emit('responseRoom', response); if (people >= maxPlayersPerRoom) { console.log('- starting game'); // startGame for room - startGame(roomId, io); + startGame(roomId); } } @@ -161,7 +161,7 @@ function isUserInRoom(playerName, roomId){ return false; } -function startGame(roomId, io){ +function startGame(roomId){ console.log('>> Room: ' + roomId + ': Requesting game...'); let people = roomData[roomId].players; @@ -187,21 +187,21 @@ function startGame(roomId, io){ // https://stackoverflow.com/a/25028953 //console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true)) - let clients = io.sockets.adapter.rooms.get(roomId); + let clients = global.io.sockets.adapter.rooms.get(roomId); // This should return the deck data, etc. for each client // ideally only returning the items that the user can/should // see i.e. shouldn't give them the inDeck card list just a counter // shouldn't have opponent card data/their hand shouldn't be flipped - roomMod.roomGeneration().then(data => { + roomMod.roomGeneration(roomId).then(data => { response.success = true; response.message = data; // Each player then gets the roomGeneration stuff for (const clientId of clients) { - const clientSocket = io.sockets.sockets.get(clientId); + const clientSocket = global.io.sockets.sockets.get(clientId); console.log('>> responseStartGame: '+clientSocket.playerId); // Emit to client socket - io.to(clientSocket.id).emit('responseStartGame', response); + global.io.to(clientSocket.id).emit('responseStartGame', response); } }) @@ -209,9 +209,9 @@ function startGame(roomId, io){ response.message = err; // Each player then gets the error message for (const clientId of clients) { - const clientSocket = io.sockets.sockets.get(clientId); + const clientSocket = global.io.sockets.sockets.get(clientId); // Emit to client socket - io.to(clientSocket.id).emit('responseStartGame', err); + global.io.to(clientSocket.id).emit('responseStartGame', err); } }); diff --git a/server.js b/server.js index c19a614..7e875d3 100644 --- a/server.js +++ b/server.js @@ -7,6 +7,11 @@ const app = express(); const http = require('http').Server(app); const port = process.env.PORT || 3000; const io = require('socket.io')(http); +global.io = io; + +// To log the player sockets, so they can be easily referred to +// maybe jank, but can't see alternative currently +global.playerSocket = {}; // util is what nodejs uses for console.log, but has a depth of 2 set // so console.logs show [Array]/[Object] instead of useful info. @@ -28,15 +33,15 @@ function onConnection(socket){ // Rooms (joining, creating, etc) socket.on('requestRooms', function(filter) { - rooms.requestRooms(socket, io, filter); + rooms.requestRooms(socket, filter); }); socket.on('requestJoinRoom', function(playerName, roomId) { - rooms.requestJoinRoom(socket, io, playerName, roomId); + rooms.requestJoinRoom(socket, playerName, roomId); }); socket.on('requestCreateRoom', function(playerName) { - rooms.requestCreateRoom(socket, io, playerName); + rooms.requestCreateRoom(socket, playerName); }); // Game (actual things relating to the game) From 16e307c32cf7d580f6ee18c3d52b4acbd6277687 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 20:06:42 +0000 Subject: [PATCH 6/8] Add 'playerData' with socketId to roomMod --- components.js | 4 ++++ public/main.js | 5 +++++ roomMod.js | 41 ++++++++++++++++++++++++++++++----------- rooms.js | 2 +- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/components.js b/components.js index c23847d..c1617b7 100644 --- a/components.js +++ b/components.js @@ -7,6 +7,10 @@ const component = { //item : [], //itemCount : 0, + roomId : null, + turn : 0, + playerTurn : 0, + // Card Stuff cardData : {}, cardFace : {}, diff --git a/public/main.js b/public/main.js index dbb0606..026e162 100644 --- a/public/main.js +++ b/public/main.js @@ -182,3 +182,8 @@ socket.on('responseStartGame', function (data) { loadBoard(data.message); }); +socket.on('alert', function (data) { + console.log('<< alert'); + alert(data.message); +}); + diff --git a/roomMod.js b/roomMod.js index a305e51..c126695 100644 --- a/roomMod.js +++ b/roomMod.js @@ -36,21 +36,37 @@ function setTeams(){ } -function roomGeneration(playerCount, teamCount = null, playerTeams = null){ +function roomGeneration(roomId){ return new Promise((resolve, reject) => { (async () => { + // Player's sockets + console.log('--- Room for generation ---'); + console.log(io.sockets.adapter.rooms.get(roomId)); + let itemData = startItemCount(); - // This will be passed by the joinRoom for each player, then built - // on startGame) - // Need to make it work if they're the same deck too - let playerData = [ - {'playerId': 1, 'deck':{'playerId':1,'deckId':1}}, - {'playerId': 2, 'deck':{'playerId':2,'deckId':1}}, - ]; + // Player data with Sockets + let playerData = []; + let i = 1; + let clients = global.io.sockets.adapter.rooms.get(roomId); + for (const clientId of clients) { + const clientSocket = global.io.sockets.sockets.get(clientId); + + playerData.push({ + 'playerId': clientSocket.playerId + ,'deck':{'playerId':i,'deckId':1} + ,'socketId': clientSocket.id // TODO: ONLY FOR SERVERSIDE!!! + }); + + i++; + } - // Add players for the room + // Test alert so that different data/emits can be sent per-player + global.io.to(playerData[0].socketId).emit('alert', {'message': 'roomMod.js test'}); + + + // Add players for the room (seperate to playerData, currently what's used) itemData = setPlayers(playerData, itemData); // TODO: Get their selected decks @@ -61,8 +77,11 @@ function roomGeneration(playerCount, teamCount = null, playerTeams = null){ // Generate the decks, and card within the deck cardLists [itemData] = await Promise.all([ cardGen.requestDeck(itemData) ]); - //console.log('deckData'); - //console.log(deckData); + // Some room stuff, likely change this + itemData.roomId = roomId; + itemData.turn = 0; // The turn count of the match + itemData.playersTurn = 0; // This means it's playerData[0] turn + return resolve(itemData); })() }); diff --git a/rooms.js b/rooms.js index 623d364..3233d76 100644 --- a/rooms.js +++ b/rooms.js @@ -7,7 +7,7 @@ let roomData = {}; // Variables for server overall let numRooms = 0; const maxRooms = 3; -const maxPlayersPerRoom = 1; +const maxPlayersPerRoom = 2; const maxSpectatorsPerRoom = 0; function requestRooms(socket, filter){ From ea7c9b8596885102f09e3333e942769f732c4cdf Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 21:10:05 +0000 Subject: [PATCH 7/8] Add 'pass', make roomData global+add item/player Add a start of 'pass' mechanic (does nothing yet, just emits) Make roomData global Add itemData and playerData to roomData so it's all easy to access --- gameMod.js | 25 +++++++++++++++++++++++++ public/board.js | 2 ++ public/index.html | 4 ++++ public/main.js | 11 +++++++++++ roomMod.js | 15 +++++++++++---- rooms.js | 14 +++++--------- server.js | 18 +++++++++++++++++- 7 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 gameMod.js diff --git a/gameMod.js b/gameMod.js new file mode 100644 index 0000000..6d94736 --- /dev/null +++ b/gameMod.js @@ -0,0 +1,25 @@ +// For anything related to the actual game itself (kinda) +// this will be split into different bits, but should be what manages a rooms +// game states, and alladat +// Basically here to prevent circular dependencies (where I can) + + +// PlayerId is using array 0,1,2 for now, not the actual id +// actual Id would be better, but the player should be passed correctly +// from the client. They can edit data, but server-side validation SHOULD prevent +// in the future +function passTurn(roomId, playerId){ + + // Check playerId (not correct atm) before doing the stuff, to verify the user + // IS the user, and in the room + + // Test alert so that different data/emits can be sent per-player + global.socketAlert(roomData[roomId].playerData[0].socketId, 'Pass', 'alert'); + +} + + +module.exports = { + passTurn +}; + diff --git a/public/board.js b/public/board.js index cd51410..5286186 100644 --- a/public/board.js +++ b/public/board.js @@ -42,6 +42,7 @@ let cardColours = {}; let cardManaColour = {}; let cardEffect = {}; let inEvent = null; +let roomId = null; // To disable drawing each time something changes let drawEachEvent = true; // For disabling draw each time and only occuring where I want to test @@ -1298,6 +1299,7 @@ function loadBoard(data) { cardColours = data.cardColours; cardManaColour = data.cardManaColour; cardEffect = data.cardEffect; + roomId = data.roomId; // Passives flight = data.flight; diff --git a/public/index.html b/public/index.html index 83a167a..c90e939 100644 --- a/public/index.html +++ b/public/index.html @@ -17,6 +17,10 @@ +
+ +

+
diff --git a/public/main.js b/public/main.js index 026e162..7ebcf74 100644 --- a/public/main.js +++ b/public/main.js @@ -182,8 +182,19 @@ socket.on('responseStartGame', function (data) { loadBoard(data.message); }); +// ALERTS socket.on('alert', function (data) { console.log('<< alert'); alert(data.message); }); +// GAME SERVER STUFF +// PASS TURN +function passTurn(){ + // TODO: Use itemData.player/itemData.room or whatnot here + console.log(roomId); + let playerId = prompt('PlayerId 0/1', 0); // TODO: very temp, will be playerId/arrayId + console.log('+ passTurn'); + socket.emit('passTurn', roomId, playerId); +} + diff --git a/roomMod.js b/roomMod.js index c126695..f2bba43 100644 --- a/roomMod.js +++ b/roomMod.js @@ -62,11 +62,8 @@ function roomGeneration(roomId){ i++; } - // Test alert so that different data/emits can be sent per-player - global.io.to(playerData[0].socketId).emit('alert', {'message': 'roomMod.js test'}); - - // Add players for the room (seperate to playerData, currently what's used) + // ?? itemData = setPlayers(playerData, itemData); // TODO: Get their selected decks @@ -82,11 +79,21 @@ function roomGeneration(roomId){ itemData.turn = 0; // The turn count of the match itemData.playersTurn = 0; // This means it's playerData[0] turn + // Room has just been created, so add the itemData and playerData to the room + // on server-side so it's easily accessible + roomData[roomId].itemData = itemData; + roomData[roomId].playerData = playerData; + + // Return the all the itemData to the client(s) + // TODO: This will need to give different data for each, or at least + // to differ the boardside return resolve(itemData); })() }); } +// TODO: disconnect, reconnect, resume + module.exports = { startItemCount ,setPlayers diff --git a/rooms.js b/rooms.js index 3233d76..10e51e5 100644 --- a/rooms.js +++ b/rooms.js @@ -1,9 +1,5 @@ const roomMod = require('./roomMod'); -// Data for rooms only atm -let data = {}; -let roomData = {}; - // Variables for server overall let numRooms = 0; const maxRooms = 3; @@ -24,7 +20,7 @@ function getRooms(filter = 'all', dump = false){ console.log('+ getRooms'); let response = { random: 'randomStuff', - roomData: roomData, + roomData: global.roomData, }; if(dump){ @@ -86,7 +82,7 @@ function createRoom(roomId = false, dump = true){ room['people'] = 0; room['playerIds'] = {}; - roomData[roomId] = room; + global.roomData[roomId] = room; numRooms = numRooms + 1; response = { @@ -106,7 +102,7 @@ function requestJoinRoom(socket, playerName, roomId){ console.log('+ requestJoinRoom recieved'); - let room = roomData[roomId]; + let room = global.roomData[roomId]; // https://stackoverflow.com/a/18096649 socket.playerId = playerName; @@ -155,7 +151,7 @@ function requestJoinRoom(socket, playerName, roomId){ // Will need to be different to playerName in future (in case dupes) // would use playerId TODO function isUserInRoom(playerName, roomId){ - if(playerName in roomData[roomId]['playerIds']){ + if(playerName in global.roomData[roomId]['playerIds']){ return true; } return false; @@ -164,7 +160,7 @@ function isUserInRoom(playerName, roomId){ function startGame(roomId){ console.log('>> Room: ' + roomId + ': Requesting game...'); - let people = roomData[roomId].players; + let people = global.roomData[roomId].players; /* try { //people = io.sockets.adapter.rooms.get(roomId).size; diff --git a/server.js b/server.js index 7e875d3..255de91 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const express = require('express'); const database = require('./database'); const rooms = require('./rooms'); +const gameMod = require('./gameMod'); const app = express(); const http = require('http').Server(app); @@ -11,7 +12,9 @@ global.io = io; // To log the player sockets, so they can be easily referred to // maybe jank, but can't see alternative currently -global.playerSocket = {}; +global.playerSocket = {}; +global.roomData = {}; // Made global for now, as to not replicate. Maybe sub-optimal? + // util is what nodejs uses for console.log, but has a depth of 2 set // so console.logs show [Array]/[Object] instead of useful info. @@ -45,6 +48,19 @@ function onConnection(socket){ }); // Game (actual things relating to the game) + // The socket should only be in one game, so socket.on should + // do this, but passing room/player anyways as it's how I've written some + // roomData bits. TODO: Look if I can do this better... + socket.on('passTurn', function(roomId, playerId) { + gameMod.passTurn(roomId, playerId); + }); + +} +// Globals for easier clientside alerts/logs, etc. +global.socketAlert = function(socket, message, type = 'alert'){ + global.io.to(socket).emit( + type, {'message': message} + ); } From 35330e7850e7a8bb7c8d884b062ed495ffa38ce5 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 22:08:40 +0000 Subject: [PATCH 8/8] Add playerId/Order in a jamjarred kinda way --- roomMod.js | 10 +++++++++- rooms.js | 51 +++++++++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/roomMod.js b/roomMod.js index f2bba43..be7b19f 100644 --- a/roomMod.js +++ b/roomMod.js @@ -48,20 +48,27 @@ function roomGeneration(roomId){ // Player data with Sockets let playerData = []; + let playerOrder = {}; // Need a better name let i = 1; let clients = global.io.sockets.adapter.rooms.get(roomId); for (const clientId of clients) { const clientSocket = global.io.sockets.sockets.get(clientId); + // Which order the player is? Just using the array + playerOrder[clientSocket.playerId] = playerData.length; + playerData.push({ - 'playerId': clientSocket.playerId + 'playerDataId': playerData.length + ,'playerId': clientSocket.playerId ,'deck':{'playerId':i,'deckId':1} ,'socketId': clientSocket.id // TODO: ONLY FOR SERVERSIDE!!! }); i++; + } + // Add players for the room (seperate to playerData, currently what's used) // ?? itemData = setPlayers(playerData, itemData); @@ -83,6 +90,7 @@ function roomGeneration(roomId){ // on server-side so it's easily accessible roomData[roomId].itemData = itemData; roomData[roomId].playerData = playerData; + roomData[roomId].playerOrder = playerOrder; // Return the all the itemData to the client(s) // TODO: This will need to give different data for each, or at least diff --git a/rooms.js b/rooms.js index 10e51e5..7f3bfa5 100644 --- a/rooms.js +++ b/rooms.js @@ -157,7 +157,7 @@ function isUserInRoom(playerName, roomId){ return false; } -function startGame(roomId){ +async function startGame(roomId){ console.log('>> Room: ' + roomId + ': Requesting game...'); let people = global.roomData[roomId].players; @@ -189,27 +189,34 @@ function startGame(roomId){ // ideally only returning the items that the user can/should // see i.e. shouldn't give them the inDeck card list just a counter // shouldn't have opponent card data/their hand shouldn't be flipped - roomMod.roomGeneration(roomId).then(data => { - response.success = true; - response.message = data; - // Each player then gets the roomGeneration stuff - for (const clientId of clients) { - const clientSocket = global.io.sockets.sockets.get(clientId); - console.log('>> responseStartGame: '+clientSocket.playerId); - // Emit to client socket - global.io.to(clientSocket.id).emit('responseStartGame', response); - } - - }) - .catch(err => { - response.message = err; - // Each player then gets the error message - for (const clientId of clients) { - const clientSocket = global.io.sockets.sockets.get(clientId); - // Emit to client socket - global.io.to(clientSocket.id).emit('responseStartGame', err); - } - }); + + // Not sure how to catch errors for these await alls + // TODO: Look into error handling for await alls + const [itemData] = + await Promise.all([ + roomMod.roomGeneration(roomId), + ]); + + + // data is the 'itemData' not all the roomData + response.success = true; + response.message = itemData; + + // Each player then gets sent the roomGeneration stuff + for (const clientId of clients) { + + const clientSocket = global.io.sockets.sockets.get(clientId); + console.log('>> responseStartGame: '+clientSocket.playerId); + + // TODO: TESTING STUFF, REMOVE WHEN SORTED + let message = 'You are player: '+roomData[roomId].playerOrder[clientSocket.playerId]; + global.socketAlert(clientSocket.id, message, 'alert'); + + // Emit the itemData to client socket (TODO: only emit what each player should see/recieve) + global.io.to(clientSocket.id).emit('responseStartGame', response); + + } + }