You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
5.5 KiB
JavaScript
155 lines
5.5 KiB
JavaScript
const gameHelper = require('./gameHelper');
|
|
|
|
// 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){
|
|
|
|
// TODO:Check playerId and roomId before doing the stuff, to verify the user
|
|
// IS the user, and in the room
|
|
|
|
let playerTurn = global.roomData[roomId].itemData.component.playerTurn;
|
|
//global.socketAlert(roomData[roomId].playerData[playerId].socketId, playerTurn, 'log');
|
|
|
|
if(playerTurn != playerId){
|
|
global.socketAlert(global.getPlayerSocketFromRoom(playerId, roomId), 'Not your turn', 'alert');
|
|
return false;
|
|
};
|
|
|
|
// Turns are 0,1,0,1 at the mo, no coinflip or re-order, etc. so this JANK is ok for now
|
|
// %2 as 2 players and Ids are 0 and 1 so it works
|
|
let newPlayerTurn = (playerTurn + 1)%maxPlayersPerRoom;
|
|
global.roomData[roomId].itemData.component.playerTurn = newPlayerTurn;
|
|
|
|
// If it's back to the player that went first, the turn count increased too
|
|
if(playerTurn == 0){
|
|
global.roomData[roomId].itemData.component.turn++;
|
|
}
|
|
|
|
// Send turn data to each player
|
|
global.socketResponsePassTurn(roomId);
|
|
|
|
// 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');
|
|
|
|
// Start of the new players turn, draw a card
|
|
drawACard(roomId, newPlayerTurn);
|
|
|
|
}
|
|
|
|
function drawACard(roomId, playerId){
|
|
|
|
if(global.roomData[roomId].itemData.component.cardCount.hand[playerId] >= 2){
|
|
global.socketAlert(roomData[roomId].playerData[playerId].socketId, 'Hand full; cannot draw card', 'alert');
|
|
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
|
|
let fromPosition = global.roomData[roomId].itemData.component.cardCount.deck[playerId]; // 'top' of deck
|
|
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?
|
|
|
|
// Get each card from the deck
|
|
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
|
|
global.roomData[roomId].itemData.component.cardCount.deck[playerId]--;
|
|
// And increase the hand size by 1
|
|
global.roomData[roomId].itemData.component.cardCount.hand[playerId]++;
|
|
|
|
// Then emit the deckSize and hand size to all the player's sockets
|
|
global.socketResponseDrawCard(roomId, playerId);
|
|
|
|
// Emit the 'hand' and related cardData for cards in the players hand
|
|
// Could merge this with the top?
|
|
global.socketResponsePlayerDrewCard(roomId, playerId);
|
|
|
|
}
|
|
|
|
|
|
// DATA RETURNER DUDES
|
|
// 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){
|
|
|
|
let handEntities = {};
|
|
let handPositions = {};
|
|
let handCardData = {};
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
'handEntities': handEntities,
|
|
'handPositions': handPositions,
|
|
'handCardData': handCardData
|
|
};
|
|
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
passTurn
|
|
,getPlayerHandData
|
|
,drawACard
|
|
};
|
|
|