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/gameHelper.js

93 lines
2.5 KiB
JavaScript

// Moves card positions down/up for the old/new element for player in room
function setCardPosition(roomId, player, card, newPosition, newElement, oldPosition, oldElement){
// Move anything in the old boardElement after the old listPosition down by one
moveElementPositions(roomId, player, 0, oldElement, oldPosition);
// Move anything in the new boardElement after (including) the new listPosition up by one
moveElementPositions(roomId, player, 1, newElement, newPosition);
// Then fit the card into the new gap that's opened up
listPosition[card] = newPosition;
// Remove from oldElement
delete oldElement[card]
// Add to newElement
newElement[card] = newPosition;
}
// direction 0 up, 1 down
function moveElementPositions(roomId, player, direction, element, position){
position = position;
listPosition = global.roomData[roomId].itemData.component.listPosition;
for (const [key, value] of Object.entries(element)) {
if(global.roomData[roomId].itemData.component.player[key] != player){
continue;
}
// Move down from (not including) the position passed
if(direction == 0 && listPosition[key] > position){
listPosition[key]--;
}
// Move everything from (including) the new position up
if(direction == 1 && listPosition[key] >= position){
console.log('hit');
console.log(listPosition[key]);
console.log(position);
listPosition[key]++;
}
}
}
function canAttack(roomId, playerId, cardId){
if(cardId in global.roomData[roomId].itemData.component.cardStatus.tapped){
return false;
}
return true;
}
function getTargetableCards(roomId, playerId, cardId){
// i.e. If no flight/reach, you cant attack flight, if not all shield tapped, cant attack tapped shield
// TODO: The above, for now just letting all units/shields targetable
let targetable = {};
// Board
for (const [key, value] of Object.entries(global.roomData[roomId].itemData.component.board)) {
// If not the players card (for now, will need better checks in future)
if(global.roomData[roomId].itemData.player[key] !== playerId){
targetable[key] = key;
}
}
// Shield
for (const [key, value] of Object.entries(global.roomData[roomId].itemData.component.shield)) {
// If not the players card (for now, will need better checks in future)
if(global.roomData[roomId].itemData.player[key] !== playerId){
//if(!(key in global.roomData[roomId].itemData.player[playerId])){
targetable[key] = key;
}
}
console.log(targetable);
return targetable;
}
module.exports = {
setCardPosition
,canAttack
,getTargetableCards
};