Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parsing JSON players file with Aeson
- Loading branch information
1 parent
f4010b9
commit cb6cb51
Showing
5 changed files
with
64 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,49 @@ | ||
module Main where | ||
|
||
import Paizo.Core.Types (Player) | ||
import Paizo.Plugin.FileLoader (getPlayers) | ||
|
||
import Control.Monad (when) | ||
import Control.Monad.State | ||
import System.IO (stdin, stdout, hSetEcho, hSetBuffering, BufferMode(..)) | ||
|
||
data AppConfig = AppConfig | ||
{ crRound :: Int | ||
} deriving (Show) | ||
data AppConfig = | ||
AppConfig { players :: [Player] | ||
, crRound :: Int | ||
} deriving (Show) | ||
|
||
type App = StateT AppConfig IO | ||
|
||
loadEngine :: App () | ||
loadEngine = liftIO $ do | ||
hSetEcho stdin False | ||
hSetBuffering stdin NoBuffering | ||
hSetBuffering stdout NoBuffering | ||
initGUI :: App () | ||
initGUI = liftIO $ do | ||
hSetEcho stdin False | ||
hSetBuffering stdin NoBuffering | ||
hSetBuffering stdout NoBuffering | ||
|
||
printT :: (MonadIO m) => String -> m () | ||
printT s = liftIO . putStrLn $ s | ||
|
||
loadGame :: App () | ||
loadGame = printT "Loading game" | ||
loadGame = do | ||
printT "Loading game" | ||
players <- liftIO getPlayers | ||
put AppConfig { players = players, crRound = 0 } | ||
|
||
gameLoop :: App () | ||
gameLoop = do | ||
printT "Play round" | ||
config <- get | ||
put $ AppConfig { crRound = crRound config + 1 } | ||
put $ AppConfig { players = players config, crRound = crRound config + 1 } | ||
char <- liftIO getChar | ||
when (char == 'c') gameLoop | ||
|
||
main2 :: App () | ||
main2 = do | ||
loadEngine | ||
loadGame | ||
gameLoop | ||
main2 = initGUI >> loadGame >> gameLoop | ||
|
||
main :: IO () | ||
main = do | ||
putStrLn "hello world" | ||
--main :: IO () | ||
main = runApp main2 | ||
|
||
runApp :: App a -> IO (a, AppConfig) | ||
runApp k = | ||
let config = AppConfig 0 | ||
let config = AppConfig [] 0 | ||
in runStateT k config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
|
||
module Paizo.Core.Types ( | ||
-- * Player | ||
Player(..) | ||
) where | ||
|
||
import Data.Aeson | ||
import GHC.Generics | ||
|
||
data Player = Player { | ||
hitPoints :: Int | ||
, consScore :: Int | ||
} deriving (Show) | ||
} deriving (Show, Generic) | ||
|
||
-- Instances to convert our type to/from JSON. | ||
|
||
instance FromJSON Player | ||
instance ToJSON Player |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Paizo.Plugin.FileLoader ( | ||
-- * Functions | ||
getPlayers | ||
) where | ||
|
||
import Paizo.Core.Types (Player) | ||
|
||
import Control.Monad.IO.Class (liftIO) | ||
import Data.Aeson (eitherDecode) | ||
import qualified Data.ByteString.Lazy as B | ||
|
||
-- | Location of the players file | ||
jsonFile :: FilePath | ||
jsonFile = "players.json" | ||
|
||
-- | Read the local copy of the JSON file. | ||
getJSON :: IO B.ByteString | ||
getJSON = B.readFile jsonFile | ||
|
||
-- | Returns the list of players from JSON file | ||
getPlayers :: IO [Player] | ||
getPlayers = do | ||
d <- (eitherDecode <$> getJSON) :: IO (Either String [Player]) | ||
case d of | ||
Left err -> return [] | ||
Right players -> return players |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
{ "hitPoints": 20, "consScore": 5 }, | ||
{ "hitPoints": 15, "consScore": 7 } | ||
] |