From 0abc020d13c8aa12435a6348347c81c5514e9678 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Fri, 18 Oct 2019 17:54:27 +0200 Subject: [PATCH] Factorize code for turned cards and make sure selected hand cards are unselected after being played --- www/game.js | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/www/game.js b/www/game.js index f56079d..767ea4b 100644 --- a/www/game.js +++ b/www/game.js @@ -174,7 +174,7 @@ function Game(modules) { movingCards.push([sets[side].hand, dest, card]); } else { var cardSet = {}; - cardSet[card.name] = turnedCard(card.name); + cardSet[card.name] = new TurnedCard(card.name); movingCards.push([{card: cardSet, dom: deck}, dest, card]); } return movingCards; @@ -300,18 +300,11 @@ function Game(modules) { } } - function turnedCard(cardName) { - var card = new Card(cardName); - card.dom.id = "turned"; - deck.appendChild(card.dom); - return card; - } - function setTurned(cardName) { - turnedCard(cardName); + turnedCard = new TurnedCard(cardName); if(status.playing) { - selected = cardName; - showCandidates(modules.hanafuda.Card[selected], true); + selected = turnedCard; + showCandidates(modules.hanafuda.Card[cardName], true); } } @@ -319,6 +312,12 @@ function Game(modules) { matchingInRiver(card).forEach(function(riverCard) {riverCard.setCandidate(yes);}); } + function setSelected(yes) { + selected = yes ? this : null; + this.dom.classList.toggle('selected', yes); + showCandidates(this.value, yes); + } + function Card(name) { this.value = modules.hanafuda.Card[name]; this.name = name; @@ -343,9 +342,8 @@ function Game(modules) { var card = this; return function() { if(card.candidate) { - var withCard = selected; - selected = null; - showCandidates(card.value, false); + var withCard = selected.name; + selected.setSelected(false); play( status.step == 'ToPlay' ? {capture: [withCard, card.name]} : {choose: card.name} ); @@ -358,6 +356,15 @@ function Game(modules) { this.dom.classList.toggle("candidate", yes); } + function TurnedCard() { + Card.apply(this, arguments); + this.dom.id = "turned"; + deck.appendChild(this.dom); + } + + TurnedCard.prototype.onClick = Card.prototype.onClick; + TurnedCard.prototype.setSelected = setSelected; + function HandCard() { Card.apply(this, arguments); } @@ -367,7 +374,7 @@ function Game(modules) { return function() { if(status.playing && status.step == "ToPlay") { if(selected != undefined) { - sets.you.hand.card[selected].setSelected(false); + selected.setSelected(false); } else { card.play(); } @@ -375,11 +382,7 @@ function Game(modules) { }; }; - HandCard.prototype.setSelected = function(yes) { - selected = yes ? this.name : null; - this.dom.classList.toggle('selected', yes); - showCandidates(this.value, yes); - } + HandCard.prototype.setSelected = setSelected; HandCard.prototype.play = function() { var matching = matchingInRiver(this.value);