about summary refs log tree commit diff
path: root/src/Main.hs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-11-29T19·33-0500
committerGriffin Smith <root@gws.fyi>2019-11-29T19·33-0500
commitf37d0f75c0b4a77c8e35192c24c6fdb6f2bc4619 (patch)
tree0af3e636f1a2dcb0a0e179895e4a41f2fab45f69 /src/Main.hs
parent2f2e5a0b684f886a7585161d30e8cda962c7eefb (diff)
Implement saving+loading the game
Implement ToJSON and FromJSON for all of the various pieces of the game
state, and add a pair of functions saveGame/loadGame implementing a
prism to save the game as zlib-compressed JSON. To test this, there's
now Arbitrary, CoArbitrary, and Function instances for all the parts of
the game state - to get around circular imports with the concrete
entities this unfortunately is happening via orphan instances, plus an
hs-boot file to break a circular import that was just a little too hard
to remove by moving things around. Ugh.
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 61640c3646..2e9d8c41ee 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -6,6 +6,7 @@ import qualified Options.Applicative as Opt
 import           System.Random
 import           Control.Monad.Random (getRandom)
 import           Control.Exception (finally)
+import           System.Exit (die)
 --------------------------------------------------------------------------------
 import qualified Xanthous.Game as Game
 import           Xanthous.App (makeApp)
@@ -45,6 +46,7 @@ parseRunParams = RunParams
 
 data Command
   = Run RunParams
+  | Load FilePath
   | Generate GeneratorInput Dimensions
 
 parseDimensions :: Opt.Parser Dimensions
@@ -64,6 +66,10 @@ parseCommand = (<|> Run <$> parseRunParams) $ Opt.subparser
       (Opt.info
        (Run <$> parseRunParams)
        (Opt.progDesc "Run the game"))
+  <> Opt.command "load"
+      (Opt.info
+       (Load <$> Opt.argument Opt.str (Opt.metavar "FILE"))
+       (Opt.progDesc "Load a saved game"))
   <> Opt.command "generate"
       (Opt.info
        (Generate
@@ -78,6 +84,9 @@ optParser = Opt.info
   (parseCommand <**> Opt.helper)
   (Opt.header "Xanthous: a WIP TUI RPG")
 
+thanks :: IO ()
+thanks = putStr "\n\n" >> putStrLn "Thanks for playing Xanthous!"
+
 runGame :: RunParams -> IO ()
 runGame rparams = do
   app <- makeApp
@@ -94,6 +103,15 @@ runGame rparams = do
     putStr "\n\n"
   pure ()
 
+loadGame :: FilePath -> IO ()
+loadGame saveFile = do
+  app <- makeApp
+  gameState <- maybe (die "Invalid save file!") pure
+              =<< Game.loadGame . fromStrict <$> readFile @IO saveFile
+  _game' <- gameState `deepseq` defaultMain app gameState `finally` thanks
+  pure ()
+
+
 runGenerate :: GeneratorInput -> Dimensions -> IO ()
 runGenerate input dims = do
   randGen <- getStdGen
@@ -109,6 +127,7 @@ runGenerate input dims = do
 
 runCommand :: Command -> IO ()
 runCommand (Run runParams) = runGame runParams
+runCommand (Load saveFile) = loadGame saveFile
 runCommand (Generate input dims) = runGenerate input dims
 
 main :: IO ()