diff --git a/hannah.cabal b/hannah.cabal index f8b9360..7ae5d61 100644 --- a/hannah.cabal +++ b/hannah.cabal @@ -21,15 +21,19 @@ executable hannah other-modules: AI , Automaton , Config + , Session -- other-extensions: build-depends: aeson , base >=4.9 && <4.13 , bytestring , containers + , directory + , filepath , hanafuda , hanafuda-APILanguage , mtl , text , websockets hs-source-dirs: src + ghc-options: -Wall default-language: Haskell2010 diff --git a/src/Config.hs b/src/Config.hs index f6cad11..04a7c4a 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -1,5 +1,6 @@ module Config ( host + , libDir , path , port ) where @@ -7,6 +8,9 @@ module Config ( host :: String host = "koikoi.menf.in" +libDir :: FilePath +libDir = "/var/lib/hannah" + path :: String path = "/play/" diff --git a/src/Session.hs b/src/Session.hs new file mode 100644 index 0000000..fce1782 --- /dev/null +++ b/src/Session.hs @@ -0,0 +1,42 @@ +{-# LANGUAGE NamedFieldPuns #-} +module Session ( + State(..) + , initial + , store + ) where + +import Config (libDir) +import Data.Map (Map) +import qualified Data.Map as Map (empty) +import Hanafuda.KoiKoi (PlayerID) +import qualified Hanafuda.Message as Message (PublicGame) +import System.Directory (createDirectoryIfMissing, doesFileExist) +import System.FilePath (()) + +data State = + New + | Connected { + playerID :: PlayerID + , games :: Map PlayerID Message.PublicGame + } + deriving Show + +stateFile :: FilePath +stateFile = libDir "state" + +ifM :: IO Bool -> IO a -> IO a -> IO a +ifM condition caseTrue caseFalse = + condition >>= (\b -> if b then caseTrue else caseFalse) + +initial :: IO State +initial = do + createDirectoryIfMissing True libDir + ifM (doesFileExist stateFile) + (do + playerID <- read <$> readFile stateFile + return $ Connected {playerID, games = Map.empty} + ) + (return New) + +store :: PlayerID -> IO () +store = writeFile stateFile . show