webclient/js/GUI/Screen/Game/State.js

86 lines
1.7 KiB
JavaScript

import Hanafuda;
import Messaging;
import * as Fun from UnitJS.Fun;
var dom = {
deck: document.getElementById('deck'),
rest: document.getElementById('rest'),
status: document.getElementById('status')
};
var selected;
var sets = {};
var state = {
game: null,
playing: false,
step: null,
month: null,
turnedCard: null,
};
return {
dom: dom,
getSelected: getSelected,
init: init,
matchingInRiver: matchingInRiver,
play: play,
select: select,
sets: sets,
state: state
};
function init() {
buildSets();
}
function buildSets() {
['river', 'you', 'them'].forEach(function(id) {
var setDom = document.getElementById(id);
if(setDom.tagName.toLowerCase() == 'ul') {
sets[id] = {card: null, dom: setDom};
} else {
sets[id] = {};
for(var i = 0; i < setDom.children.length; i++) {
if(setDom.children[i].tagName.toLowerCase() == 'ul') {
var child = setDom.children[i].className;
sets[id][child] = {card: {}, dom: setDom.children[i]};
}
}
}
});
}
function getSelected() {
return selected;
}
function matchingInRiver(card) {
return Fun.mapFilter(
Fun.of(sets.river.card),
Fun.defined
)(Hanafuda.sameMonth(card).map(Fun.proj('name')));
}
function play(move) {
Messaging.send({
tag: "Play",
move: move,
onGame: state.game
});
}
function showCandidates(card, yes) {
matchingInRiver(card).forEach(
function(riverCard) {riverCard.setCandidate(yes);}
);
}
function select(cardOrFalse) {
if(typeof cardOrFalse == 'object') {
selected = cardOrFalse;
}
selected.dom.classList.toggle('selected', !!cardOrFalse);
showCandidates(selected.value, !!cardOrFalse);
if(cardOrFalse === false) {
selected = null;
}
}