Use lighter syntax and adapt client code

This commit is contained in:
Tissevert 2019-10-17 19:50:21 +02:00
parent 55ec64fafc
commit 7804aeecef
2 changed files with 32 additions and 32 deletions

View file

@ -35,8 +35,8 @@ exportPlayers game =
let (Player.Players players) = KoiKoi.players game in let (Player.Players players) = KoiKoi.players game in
players players
extractPrivateState :: PlayerID -> Game -> PrivateState privateState :: PlayerID -> Game -> PrivateState
extractPrivateState playerID game = PrivateState { privateState playerID game = PrivateState {
opponentHand = getHand opponentID players opponentHand = getHand opponentID players
, deck = KoiKoi.deck game , deck = KoiKoi.deck game
} }
@ -53,8 +53,8 @@ publicPlayer player = PublicPlayer {
, yakus = Player.yakus player , yakus = Player.yakus player
} }
extractPublicState :: Game -> PublicState publicState :: Game -> PublicState
extractPublicState game = PublicState { publicState game = PublicState {
mode = KoiKoi.mode game mode = KoiKoi.mode game
, scores = KoiKoi.scores game , scores = KoiKoi.scores game
, month = KoiKoi.month game , month = KoiKoi.month game
@ -76,13 +76,13 @@ export playerID game = do
return $ PublicGame { return $ PublicGame {
nonce = Saltine.encode n nonce = Saltine.encode n
, playerHand = getHand playerID (KoiKoi.players game) , playerHand = getHand playerID (KoiKoi.players game)
, privateState = secretbox encrypt n $ toJSON privateState , private = secretbox encrypt n $ toJSON private
, publicState , public
, publicSignature = signDetached (secret sign) $ toJSON publicState , publicSignature = signDetached (secret sign) $ toJSON public
} }
where where
publicState = extractPublicState game public = publicState game
privateState = extractPrivateState playerID game private = privateState playerID game
toJSON :: ToJSON a => a -> ByteString toJSON :: ToJSON a => a -> ByteString
toJSON = toStrict . encode toJSON = toStrict . encode

View file

@ -53,7 +53,7 @@ function Game(modules) {
} }
function handleGameMessage(o) { function handleGameMessage(o) {
if(o.game.publicState.turns == 0) { if(o.state.public.turns == 0) {
if(o.logs.length > 0) { // but still some logs, from the previous round if(o.logs.length > 0) { // but still some logs, from the previous round
return modules.async.sequence(applyDiff(o), setGame(o)); // so play the diff, then set the new round return modules.async.sequence(applyDiff(o), setGame(o)); // so play the diff, then set the new round
} else { } else {
@ -66,13 +66,13 @@ function Game(modules) {
function setGame(o) { function setGame(o) {
return function(f) { return function(f) {
setStatus(o.game); setStatus(o.state);
setCaptures(o.game); setCaptures(o.state);
[ [
[sets.river, o.game.river, RiverCard], [sets.river, o.state.public.river, RiverCard],
[sets.you.hand, o.game.players[modules.session.getKey()].hand, HandCard] [sets.you.hand, o.state.playerHand, HandCard]
].forEach(function(args) {setCardSet.apply(null, args)}); ].forEach(function(args) {setCardSet.apply(null, args)});
setTheirCards(o.game); setTheirCards(o.state);
handleStep(o)(f); handleStep(o)(f);
}; };
} }
@ -96,10 +96,10 @@ function Game(modules) {
function handleTurnedCard(o, f) { function handleTurnedCard(o, f) {
if(status.step == "Turned") { if(status.step == "Turned") {
setTurned(o.game.step.contents); setTurned(o.state.public.step.contents);
} else { } else {
if(status.step == "ToPlay" && o.game.playing == o.game.oyake) { if(status.step == "ToPlay" && o.state.public.playing == o.state.public.oyake) {
rest.className = ["card", "turn" + o.game.publicState.turns].join(' '); rest.className = ["card", "turn" + o.state.public.turns].join(' ');
} }
if(deck.lastChild.id != "rest") { if(deck.lastChild.id != "rest") {
deck.removeChild(deck.lastChild); deck.removeChild(deck.lastChild);
@ -120,7 +120,7 @@ function Game(modules) {
function theyScored(o, f) { function theyScored(o, f) {
modules.screen.dialog({ modules.screen.dialog({
text: modules.i18n.get('theyScored')(modules.room.name(o.game.playing)), text: modules.i18n.get('theyScored')(modules.room.name(o.state.public.playing)),
answers: [ answers: [
{label: 'ok', action: f} {label: 'ok', action: f}
] ]
@ -129,10 +129,10 @@ function Game(modules) {
function gameEnd(o, f) { function gameEnd(o, f) {
var winner, maxScore; var winner, maxScore;
for(var key in o.game.scores) { for(var key in o.state.public.scores) {
if(maxScore == undefined || o.game.scores[key] > maxScore) { if(maxScore == undefined || o.state.public.scores[key] > maxScore) {
winner = key; winner = key;
maxScore = o.game.scores[key]; maxScore = o.state.public.scores[key];
} }
} }
modules.screen.dialog({ modules.screen.dialog({
@ -151,7 +151,7 @@ function Game(modules) {
function applyDiff(o) { function applyDiff(o) {
return modules.async.sequence.apply(null, return modules.async.sequence.apply(null,
o.logs.map(animate).concat( o.logs.map(animate).concat(
modules.async.apply(setStatus, o.game), modules.async.apply(setStatus, o.state),
handleStep(o) handleStep(o)
) )
); );
@ -246,35 +246,35 @@ function Game(modules) {
function setStatus(game) { function setStatus(game) {
modules.dom.clear(status.dom); modules.dom.clear(status.dom);
status.step = game.step.tag; status.step = game.public.step.tag;
if(game.month != status.month) { if(game.public.month != status.month) {
status.month = game.month; status.month = game.public.month;
} }
status.dom.appendChild( status.dom.appendChild(
modules.dom.make('li', {textContent: modules.i18n.get('monthFlower')(modules.i18n.get(status.month))}) modules.dom.make('li', {textContent: modules.i18n.get('monthFlower')(modules.i18n.get(status.month))})
); );
var turn = null; var turn = null;
status.playing = modules.session.is(game.playing); status.playing = modules.session.is(game.public.playing);
if(status.playing) { if(status.playing) {
sets.you.hand.dom.classList.toggle("yourTurn", status.step == "ToPlay"); sets.you.hand.dom.classList.toggle("yourTurn", status.step == "ToPlay");
turn = modules.i18n.get("yourTurn"); turn = modules.i18n.get("yourTurn");
} else { } else {
sets.you.hand.dom.classList.remove("yourTurn"); sets.you.hand.dom.classList.remove("yourTurn");
turn = modules.i18n.get('playing')(modules.room.name(game.playing)); turn = modules.i18n.get('playing')(modules.room.name(game.public.playing));
} }
status.dom.appendChild(modules.dom.make('li', {textContent: turn})); status.dom.appendChild(modules.dom.make('li', {textContent: turn}));
} }
function setCaptures(game) { function setCaptures(game) {
for(var key in game.players) { for(var key in game.public.players) {
var elem = document.getElementById(modules.session.is(key) ? "you" : "them"); var elem = document.getElementById(modules.session.is(key) ? "you" : "them");
elem.getElementsByClassName('score')[0].textContent = game.scores[key] + " pts"; elem.getElementsByClassName('score')[0].textContent = game.public.scores[key] + " pts";
var byClass = {} var byClass = {}
Object.values(modules.hanafuda.Family).forEach(function(family) { Object.values(modules.hanafuda.Family).forEach(function(family) {
byClass[family.class] = elem.getElementsByClassName(family.class)[0]; byClass[family.class] = elem.getElementsByClassName(family.class)[0];
modules.dom.clear(byClass[family.class]); modules.dom.clear(byClass[family.class]);
}); });
game.players[key].meld.forEach(function(cardName) { game.public.players[key].meld.forEach(function(cardName) {
var card = new Card(cardName); var card = new Card(cardName);
byClass[card.value.family.class].appendChild(card.dom); byClass[card.value.family.class].appendChild(card.dom);
}); });
@ -294,7 +294,7 @@ function Game(modules) {
function setTheirCards(game) { function setTheirCards(game) {
var turnsTheyPlayed = Math.floor( var turnsTheyPlayed = Math.floor(
(game.publicState.turns + (modules.session.is(game.oyake) ? 0 : 1)) / 2 (game.public.turns + (modules.session.is(game.public.oyake) ? 0 : 1)) / 2
); );
modules.dom.clear(sets.them.hand.dom); modules.dom.clear(sets.them.hand.dom);
for(var i = 0; i < 8 - turnsTheyPlayed; i++) { for(var i = 0; i < 8 - turnsTheyPlayed; i++) {