98 lines
2.4 KiB
JavaScript
98 lines
2.4 KiB
JavaScript
function Game(modules) {
|
|
var sets = {
|
|
river : cardSet('river'),
|
|
hand : cardSet('hand'),
|
|
};
|
|
var selected = null;
|
|
|
|
modules.messaging.addEventListener(["NewGame"], function(o) {
|
|
o.game.river.forEach(function(cardName) {
|
|
var card = new RiverCard(cardName);
|
|
sets.river.card[card.value] = card;
|
|
sets.river.dom.appendChild(card.dom);
|
|
});
|
|
o.game.players[modules.session.getKey()].hand.forEach(function(cardName) {
|
|
var card = new HandCard(cardName);
|
|
sets.hand.card[card.value] = card;
|
|
sets.hand.dom.appendChild(card.dom);
|
|
});
|
|
});
|
|
|
|
function cardSet(id) {
|
|
return {
|
|
card: {},
|
|
dom: document.getElementById(id)
|
|
};
|
|
}
|
|
|
|
function matchingInRiver(card) {
|
|
return modules.fun.mapFilter(
|
|
modules.fun.of(sets.river.card),
|
|
modules.fun.isSet
|
|
)(modules.hanafuda.sameMonth(card));
|
|
}
|
|
|
|
function Card(name) {
|
|
this.value = modules.hanafuda.Card[name];
|
|
this.name = name;
|
|
this.dom = document.createElement('li');
|
|
this.dom.className = "card";
|
|
this.dom.textContent = name;
|
|
this.dom.addEventListener('click', this.onClick());
|
|
}
|
|
|
|
Card.prototype.onClick = function() {return function() {};};
|
|
|
|
function RiverCard() {
|
|
Card.apply(this, arguments);
|
|
this.candidate = false;
|
|
}
|
|
|
|
RiverCard.prototype.onClick = function() {
|
|
var card = this;
|
|
return function() {
|
|
if(card.candidate) {
|
|
modules.messaging.send({
|
|
tag: "Play",
|
|
move: {capture: [sets.hand.card[selected].name, card.name]}
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
RiverCard.prototype.setCandidate = function(yes) {
|
|
this.candidate = yes;
|
|
this.dom.classList.toggle("candidate", yes);
|
|
}
|
|
|
|
function HandCard() {
|
|
Card.apply(this, arguments);
|
|
}
|
|
|
|
HandCard.prototype.onClick = function() {
|
|
var card = this;
|
|
return function() {
|
|
if(selected != undefined) {
|
|
sets.hand.card[selected].setSelected(false);
|
|
} else {
|
|
card.play();
|
|
}
|
|
};
|
|
};
|
|
|
|
HandCard.prototype.setSelected = function(yes) {
|
|
selected = yes ? this.value : null;
|
|
this.dom.classList.toggle('selected', yes);
|
|
matchingInRiver(this.value).forEach(function(card) {card.setCandidate(yes);});
|
|
}
|
|
|
|
HandCard.prototype.play = function() {
|
|
var matching = matchingInRiver(this.value);
|
|
if(matching.length > 1) {
|
|
this.setSelected(true);
|
|
} else {
|
|
modules.messaging.send({tag: "Play", move: {play: this.name}})
|
|
}
|
|
}
|
|
|
|
}
|