diff --git a/public/board.js b/public/board.js index 417f068..4e5a2b2 100644 --- a/public/board.js +++ b/public/board.js @@ -661,7 +661,10 @@ class Board{ // TODO: May be best to tap in canPayMana while looping reqs. and if the reqs aren't hit untap the mana that were tapped in that call? // This atm will loop manaReq and tap. let tapped = this.tapManaRequired(itemKey, fromPlayer); - alert('tapManaRequired: '+tapped); + if(tapped != true){ + alert('tapManaRequired: '+tapped); + return false; + } } } @@ -677,6 +680,7 @@ class Board{ // TODO: Look at combining or normalising this and canPayMana() let manaRequired = this.getManaTotalOf(itemToPayCost); let noManaReq = {1:0, 2:0, 3:0, 4:0}; + let manaToTap = []; // Loop all the mana let items = this.getItems('mana', playerId, null, null); @@ -684,33 +688,98 @@ class Board{ let mana = items[item]; - if(this.isTapped(mana)){ + if(this.isTapped(mana) || this.isSelected(mana)){ continue; } let colourId = cardManaColour[mana][0]; - console.log(JSON.stringify(manaRequired)); + console.log(JSON.stringify(manaRequired[0])); // Loop the requirements of the cost to pay - for (const manaColour in manaRequired) { + for (const manaColour in manaRequired[0]) { //console.log(manaColour+' '+manaRequired[manaColour]+', '+colourId); // If the colour of the mana is in the requirements // of the cost to pay, reduce the cost for that colour by // 1 and tap the mana - if(manaColour == colourId && manaRequired[manaColour] > 0){ + if(manaColour == colourId && manaRequired[0][manaColour] > 0){ // Reduce the required mana - manaRequired[colourId]--; - this.tapCard(mana); + manaRequired[0][colourId]--; + manaToTap.push(mana); + this.selectCard(mana); // Make selected so that we know to tap } } // End loop once mana Req are fulfilled - if(JSON.stringify(manaRequired) == JSON.stringify(noManaReq)){ - return true; + if(JSON.stringify(manaRequired[0]) == JSON.stringify(noManaReq)){ + break; + } + } + + // Now all the req. mana colours are tapped, check if the cost requires more + // For 3 cost cards that only require 1 red, two more mana (any colour) + // need to be tapped + // TODO: Better more efficiently and let user select... + let itemCostRemaining = cardData[itemToPayCost].cost - manaRequired[1]; + if(itemCostRemaining > 0){ + /* + let tapRequirement = (cardData[itemToPayCost].cost - manaRequired[1]); + alert('Tap: '+(tapRequirement)+' more mana to play'); + + // start mana tap event + */ + + // TODO: decide 100% how cards are to be played/payed for + // don't want to slam time into a mana system if it'll be nuked. + // For now, reloop and tap first available mana so that card + // payment is satiated + // Using same items from above + for(let item = 0; item < items.length; item++){ + + let mana = items[item]; + + if(this.isTapped(mana) || this.isSelected(mana)){ + continue; + } + + if(itemCostRemaining > 0){ + manaToTap.push(mana); + itemCostRemaining--; + } + } } - return('tapMana could not tap correct mana'); + if(itemCostRemaining > 0){ + return false; // Didn't find another mana to tap so cost not satiated + } + + // If the mana to tap has been satitated then tap the mana selected + manaToTap.forEach(manaId => { + this.tapCard(manaId); + }); + + // And continue with whatever we were doing + return true; + + } + + isSelected(itemKey){ + if(cardStatus[itemKey] == 'selected'){ return true; } + return false; + } + selectCard(itemKey){ + // Sets card to 'selected/targetted' to show which have been selected + if(!this.isSelected[itemKey]){ + cardStatus[itemKey] = 'selected'; + } + return 'Cannot select a selected card'; + } + deselectCard(itemKey){ + if(this.isSelected[itemKey]){ + cardStatus[itemKey] = null; + return true; + } + return 'Cannot unselect an unselected card'; } getItemKey(boardElementId, listPositionId, playerFrom){ @@ -754,7 +823,7 @@ class Board{ // have the colour? May be a silly thought, but // Loop the card to be played, and total its mana requirements - let manaRequired = this.getManaTotalOf(itemToPlay); + let manaRequired = this.getManaTotalOf(itemToPlay)[0]; // player check for this may need changing for "play from opp hand" etc? @@ -820,17 +889,19 @@ class Board{ getManaTotalOf(itemKey){ let manaTotal = {1:0, 2:0, 3:0, 4:0}; let manaColours = cardColours[itemKey]; + let totalManaColourReq = 0; console.log(manaColours.length); for(let i = 0; i < manaColours.length; i++){ // Add one to the available mana let colourId = manaColours[i][0]; manaTotal[colourId] += manaColours[i][1]; // Add the cost + totalManaColourReq += manaColours[i][1]; } //console.log('manaTotal: '); //console.log(manaTotal); - return manaTotal; + return [manaTotal, totalManaColourReq]; } inspectCard(cardToInspect){