built database

This commit is contained in:
Jonathan Lamothe
2019-08-16 11:49:04 -04:00
parent 505d0d8e96
commit 0be1a64119
2 changed files with 60 additions and 0 deletions
+35
View File
@@ -3,11 +3,15 @@
module Mtlstats.Types (
-- * Types
ProgState (..),
Database (..),
Player (..),
PlayerStats (..),
Goalie (..),
GoalieStats (..),
-- * Lenses
-- ** Database Lenses
dbPlayers,
dbGoalies,
-- ** Player Lenses
pNumber,
pName,
@@ -32,6 +36,7 @@ module Mtlstats.Types (
gsLosses,
gsTies,
-- * Constructors
newDatabase,
newPlayer,
newPlayerStats,
newGoalie,
@@ -58,6 +63,28 @@ import Lens.Micro.TH (makeLenses)
-- | Represents the program state
data ProgState = ProgState
-- | Represents the database
data Database = Database
{ _dbPlayers :: [Player]
-- ^ The list of players
, _dbGoalies :: [Goalie]
-- ^ The lidt of goalies
} deriving (Eq, Show)
instance FromJSON Database where
parseJSON = withObject "Database" $ \v -> Database
<$> v .: "players"
<*> v .: "goalies"
instance ToJSON Database where
toJSON (Database ps gs) = object
[ "players" .= ps
, "goalies" .= gs
]
toEncoding (Database ps gs) = pairs $
"players" .= ps <>
"goalies" .= gs
-- | Represents a (non-goalie) player
data Player = Player
{ _pNumber :: Int
@@ -201,11 +228,19 @@ instance ToJSON GoalieStats where
"losses" .= l <>
"ties" .= t
makeLenses ''Database
makeLenses ''Player
makeLenses ''PlayerStats
makeLenses ''Goalie
makeLenses ''GoalieStats
-- | Constructor for a 'Database'
newDatabase :: Database
newDatabase = Database
{ _dbPlayers = []
, _dbGoalies = []
}
-- | Constructor for a 'Player'
newPlayer
:: Int