From 323ca81c96e4186747f06b6178d71d49e98c6066 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Fri, 31 Jul 2020 13:35:17 +0200 Subject: Sync opponent proposals --- server/lib/Cafp/Game.hs | 10 ++++++++-- server/lib/Cafp/Main/GenerateElmTypes.hs | 1 + server/lib/Cafp/Messages.hs | 9 ++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) (limited to 'server') diff --git a/server/lib/Cafp/Game.hs b/server/lib/Cafp/Game.hs index 336b85c..9c2d2e4 100644 --- a/server/lib/Cafp/Game.hs +++ b/server/lib/Cafp/Game.hs @@ -20,6 +20,7 @@ import Cafp.Messages import Control.Lens (at, ix, over, to, (%~), (&), (.~), (^.), (^?), _2) import Control.Lens.TH (makeLenses, makePrisms) +import Control.Monad (guard) import qualified Data.HashMap.Strict as HMS import Data.Maybe (fromMaybe) import Data.Text (Text) @@ -73,14 +74,19 @@ processClientMessage pid msg game = case msg of -- Bad card proposed. | not $ validWhiteCard (game ^. gameCards) c -> game -- Proposal already made. - | Just _ <- game ^? gameTable . _TableProposing . _2 . at pid -> game + | Just _ <- game ^? gameTable . _TableProposing . _2 . ix pid -> game -- TODO: Check that the card is in the hand of the player. | otherwise -> game & gameTable . _TableProposing . _2 . at pid .~ Just c gameViewForPlayer :: PlayerId -> Game -> GameView gameViewForPlayer self game = - let opponents = map snd . HMS.toList . HMS.delete self $ game ^. gamePlayers + let opponents = do + (pid, oname) <- HMS.toList $ game ^. gamePlayers + guard $ pid /= self + pure $ Opponent oname $ case game ^. gameTable of + TableProposing _ proposals -> HMS.member pid proposals + name = fromMaybe "" $ game ^. gamePlayers . at self table = case game ^. gameTable of diff --git a/server/lib/Cafp/Main/GenerateElmTypes.hs b/server/lib/Cafp/Main/GenerateElmTypes.hs index 677bc5c..b1e6efe 100644 --- a/server/lib/Cafp/Main/GenerateElmTypes.hs +++ b/server/lib/Cafp/Main/GenerateElmTypes.hs @@ -13,6 +13,7 @@ main = putStrLn $ makeElmModule "Messages" [ DefineElm (Proxy :: Proxy BlackCard) , DefineElm (Proxy :: Proxy WhiteCard) , DefineElm (Proxy :: Proxy Cards) + , DefineElm (Proxy :: Proxy Opponent) , DefineElm (Proxy :: Proxy TableView) , DefineElm (Proxy :: Proxy GameView) , DefineElm (Proxy :: Proxy ServerMessage) diff --git a/server/lib/Cafp/Messages.hs b/server/lib/Cafp/Messages.hs index ff3f612..dc17168 100644 --- a/server/lib/Cafp/Messages.hs +++ b/server/lib/Cafp/Messages.hs @@ -3,6 +3,7 @@ module Cafp.Messages ( BlackCard (..) , WhiteCard (..) , Cards (..) + , Opponent (..) , TableView (..) , GameView (..) , ServerMessage (..) @@ -22,12 +23,17 @@ data Cards = Cards , cardsWhite :: Vector Text } deriving (Show) +data Opponent = Opponent + { opponentName :: Text + , opponentReady :: Bool + } deriving (Show) + data TableView = Proposing BlackCard (Maybe WhiteCard) deriving (Show) data GameView = GameView - { gameViewOpponents :: [Text] + { gameViewOpponents :: [Opponent] , gameViewMyName :: Text , gameViewTable :: TableView , gameViewHand :: [WhiteCard] @@ -48,6 +54,7 @@ data ClientMessage deriveBoth defaultOptions ''BlackCard deriveBoth defaultOptions ''WhiteCard deriveBoth (defaultOptionsDropLower 5) ''Cards +deriveBoth (defaultOptionsDropLower 8) ''Opponent deriveBoth defaultOptions ''TableView deriveBoth (defaultOptionsDropLower 8) ''GameView deriveBoth defaultOptions ''ServerMessage -- cgit v1.2.3