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} + ); }