2019-08-24 23:29:40 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
module Messaging (
|
|
|
|
FromClient(..)
|
|
|
|
, T(..)
|
|
|
|
, broadcast
|
|
|
|
, get
|
|
|
|
, notifyPlayers
|
|
|
|
, receive
|
|
|
|
, relay
|
|
|
|
, send
|
|
|
|
, sendTo
|
|
|
|
) where
|
|
|
|
|
2019-10-13 22:00:35 +02:00
|
|
|
import qualified App (Context(..), T, connection, debug, server)
|
|
|
|
import Control.Monad.Reader (asks, lift)
|
|
|
|
import Data.Aeson (eitherDecode', encode)
|
|
|
|
import Data.ByteString.Lazy.Char8 (unpack)
|
2019-08-24 23:29:40 +02:00
|
|
|
import Data.Foldable (forM_)
|
2019-10-13 22:00:35 +02:00
|
|
|
import Data.List (intercalate)
|
2019-08-24 23:29:40 +02:00
|
|
|
import Data.Map (keys)
|
2019-10-17 18:58:39 +02:00
|
|
|
import qualified Hanafuda.KoiKoi as KoiKoi (Action, Game(..), PlayerID)
|
2019-10-13 22:00:35 +02:00
|
|
|
import Hanafuda.Message (FromClient(..), T(..))
|
|
|
|
import qualified Hanafuda.Message as Message (T)
|
2019-08-24 23:29:40 +02:00
|
|
|
import Network.WebSockets (receiveData, sendTextData)
|
2019-10-22 17:52:13 +02:00
|
|
|
import qualified Game (exportGame)
|
2019-08-24 23:29:40 +02:00
|
|
|
import qualified Server (T(..), get)
|
2019-10-13 22:00:35 +02:00
|
|
|
import qualified Session (T(..))
|
2019-08-24 23:29:40 +02:00
|
|
|
|
|
|
|
sendTo :: [KoiKoi.PlayerID] -> Message.T -> App.T ()
|
|
|
|
sendTo playerIDs obj = do
|
|
|
|
sessions <- getSessions <$> App.server
|
|
|
|
App.debug $ '(' : intercalate ", " recipients ++ ") <" ++ (unpack encoded)
|
|
|
|
lift $ forM_ (Session.connection <$> sessions) $ flip sendTextData encoded
|
|
|
|
where
|
|
|
|
encoded = encode $ obj
|
|
|
|
getSessions server = (\playerID -> Server.get playerID server) <$> playerIDs
|
|
|
|
recipients = show <$> playerIDs
|
|
|
|
|
|
|
|
send :: Message.T -> App.T ()
|
|
|
|
send obj = do
|
2019-11-05 18:14:24 +01:00
|
|
|
sessionID <- asks App.sessionID
|
|
|
|
sendTo [sessionID] obj
|
2019-08-24 23:29:40 +02:00
|
|
|
|
|
|
|
broadcast :: Message.T -> App.T ()
|
|
|
|
broadcast obj =
|
|
|
|
App.server >>= flip sendTo obj . keys . Server.sessions
|
|
|
|
|
|
|
|
relay :: FromClient -> (Message.T -> App.T ()) -> App.T ()
|
|
|
|
relay message f = do
|
|
|
|
App.debug "Relaying"
|
2019-11-05 18:14:24 +01:00
|
|
|
(\from -> f $ Relay {from, message}) =<< asks App.sessionID
|
2019-08-24 23:29:40 +02:00
|
|
|
|
|
|
|
receive :: App.T FromClient
|
|
|
|
receive = do
|
|
|
|
received <- ((lift . receiveData) =<< App.connection)
|
|
|
|
App.debug $ '>':(unpack received)
|
|
|
|
case eitherDecode' received of
|
|
|
|
Left errorMessage -> send (Error errorMessage) >> receive
|
|
|
|
Right clientMessage -> return clientMessage
|
|
|
|
|
|
|
|
get :: App.T FromClient
|
|
|
|
get =
|
|
|
|
receive >>= pong
|
|
|
|
where
|
|
|
|
pong Ping = send Pong >> get
|
|
|
|
pong m = return m
|
|
|
|
|
|
|
|
notifyPlayers :: KoiKoi.Game -> [KoiKoi.Action] -> App.T ()
|
|
|
|
notifyPlayers game logs =
|
2019-10-17 18:58:39 +02:00
|
|
|
forM_ (keys $ KoiKoi.scores game) $ \k -> do
|
2019-10-22 17:52:13 +02:00
|
|
|
state <- Game.exportGame k game
|
2019-10-17 18:58:39 +02:00
|
|
|
sendTo [k] $ Game {state, logs}
|