From dcc2488e37c0b0399552ac02822867451fc57792 Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Fri, 10 Nov 2023 16:38:49 +0100 Subject: [PATCH] Seperate libraries and some parser functionality --- Notes.md | 5 ++ app/Main.hs | 16 ++--- lib/DND.hs | 4 +- lib/DND/Sheet.hs | 122 +-------------------------------------- lib/DND/Sheet/Content.hs | 120 ++++++++++++++++++++++++++++++++++++++ lib/DND/Sheet/Parser.hs | 35 +++++++++++ sheet-parser-hs.cabal | 8 ++- 7 files changed, 177 insertions(+), 133 deletions(-) create mode 100644 Notes.md create mode 100644 lib/DND/Sheet/Content.hs create mode 100644 lib/DND/Sheet/Parser.hs diff --git a/Notes.md b/Notes.md new file mode 100644 index 0000000..8f03685 --- /dev/null +++ b/Notes.md @@ -0,0 +1,5 @@ +# Some development notes + +## Maps + +Consider thinking about using the `Data.Map.Map` datatype instead of `[Skill]` and `[Stat]`. \ No newline at end of file diff --git a/app/Main.hs b/app/Main.hs index 2c9d29d..73e1297 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,17 +1,13 @@ module Main where -import Data.Aeson -import GHC.Generics -import qualified Data.ByteString.Lazy as B -import qualified Data.ByteString.Lazy.UTF8 as BSU -import DND.Sheet -import DND.Bob +import DND.Sheet.Parser testfile :: FilePath testfile = "./example.json" -createExample :: IO () -createExample = encodeFile testfile bob - main :: IO () -main = createExample +main = do + createExample testfile + sheet <- parseSheet testfile + putStrLn $ "wrote example character named \"" ++ getName sheet ++ "\" to: " ++ testfile + listSkills sheet \ No newline at end of file diff --git a/lib/DND.hs b/lib/DND.hs index c2a5916..0d0cfe2 100644 --- a/lib/DND.hs +++ b/lib/DND.hs @@ -1,7 +1,7 @@ module DND -( module DND.Sheet +( module DND.Sheet.Content , module DND.Bob ) where -import DND.Sheet +import DND.Sheet.Content import DND.Bob diff --git a/lib/DND/Sheet.hs b/lib/DND/Sheet.hs index 63bc7a6..b3df8aa 100644 --- a/lib/DND/Sheet.hs +++ b/lib/DND/Sheet.hs @@ -1,121 +1,5 @@ -{-# LANGUAGE DeriveGeneric #-} - module DND.Sheet -( Character (..) -, Preamble (..) -, Trivia (..) -, Skill (..) -, Stat (..) -, ScoreMod(..) -, Spell(..) -, SpellComponent(..) -, Feature(..) -) -where +( module DND.Sheet.Content +) where -import Data.Aeson -import GHC.Generics - -instance FromJSON Character -instance ToJSON Character - -instance FromJSON Preamble -instance ToJSON Preamble - -instance FromJSON Trivia -instance ToJSON Trivia - -instance FromJSON Stat -instance ToJSON Stat - -instance FromJSON Skill -instance ToJSON Skill - -instance FromJSON ScoreMod -instance ToJSON ScoreMod - -instance FromJSON Spell -instance ToJSON Spell - -instance FromJSON SpellComponent -instance ToJSON SpellComponent - -data ScoreMod = None - | Proficient - | Expertise - deriving (Show, Eq, Ord, Generic) - -data Character = Character - { preamble :: Preamble - , trivia :: Trivia - , stats :: [Stat] - , spells :: Maybe [Spell] - --, feats :: [Feat] - } deriving ( Show, Eq, Ord, Generic) - -data Preamble = Preamble - { charLevel :: Int - , charName :: String - , charRace :: String - , charClass :: String - , jackOfAllTrades :: Bool - } deriving ( Show, Eq, Ord, Generic) - -data Trivia = Trivia - { background :: String - , personalityTrait :: String - , ideals :: String - , bonds :: String - , flaws :: String - , quirk :: String - } deriving ( Show, Eq, Ord, Generic) - -data Skill = Skill - { skillName :: String - , skillMod :: ScoreMod - } deriving ( Show, Eq, Ord, Generic) - -data Stat = Stat - { stat :: String - , score :: Int - , proficient :: Bool - , skills :: [Skill] - } deriving ( Show, Eq, Ord, Generic) - -data Spell = Spell - { spellName :: String - , spellLevel :: Int - , castingTime :: String - , range :: Int - , components :: [SpellComponent] - , duration :: String - , attackSave :: String - , damageEffect :: String - , description :: String - , school :: String - , atHighLevel :: String - } deriving ( Show, Eq, Ord, Generic) - -data SpellComponent = Verbal - | Somatic - | Material - deriving ( Show, Eq, Ord, Generic) - -data Feature = StatIncrease - { increase :: [Stat] } - | SetProficiency - { changeModState :: [ScoreMod] - , featDescription :: String - } - | SetExpertise - { changeModState :: [ScoreMod] - , featDescription :: String - } - | AddSpell - { addSpell :: [Spell] - , featDescription :: String - } - | Roleplay - { featDescription :: String - } - deriving ( Show, Eq, Ord, Generic) +import DND.Sheet.Content diff --git a/lib/DND/Sheet/Content.hs b/lib/DND/Sheet/Content.hs new file mode 100644 index 0000000..bff79a3 --- /dev/null +++ b/lib/DND/Sheet/Content.hs @@ -0,0 +1,120 @@ +{-# LANGUAGE DeriveGeneric #-} + +module DND.Sheet.Content +( Character (..) +, Preamble (..) +, Trivia (..) +, Skill (..) +, Stat (..) +, ScoreMod(..) +, Spell(..) +, SpellComponent(..) +, Feature(..) +) where + +import Data.Aeson ( FromJSON, ToJSON ) +import GHC.Generics ( Generic ) + +instance FromJSON Character +instance ToJSON Character + +instance FromJSON Preamble +instance ToJSON Preamble + +instance FromJSON Trivia +instance ToJSON Trivia + +instance FromJSON Stat +instance ToJSON Stat + +instance FromJSON Skill +instance ToJSON Skill + +instance FromJSON ScoreMod +instance ToJSON ScoreMod + +instance FromJSON Spell +instance ToJSON Spell + +instance FromJSON SpellComponent +instance ToJSON SpellComponent + +data ScoreMod = None + | Proficient + | Expertise + deriving (Show, Eq, Ord, Generic, Enum) + +data Character = Character + { preamble :: Preamble + , trivia :: Trivia + , stats :: [Stat] + , spells :: Maybe [Spell] + --, feats :: [Feat] + } deriving ( Show, Eq, Ord, Generic) + +data Preamble = Preamble + { charLevel :: Int + , charName :: String + , charRace :: String + , charClass :: String + , jackOfAllTrades :: Bool + } deriving ( Show, Eq, Ord, Generic) + +data Trivia = Trivia + { background :: String + , personalityTrait :: String + , ideals :: String + , bonds :: String + , flaws :: String + , quirk :: String + } deriving ( Show, Eq, Ord, Generic) + +data Skill = Skill + { skillName :: String + , skillMod :: ScoreMod + } deriving ( Show, Eq, Ord, Generic) + +data Stat = Stat + { stat :: String + , score :: Int + , proficient :: Bool + , skills :: [Skill] + } deriving ( Show, Eq, Ord, Generic) + +data Spell = Spell + { spellName :: String + , spellLevel :: Int + , castingTime :: String + , range :: Int + , components :: [SpellComponent] + , duration :: String + , attackSave :: String + , damageEffect :: String + , description :: String + , school :: String + , atHighLevel :: String + } deriving ( Show, Eq, Ord, Generic) + +data SpellComponent = Verbal + | Somatic + | Material + deriving ( Show, Eq, Ord, Generic) + +data Feature = StatIncrease + { increase :: [Stat] } + | SetProficiency + { changeModState :: [ScoreMod] + , featDescription :: String + } + | SetExpertise + { changeModState :: [ScoreMod] + , featDescription :: String + } + | AddSpell + { addSpell :: [Spell] + , featDescription :: String + } + | Roleplay + { featDescription :: String + } + deriving ( Show, Eq, Ord, Generic) diff --git a/lib/DND/Sheet/Parser.hs b/lib/DND/Sheet/Parser.hs new file mode 100644 index 0000000..eee84ae --- /dev/null +++ b/lib/DND/Sheet/Parser.hs @@ -0,0 +1,35 @@ +module DND.Sheet.Parser +(getSheet +, parseSheet +, listSkills +, createExample +, getName +) where + +import Data.Aeson +import DND.Sheet.Content +import DND.Bob(bob) +import qualified Data.ByteString.Lazy as B +import qualified Data.ByteString.Lazy.UTF8 as BSU +import Data.Maybe (fromJust) + +getSheet :: FilePath -> IO B.ByteString +getSheet = B.readFile + +parseSheet :: FilePath -> IO Character +parseSheet x = do + file <- B.readFile x + let character = decode file :: Maybe Character + return $ fromJust character + +listSkills :: Character -> IO () +listSkills = mapM_ (mapM_ putStrLn) + . filter (not . null) + . map (map skillName . skills) + . stats + +createExample :: FilePath -> IO () +createExample = flip encodeFile bob + +getName :: Character -> String +getName = charName . preamble \ No newline at end of file diff --git a/sheet-parser-hs.cabal b/sheet-parser-hs.cabal index 616bee8..8d363e0 100644 --- a/sheet-parser-hs.cabal +++ b/sheet-parser-hs.cabal @@ -57,11 +57,15 @@ library import: warnings exposed-modules: DND - , DND.Sheet , DND.Bob + , DND.Sheet + , DND.Sheet.Content + , DND.Sheet.Parser build-depends: base ^>=4.16.4.0 , aeson + , bytestring + , utf8-string hs-source-dirs: lib @@ -84,9 +88,9 @@ executable sheet-parser-hs build-depends: base ^>=4.16.4.0 , optparse-applicative , aeson + , sheet-parser-hs , bytestring , utf8-string - , sheet-parser-hs -- Directories containing source files. hs-source-dirs: app