2020-01-15 18:01:07 +01:00
|
|
|
import * as Card from GUI.Card;
|
|
|
|
import * as HandCard from GUI.Card.HandCard;
|
|
|
|
import * as RiverCard from GUI.Card.RiverCard;
|
|
|
|
import * as TurnedCard from GUI.Card.TurnedCard;
|
|
|
|
import * as Screen from GUI.Screen;
|
|
|
|
import {animate, catchUp, delay, getQueue} from GUI.Screen.Game.Animation;
|
2020-01-16 16:45:23 +01:00
|
|
|
import {
|
2020-01-25 10:56:55 +01:00
|
|
|
dom, init as initState, play, select, sets, globalState
|
2020-01-16 16:45:23 +01:00
|
|
|
} from GUI.Screen.Game.State;
|
2020-01-14 17:27:56 +01:00
|
|
|
import Hanafuda;
|
|
|
|
import I18n;
|
|
|
|
import Messaging;
|
|
|
|
import players from Room;
|
2020-01-15 18:01:07 +01:00
|
|
|
import Save;
|
2020-01-14 17:27:56 +01:00
|
|
|
import Session;
|
|
|
|
import StatusHandler;
|
|
|
|
import * as Async from UnitJS.Async;
|
|
|
|
import * as Dom from UnitJS.Dom;
|
|
|
|
import * as Fun from UnitJS.Fun;
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: init
|
|
|
|
}
|
|
|
|
|
2020-01-15 18:01:07 +01:00
|
|
|
function init(gameID) {
|
|
|
|
initState();
|
|
|
|
initMessageHandlers();
|
|
|
|
Async.run(
|
|
|
|
Async.bind(
|
2020-01-20 22:58:57 +01:00
|
|
|
getSavedStates(gameID),
|
|
|
|
function(states) {
|
2020-01-15 18:01:07 +01:00
|
|
|
return Async.sequence(
|
|
|
|
startSession(),
|
2020-01-20 22:58:57 +01:00
|
|
|
previously(states)
|
2020-01-15 18:01:07 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-16 22:33:41 +01:00
|
|
|
function backToMain() {
|
|
|
|
window.location = '..';
|
|
|
|
}
|
|
|
|
|
2020-01-15 18:01:07 +01:00
|
|
|
function fail(errorCode) {
|
|
|
|
return function(f) {
|
|
|
|
Screen.dialog({
|
|
|
|
text: I18n.get(errorCode),
|
2020-01-16 22:33:41 +01:00
|
|
|
answers: [{label: 'backToMain', action: backToMain}]
|
2020-01-15 18:01:07 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function getSavedStates(gameID) {
|
2020-01-25 10:56:55 +01:00
|
|
|
var states = Save.get('games.state.Game#' + gameID);
|
2020-01-20 22:58:57 +01:00
|
|
|
return states != undefined ? Async.wrap(states) : fail('gameNotFound');
|
2020-01-15 18:01:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function startSession() {
|
|
|
|
var name = Save.get('player.name');
|
|
|
|
if(name != undefined) {
|
|
|
|
Session.start(name);
|
|
|
|
return Async.wrap();
|
|
|
|
} else {
|
|
|
|
return fail('noNameToPlay');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function previously(states) {
|
|
|
|
if(states.former != undefined && states.latest.logs.length > 0) {
|
2020-01-18 23:10:22 +01:00
|
|
|
return Async.sequence(
|
2020-01-20 22:58:57 +01:00
|
|
|
setGame(states.former),
|
|
|
|
handleGameMessage(states.latest)
|
2020-01-18 23:10:22 +01:00
|
|
|
);
|
|
|
|
} else {
|
2020-01-20 22:58:57 +01:00
|
|
|
return setGame(states.latest);
|
2020-01-18 23:10:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 18:01:07 +01:00
|
|
|
function initMessageHandlers() {
|
|
|
|
window.addEventListener('focus', catchUp);
|
2020-01-14 17:27:56 +01:00
|
|
|
Messaging.addEventListener(["Game"], function(o) {
|
2020-01-20 22:58:57 +01:00
|
|
|
delay(handleGameMessage(o.state));
|
2020-01-15 18:01:07 +01:00
|
|
|
if(document.hasFocus() && getQueue().length == 1) {
|
|
|
|
catchUp();
|
2020-01-14 17:27:56 +01:00
|
|
|
} else {
|
|
|
|
StatusHandler.set("♪");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function handleGameMessage(state) {
|
|
|
|
if(state.public.coordinates.turn == 0) {
|
|
|
|
if(state.logs.length > 0) { // but still some logs, from the previous round
|
|
|
|
return Async.sequence(applyDiff(state), setGame(state)); // so play the diff, then set the new round
|
2020-01-14 17:27:56 +01:00
|
|
|
} else {
|
2020-01-20 22:58:57 +01:00
|
|
|
return setGame(state); // directly set a whole new game
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-20 22:58:57 +01:00
|
|
|
return applyDiff(state);
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function setGame(state) {
|
2020-01-14 17:27:56 +01:00
|
|
|
return function(f) {
|
2020-01-20 22:58:57 +01:00
|
|
|
setStatus(state);
|
|
|
|
setCaptures(state);
|
2020-01-14 17:27:56 +01:00
|
|
|
[
|
2020-01-20 22:58:57 +01:00
|
|
|
[sets.river, state.public.river, RiverCard.make],
|
|
|
|
[sets.you.hand, state.playerHand, HandCard.make]
|
2020-01-14 17:27:56 +01:00
|
|
|
].forEach(function(args) {setCardSet.apply(null, args)});
|
2020-01-20 22:58:57 +01:00
|
|
|
setTheirCards(state);
|
|
|
|
handleStep(state)(f);
|
2020-01-14 17:27:56 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function handleStep(state) {
|
2020-01-14 17:27:56 +01:00
|
|
|
return function(f) {
|
2020-01-20 22:58:57 +01:00
|
|
|
handleTurnedCard(state, f);
|
2020-01-25 10:56:55 +01:00
|
|
|
if(globalState.step == "Scored") {
|
|
|
|
if(globalState.playing) {
|
2020-01-20 22:58:57 +01:00
|
|
|
askKoikoi(state, f);
|
2020-01-14 17:27:56 +01:00
|
|
|
} else {
|
2020-01-20 22:58:57 +01:00
|
|
|
theyScored(state, f);
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
2020-01-25 10:56:55 +01:00
|
|
|
} else if (globalState.step == "Over") {
|
2020-01-20 22:58:57 +01:00
|
|
|
gameEnd(state, f);
|
2020-01-14 17:27:56 +01:00
|
|
|
} else {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function handleTurnedCard(state, f) {
|
2020-01-25 10:56:55 +01:00
|
|
|
if(globalState.step == "Turned") {
|
2020-01-20 22:58:57 +01:00
|
|
|
setTurned(state.public.step.contents);
|
2020-01-14 17:27:56 +01:00
|
|
|
} else {
|
2020-01-25 10:56:55 +01:00
|
|
|
if(globalState.step == "ToPlay" && state.public.playing == state.public.oyake) {
|
|
|
|
dom.rest.className = ["card", "turn" + state.public.coordinates.turn].join(' ');
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
|
|
|
if(deck.lastChild.id != "rest") {
|
|
|
|
deck.removeChild(deck.lastChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function askKoikoi(state, f) {
|
2020-01-14 17:27:56 +01:00
|
|
|
Screen.dialog({
|
|
|
|
text: I18n.get('youScored'),
|
|
|
|
answers: [
|
|
|
|
{label: 'endRound', action: function() {play({koiKoi: false}); f();}},
|
|
|
|
{label: 'koikoi', action: function() {play({koiKoi: true}); f();}}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function theyScored(state, f) {
|
2020-01-14 17:27:56 +01:00
|
|
|
Screen.dialog({
|
2020-01-20 22:58:57 +01:00
|
|
|
text: I18n.get('theyScored')(players.get(state.public.playing)),
|
2020-01-14 17:27:56 +01:00
|
|
|
answers: [
|
|
|
|
{label: 'ok', action: f}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function gameEnd(state, f) {
|
2020-01-14 17:27:56 +01:00
|
|
|
var winner, maxScore;
|
2020-01-20 22:58:57 +01:00
|
|
|
for(var key in state.public.scores) {
|
|
|
|
if(maxScore == undefined || state.public.scores[key] > maxScore) {
|
2020-01-14 17:27:56 +01:00
|
|
|
winner = key;
|
2020-01-20 22:58:57 +01:00
|
|
|
maxScore = state.public.scores[key];
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Screen.dialog({
|
|
|
|
text: I18n.get(Session.is(winner) ? 'won' : 'lost'),
|
2020-01-16 22:33:41 +01:00
|
|
|
answers: [{label: 'endGame', action: backToMain}]
|
2020-01-14 17:27:56 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function applyDiff(state) {
|
2020-01-14 17:27:56 +01:00
|
|
|
return Async.sequence.apply(null,
|
2020-01-20 22:58:57 +01:00
|
|
|
state.logs.map(animate).concat(
|
|
|
|
Async.apply(setStatus, state),
|
|
|
|
handleStep(state)
|
2020-01-14 17:27:56 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setStatus(game) {
|
2020-01-15 18:01:07 +01:00
|
|
|
Dom.clear(dom.status);
|
2020-01-25 10:56:55 +01:00
|
|
|
globalState.game = game;
|
|
|
|
globalState.step = game.public.step.tag;
|
|
|
|
if(game.public.coordinates.month != globalState.month) {
|
|
|
|
globalState.month = game.public.coordinates.month;
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
2020-01-15 18:01:07 +01:00
|
|
|
dom.status.appendChild(
|
2020-01-25 10:56:55 +01:00
|
|
|
Dom.make('li', {textContent: I18n.get('monthFlower')(I18n.get(globalState.month))})
|
2020-01-14 17:27:56 +01:00
|
|
|
);
|
|
|
|
var turn = null;
|
2020-01-25 10:56:55 +01:00
|
|
|
globalState.playing = Session.is(game.public.playing);
|
|
|
|
if(globalState.playing) {
|
|
|
|
sets.you.hand.dom.classList.toggle("yourTurn", globalState.step == "ToPlay");
|
2020-01-14 17:27:56 +01:00
|
|
|
turn = I18n.get("yourTurn");
|
|
|
|
} else {
|
|
|
|
sets.you.hand.dom.classList.remove("yourTurn");
|
|
|
|
turn = I18n.get('playing')(players.get(game.public.playing));
|
|
|
|
}
|
2020-01-15 18:01:07 +01:00
|
|
|
dom.status.appendChild(Dom.make('li', {textContent: turn}));
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setCaptures(game) {
|
|
|
|
for(var key in game.public.players) {
|
|
|
|
var elem = document.getElementById(Session.is(key) ? "you" : "them");
|
|
|
|
elem.getElementsByClassName('score')[0].textContent = game.public.scores[key] + " pts";
|
|
|
|
var byClass = {}
|
|
|
|
Object.values(Hanafuda.Family).forEach(function(family) {
|
|
|
|
byClass[family.class] = elem.getElementsByClassName(family.class)[0];
|
|
|
|
Dom.clear(byClass[family.class]);
|
|
|
|
});
|
|
|
|
game.public.players[key].meld.forEach(function(cardName) {
|
2020-01-15 18:01:07 +01:00
|
|
|
var card = Card.make(cardName);
|
2020-01-14 17:27:56 +01:00
|
|
|
byClass[card.value.family.class].appendChild(card.dom);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCardSet(set, cardNames, constructor) {
|
2020-01-16 16:45:23 +01:00
|
|
|
constructor = constructor || Card.make;
|
2020-01-14 17:27:56 +01:00
|
|
|
set.card = {};
|
|
|
|
Dom.clear(set.dom);
|
|
|
|
cardNames.forEach(function(cardName) {
|
2020-01-15 18:01:07 +01:00
|
|
|
var card = constructor(cardName);
|
2020-01-14 17:27:56 +01:00
|
|
|
set.card[cardName] = card;
|
|
|
|
set.dom.appendChild(card.dom);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTheirCards(game) {
|
|
|
|
var turnsTheyPlayed = Math.floor(
|
2020-01-18 09:35:51 +01:00
|
|
|
(game.public.coordinates.turn + (Session.is(game.public.oyake) ? 0 : 1)) / 2
|
2020-01-14 17:27:56 +01:00
|
|
|
);
|
|
|
|
Dom.clear(sets.them.hand.dom);
|
|
|
|
for(var i = 0; i < 8 - turnsTheyPlayed; i++) {
|
|
|
|
sets.them.hand.dom.appendChild(Dom.make('li', {class: "card"}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTurned(cardName) {
|
2020-01-25 10:56:55 +01:00
|
|
|
globalState.turnedCard = TurnedCard.make(cardName);
|
|
|
|
if(globalState.playing) {
|
|
|
|
select(globalState.turnedCard);
|
2020-01-14 17:27:56 +01:00
|
|
|
}
|
|
|
|
}
|