175 lines
4.8 KiB
JavaScript
175 lines
4.8 KiB
JavaScript
function Game(modules) {
|
|
var turned = document.getElementById('turned');
|
|
var status = {
|
|
dom: document.getElementById('status'),
|
|
playing: false,
|
|
step: null,
|
|
month: null
|
|
};
|
|
var sets = {
|
|
river: cardSet('river'),
|
|
hand: cardSet('hand'),
|
|
};
|
|
var selected = null;
|
|
|
|
modules.messaging.addEventListener(["Game"], function(o) {
|
|
status.step = o.game.step.tag;
|
|
setStatus(o.game);
|
|
setCaptures(o.game);
|
|
[
|
|
{name: 'river', cards: o.game.river},
|
|
{name: 'hand', cards: o.game.players[modules.session.getKey()].hand}
|
|
].forEach(setCardSet);
|
|
if(status.step == "Turned") {
|
|
turned.textContent = o.game.step.contents;
|
|
turned.classList.remove("hidden");
|
|
if(status.playing) {
|
|
selected = o.game.step.contents;
|
|
showCandidates(modules.hanafuda.Card[selected], true);
|
|
}
|
|
} else {
|
|
turned.classList.add("hidden");
|
|
}
|
|
if(status.playing && status.step == "Scored") {
|
|
play({koiKoi: confirm(
|
|
"You scored ! Do you want to go on ? Press cancel to just get your points and end current month"
|
|
)});
|
|
}
|
|
});
|
|
|
|
function play(move) {
|
|
modules.messaging.send({
|
|
tag: "Play",
|
|
move: move
|
|
});
|
|
}
|
|
|
|
function cardSet(id) {
|
|
return {
|
|
card: null,
|
|
dom: document.getElementById(id)
|
|
};
|
|
}
|
|
|
|
function matchingInRiver(card) {
|
|
return modules.fun.mapFilter(
|
|
modules.fun.of(sets.river.card),
|
|
modules.fun.isSet
|
|
)(modules.hanafuda.sameMonth(card).map(modules.fun.proj('name')));
|
|
}
|
|
|
|
function setStatus(game) {
|
|
modules.dom.clear(status.dom);
|
|
status.month = game.month;
|
|
var month = document.createElement('li');
|
|
month.textContent = "This month's flower is the " + status.month;
|
|
status.dom.appendChild(month);
|
|
var playing = document.createElement('li');
|
|
status.playing = modules.session.is(game.playing);
|
|
if(status.playing) {
|
|
sets.hand.dom.classList.toggle("yourTurn", status.step == "ToPlay");
|
|
playing.textContent = "Your turn";
|
|
} else {
|
|
sets.hand.dom.classList.remove("yourTurn");
|
|
playing.textContent = modules.room.name(game.playing) + " is playing";
|
|
}
|
|
status.dom.appendChild(playing);
|
|
}
|
|
|
|
function setCaptures(game) {
|
|
for(var key in game.players) {
|
|
var elem = document.getElementById(modules.session.is(key) ? "own" : "opponent");
|
|
elem.getElementsByClassName('score')[0].textContent = game.scores[key] + " pts";
|
|
Array.prototype.forEach.call(elem.getElementsByTagName('ul'), modules.dom.clear);
|
|
game.players[key].meld.forEach(function(cardName) {
|
|
var card = new Card(cardName);
|
|
elem.getElementsByClassName(card.value.family.class)[0].appendChild(card.dom);
|
|
});
|
|
}
|
|
}
|
|
|
|
function setCardSet(conf) {
|
|
var set = sets[conf.name];
|
|
var constructor = conf.name == "river" ? RiverCard : HandCard;
|
|
modules.dom.clear(set.dom);
|
|
set.card = {};
|
|
conf.cards.forEach(function(cardName) {
|
|
var card = new constructor(cardName);
|
|
set.card[cardName] = card;
|
|
set.dom.appendChild(card.dom);
|
|
});
|
|
}
|
|
|
|
function showCandidates(card, yes) {
|
|
matchingInRiver(card).forEach(function(riverCard) {riverCard.setCandidate(yes);});
|
|
}
|
|
|
|
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) {
|
|
var withCard = selected;
|
|
selected = null;
|
|
play(
|
|
status.step == 'ToPlay' ? {capture: [withCard, card.name]} : {choose: 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() {
|
|
if(status.playing && status.step == "ToPlay") {
|
|
var card = this;
|
|
return function() {
|
|
if(selected != undefined) {
|
|
sets.hand.card[selected].setSelected(false);
|
|
} else {
|
|
card.play();
|
|
}
|
|
};
|
|
} else {
|
|
return function() {};
|
|
}
|
|
};
|
|
|
|
HandCard.prototype.setSelected = function(yes) {
|
|
selected = yes ? this.name : null;
|
|
this.dom.classList.toggle('selected', yes);
|
|
showCandidates(this.value, yes);
|
|
}
|
|
|
|
HandCard.prototype.play = function() {
|
|
var matching = matchingInRiver(this.value);
|
|
if(matching.length > 1) {
|
|
this.setSelected(true);
|
|
} else {
|
|
play({play: this.name});
|
|
}
|
|
}
|
|
}
|