2018-05-13 18:08:12 +02:00
|
|
|
function Game(modules) {
|
2018-05-26 21:26:12 +02:00
|
|
|
var deck = document.getElementById("deck");
|
|
|
|
var rest = document.getElementById("rest");
|
2018-05-16 22:59:22 +02:00
|
|
|
var status = {
|
2018-05-21 23:28:33 +02:00
|
|
|
dom: document.getElementById("status"),
|
2018-05-16 22:59:22 +02:00
|
|
|
playing: false,
|
|
|
|
step: null,
|
2018-07-28 12:48:31 +02:00
|
|
|
month: null
|
2018-05-16 22:59:22 +02:00
|
|
|
};
|
2018-05-13 18:08:12 +02:00
|
|
|
var sets = {
|
2018-05-21 23:28:33 +02:00
|
|
|
river: {
|
|
|
|
card: null,
|
|
|
|
dom: document.getElementById("river")
|
|
|
|
},
|
2018-05-26 21:26:12 +02:00
|
|
|
yourHand: {
|
2018-05-21 23:28:33 +02:00
|
|
|
card: null,
|
|
|
|
dom: document.getElementById("you").getElementsByClassName("hand")[0]
|
2018-05-26 21:26:12 +02:00
|
|
|
},
|
|
|
|
theirHand: {
|
|
|
|
dom: document.getElementById("them").getElementsByClassName("hand")[0]
|
2018-05-21 23:28:33 +02:00
|
|
|
}
|
2018-05-13 18:08:12 +02:00
|
|
|
};
|
|
|
|
var selected = null;
|
|
|
|
|
2018-05-16 22:59:22 +02:00
|
|
|
modules.messaging.addEventListener(["Game"], function(o) {
|
|
|
|
setStatus(o.game);
|
|
|
|
setCaptures(o.game);
|
|
|
|
[
|
2018-05-21 23:28:33 +02:00
|
|
|
['river', o.game.river, RiverCard],
|
2018-05-26 21:26:12 +02:00
|
|
|
['yourHand', o.game.players[modules.session.getKey()].hand, HandCard]
|
2018-05-21 23:28:33 +02:00
|
|
|
].forEach(function(args) {setCardSet.apply(null, args)});
|
2018-07-28 12:48:31 +02:00
|
|
|
setTheirCards(o.game);
|
2018-05-16 22:59:22 +02:00
|
|
|
if(status.step == "Turned") {
|
2018-05-21 23:28:33 +02:00
|
|
|
setTurned(o.game.step.contents);
|
2018-05-16 22:59:22 +02:00
|
|
|
} else {
|
2018-05-26 21:26:12 +02:00
|
|
|
if(status.step == "ToPlay" && o.game.playing == o.game.oyake) {
|
2018-07-28 12:48:31 +02:00
|
|
|
rest.className = ["card", "count" + o.game.deck].join(' ');
|
2018-05-26 21:26:12 +02:00
|
|
|
}
|
|
|
|
if(deck.lastChild.id != "rest") {
|
|
|
|
deck.removeChild(deck.lastChild);
|
|
|
|
}
|
2018-05-16 22:59:22 +02:00
|
|
|
}
|
2018-08-02 00:00:13 +02:00
|
|
|
if(status.step == "Scored") {
|
|
|
|
if(status.playing) {
|
|
|
|
modules.screen.dialog({
|
|
|
|
text: "You scored ! Do you want to get your points and end the round or KoiKoi ?",
|
|
|
|
answers: {
|
|
|
|
'End the round': function() {play({koiKoi: false})},
|
|
|
|
'KoiKoi !': function() {play({koiKoi: true});}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
modules.screen.dialog({
|
|
|
|
text: modules.room.name(o.game.playing) + " scored",
|
|
|
|
answers: {
|
|
|
|
'Ok': function() {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-16 22:59:22 +02:00
|
|
|
}
|
2018-05-13 18:08:12 +02:00
|
|
|
});
|
|
|
|
|
2018-05-16 22:59:22 +02:00
|
|
|
function play(move) {
|
|
|
|
modules.messaging.send({
|
|
|
|
tag: "Play",
|
|
|
|
move: move
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-13 18:08:12 +02:00
|
|
|
function matchingInRiver(card) {
|
|
|
|
return modules.fun.mapFilter(
|
|
|
|
modules.fun.of(sets.river.card),
|
|
|
|
modules.fun.isSet
|
2018-05-16 22:59:22 +02:00
|
|
|
)(modules.hanafuda.sameMonth(card).map(modules.fun.proj('name')));
|
|
|
|
}
|
|
|
|
|
|
|
|
function setStatus(game) {
|
|
|
|
modules.dom.clear(status.dom);
|
2018-05-26 21:26:12 +02:00
|
|
|
status.step = game.step.tag;
|
|
|
|
if(game.month != status.month) {
|
|
|
|
status.month = game.month;
|
|
|
|
}
|
|
|
|
status.dom.appendChild(
|
|
|
|
modules.dom.make('li', {textContent: "This month's flower is the " + status.month})
|
|
|
|
);
|
|
|
|
var turn = null;
|
2018-05-16 22:59:22 +02:00
|
|
|
status.playing = modules.session.is(game.playing);
|
|
|
|
if(status.playing) {
|
2018-05-26 21:26:12 +02:00
|
|
|
sets.yourHand.dom.classList.toggle("yourTurn", status.step == "ToPlay");
|
|
|
|
turn = "Your turn";
|
2018-05-16 22:59:22 +02:00
|
|
|
} else {
|
2018-05-26 21:26:12 +02:00
|
|
|
sets.yourHand.dom.classList.remove("yourTurn");
|
|
|
|
turn = modules.room.name(game.playing) + " is playing";
|
2018-05-16 22:59:22 +02:00
|
|
|
}
|
2018-05-26 21:26:12 +02:00
|
|
|
status.dom.appendChild(modules.dom.make('li', {textContent: turn}));
|
2018-05-16 22:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setCaptures(game) {
|
|
|
|
for(var key in game.players) {
|
2018-05-21 23:28:33 +02:00
|
|
|
var elem = document.getElementById(modules.session.is(key) ? "you" : "them");
|
2018-05-16 22:59:22 +02:00
|
|
|
elem.getElementsByClassName('score')[0].textContent = game.scores[key] + " pts";
|
2018-05-21 23:28:33 +02:00
|
|
|
var byClass = {}
|
|
|
|
Object.values(modules.hanafuda.Family).forEach(function(family) {
|
|
|
|
byClass[family.class] = elem.getElementsByClassName(family.class)[0];
|
|
|
|
modules.dom.clear(byClass[family.class]);
|
|
|
|
});
|
2018-05-16 22:59:22 +02:00
|
|
|
game.players[key].meld.forEach(function(cardName) {
|
|
|
|
var card = new Card(cardName);
|
2018-05-21 23:28:33 +02:00
|
|
|
byClass[card.value.family.class].appendChild(card.dom);
|
2018-05-16 22:59:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 23:28:33 +02:00
|
|
|
function setCardSet(setName, cardNames, constructor) {
|
|
|
|
constructor = constructor || Card;
|
|
|
|
var set = sets[setName];
|
2018-05-16 22:59:22 +02:00
|
|
|
set.card = {};
|
2018-05-21 23:28:33 +02:00
|
|
|
modules.dom.clear(set.dom);
|
|
|
|
cardNames.forEach(function(cardName) {
|
2018-05-16 22:59:22 +02:00
|
|
|
var card = new constructor(cardName);
|
|
|
|
set.card[cardName] = card;
|
|
|
|
set.dom.appendChild(card.dom);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-28 12:48:31 +02:00
|
|
|
function setTheirCards(game) {
|
2018-05-26 21:26:12 +02:00
|
|
|
var turnsTheyPlayed = Math.floor(
|
2018-07-28 12:48:31 +02:00
|
|
|
(24 - game.deck + (modules.session.is(game.oyake) ? 0 : 1)) / 2
|
2018-05-26 21:26:12 +02:00
|
|
|
);
|
|
|
|
modules.dom.clear(sets.theirHand.dom);
|
|
|
|
for(var i = 0; i < 8 - turnsTheyPlayed; i++) {
|
|
|
|
sets.theirHand.dom.appendChild(modules.dom.make('li', {class: "card"}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 23:28:33 +02:00
|
|
|
function setTurned(cardName) {
|
|
|
|
var card = new Card(cardName);
|
2018-05-26 21:26:12 +02:00
|
|
|
card.dom.id = "turned";
|
|
|
|
deck.appendChild(card.dom);
|
2018-05-21 23:28:33 +02:00
|
|
|
if(status.playing) {
|
|
|
|
selected = cardName;
|
|
|
|
showCandidates(modules.hanafuda.Card[selected], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 22:59:22 +02:00
|
|
|
function showCandidates(card, yes) {
|
|
|
|
matchingInRiver(card).forEach(function(riverCard) {riverCard.setCandidate(yes);});
|
2018-05-13 18:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Card(name) {
|
|
|
|
this.value = modules.hanafuda.Card[name];
|
|
|
|
this.name = name;
|
2018-05-26 21:26:12 +02:00
|
|
|
this.dom = modules.dom.make('li', {
|
|
|
|
class: [
|
|
|
|
"card",
|
|
|
|
"value" + modules.hanafuda.getValue(this.value),
|
|
|
|
"month" + this.value.flower
|
|
|
|
],
|
|
|
|
onClick: this.onClick()
|
|
|
|
});
|
2018-05-13 18:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-05-16 22:59:22 +02:00
|
|
|
var withCard = selected;
|
|
|
|
selected = null;
|
|
|
|
play(
|
|
|
|
status.step == 'ToPlay' ? {capture: [withCard, card.name]} : {choose: card.name}
|
|
|
|
);
|
2018-05-13 18:08:12 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
RiverCard.prototype.setCandidate = function(yes) {
|
|
|
|
this.candidate = yes;
|
|
|
|
this.dom.classList.toggle("candidate", yes);
|
|
|
|
}
|
|
|
|
|
|
|
|
function HandCard() {
|
|
|
|
Card.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
HandCard.prototype.onClick = function() {
|
2018-05-16 22:59:22 +02:00
|
|
|
if(status.playing && status.step == "ToPlay") {
|
|
|
|
var card = this;
|
|
|
|
return function() {
|
|
|
|
if(selected != undefined) {
|
2018-05-26 21:26:12 +02:00
|
|
|
sets.yourHand.card[selected].setSelected(false);
|
2018-05-16 22:59:22 +02:00
|
|
|
} else {
|
|
|
|
card.play();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return function() {};
|
|
|
|
}
|
2018-05-13 18:08:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
HandCard.prototype.setSelected = function(yes) {
|
2018-05-16 22:59:22 +02:00
|
|
|
selected = yes ? this.name : null;
|
2018-05-13 18:08:12 +02:00
|
|
|
this.dom.classList.toggle('selected', yes);
|
2018-05-16 22:59:22 +02:00
|
|
|
showCandidates(this.value, yes);
|
2018-05-13 18:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HandCard.prototype.play = function() {
|
|
|
|
var matching = matchingInRiver(this.value);
|
|
|
|
if(matching.length > 1) {
|
|
|
|
this.setSelected(true);
|
|
|
|
} else {
|
2018-05-16 22:59:22 +02:00
|
|
|
play({play: this.name});
|
2018-05-13 18:08:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|