@ -1,3 +1,5 @@
const gameHelper = require ( './gameHelper' ) ;
// For anything related to the actual game itself (kinda)
// For anything related to the actual game itself (kinda)
// this will be split into different bits, but should be what manages a rooms
// this will be split into different bits, but should be what manages a rooms
// game states, and alladat
// game states, and alladat
@ -17,7 +19,7 @@ function passTurn(roomId, playerId){
//global.socketAlert(roomData[roomId].playerData[playerId].socketId, playerTurn, 'log');
//global.socketAlert(roomData[roomId].playerData[playerId].socketId, playerTurn, 'log');
if ( playerTurn != playerId ) {
if ( playerTurn != playerId ) {
global . socketAlert ( roomData[ roomId ] . playerData [ playerId ] . socketId , 'Not your turn' , 'alert' ) ;
global . socketAlert ( global. getPlayerSocketFromRoom ( playerId , roomId ) , 'Not your turn' , 'alert' ) ;
return false ;
return false ;
} ;
} ;
@ -32,31 +34,15 @@ function passTurn(roomId, playerId){
}
}
// Send turn data to each player
// Send turn data to each player
// Probably via the sockets in room (as could be spectators in future)
global . socketResponsePassTurn ( roomId ) ;
let clients = global . io . sockets . adapter . rooms . get ( roomId ) ;
for ( const clientId of clients ) {
const clientSocket = global . io . sockets . sockets . get ( clientId ) ;
console . log ( '>> ' + clientSocket . playerId + ' passed turn' ) ;
// Send the data back
let turnData = {
'playerTurn' : newPlayerTurn ,
'turn' : global . roomData [ roomId ] . itemData . component . turn ,
} ;
global . io . to ( clientSocket . id ) . emit ( 'responsePassTurn' , turnData ) ;
}
// Let the player know it's their turn via alert too (in case tabbed out)
// Let the player know it's their turn via alert too (in case tabbed out)
// TODO: This could probably be done front-end from the newPlayerTurn in socketResponsePassTurn
global . socketAlert ( roomData [ roomId ] . playerData [ newPlayerTurn ] . socketId , 'Your turn' , 'alert' ) ;
global . socketAlert ( roomData [ roomId ] . playerData [ newPlayerTurn ] . socketId , 'Your turn' , 'alert' ) ;
// Start of the new players turn, draw a card
// Start of the new players turn, draw a card
drawACard ( roomId , newPlayerTurn ) ;
drawACard ( roomId , newPlayerTurn ) ;
}
}
function drawACard ( roomId , playerId ) {
function drawACard ( roomId , playerId ) {
@ -65,49 +51,104 @@ function drawACard(roomId, playerId){
global . socketAlert ( roomData [ roomId ] . playerData [ playerId ] . socketId , 'Hand full; cannot draw card' , 'alert' ) ;
global . socketAlert ( roomData [ roomId ] . playerData [ playerId ] . socketId , 'Hand full; cannot draw card' , 'alert' ) ;
return false ;
return false ;
}
}
if ( global . roomData [ roomId ] . itemData . component . cardCount . deck [ playerId ] <= 0 ) {
global . socketAlert ( roomData [ roomId ] . playerData [ playerId ] . socketId , 'Deck empty; cannot draw card' , 'alert' ) ;
return false ;
}
// TODO: Check no card event/trigger occured that prevents/change draw card
// Change position to last position available in hand
// Change position to last position available in hand
let fromPosition = global . roomData [ roomId ] . itemData . component . cardCount . deck [ playerId ] ; // 'Bottom' of deck
let fromPosition = global . roomData [ roomId ] . itemData . component . cardCount . deck [ playerId ] ; // ' top ' of deck
let toPosition = global . roomData [ roomId ] . itemData . component . cardCount . hand [ playerId ] ; // Rightmost hand pos
let toPosition = global . roomData [ roomId ] . itemData . component . cardCount . hand [ playerId ] + 1 ; // Rightmost hand pos (starting at 1)
// ECSey att2, there's surely a better way of getting playerX top card within inDeck?
// Tried unions but it messes up the object data. Maybe need to have no data in each object
// and have it literally just be keys?
// from inDeck to hand
// Get each card from the deck
// change the listPositions of both (only return listPosition of hand ids?)
for ( const [ key , value ] of Object . entries ( global . roomData [ roomId ] . itemData . component . inDeck ) ) {
// Key is the entity here
// If the card inDeck does not belongs to the player, skip over it
if ( global . roomData [ roomId ] . itemData . component . player [ key ] != playerId ) {
continue ;
}
// If the card isn't the last (bottom) card of deck, skip over it
// TODO: -1 is jank, sort so listPositions all start from 1..x
if ( global . roomData [ roomId ] . itemData . component . listPosition [ key ] != fromPosition ) {
continue ;
}
// The main man
// Move positions in hand/deck, and put the item from the deck into the hand
gameHelper . setCardPosition ( roomId , playerId , key , toPosition , global . roomData [ roomId ] . itemData . component . hand , fromPosition , global . roomData [ roomId ] . itemData . component . inDeck ) ;
}
// Reduce deckSize by 1 for the player that drew
// Reduce deckSize by 1 for the player that drew
global . roomData [ roomId ] . itemData . component . cardCount . deck [ playerId ] -- ;
global . roomData [ roomId ] . itemData . component . cardCount . deck [ playerId ] -- ;
// And increase the hand size by 1
// And increase the hand size by 1
global . roomData [ roomId ] . itemData . component . cardCount . hand [ playerId ] ++ ;
global . roomData [ roomId ] . itemData . component . cardCount . hand [ playerId ] ++ ;
// TODO: Move card from deck to hand for the player. Dupe what I've got in board for
// Then emit the deckSize and hand size to all the player's sockets
// this, but tidy it up
global . socketResponseDrawCard ( roomId , playerId ) ;
//global.roomData[roomId].itemData.component.inDeck = newPlayerTurn;
//global.roomData[roomId].itemData.component.hand = newPlayerTurn;
// Emit the 'hand' and related cardData for cards in the players hand
// Could merge this with the top?
global . socketResponsePlayerDrewCard ( roomId , playerId ) ;
}
// Then emit the deckSize and hand size to all the player's sockets
let clients = global . io . sockets . adapter . rooms . get ( roomId ) ;
for ( const clientId of clients ) {
const clientSocket = global . io . sockets . sockets . get ( clientId ) ;
// DATA RETURNER DUDES
console . log ( '>> ' + clientSocket . playerId + ' drew card' ) ;
// TODO: Where to put this? Kind of a helper, kind of functionality. Hmmmmm
// maybe do a dataHelper? then anything to return data can be included there?
// TODO: May get all the data from hand, board, grave, etc. in functions
// like this, then union all the data and return that in one swoomp
// Probably better to just get all the keys from the boardlemenets and do
// the loop once though...
function getPlayerHandData ( roomId , playerId ) {
// Send the data back
let handEntities = { } ;
global . io . to ( clientSocket . id ) . emit (
let handPositions = { } ;
'responseDrawCard'
let handCardData = { } ;
, global . roomData [ roomId ] . itemData . component . cardCount
) ;
for ( const [ key , value ] of Object . entries ( global . roomData [ roomId ] . itemData . component . hand ) ) {
// Key is entity ID here
// If the entity in hand belongs to the player, then they are allowed its data
if ( global . roomData [ roomId ] . itemData . component . player [ key ] == playerId ) {
// Get entity of items in the hand
handEntities [ key ] = global . roomData [ roomId ] . itemData . component . hand [ key ] ;
// Get listPosition of just items in the hand
handPositions [ key ] = global . roomData [ roomId ] . itemData . component . listPosition [ key ] ;
// Same for cardData
handCardData [ key ] = global . roomData [ roomId ] . itemData . component . cardData [ key ] ; // TODO: Nothing on client side?
// Leaving other bits for now
}
}
// Emit the 'hand' and related cardData for cards in the players hand
}
return {
'handEntities' : handEntities ,
'handPositions' : handPositions ,
'handCardData' : handCardData
} ;
}
}
module . exports = {
module . exports = {
passTurn
passTurn
, getPlayerHandData
, drawACard
} ;
} ;