WIP : Start redesigning the protocol / informations kept on the server. Breaks pretty much everything

This commit is contained in:
Tissevert 2019-10-23 17:47:18 +02:00
parent 8c1902e6fd
commit a05d57fcea
3 changed files with 12 additions and 23 deletions

View file

@ -8,37 +8,36 @@ import Control.Monad.Reader (asks)
import qualified Game (new, play) import qualified Game (new, play)
import qualified Hanafuda.Message as Message (FromClient(..), T(..)) import qualified Hanafuda.Message as Message (FromClient(..), T(..))
import qualified Messaging ( import qualified Messaging (
broadcast, get, notifyPlayers, relay, send, sendTo, update broadcast, get, notifyPlayers, relay, send, sendTo
) )
import qualified RW (RW(..)) import qualified RW (RW(..))
import qualified Server (logIn, logOut, update, room) import qualified Server (logIn, logOut, update, room)
import qualified Session (Status(..), T(..), Update) import qualified Session (Status(..), T(..), Update)
receive :: Session.Status -> Message.FromClient -> App.T () receive :: Message.FromClient -> Bool -> App.T ()
receive (Session.LoggedIn False) logIn@(Message.LogIn login) = receive logIn@(Message.LogIn login) False =
asks App.playerID >>= App.try . (Server.logIn login) asks App.playerID >>= App.try . (Server.logIn login)
>>= maybe >>= maybe
(Messaging.relay logIn Messaging.broadcast >> setSessionStatus (Session.LoggedIn True)) (Messaging.relay logIn Messaging.broadcast >> setSessionStatus (Session.LoggedIn True))
sendError sendError
receive (Session.LoggedIn True) logOut@Message.LogOut = do receive logOut@Message.LogOut True = do
Messaging.relay logOut Messaging.broadcast Messaging.relay logOut Messaging.broadcast
asks App.playerID >>= App.update_ . Server.logOut asks App.playerID >>= App.update_ . Server.logOut
setSessionStatus (Session.LoggedIn False) setSessionStatus (Session.Status False)
receive (Session.LoggedIn True) invitation@(Message.Invitation {Message.to}) = do receive invitation@(Message.Invitation {Message.to}) True = do
session <- App.get to session <- App.get to
case Session.status session of case Session.status session of
Session.LoggedIn True -> do Session.LoggedIn True -> do
from <- asks App.playerID from <- asks App.playerID
App.update_ (Server.update to (RW.set $ Session.Answering from :: Session.Update)) App.update_ (Server.update to (RW.set $ Session.Answering from :: Session.Update))
Messaging.broadcast $ Messaging.update {Message.paired = [from, to]}
(Messaging.relay invitation $ Messaging.sendTo [to]) (Messaging.relay invitation $ Messaging.sendTo [to])
setSessionStatus (Session.Waiting to) setSessionStatus (Session.Waiting to)
_ -> sendError "They just left" _ -> sendError "They just left"
receive (Session.Answering to) message@(Message.Answer {Message.accept}) = do receive (Session.LoggedIn True) message@(Message.Answer {Message.accept}) = do
session <- App.get to session <- App.get to
playerID <- asks App.playerID playerID <- asks App.playerID
case Session.status session of case Session.status session of
@ -51,7 +50,6 @@ receive (Session.Answering to) message@(Message.Answer {Message.accept}) = do
Messaging.notifyPlayers game [] Messaging.notifyPlayers game []
return Session.Playing return Session.Playing
else do else do
Messaging.broadcast $ Messaging.update {Message.alone = [for, to]}
return $ Session.LoggedIn True return $ Session.LoggedIn True
App.update_ $ Server.update to (RW.set newStatus :: Session.Update) App.update_ $ Server.update to (RW.set newStatus :: Session.Update)
setSessionStatus newStatus setSessionStatus newStatus
@ -80,8 +78,7 @@ setSessionStatus newStatus = do
loop :: App.T () loop :: App.T ()
loop = do loop = do
message <- Messaging.get message <- Messaging.get
status <- Session.status <$> App.current receive message (Status.loggedIn . Session.status <$> App.current)
status `receive` message
loop loop
start :: App.T () start :: App.T ()

View file

@ -10,7 +10,6 @@ module Messaging (
, relay , relay
, send , send
, sendTo , sendTo
, update
) where ) where
import qualified App (Context(..), T, connection, debug, server) import qualified App (Context(..), T, connection, debug, server)
@ -67,9 +66,6 @@ get =
pong Ping = send Pong >> get pong Ping = send Pong >> get
pong m = return m pong m = return m
update :: T
update = Update {alone = [], paired = []}
notifyPlayers :: KoiKoi.Game -> [KoiKoi.Action] -> App.T () notifyPlayers :: KoiKoi.Game -> [KoiKoi.Action] -> App.T ()
notifyPlayers game logs = notifyPlayers game logs =
forM_ (keys $ KoiKoi.scores game) $ \k -> do forM_ (keys $ KoiKoi.scores game) $ \k -> do

View file

@ -7,16 +7,12 @@ module Session (
, open , open
) where ) where
import Hanafuda.KoiKoi (PlayerID)
import Network.WebSockets (Connection) import Network.WebSockets (Connection)
import qualified RW (RW(..)) import qualified RW (RW(..))
data Status = newtype Status = Status {
LoggedIn Bool loggedIn :: Bool
| Answering PlayerID } deriving (Show)
| Waiting PlayerID
| Playing
deriving (Show)
data T = T { data T = T {
connection :: Connection connection :: Connection
@ -31,5 +27,5 @@ instance RW.RW Status T where
open :: Connection -> T open :: Connection -> T
open connection = T { open connection = T {
connection connection
, status = LoggedIn False , status = Status False
} }