Seperate libraries and some parser functionality

This commit is contained in:
Nox Sluijtman 2023-11-10 16:38:49 +01:00
parent 4c46789ae4
commit dcc2488e37
7 changed files with 177 additions and 133 deletions

5
Notes.md Normal file
View file

@ -0,0 +1,5 @@
# Some development notes
## Maps
Consider thinking about using the `Data.Map.Map` datatype instead of `[Skill]` and `[Stat]`.

View file

@ -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

View file

@ -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

View file

@ -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

120
lib/DND/Sheet/Content.hs Normal file
View file

@ -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)

35
lib/DND/Sheet/Parser.hs Normal file
View file

@ -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

View file

@ -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