about summary refs log tree commit diff
path: root/src/Xanthous/Game.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Xanthous/Game.hs')
-rw-r--r--src/Xanthous/Game.hs33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/Xanthous/Game.hs b/src/Xanthous/Game.hs
index bbcf19ede4af..14b8230218ab 100644
--- a/src/Xanthous/Game.hs
+++ b/src/Xanthous/Game.hs
@@ -31,12 +31,39 @@ module Xanthous.Game
     -- * App monad
   , AppT(..)
 
+    -- * Saving the game
+  , saveGame
+  , loadGame
+  , saved
+
     -- * Debug State
   , DebugState(..)
   , debugState
   , allRevealed
   ) where
 --------------------------------------------------------------------------------
-import Xanthous.Game.State
-import Xanthous.Game.Lenses
-import Xanthous.Game.Arbitrary ()
+import qualified Codec.Compression.Zlib as Zlib
+import           Codec.Compression.Zlib.Internal (DecompressError)
+import qualified Data.Aeson as JSON
+import           System.IO.Unsafe
+--------------------------------------------------------------------------------
+import           Xanthous.Prelude
+import           Xanthous.Game.State
+import           Xanthous.Game.Lenses
+import           Xanthous.Game.Arbitrary ()
+import           Xanthous.Entities.Entities ()
+--------------------------------------------------------------------------------
+
+saveGame :: GameState -> LByteString
+saveGame = Zlib.compress . JSON.encode
+
+loadGame :: LByteString -> Maybe GameState
+loadGame = JSON.decode <=< decompressZlibMay
+  where
+    decompressZlibMay bs
+      = unsafeDupablePerformIO
+      $ (let r = Zlib.decompress bs in r `seq` pure (Just r))
+      `catch` \(_ :: DecompressError) -> pure Nothing
+
+saved :: Prism' LByteString GameState
+saved = prism' saveGame loadGame