Simplify saving / loading hannah's state
This commit is contained in:
parent
b95a7c958d
commit
22c15bd30e
1 changed files with 6 additions and 10 deletions
|
@ -24,19 +24,15 @@ data State =
|
||||||
stateFile :: FilePath
|
stateFile :: FilePath
|
||||||
stateFile = libDir </> "state"
|
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 :: IO State
|
||||||
initial = do
|
initial = do
|
||||||
createDirectoryIfMissing True libDir
|
createDirectoryIfMissing True libDir
|
||||||
ifM (doesFileExist stateFile)
|
fileExists <- doesFileExist stateFile
|
||||||
(do
|
if fileExists
|
||||||
|
then do
|
||||||
playerID <- read <$> readFile stateFile
|
playerID <- read <$> readFile stateFile
|
||||||
return $ Connected {playerID, games = Map.empty}
|
return $ Connected {playerID, games = Map.empty}
|
||||||
)
|
else return New
|
||||||
(return New)
|
|
||||||
|
|
||||||
store :: PlayerID -> IO ()
|
store :: PlayerID -> IO ()
|
||||||
store = writeFile stateFile . show
|
store = writeFile stateFile . show
|
||||||
|
|
Loading…
Reference in a new issue