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
develop^2
Nathan Steel 1 year ago
parent 16e307c32c
commit ea7c9b8596

@ -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
};

@ -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;

@ -17,6 +17,10 @@
</div>
<canvas id="canvas" width="1000" height="600"></canvas>
<hr>
<button onclick="passTurn()">Pass Turn</button>
<br><br>
<hr>
<button onclick="untapAllZones()">Untap all</button>
<button onclick="echoCards()">Print cardlist to console</button>

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

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

@ -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;

@ -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);
@ -12,6 +13,8 @@ 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.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}
);
}

Loading…
Cancel
Save