Factorize code for turned cards and make sure selected hand cards are unselected after being played

This commit is contained in:
Tissevert 2019-10-18 17:54:27 +02:00
parent c21a8f6512
commit 0abc020d13

View file

@ -174,7 +174,7 @@ function Game(modules) {
movingCards.push([sets[side].hand, dest, card]); movingCards.push([sets[side].hand, dest, card]);
} else { } else {
var cardSet = {}; var cardSet = {};
cardSet[card.name] = turnedCard(card.name); cardSet[card.name] = new TurnedCard(card.name);
movingCards.push([{card: cardSet, dom: deck}, dest, card]); movingCards.push([{card: cardSet, dom: deck}, dest, card]);
} }
return movingCards; 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) { function setTurned(cardName) {
turnedCard(cardName); turnedCard = new TurnedCard(cardName);
if(status.playing) { if(status.playing) {
selected = cardName; selected = turnedCard;
showCandidates(modules.hanafuda.Card[selected], true); showCandidates(modules.hanafuda.Card[cardName], true);
} }
} }
@ -319,6 +312,12 @@ function Game(modules) {
matchingInRiver(card).forEach(function(riverCard) {riverCard.setCandidate(yes);}); 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) { function Card(name) {
this.value = modules.hanafuda.Card[name]; this.value = modules.hanafuda.Card[name];
this.name = name; this.name = name;
@ -343,9 +342,8 @@ function Game(modules) {
var card = this; var card = this;
return function() { return function() {
if(card.candidate) { if(card.candidate) {
var withCard = selected; var withCard = selected.name;
selected = null; selected.setSelected(false);
showCandidates(card.value, false);
play( play(
status.step == 'ToPlay' ? {capture: [withCard, card.name]} : {choose: card.name} status.step == 'ToPlay' ? {capture: [withCard, card.name]} : {choose: card.name}
); );
@ -358,6 +356,15 @@ function Game(modules) {
this.dom.classList.toggle("candidate", yes); 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() { function HandCard() {
Card.apply(this, arguments); Card.apply(this, arguments);
} }
@ -367,7 +374,7 @@ function Game(modules) {
return function() { return function() {
if(status.playing && status.step == "ToPlay") { if(status.playing && status.step == "ToPlay") {
if(selected != undefined) { if(selected != undefined) {
sets.you.hand.card[selected].setSelected(false); selected.setSelected(false);
} else { } else {
card.play(); card.play();
} }
@ -375,11 +382,7 @@ function Game(modules) {
}; };
}; };
HandCard.prototype.setSelected = function(yes) { HandCard.prototype.setSelected = setSelected;
selected = yes ? this.name : null;
this.dom.classList.toggle('selected', yes);
showCandidates(this.value, yes);
}
HandCard.prototype.play = function() { HandCard.prototype.play = function() {
var matching = matchingInRiver(this.value); var matching = matchingInRiver(this.value);