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.
cardGame/gameMod.js

196 lines
6.9 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);
}
// Shuffle the deck 'randomly' for a certain player
function shuffleDeck(roomId, playerId){
// Create a tempDeck array of same length of the player deck
let deckLength = global.roomData[roomId].itemData.component.cardCount.deck[playerId];
let tempDeck = Array.from(Array(deckLength).keys());
// Loop the tempDeck and shuffle
// https://stackoverflow.com/a/73603221
for(let i = 0; i < deckLength; i++){
// picks the random number between 0 and length of the deck (-1 so 0..34)
let shuffle = Math.floor(Math.random() * (tempDeck.length - 1));
// swap the current listPosition with a random one with the deck count
[ tempDeck[i], tempDeck[shuffle] ] = [ tempDeck[shuffle], tempDeck[i] ];
}
let tempDeckItem = 0;
// Now change the related inDeck entities listPositions to match the random number from tempDeck
for (const [key, value] of Object.entries(global.roomData[roomId].itemData.component.inDeck)) {
if(global.roomData[roomId].itemData.component.player[key] != playerId){
continue;
}
// Add the tempDeckItems number as the listPosition for the inDeck item
// + 1 as listPositions start at 1
global.roomData[roomId].itemData.component.listPosition[key] = tempDeck[tempDeckItem] + 1;
// Move to next tempDeckItem
tempDeckItem++;
}
global.socketResponseShuffleDeck(roomId, playerId, true);
}
// 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
,shuffleDeck
};