Remove all game storing from the server : a lot of remaining protocol operations are now meaningless and will require a huge redesign

This commit is contained in:
Tissevert 2019-10-23 15:27:09 +02:00
parent d1eb8e957e
commit 8c1902e6fd
4 changed files with 14 additions and 45 deletions

View file

@ -5,17 +5,13 @@ module Automaton (
import qualified App (Context(..), T, current, debug, get, server, try, update_) import qualified App (Context(..), T, current, debug, get, server, try, update_)
import Control.Monad.Reader (asks) import Control.Monad.Reader (asks)
import Data.Map (Map, (!?))
import qualified Game (new, play) import qualified Game (new, play)
import qualified Hanafuda.KoiKoi as KoiKoi (
Game(..), GameID, Step(..)
)
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, update
) )
import qualified RW (RW(..)) import qualified RW (RW(..))
import qualified Server (endGame, get, 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 :: Session.Status -> Message.FromClient -> App.T ()
@ -51,10 +47,9 @@ receive (Session.Answering to) message@(Message.Answer {Message.accept}) = do
newStatus <- newStatus <-
if accept if accept
then do then do
gameID <- Game.new (for, to) game <- Game.new (for, to)
game <- Server.get gameID <$> App.server
Messaging.notifyPlayers game [] Messaging.notifyPlayers game []
return $ Session.Playing gameID return Session.Playing
else do else do
Messaging.broadcast $ Messaging.update {Message.alone = [for, to]} Messaging.broadcast $ Messaging.update {Message.alone = [for, to]}
return $ Session.LoggedIn True return $ Session.LoggedIn True
@ -62,27 +57,14 @@ receive (Session.Answering to) message@(Message.Answer {Message.accept}) = do
setSessionStatus newStatus setSessionStatus newStatus
_ -> sendError "They're not waiting for your answer" _ -> sendError "They're not waiting for your answer"
receive (Session.Playing gameID) (Message.Play {Message.move, Message.onGame}) = do receive Session.Playing (Message.Play {Message.move, Message.onGame}) = do
playerID <- asks App.playerID playerID <- asks App.playerID
result <- Game.play playerID move onGame result <- Game.play playerID move onGame
case result of case result of
Left message -> sendError message Left message -> sendError message
Right (newGame, logs) -> do Right (newGame, logs) -> Messaging.notifyPlayers newGame logs
case KoiKoi.step newGame of
KoiKoi.Over -> do
App.debug $ "Game " ++ show gameID ++ " ended"
App.update_ $ Server.endGame gameID
_ -> return ()
Messaging.notifyPlayers newGame logs
receive (Session.Playing gameID) Message.Quit = do receive Session.Playing Message.Quit = setSessionStatus (Session.LoggedIn True)
games <- (RW.get <$> App.server :: App.T (Map KoiKoi.GameID KoiKoi.Game))
case games !? gameID of
Nothing -> do
playerID <- asks App.playerID
Messaging.broadcast $ Messaging.update {Message.alone = [playerID]}
setSessionStatus (Session.LoggedIn True)
_ -> sendError "Game is still running"
receive state _ = sendError $ "Invalid message in state " ++ show state receive state _ = sendError $ "Invalid message in state " ++ show state

View file

@ -5,7 +5,7 @@ module Game (
, play , play
) where ) where
import qualified App (T, server, update) import qualified App (T, server)
import Control.Monad.Except (runExceptT) import Control.Monad.Except (runExceptT)
import Control.Monad.Reader (lift) import Control.Monad.Reader (lift)
import Control.Monad.Writer (runWriterT) import Control.Monad.Writer (runWriterT)
@ -17,7 +17,7 @@ import Data.ByteString (ByteString)
import Data.ByteString.Lazy (fromStrict, toStrict) import Data.ByteString.Lazy (fromStrict, toStrict)
import Data.Map ((!), Map, mapWithKey) import Data.Map ((!), Map, mapWithKey)
import qualified Hanafuda (Pack) import qualified Hanafuda (Pack)
import Hanafuda.KoiKoi (Game, GameID, Mode(..), Player, PlayerID, Players) import Hanafuda.KoiKoi (Game, Mode(..), Player, PlayerID, Players)
import qualified Hanafuda.KoiKoi as KoiKoi ( import qualified Hanafuda.KoiKoi as KoiKoi (
Action, Game(..), Move(..), play, new Action, Game(..), Move(..), play, new
) )
@ -25,11 +25,10 @@ import Hanafuda.Message (PrivateState(..), PublicGame(..), PublicPlayer(..), Pub
import qualified Hanafuda.Player as Player (Player(..), Players(..), get) import qualified Hanafuda.Player as Player (Player(..), Players(..), get)
import Keys (T(..)) import Keys (T(..))
import qualified Keys (public, secret) import qualified Keys (public, secret)
import qualified Server (T(..), register) import qualified Server (T(..))
new :: (PlayerID, PlayerID) -> App.T GameID new :: (PlayerID, PlayerID) -> App.T Game
new (for, to) = new (for, to) = lift $ KoiKoi.new (for, to) WholeYear
Server.register <$> (lift $ KoiKoi.new (for, to) WholeYear) >>= App.update
exportPlayers :: Game -> Map PlayerID Player exportPlayers :: Game -> Map PlayerID Player
exportPlayers game = exportPlayers game =

View file

@ -6,7 +6,6 @@
module Server ( module Server (
T(..) T(..)
, disconnect , disconnect
, endGame
, get , get
, logIn , logIn
, logOut , logOut
@ -21,7 +20,7 @@ import qualified Data.Map as Map (empty)
import Data.Set (Set, member) import Data.Set (Set, member)
import qualified Data.Set as Set (delete, empty, insert) import qualified Data.Set as Set (delete, empty, insert)
import Data.Text (Text) import Data.Text (Text)
import Hanafuda.KoiKoi (Game, GameID, PlayerID) import Hanafuda.KoiKoi (PlayerID)
import Hanafuda.Message (PlayerStatus(..), Room) import Hanafuda.Message (PlayerStatus(..), Room)
import Keys (getKeys) import Keys (getKeys)
import qualified Keys (T) import qualified Keys (T)
@ -31,12 +30,10 @@ import qualified Session (Status(..), T(..), Update)
type Names = Set Text type Names = Set Text
type Players = Map PlayerID Text type Players = Map PlayerID Text
type Sessions = Map PlayerID Session.T type Sessions = Map PlayerID Session.T
type Games = Map GameID Game
data T = T { data T = T {
names :: Names names :: Names
, players :: Players , players :: Players
, sessions :: Sessions , sessions :: Sessions
, games :: Games
, keys :: Keys.T , keys :: Keys.T
} }
@ -52,10 +49,6 @@ instance RW.RW Sessions T where
get = sessions get = sessions
set sessions server = server {sessions} set sessions server = server {sessions}
instance RW.RW Games T where
get = games
set games server = server {games}
export :: Sessions -> PlayerID -> Text -> PlayerStatus export :: Sessions -> PlayerID -> Text -> PlayerStatus
export sessions playerID playerName = PlayerStatus (playerName, alone) export sessions playerID playerName = PlayerStatus (playerName, alone)
where where
@ -72,7 +65,6 @@ new = getKeys >>= \keys -> return $ T {
names = Set.empty names = Set.empty
, players = Map.empty , players = Map.empty
, sessions = Map.empty , sessions = Map.empty
, games = Map.empty
, keys , keys
} }
@ -92,10 +84,6 @@ disconnect :: PlayerID -> T -> T
disconnect playerID = disconnect playerID =
RW.update (delete playerID :: Sessions -> Sessions) . logOut playerID RW.update (delete playerID :: Sessions -> Sessions) . logOut playerID
endGame :: GameID -> T -> T
endGame playerID =
RW.update (delete playerID :: Games -> Games)
logIn :: Text -> PlayerID -> T -> Either String T logIn :: Text -> PlayerID -> T -> Either String T
logIn name playerID server = logIn name playerID server =
RW.update (Set.insert name) . RW.update (Set.insert name) .

View file

@ -7,7 +7,7 @@ module Session (
, open , open
) where ) where
import Hanafuda.KoiKoi (GameID, PlayerID) import Hanafuda.KoiKoi (PlayerID)
import Network.WebSockets (Connection) import Network.WebSockets (Connection)
import qualified RW (RW(..)) import qualified RW (RW(..))
@ -15,7 +15,7 @@ data Status =
LoggedIn Bool LoggedIn Bool
| Answering PlayerID | Answering PlayerID
| Waiting PlayerID | Waiting PlayerID
| Playing GameID | Playing
deriving (Show) deriving (Show)
data T = T { data T = T {