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.
356 lines
12 KiB
JavaScript
356 lines
12 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);
|
|
|
|
}
|
|
|
|
// TODO: Rename and rejig the 3 play from hand functions
|
|
function playFromHand(roomId, playerId, position){
|
|
|
|
let cardId = null;
|
|
// Get the cardId of the card from position within players hand
|
|
for (const [key, value] of Object.entries(global.roomData[roomId].itemData.component.hand)) {
|
|
// Key is the entity here
|
|
|
|
// If the card in hand's position does not match the position passed, skip it
|
|
if(global.roomData[roomId].itemData.component.listPosition[key] != position){
|
|
continue;
|
|
}
|
|
|
|
cardId = key;
|
|
break;
|
|
|
|
}
|
|
|
|
// Then play the card
|
|
if(cardId == null){
|
|
// TODO: Respond to player (who triggered play) that this
|
|
// is an 'illegal/errored' move via socket
|
|
return false;
|
|
}
|
|
|
|
playACardFromHand(roomId, playerId, cardId);
|
|
|
|
}
|
|
|
|
function playACardFromHand(roomId, playerId, cardId){
|
|
|
|
// Add the card to field (and its effect to 'stack') or spell to the 'stack'
|
|
if(playACard(roomId, playerId, cardId, 'hand') !== true){
|
|
// TODO: Return socket to player about 'illegal move' and why
|
|
return false;
|
|
}
|
|
|
|
// If the card can be played/had been
|
|
// Reduce handSize by 1 for the player that played the card
|
|
global.roomData[roomId].itemData.component.cardCount.hand[playerId]--;
|
|
|
|
|
|
// TODO: Send a socket response for 'played a card from hand' with cardData
|
|
// of said card. This is sent so that it can be 'chained' and added to the stack
|
|
// On play, it is put onto field/activated but the effect(s) don't occur until both/all
|
|
// players have a chance to 'chain', once the 'chain' is completed, the effect(s) trigger
|
|
global.socketResponsePlayFromHand(roomId, playerId, cardId);
|
|
|
|
|
|
|
|
}
|
|
|
|
// 'Play' a card is activation with cost (triggering play events)
|
|
// 'Summon' puts it onto the board without play events
|
|
// yada yada to think about in future
|
|
function playACard(roomId, playerId, cardId, playedFrom){
|
|
|
|
// Play a unit
|
|
if(cardId in global.roomData[roomId].itemData.component.type.unit){
|
|
// Play to board. If there's a 'onPlay' effect, add that to the 'stack'
|
|
return playAUnit(roomId, playerId, cardId, playedFrom);
|
|
}
|
|
|
|
// Cast a spell
|
|
if(cardId in global.roomData[roomId].itemData.component.type.spell){
|
|
// Add the card/effect onto the 'stack'. When complete/cancelled send to grave
|
|
return playASpell(roomId, playerId, cardId, playedFrom);
|
|
}
|
|
|
|
// Add a token
|
|
if(cardId in global.roomData[roomId].itemData.component.type.token){
|
|
// ???? Tokens maybe just onto other cards as 'equips' or as standalones? IDK
|
|
return playAToken(roomId, playerId, cardId, playedFrom);
|
|
}
|
|
|
|
}
|
|
|
|
function playAUnit(roomId, playerId, cardId, playedFrom){
|
|
|
|
console.log('playAUnit');
|
|
|
|
// TODO: Costs
|
|
|
|
}
|
|
function playASpell(roomId, playerId, cardId, playedFrom){
|
|
|
|
console.log('playASpell');
|
|
|
|
// TODO: Pay costs (Need to play mana first...)
|
|
|
|
// TODO: If spell has different effects, select which one/ensure
|
|
// correct one is used based on criteria
|
|
|
|
// Add to stack (each part of the spells effect)
|
|
// TODO: Use actual effects, for now just adding a 'drawCard' for testing
|
|
|
|
addToStack(roomId, playerId, cardId, null);
|
|
|
|
}
|
|
function playAToken(roomId, playerId, cardId, playedFrom){
|
|
|
|
}
|
|
|
|
// Not 100% sure how to implement the stack
|
|
// TODO: Make it better
|
|
// TODO: Make it do actual things, currently just adds 'drawCard' effect
|
|
function addToStack(roomId, playerId, cardId, effectId){
|
|
|
|
// Stack does its own effect, or 'counter' etc. the card prior to it on the stack
|
|
// etc. etc.
|
|
// TODO: Add card effect to stack in reverse order OR have the stack work from x..0
|
|
// prolly the latter, makes sense to me
|
|
|
|
let stack = global.roomData[roomId].itemData.component.stack;
|
|
let stackLength = Object.keys(stack).length;
|
|
|
|
// TODO: First ensure the cardEffects are added in their step order 1..x
|
|
// Add as next event in the stack 1..x
|
|
// TODO: Use actual effect, not just 'draw' as that's just for testing
|
|
global.roomData[roomId].itemData.component.stack[stackLength + 1] =
|
|
{
|
|
'cardId': cardId
|
|
,'effect': null
|
|
,'effectStep': null
|
|
,'targetCard': null
|
|
,'targetPlayer': playerId
|
|
};
|
|
|
|
console.log(global.roomData[roomId].itemData.component.stack);
|
|
|
|
// TODO: TEMP, this will need to wait for a 'resolve' accept from both players before the stack
|
|
// would trigger.
|
|
resolveStack(roomId);
|
|
|
|
}
|
|
|
|
function resolveStack(roomId){
|
|
|
|
// Does the next effect in the stack, if something
|
|
// is to chain onto the stack that would instead trigger
|
|
// 'addToStack' after paying any costs
|
|
|
|
// If there is anything in the stack
|
|
let stackLength = Object.keys(global.roomData[roomId].itemData.component.stack).length;
|
|
if(stackLength > 0){
|
|
|
|
// Trigger the last (most recently added to) the stack effect
|
|
// THIS WILL NOW ACTUALLY CAST THE EFFECT STEP WITHOUT INTERRUPT
|
|
// While the stack is being resolved their are no counters/chains until
|
|
// the next stack action which players will get option to chain or not again
|
|
|
|
console.log(
|
|
global.roomData[roomId].itemData.component.stack[stackLength]
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
|
,playFromHand
|
|
};
|
|
|