|
|
|
@ -10,6 +10,40 @@ const gameHelper = require('./gameHelper');
|
|
|
|
// actual Id would be better, but the player should be passed correctly
|
|
|
|
// 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
|
|
|
|
// from the client. They can edit data, but server-side validation SHOULD prevent
|
|
|
|
// in the future
|
|
|
|
// in the future
|
|
|
|
|
|
|
|
function gameStart(roomId){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Each player shuffles
|
|
|
|
|
|
|
|
for(const player of roomData[roomId].playerData){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Make sure this only does for players, not spectators (in fut.)
|
|
|
|
|
|
|
|
shuffleDeck(roomId, player.playerDataId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Each player plays X shield
|
|
|
|
|
|
|
|
for(const player of roomData[roomId].playerData){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Make sure this only does for players, not spectators (in fut.)
|
|
|
|
|
|
|
|
// If shieldCount is less than the 'most' shield at start of game
|
|
|
|
|
|
|
|
for(let shieldCount = 0; shieldCount < 4; shieldCount++){
|
|
|
|
|
|
|
|
playShield(roomId, player.playerDataId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Each player draws X cards to hand
|
|
|
|
|
|
|
|
for(const player of roomData[roomId].playerData){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Make sure this only does for players, not spectators (in fut.)
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
for(let handCount = 0; handCount < 1; handCount++){
|
|
|
|
|
|
|
|
drawACard(roomId, player.playerDataId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function passTurn(roomId, playerId){
|
|
|
|
function passTurn(roomId, playerId){
|
|
|
|
|
|
|
|
|
|
|
|
// TODO:Check playerId and roomId before doing the stuff, to verify the user
|
|
|
|
// TODO:Check playerId and roomId before doing the stuff, to verify the user
|
|
|
|
@ -45,6 +79,49 @@ function passTurn(roomId, playerId){
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function playShield(roomId, playerId){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(global.roomData[roomId].itemData.component.shield[playerId] >= 2){
|
|
|
|
|
|
|
|
global.socketAlert(roomData[roomId].playerData[playerId].socketId, 'Shield full; cannot play shield', 'alert');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Change position to last position available in shield zone
|
|
|
|
|
|
|
|
let fromPosition = global.roomData[roomId].itemData.component.cardCount.deck[playerId]; // 'top' of deck
|
|
|
|
|
|
|
|
let toPosition = global.roomData[roomId].itemData.component.cardCount.shield[playerId]+1; // newest shield pos.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: This is essential the same as in drawACard() so should be normalised into a function
|
|
|
|
|
|
|
|
// 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 shield
|
|
|
|
|
|
|
|
gameHelper.setCardPosition(roomId, playerId, key, toPosition, global.roomData[roomId].itemData.component.shield, 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 shield size by 1
|
|
|
|
|
|
|
|
global.roomData[roomId].itemData.component.cardCount.shield[playerId]++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Then emit the deckSize and hand size to all the player's sockets
|
|
|
|
|
|
|
|
global.socketResponsePlayedShield(roomId, playerId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function drawACard(roomId, playerId){
|
|
|
|
function drawACard(roomId, playerId){
|
|
|
|
|
|
|
|
|
|
|
|
if(global.roomData[roomId].itemData.component.cardCount.hand[playerId] >= 2){
|
|
|
|
if(global.roomData[roomId].itemData.component.cardCount.hand[playerId] >= 2){
|
|
|
|
@ -103,8 +180,11 @@ function drawACard(roomId, playerId){
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function playManaFromHand(roomId, playerId, position){
|
|
|
|
|
|
|
|
playFromHand(roomId, playerId, position, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
// TODO: Rename and rejig the 3 play from hand functions
|
|
|
|
// TODO: Rename and rejig the 3 play from hand functions
|
|
|
|
function playFromHand(roomId, playerId, position){
|
|
|
|
function playFromHand(roomId, playerId, position, mana = false){
|
|
|
|
|
|
|
|
|
|
|
|
let cardId = null;
|
|
|
|
let cardId = null;
|
|
|
|
// Get the cardId of the card from position within players hand
|
|
|
|
// Get the cardId of the card from position within players hand
|
|
|
|
@ -129,21 +209,28 @@ function playFromHand(roomId, playerId, position){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Attempt to play the card from hand
|
|
|
|
// Attempt to play the card from hand
|
|
|
|
if(!playACardFromHand(roomId, playerId, cardId)){
|
|
|
|
if(!playACardFromHand(roomId, playerId, cardId, mana)){
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function playACardFromHand(roomId, playerId, cardId){
|
|
|
|
function playACardFromHand(roomId, playerId, cardId, mana = false){
|
|
|
|
|
|
|
|
|
|
|
|
// Add the card to field (and its effect to 'stack') or spell to the 'stack'
|
|
|
|
// Add the card to field (and its effect to 'stack') or spell to the 'stack'
|
|
|
|
if(playACard(roomId, playerId, cardId, 'hand') !== true){
|
|
|
|
if(playACard(roomId, playerId, cardId, 'hand', mana) !== true){
|
|
|
|
// TODO: Return socket to player about 'illegal move' and why
|
|
|
|
// TODO: Return socket to player about 'illegal move' and why
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(mana){
|
|
|
|
|
|
|
|
global.socketResponsePlayManaFromHand(roomId, playerId, cardId);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Maybe update with 'location played to' so animations, draws, etc. are correct
|
|
|
|
global.socketResponsePlayFromHand(roomId, playerId, cardId);
|
|
|
|
global.socketResponsePlayFromHand(roomId, playerId, cardId);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Above can probably be the same/similar to what is added to the roomData on
|
|
|
|
// TODO: Above can probably be the same/similar to what is added to the roomData on
|
|
|
|
// client-end (when it's fully done)
|
|
|
|
// client-end (when it's fully done)
|
|
|
|
|
|
|
|
|
|
|
|
@ -154,7 +241,13 @@ function playACardFromHand(roomId, playerId, cardId){
|
|
|
|
// 'Play' a card is activation with cost (triggering play events)
|
|
|
|
// 'Play' a card is activation with cost (triggering play events)
|
|
|
|
// 'Summon' puts it onto the board without play events
|
|
|
|
// 'Summon' puts it onto the board without play events
|
|
|
|
// yada yada to think about in future
|
|
|
|
// yada yada to think about in future
|
|
|
|
function playACard(roomId, playerId, cardId, playedFrom){
|
|
|
|
function playACard(roomId, playerId, cardId, playedFrom, mana = false){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Play a mana (a card that was played as mana, loses it's normal card data)
|
|
|
|
|
|
|
|
if(mana){
|
|
|
|
|
|
|
|
// Play to board. If there's a 'onPlay' effect, add that to the 'stack'
|
|
|
|
|
|
|
|
return playMana(roomId, playerId, cardId, playedFrom);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Play a unit
|
|
|
|
// Play a unit
|
|
|
|
if(cardId in global.roomData[roomId].itemData.component.type.unit){
|
|
|
|
if(cardId in global.roomData[roomId].itemData.component.type.unit){
|
|
|
|
@ -256,6 +349,40 @@ function playAToken(roomId, playerId, cardId, playedFrom){
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function playMana(roomId, playerId, cardId, playedFrom){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('playMana');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Check if mana can be played (default 10 max)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Check if a mana has already been played this turn (default 1 per turn per player)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(playedFrom == 'hand'){
|
|
|
|
|
|
|
|
// Remove from hand
|
|
|
|
|
|
|
|
removeFromHand(roomId, playerId, cardId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add card to mana zone
|
|
|
|
|
|
|
|
global.roomData[roomId].itemData.component.mana[cardId] = cardId;
|
|
|
|
|
|
|
|
global.roomData[roomId].itemData.component.cardCount.mana[playerId]++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(global.roomData[roomId].itemData.component.mana);
|
|
|
|
|
|
|
|
console.log(global.roomData[roomId].itemData.component.cardCount.mana[playerId]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Change list positions of hand and mana
|
|
|
|
|
|
|
|
gameHelper.setCardPosition(roomId, playerId, cardId
|
|
|
|
|
|
|
|
, global.roomData[roomId].itemData.component.cardCount.mana[playerId]
|
|
|
|
|
|
|
|
, global.roomData[roomId].itemData.component.mana
|
|
|
|
|
|
|
|
, global.roomData[roomId].itemData.component.listPosition[cardId]
|
|
|
|
|
|
|
|
, global.roomData[roomId].itemData.component.hand
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mana has been played
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Not 100% sure how to implement the stack
|
|
|
|
// Not 100% sure how to implement the stack
|
|
|
|
// TODO: Make it better
|
|
|
|
// TODO: Make it better
|
|
|
|
@ -460,6 +587,8 @@ module.exports = {
|
|
|
|
,drawACard
|
|
|
|
,drawACard
|
|
|
|
,shuffleDeck
|
|
|
|
,shuffleDeck
|
|
|
|
,playFromHand
|
|
|
|
,playFromHand
|
|
|
|
|
|
|
|
,playManaFromHand
|
|
|
|
,acceptResolveStack
|
|
|
|
,acceptResolveStack
|
|
|
|
|
|
|
|
,gameStart
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|