2019-08-19 23:57:24 -04:00
|
|
|
{- |
|
|
|
|
|
|
|
|
mtlstats
|
|
|
|
Copyright (C) 2019 Rhéal Lamothe
|
|
|
|
<rheal.lamothe@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or (at
|
|
|
|
your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2019-08-22 13:05:25 -04:00
|
|
|
{-# LANGUAGE LambdaCase, OverloadedStrings, TemplateHaskell #-}
|
2019-08-09 11:06:13 -04:00
|
|
|
|
|
|
|
module Mtlstats.Types (
|
|
|
|
-- * Types
|
2019-09-04 13:47:17 -04:00
|
|
|
Controller (..),
|
2019-08-23 09:22:32 -04:00
|
|
|
Action,
|
2019-08-13 10:11:10 -04:00
|
|
|
ProgState (..),
|
2019-08-20 01:40:59 -04:00
|
|
|
ProgMode (..),
|
2019-09-07 11:33:03 -04:00
|
|
|
GameState (..),
|
2019-08-22 01:18:02 -04:00
|
|
|
GameType (..),
|
2019-09-08 12:06:38 -04:00
|
|
|
CreatePlayerState (..),
|
2019-08-16 11:49:04 -04:00
|
|
|
Database (..),
|
2019-08-09 11:06:13 -04:00
|
|
|
Player (..),
|
|
|
|
PlayerStats (..),
|
2019-08-11 10:24:39 -04:00
|
|
|
Goalie (..),
|
|
|
|
GoalieStats (..),
|
2019-08-26 10:20:10 -04:00
|
|
|
GameStats (..),
|
2019-08-24 16:23:56 -04:00
|
|
|
Prompt (..),
|
2019-08-09 11:06:13 -04:00
|
|
|
-- * Lenses
|
2019-08-19 23:58:18 -04:00
|
|
|
-- ** ProgState Lenses
|
|
|
|
database,
|
2019-08-20 01:40:59 -04:00
|
|
|
progMode,
|
2019-08-24 19:02:29 -04:00
|
|
|
inputBuffer,
|
2019-10-11 22:24:27 -04:00
|
|
|
scrollOffset,
|
2019-08-22 13:05:25 -04:00
|
|
|
-- ** ProgMode Lenses
|
2019-08-28 01:04:03 -04:00
|
|
|
gameStateL,
|
2019-09-09 11:43:37 -04:00
|
|
|
createPlayerStateL,
|
2019-08-27 13:07:43 -04:00
|
|
|
-- ** GameState Lenses
|
2019-08-31 10:32:03 -04:00
|
|
|
gameYear,
|
|
|
|
gameMonth,
|
|
|
|
gameDay,
|
2019-08-27 13:07:43 -04:00
|
|
|
gameType,
|
|
|
|
otherTeam,
|
|
|
|
homeScore,
|
|
|
|
awayScore,
|
2019-08-29 00:12:30 -04:00
|
|
|
overtimeFlag,
|
2019-09-06 11:21:46 -04:00
|
|
|
dataVerified,
|
2019-09-14 00:09:28 -04:00
|
|
|
pointsAccounted,
|
2019-09-25 02:41:35 -04:00
|
|
|
goalBy,
|
|
|
|
assistsBy,
|
2019-10-03 01:10:44 -04:00
|
|
|
gamePlayerStats,
|
2019-10-01 00:02:49 -04:00
|
|
|
confirmGoalDataFlag,
|
2019-10-09 00:50:10 -04:00
|
|
|
selectedPlayer,
|
|
|
|
pMinsRecorded,
|
2019-09-08 12:06:38 -04:00
|
|
|
-- ** CreatePlayerState Lenses
|
|
|
|
cpsNumber,
|
|
|
|
cpsName,
|
|
|
|
cpsPosition,
|
2019-09-13 02:26:03 -04:00
|
|
|
cpsSuccessCallback,
|
|
|
|
cpsFailureCallback,
|
2019-08-16 11:49:04 -04:00
|
|
|
-- ** Database Lenses
|
|
|
|
dbPlayers,
|
|
|
|
dbGoalies,
|
2019-08-19 09:31:24 -04:00
|
|
|
dbGames,
|
2019-08-27 11:10:57 -04:00
|
|
|
dbHomeGameStats,
|
|
|
|
dbAwayGameStats,
|
2019-08-09 11:06:13 -04:00
|
|
|
-- ** Player Lenses
|
|
|
|
pNumber,
|
|
|
|
pName,
|
|
|
|
pPosition,
|
|
|
|
pYtd,
|
|
|
|
pLifetime,
|
|
|
|
-- ** PlayerStats Lenses
|
|
|
|
psGoals,
|
|
|
|
psAssists,
|
|
|
|
psPMin,
|
2019-08-11 10:24:39 -04:00
|
|
|
-- ** Goalie Lenses
|
|
|
|
gNumber,
|
|
|
|
gName,
|
|
|
|
gYtd,
|
|
|
|
gLifetime,
|
|
|
|
-- ** GoalieStats Lenses
|
|
|
|
gsGames,
|
|
|
|
gsMinsPlayed,
|
|
|
|
gsGoalsAllowed,
|
|
|
|
gsWins,
|
|
|
|
gsLosses,
|
|
|
|
gsTies,
|
2019-08-26 10:20:10 -04:00
|
|
|
-- ** GameStats Lenses
|
|
|
|
gmsWins,
|
|
|
|
gmsLosses,
|
|
|
|
gmsOvertime,
|
2019-09-21 00:03:33 -04:00
|
|
|
gmsGoalsFor,
|
|
|
|
gmsGoalsAgainst,
|
2019-08-09 11:06:13 -04:00
|
|
|
-- * Constructors
|
2019-08-19 23:58:18 -04:00
|
|
|
newProgState,
|
2019-08-22 01:18:02 -04:00
|
|
|
newGameState,
|
2019-09-08 12:06:38 -04:00
|
|
|
newCreatePlayerState,
|
2019-08-16 11:49:04 -04:00
|
|
|
newDatabase,
|
2019-08-09 11:06:13 -04:00
|
|
|
newPlayer,
|
|
|
|
newPlayerStats,
|
2019-08-11 10:24:39 -04:00
|
|
|
newGoalie,
|
|
|
|
newGoalieStats,
|
2019-08-26 10:20:10 -04:00
|
|
|
newGameStats,
|
2019-08-22 03:06:36 -04:00
|
|
|
-- * Helper Functions
|
2019-08-27 11:44:45 -04:00
|
|
|
-- ** GameState Helpers
|
2019-08-22 14:59:19 -04:00
|
|
|
teamScore,
|
2019-08-27 12:06:53 -04:00
|
|
|
otherScore,
|
2019-09-02 18:50:21 -04:00
|
|
|
homeTeam,
|
|
|
|
awayTeam,
|
2019-08-28 13:02:50 -04:00
|
|
|
gameWon,
|
2019-08-30 00:44:40 -04:00
|
|
|
gameLost,
|
2019-08-28 01:47:30 -04:00
|
|
|
gameTied,
|
2019-09-14 00:42:04 -04:00
|
|
|
unaccountedPoints,
|
2019-08-30 18:57:56 -04:00
|
|
|
-- ** GameStats Helpers
|
2019-09-03 14:15:29 -04:00
|
|
|
gmsGames,
|
2019-08-30 18:57:56 -04:00
|
|
|
gmsPoints,
|
2019-08-31 09:33:26 -04:00
|
|
|
addGameStats,
|
2019-08-22 03:06:36 -04:00
|
|
|
-- ** Player Helpers
|
2019-09-19 03:11:48 -04:00
|
|
|
playerSearch,
|
2019-09-25 02:28:48 -04:00
|
|
|
playerSearchExact,
|
2019-10-01 00:58:15 -04:00
|
|
|
modifyPlayer,
|
2019-10-09 00:24:34 -04:00
|
|
|
playerSummary,
|
2019-10-15 00:51:42 -04:00
|
|
|
playerIsActive,
|
2019-10-09 00:24:34 -04:00
|
|
|
-- ** PlayerStats Helpers
|
2019-10-09 00:35:35 -04:00
|
|
|
psPoints,
|
2019-10-09 00:24:34 -04:00
|
|
|
addPlayerStats
|
2019-08-09 11:06:13 -04:00
|
|
|
) where
|
|
|
|
|
2019-08-23 09:22:32 -04:00
|
|
|
import Control.Monad.Trans.State (StateT)
|
2019-08-10 10:01:36 -04:00
|
|
|
import Data.Aeson
|
|
|
|
( FromJSON
|
|
|
|
, ToJSON
|
|
|
|
, object
|
|
|
|
, pairs
|
|
|
|
, parseJSON
|
|
|
|
, toEncoding
|
|
|
|
, toJSON
|
|
|
|
, withObject
|
|
|
|
, (.:)
|
|
|
|
, (.=)
|
|
|
|
)
|
2019-09-18 01:55:38 -04:00
|
|
|
import Data.List (isInfixOf)
|
2019-10-03 01:10:44 -04:00
|
|
|
import qualified Data.Map as M
|
2019-09-19 03:11:48 -04:00
|
|
|
import Data.Maybe (listToMaybe)
|
2019-08-22 13:05:25 -04:00
|
|
|
import Lens.Micro (Lens', lens, (&), (^.), (.~))
|
2019-08-09 11:06:13 -04:00
|
|
|
import Lens.Micro.TH (makeLenses)
|
2019-09-04 13:47:17 -04:00
|
|
|
import qualified UI.NCurses as C
|
2019-08-23 09:22:32 -04:00
|
|
|
|
2019-09-02 18:50:21 -04:00
|
|
|
import Mtlstats.Config
|
|
|
|
|
2019-09-04 13:47:17 -04:00
|
|
|
-- | Controls the program flow
|
|
|
|
data Controller = Controller
|
|
|
|
{ drawController :: ProgState -> C.Update C.CursorMode
|
|
|
|
-- ^ The drawing phase
|
|
|
|
, handleController :: C.Event -> Action Bool
|
|
|
|
-- ^ The event handler
|
|
|
|
}
|
|
|
|
|
2019-08-23 09:22:32 -04:00
|
|
|
-- | Action which maintains program state
|
2019-09-04 13:47:17 -04:00
|
|
|
type Action a = StateT ProgState C.Curses a
|
2019-08-09 11:06:13 -04:00
|
|
|
|
2019-08-13 10:11:10 -04:00
|
|
|
-- | Represents the program state
|
2019-08-20 01:40:59 -04:00
|
|
|
data ProgState = ProgState
|
2019-10-11 22:24:27 -04:00
|
|
|
{ _database :: Database
|
2019-08-23 10:10:49 -04:00
|
|
|
-- ^ The data to be saved
|
2019-10-11 22:24:27 -04:00
|
|
|
, _progMode :: ProgMode
|
2019-08-23 10:10:49 -04:00
|
|
|
-- ^ The program's mode
|
2019-10-11 22:24:27 -04:00
|
|
|
, _inputBuffer :: String
|
2019-08-24 19:02:29 -04:00
|
|
|
-- ^ Buffer for user input
|
2019-10-11 22:24:27 -04:00
|
|
|
, _scrollOffset :: Int
|
|
|
|
-- ^ The scrolling offset for the display
|
2019-09-13 02:26:03 -04:00
|
|
|
}
|
2019-08-13 10:11:10 -04:00
|
|
|
|
2019-09-07 11:33:03 -04:00
|
|
|
-- | The program mode
|
|
|
|
data ProgMode
|
|
|
|
= MainMenu
|
|
|
|
| NewSeason
|
|
|
|
| NewGame GameState
|
2019-09-09 10:51:32 -04:00
|
|
|
| CreatePlayer CreatePlayerState
|
2019-09-13 02:26:03 -04:00
|
|
|
|
|
|
|
instance Show ProgMode where
|
|
|
|
show MainMenu = "MainMenu"
|
|
|
|
show NewSeason = "NewSeason"
|
|
|
|
show (NewGame _) = "NewGame"
|
|
|
|
show (CreatePlayer _) = "CreatePlayer"
|
2019-09-07 11:33:03 -04:00
|
|
|
|
2019-08-22 01:18:02 -04:00
|
|
|
-- | The game state
|
|
|
|
data GameState = GameState
|
2019-10-01 00:02:49 -04:00
|
|
|
{ _gameYear :: Maybe Int
|
2019-08-31 10:32:03 -04:00
|
|
|
-- ^ The year the game took place
|
2019-10-01 00:02:49 -04:00
|
|
|
, _gameMonth :: Maybe Int
|
2019-08-31 10:32:03 -04:00
|
|
|
-- ^ The month the game took place
|
2019-10-01 00:02:49 -04:00
|
|
|
, _gameDay :: Maybe Int
|
2019-08-31 10:32:03 -04:00
|
|
|
-- ^ The day of the month the game took place
|
2019-10-01 00:02:49 -04:00
|
|
|
, _gameType :: Maybe GameType
|
2019-08-23 10:10:49 -04:00
|
|
|
-- ^ The type of game (home/away)
|
2019-10-01 00:02:49 -04:00
|
|
|
, _otherTeam :: String
|
2019-08-25 09:23:59 -04:00
|
|
|
-- ^ The name of the other team
|
2019-10-01 00:02:49 -04:00
|
|
|
, _homeScore :: Maybe Int
|
2019-08-23 10:10:49 -04:00
|
|
|
-- ^ The home team's score
|
2019-10-01 00:02:49 -04:00
|
|
|
, _awayScore :: Maybe Int
|
2019-08-23 10:10:49 -04:00
|
|
|
-- ^ The away team's score
|
2019-10-01 00:02:49 -04:00
|
|
|
, _overtimeFlag :: Maybe Bool
|
2019-08-29 00:12:30 -04:00
|
|
|
-- ^ Indicates whether or not the game went into overtime
|
2019-10-01 00:02:49 -04:00
|
|
|
, _dataVerified :: Bool
|
2019-09-06 11:21:46 -04:00
|
|
|
-- ^ Set to 'True' when the user confirms the entered data
|
2019-10-01 00:02:49 -04:00
|
|
|
, _pointsAccounted :: Int
|
2019-09-25 02:41:35 -04:00
|
|
|
-- ^ The number of game points accounted for
|
2019-10-01 00:02:49 -04:00
|
|
|
, _goalBy :: Maybe Int
|
2019-09-28 02:09:11 -04:00
|
|
|
-- ^ The index number of the player who scored the most recently
|
|
|
|
-- entered goal
|
2019-10-01 00:02:49 -04:00
|
|
|
, _assistsBy :: [Int]
|
2019-09-28 02:09:11 -04:00
|
|
|
-- ^ The index numbers of the players who have assisted the most
|
|
|
|
-- recently entered goal
|
2019-10-03 01:10:44 -04:00
|
|
|
, _gamePlayerStats :: M.Map Int PlayerStats
|
|
|
|
-- ^ The player stats accumulated over the game
|
2019-10-01 00:02:49 -04:00
|
|
|
, _confirmGoalDataFlag :: Bool
|
2019-10-03 01:10:44 -04:00
|
|
|
-- ^ Set when the user confirms the goal data
|
2019-10-09 00:50:10 -04:00
|
|
|
, _selectedPlayer :: Maybe Int
|
|
|
|
-- ^ Index number of the selected 'Player'
|
|
|
|
, _pMinsRecorded :: Bool
|
|
|
|
-- ^ Set when the penalty mintes have been recorded
|
2019-08-22 01:18:02 -04:00
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
-- | The type of game
|
|
|
|
data GameType
|
|
|
|
= HomeGame
|
|
|
|
| AwayGame
|
2019-08-20 11:17:24 -04:00
|
|
|
deriving (Eq, Show)
|
2019-08-20 01:40:59 -04:00
|
|
|
|
2019-09-08 12:06:38 -04:00
|
|
|
-- | Player creation status
|
|
|
|
data CreatePlayerState = CreatePlayerState
|
2019-10-30 00:45:16 -04:00
|
|
|
{ _cpsNumber :: Maybe Int
|
2019-09-08 12:06:38 -04:00
|
|
|
-- ^ The player's number
|
2019-09-13 02:26:03 -04:00
|
|
|
, _cpsName :: String
|
2019-09-08 12:06:38 -04:00
|
|
|
-- ^ The player's name
|
2019-09-13 02:26:03 -04:00
|
|
|
, _cpsPosition :: String
|
2019-09-08 12:06:38 -04:00
|
|
|
-- ^ The player's position
|
2019-09-19 04:01:28 -04:00
|
|
|
, _cpsSuccessCallback :: Action ()
|
2019-09-13 02:26:03 -04:00
|
|
|
-- ^ The function to call on success
|
2019-09-19 04:01:28 -04:00
|
|
|
, _cpsFailureCallback :: Action ()
|
2019-09-13 02:26:03 -04:00
|
|
|
-- ^ The function to call on failure
|
|
|
|
}
|
2019-09-08 12:06:38 -04:00
|
|
|
|
2019-08-16 11:49:04 -04:00
|
|
|
-- | Represents the database
|
|
|
|
data Database = Database
|
2019-08-27 11:10:57 -04:00
|
|
|
{ _dbPlayers :: [Player]
|
2019-08-16 11:49:04 -04:00
|
|
|
-- ^ The list of players
|
2019-08-27 11:10:57 -04:00
|
|
|
, _dbGoalies :: [Goalie]
|
2019-08-23 10:35:59 -04:00
|
|
|
-- ^ The list of goalies
|
2019-08-27 11:10:57 -04:00
|
|
|
, _dbGames :: Int
|
2019-08-19 09:31:24 -04:00
|
|
|
-- ^ The number of games recorded
|
2019-08-27 11:10:57 -04:00
|
|
|
, _dbHomeGameStats :: GameStats
|
|
|
|
-- ^ Statistics for home games
|
|
|
|
, _dbAwayGameStats :: GameStats
|
|
|
|
-- ^ Statistics for away games
|
2019-08-16 11:49:04 -04:00
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance FromJSON Database where
|
|
|
|
parseJSON = withObject "Database" $ \v -> Database
|
|
|
|
<$> v .: "players"
|
|
|
|
<*> v .: "goalies"
|
2019-08-19 09:31:24 -04:00
|
|
|
<*> v .: "games"
|
2019-08-27 11:10:57 -04:00
|
|
|
<*> v .: "home_game_stats"
|
|
|
|
<*> v .: "away_game_stats"
|
2019-08-16 11:49:04 -04:00
|
|
|
|
|
|
|
instance ToJSON Database where
|
2019-08-27 11:10:57 -04:00
|
|
|
toJSON (Database players goalies games hgs ags) = object
|
|
|
|
[ "players" .= players
|
|
|
|
, "goalies" .= goalies
|
|
|
|
, "games" .= games
|
|
|
|
, "home_game_stats" .= hgs
|
|
|
|
, "away_game_stats" .= ags
|
2019-08-16 11:49:04 -04:00
|
|
|
]
|
2019-08-27 11:10:57 -04:00
|
|
|
toEncoding (Database players goalies games hgs ags) = pairs $
|
|
|
|
"players" .= players <>
|
|
|
|
"goalies" .= goalies <>
|
|
|
|
"games" .= games <>
|
|
|
|
"home_game_stats" .= hgs <>
|
|
|
|
"away_game_stats" .= ags
|
2019-08-16 11:49:04 -04:00
|
|
|
|
2019-08-09 11:06:13 -04:00
|
|
|
-- | Represents a (non-goalie) player
|
|
|
|
data Player = Player
|
|
|
|
{ _pNumber :: Int
|
|
|
|
-- ^ The player's number
|
|
|
|
, _pName :: String
|
|
|
|
-- ^ The player's name
|
|
|
|
, _pPosition :: String
|
|
|
|
-- ^ The player's position
|
|
|
|
, _pYtd :: PlayerStats
|
|
|
|
-- ^ The Player's year-to-date stats
|
|
|
|
, _pLifetime :: PlayerStats
|
|
|
|
-- ^ The player's lifetime stats
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
2019-08-10 10:01:36 -04:00
|
|
|
instance FromJSON Player where
|
|
|
|
parseJSON = withObject "Player" $ \v -> Player
|
|
|
|
<$> v .: "number"
|
|
|
|
<*> v .: "name"
|
|
|
|
<*> v .: "position"
|
|
|
|
<*> v .: "ytd"
|
|
|
|
<*> v .: "lifetime"
|
|
|
|
|
|
|
|
instance ToJSON Player where
|
|
|
|
toJSON (Player num name pos ytd lt) = object
|
|
|
|
[ "number" .= num
|
|
|
|
, "name" .= name
|
|
|
|
, "position" .= pos
|
|
|
|
, "ytd" .= ytd
|
|
|
|
, "lifetime" .= lt
|
|
|
|
]
|
|
|
|
toEncoding (Player num name pos ytd lt) = pairs $
|
|
|
|
"number" .= num <>
|
|
|
|
"name" .= name <>
|
|
|
|
"position" .= pos <>
|
|
|
|
"ytd" .= ytd <>
|
|
|
|
"lifetime" .= lt
|
|
|
|
|
2019-08-09 11:06:13 -04:00
|
|
|
-- | Represents a (non-goalie) player's stats
|
|
|
|
data PlayerStats = PlayerStats
|
|
|
|
{ _psGoals :: Int
|
|
|
|
-- ^ The number of goals
|
|
|
|
, _psAssists :: Int
|
|
|
|
-- ^ The number of assists
|
|
|
|
, _psPMin :: Int
|
|
|
|
-- ^ The number of penalty minutes
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
2019-08-10 10:01:36 -04:00
|
|
|
instance FromJSON PlayerStats where
|
|
|
|
parseJSON = withObject "PlayerStats" $ \v -> PlayerStats
|
|
|
|
<$> v .: "goals"
|
|
|
|
<*> v .: "assists"
|
|
|
|
<*> v .: "penalty_mins"
|
|
|
|
|
|
|
|
instance ToJSON PlayerStats where
|
|
|
|
toJSON (PlayerStats g a pm) = object
|
|
|
|
[ "goals" .= g
|
|
|
|
, "assists" .= a
|
|
|
|
, "penalty_mins" .= pm
|
|
|
|
]
|
|
|
|
toEncoding (PlayerStats g a pm) = pairs $
|
|
|
|
"goals" .= g <>
|
|
|
|
"assists" .= a <>
|
|
|
|
"penalty_mins" .= pm
|
|
|
|
|
2019-08-11 10:24:39 -04:00
|
|
|
-- | Represents a goalie
|
|
|
|
data Goalie = Goalie
|
|
|
|
{ _gNumber :: Int
|
|
|
|
-- ^ The goalie's number
|
|
|
|
, _gName :: String
|
|
|
|
-- ^ The goalie's name
|
|
|
|
, _gYtd :: GoalieStats
|
|
|
|
-- ^ The goalie's year-to-date stats
|
|
|
|
, _gLifetime :: GoalieStats
|
|
|
|
-- ^ The goalie's lifetime stats
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance FromJSON Goalie where
|
|
|
|
parseJSON = withObject "Goalie" $ \v -> Goalie
|
|
|
|
<$> v .: "number"
|
|
|
|
<*> v .: "name"
|
|
|
|
<*> v .: "ytd"
|
|
|
|
<*> v .: "lifetime"
|
|
|
|
|
|
|
|
instance ToJSON Goalie where
|
|
|
|
toJSON (Goalie num name ytd lt) = object
|
|
|
|
[ "number" .= num
|
|
|
|
, "name" .= name
|
|
|
|
, "ytd" .= ytd
|
|
|
|
, "lifetime" .= lt
|
|
|
|
]
|
|
|
|
toEncoding (Goalie num name ytd lt) = pairs $
|
|
|
|
"number" .= num <>
|
|
|
|
"name" .= name <>
|
|
|
|
"ytd" .= ytd <>
|
|
|
|
"lifetime" .= lt
|
|
|
|
|
|
|
|
-- | Represents a goalie's stats
|
|
|
|
data GoalieStats = GoalieStats
|
|
|
|
{ _gsGames :: Int
|
|
|
|
-- ^ The number of games played
|
|
|
|
, _gsMinsPlayed :: Int
|
|
|
|
-- ^ The number of minutes played
|
|
|
|
, _gsGoalsAllowed :: Int
|
|
|
|
-- ^ The number of goals allowed
|
|
|
|
, _gsWins :: Int
|
|
|
|
-- ^ The number of wins
|
|
|
|
, _gsLosses :: Int
|
|
|
|
-- ^ The number of losses
|
|
|
|
, _gsTies :: Int
|
|
|
|
-- ^ The number of ties
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance FromJSON GoalieStats where
|
|
|
|
parseJSON = withObject "GoalieStats" $ \v -> GoalieStats
|
|
|
|
<$> v .: "games"
|
|
|
|
<*> v .: "mins_played"
|
|
|
|
<*> v .: "goals_allowed"
|
|
|
|
<*> v .: "wins"
|
|
|
|
<*> v .: "losses"
|
|
|
|
<*> v .: "ties"
|
|
|
|
|
|
|
|
instance ToJSON GoalieStats where
|
2019-10-19 00:58:41 -04:00
|
|
|
toJSON (GoalieStats g m a w l t) = object
|
2019-08-11 10:24:39 -04:00
|
|
|
[ "games" .= g
|
|
|
|
, "mins_played" .= m
|
2019-10-19 00:58:41 -04:00
|
|
|
, "goals_allowed" .= a
|
2019-08-11 10:24:39 -04:00
|
|
|
, "wins" .= w
|
|
|
|
, "losses" .= l
|
|
|
|
, "ties" .= t
|
|
|
|
]
|
2019-10-19 00:58:41 -04:00
|
|
|
toEncoding (GoalieStats g m a w l t) = pairs $
|
|
|
|
"games" .= g <>
|
|
|
|
"mins_played" .= m <>
|
|
|
|
"goals_allowed" .= a <>
|
|
|
|
"wins" .= w <>
|
|
|
|
"losses" .= l <>
|
2019-08-11 10:24:39 -04:00
|
|
|
"ties" .= t
|
|
|
|
|
2019-08-26 10:20:10 -04:00
|
|
|
-- | Game statistics
|
|
|
|
data GameStats = GameStats
|
2019-09-21 00:03:33 -04:00
|
|
|
{ _gmsWins :: Int
|
2019-08-26 10:20:10 -04:00
|
|
|
-- ^ Games won
|
2019-09-21 00:03:33 -04:00
|
|
|
, _gmsLosses :: Int
|
2019-08-26 10:20:10 -04:00
|
|
|
-- ^ Games lost
|
2019-09-21 00:03:33 -04:00
|
|
|
, _gmsOvertime :: Int
|
2019-08-26 10:20:10 -04:00
|
|
|
-- ^ Games lost in overtime
|
2019-09-21 00:03:33 -04:00
|
|
|
, _gmsGoalsFor :: Int
|
|
|
|
-- ^ Goals for the team
|
|
|
|
, _gmsGoalsAgainst :: Int
|
|
|
|
-- ^ Goals against the team
|
2019-08-26 10:20:10 -04:00
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance FromJSON GameStats where
|
|
|
|
parseJSON = withObject "GameStats" $ \v -> GameStats
|
|
|
|
<$> v .: "wins"
|
|
|
|
<*> v .: "losses"
|
|
|
|
<*> v .: "overtime"
|
2019-09-21 00:03:33 -04:00
|
|
|
<*> v .: "goals_for"
|
|
|
|
<*> v .: "goals_against"
|
2019-08-26 10:20:10 -04:00
|
|
|
|
|
|
|
instance ToJSON GameStats where
|
2019-09-21 00:03:33 -04:00
|
|
|
toJSON (GameStats w l ot gf ga) = object
|
|
|
|
[ "wins" .= w
|
|
|
|
, "losses" .= l
|
|
|
|
, "overtime" .= ot
|
|
|
|
, "goals_for" .= gf
|
|
|
|
, "goals_against" .= ga
|
2019-08-26 10:20:10 -04:00
|
|
|
]
|
2019-09-21 00:03:33 -04:00
|
|
|
toEncoding (GameStats w l ot gf ga) = pairs $
|
|
|
|
"wins" .= w <>
|
|
|
|
"losses" .= l <>
|
|
|
|
"overtime" .= ot <>
|
|
|
|
"goals_for" .= gf <>
|
|
|
|
"goals_against" .= ga
|
2019-08-26 10:20:10 -04:00
|
|
|
|
2019-08-24 16:23:56 -04:00
|
|
|
-- | Defines a user prompt
|
|
|
|
data Prompt = Prompt
|
2019-09-19 02:45:50 -04:00
|
|
|
{ promptDrawer :: ProgState -> C.Update ()
|
2019-10-30 01:28:54 -04:00
|
|
|
-- ^ Draws the prompt to the screen
|
2019-09-19 02:45:50 -04:00
|
|
|
, promptCharCheck :: Char -> Bool
|
2019-08-24 16:23:56 -04:00
|
|
|
-- ^ Determines whether or not the character is valid
|
2019-09-19 02:45:50 -04:00
|
|
|
, promptAction :: String -> Action ()
|
2019-08-24 16:23:56 -04:00
|
|
|
-- ^ Action to perform when the value is entered
|
2019-09-19 02:45:50 -04:00
|
|
|
, promptSpecialKey :: C.Key -> Action ()
|
|
|
|
-- ^ Action to perform when a special key is pressed
|
2019-08-24 16:23:56 -04:00
|
|
|
}
|
|
|
|
|
2019-08-19 23:58:18 -04:00
|
|
|
makeLenses ''ProgState
|
2019-08-22 01:18:02 -04:00
|
|
|
makeLenses ''GameState
|
2019-09-08 12:06:38 -04:00
|
|
|
makeLenses ''CreatePlayerState
|
2019-08-16 11:49:04 -04:00
|
|
|
makeLenses ''Database
|
2019-08-09 11:06:13 -04:00
|
|
|
makeLenses ''Player
|
|
|
|
makeLenses ''PlayerStats
|
2019-08-11 10:24:39 -04:00
|
|
|
makeLenses ''Goalie
|
|
|
|
makeLenses ''GoalieStats
|
2019-08-26 10:20:10 -04:00
|
|
|
makeLenses ''GameStats
|
2019-08-09 11:06:13 -04:00
|
|
|
|
2019-08-28 01:04:03 -04:00
|
|
|
gameStateL :: Lens' ProgMode GameState
|
|
|
|
gameStateL = lens
|
|
|
|
(\case
|
|
|
|
NewGame gs -> gs
|
|
|
|
_ -> newGameState)
|
|
|
|
(\_ gs -> NewGame gs)
|
|
|
|
|
2019-09-09 11:43:37 -04:00
|
|
|
createPlayerStateL :: Lens' ProgMode CreatePlayerState
|
|
|
|
createPlayerStateL = lens
|
|
|
|
(\case
|
|
|
|
CreatePlayer cps -> cps
|
|
|
|
_ -> newCreatePlayerState)
|
|
|
|
(\_ cps -> CreatePlayer cps)
|
|
|
|
|
2019-08-22 01:18:02 -04:00
|
|
|
-- | Constructor for a 'ProgState'
|
2019-08-19 23:58:18 -04:00
|
|
|
newProgState :: ProgState
|
|
|
|
newProgState = ProgState
|
2019-10-11 22:24:27 -04:00
|
|
|
{ _database = newDatabase
|
|
|
|
, _progMode = MainMenu
|
|
|
|
, _inputBuffer = ""
|
|
|
|
, _scrollOffset = 0
|
2019-08-19 23:58:18 -04:00
|
|
|
}
|
|
|
|
|
2019-08-22 01:18:02 -04:00
|
|
|
-- | Constructor for a 'GameState'
|
|
|
|
newGameState :: GameState
|
|
|
|
newGameState = GameState
|
2019-10-01 00:02:49 -04:00
|
|
|
{ _gameYear = Nothing
|
|
|
|
, _gameMonth = Nothing
|
|
|
|
, _gameDay = Nothing
|
|
|
|
, _gameType = Nothing
|
|
|
|
, _otherTeam = ""
|
|
|
|
, _homeScore = Nothing
|
|
|
|
, _awayScore = Nothing
|
|
|
|
, _overtimeFlag = Nothing
|
|
|
|
, _dataVerified = False
|
|
|
|
, _pointsAccounted = 0
|
|
|
|
, _goalBy = Nothing
|
|
|
|
, _assistsBy = []
|
2019-10-03 01:10:44 -04:00
|
|
|
, _gamePlayerStats = M.empty
|
2019-10-01 00:02:49 -04:00
|
|
|
, _confirmGoalDataFlag = False
|
2019-10-09 00:50:10 -04:00
|
|
|
, _selectedPlayer = Nothing
|
|
|
|
, _pMinsRecorded = False
|
2019-08-22 01:18:02 -04:00
|
|
|
}
|
|
|
|
|
2019-09-08 12:06:38 -04:00
|
|
|
-- | Constructor for a 'CreatePlayerState'
|
|
|
|
newCreatePlayerState :: CreatePlayerState
|
|
|
|
newCreatePlayerState = CreatePlayerState
|
2019-10-30 00:45:16 -04:00
|
|
|
{ _cpsNumber = Nothing
|
2019-09-13 02:26:03 -04:00
|
|
|
, _cpsName = ""
|
|
|
|
, _cpsPosition = ""
|
2019-09-19 04:01:28 -04:00
|
|
|
, _cpsSuccessCallback = return ()
|
|
|
|
, _cpsFailureCallback = return ()
|
2019-09-08 12:06:38 -04:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:49:04 -04:00
|
|
|
-- | Constructor for a 'Database'
|
|
|
|
newDatabase :: Database
|
|
|
|
newDatabase = Database
|
2019-08-27 11:10:57 -04:00
|
|
|
{ _dbPlayers = []
|
|
|
|
, _dbGoalies = []
|
|
|
|
, _dbGames = 0
|
|
|
|
, _dbHomeGameStats = newGameStats
|
|
|
|
, _dbAwayGameStats = newGameStats
|
2019-08-16 11:49:04 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 11:06:13 -04:00
|
|
|
-- | Constructor for a 'Player'
|
|
|
|
newPlayer
|
|
|
|
:: Int
|
|
|
|
-- ^ The player's number
|
|
|
|
-> String
|
|
|
|
-- ^ The player's name
|
|
|
|
-> String
|
|
|
|
-- ^ The player's position
|
|
|
|
-> Player
|
|
|
|
newPlayer num name pos = Player
|
|
|
|
{ _pNumber = num
|
|
|
|
, _pName = name
|
|
|
|
, _pPosition = pos
|
|
|
|
, _pYtd = newPlayerStats
|
|
|
|
, _pLifetime = newPlayerStats
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Constructor for a 'PlayerStats' value
|
|
|
|
newPlayerStats :: PlayerStats
|
|
|
|
newPlayerStats = PlayerStats
|
|
|
|
{ _psGoals = 0
|
|
|
|
, _psAssists = 0
|
|
|
|
, _psPMin = 0
|
|
|
|
}
|
|
|
|
|
2019-08-11 10:24:39 -04:00
|
|
|
-- | Constructor for a 'Goalie'
|
|
|
|
newGoalie
|
|
|
|
:: Int
|
|
|
|
-- ^ The goalie's number
|
|
|
|
-> String
|
|
|
|
-- ^ The goalie's name
|
|
|
|
-> Goalie
|
|
|
|
newGoalie num name = Goalie
|
|
|
|
{ _gNumber = num
|
|
|
|
, _gName = name
|
|
|
|
, _gYtd = newGoalieStats
|
|
|
|
, _gLifetime = newGoalieStats
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Constructor for a 'GoalieStats' value
|
|
|
|
newGoalieStats :: GoalieStats
|
|
|
|
newGoalieStats = GoalieStats
|
|
|
|
{ _gsGames = 0
|
|
|
|
, _gsMinsPlayed = 0
|
|
|
|
, _gsGoalsAllowed = 0
|
|
|
|
, _gsWins = 0
|
|
|
|
, _gsLosses = 0
|
|
|
|
, _gsTies = 0
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:20:10 -04:00
|
|
|
-- | Constructor for a 'GameStats' value
|
|
|
|
newGameStats :: GameStats
|
|
|
|
newGameStats = GameStats
|
2019-09-21 00:03:33 -04:00
|
|
|
{ _gmsWins = 0
|
|
|
|
, _gmsLosses = 0
|
|
|
|
, _gmsOvertime = 0
|
|
|
|
, _gmsGoalsFor = 0
|
|
|
|
, _gmsGoalsAgainst = 0
|
2019-08-26 10:20:10 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 11:44:45 -04:00
|
|
|
-- | Determines the team's score
|
|
|
|
teamScore :: GameState -> Maybe Int
|
|
|
|
teamScore s = case s ^. gameType of
|
|
|
|
Just HomeGame -> s ^. homeScore
|
|
|
|
Just AwayGame -> s ^. awayScore
|
2019-08-22 14:59:19 -04:00
|
|
|
Nothing -> Nothing
|
2019-08-22 03:06:36 -04:00
|
|
|
|
2019-08-27 12:06:53 -04:00
|
|
|
-- | Determines the other team's score
|
|
|
|
otherScore :: GameState -> Maybe Int
|
|
|
|
otherScore s = case s ^. gameType of
|
|
|
|
Just HomeGame -> s ^. awayScore
|
|
|
|
Just AwayGame -> s ^. homeScore
|
|
|
|
Nothing -> Nothing
|
|
|
|
|
2019-09-02 18:50:21 -04:00
|
|
|
-- | Returns the name of the home team (or an empty string if
|
|
|
|
-- unavailable)
|
|
|
|
homeTeam :: GameState -> String
|
|
|
|
homeTeam gs = case gs^.gameType of
|
|
|
|
Just HomeGame -> myTeam
|
|
|
|
Just AwayGame -> gs^.otherTeam
|
|
|
|
Nothing -> ""
|
|
|
|
|
|
|
|
-- | Returns the name of the visiting team (or an empty string if
|
|
|
|
-- unavailable)
|
|
|
|
awayTeam :: GameState -> String
|
|
|
|
awayTeam gs = case gs^.gameType of
|
|
|
|
Just HomeGame -> gs^.otherTeam
|
|
|
|
Just AwayGame -> myTeam
|
|
|
|
Nothing -> ""
|
|
|
|
|
2019-08-30 00:43:09 -04:00
|
|
|
-- | Checks if the game was won
|
|
|
|
gameWon :: GameState -> Maybe Bool
|
|
|
|
gameWon gs = (>) <$> teamScore gs <*> otherScore gs
|
|
|
|
|
2019-08-30 00:44:40 -04:00
|
|
|
-- | Checks if the game was lost
|
|
|
|
gameLost :: GameState -> Maybe Bool
|
2019-09-06 10:08:25 -04:00
|
|
|
gameLost gs = do
|
|
|
|
ot <- gs^.overtimeFlag
|
|
|
|
team <- teamScore gs
|
|
|
|
other <- otherScore gs
|
|
|
|
Just $ not ot && other > team
|
2019-08-30 00:44:40 -04:00
|
|
|
|
2019-08-30 00:43:09 -04:00
|
|
|
-- | Checks if the game has tied
|
|
|
|
gameTied :: GameState -> Maybe Bool
|
|
|
|
gameTied gs = (==) <$> gs^.homeScore <*> gs^.awayScore
|
2019-08-28 01:47:30 -04:00
|
|
|
|
2019-09-14 00:42:04 -04:00
|
|
|
-- | Checks for unaccounted points
|
|
|
|
unaccountedPoints :: GameState -> Maybe Bool
|
|
|
|
unaccountedPoints gs = do
|
|
|
|
scored <- teamScore gs
|
|
|
|
let accounted = gs^.pointsAccounted
|
|
|
|
Just $ scored > accounted
|
|
|
|
|
2019-09-03 14:15:29 -04:00
|
|
|
-- | Calculates the number of games played
|
|
|
|
gmsGames :: GameStats -> Int
|
2019-09-06 10:08:25 -04:00
|
|
|
gmsGames gs = gs^.gmsWins + gs^.gmsLosses + gs^.gmsOvertime
|
2019-09-03 14:15:29 -04:00
|
|
|
|
2019-08-30 18:57:56 -04:00
|
|
|
-- | Calculates the number of points
|
|
|
|
gmsPoints :: GameStats -> Int
|
|
|
|
gmsPoints gs = 2 * gs^.gmsWins + gs^. gmsOvertime
|
|
|
|
|
2019-08-31 09:33:26 -04:00
|
|
|
-- | Adds two 'GameStats' values together
|
|
|
|
addGameStats :: GameStats -> GameStats -> GameStats
|
|
|
|
addGameStats s1 s2 = GameStats
|
2019-09-21 00:03:33 -04:00
|
|
|
{ _gmsWins = s1^.gmsWins + s2^.gmsWins
|
|
|
|
, _gmsLosses = s1^.gmsLosses + s2^.gmsLosses
|
|
|
|
, _gmsOvertime = s1^.gmsOvertime + s2^.gmsOvertime
|
|
|
|
, _gmsGoalsFor = s1^.gmsGoalsFor + s2^.gmsGoalsFor
|
|
|
|
, _gmsGoalsAgainst = s1^.gmsGoalsAgainst + s2^.gmsGoalsAgainst
|
2019-08-31 09:33:26 -04:00
|
|
|
}
|
|
|
|
|
2019-09-18 01:55:38 -04:00
|
|
|
-- | Searches through a list of players
|
|
|
|
playerSearch
|
|
|
|
:: String
|
|
|
|
-- ^ The search string
|
|
|
|
-> [Player]
|
|
|
|
-- ^ The list of players to search
|
|
|
|
-> [(Int, Player)]
|
|
|
|
-- ^ The matching players with their index numbers
|
|
|
|
playerSearch sStr =
|
|
|
|
filter (match sStr) .
|
|
|
|
zip [0..]
|
|
|
|
where match sStr (_, p) = sStr `isInfixOf` (p^.pName)
|
2019-09-19 03:11:48 -04:00
|
|
|
|
|
|
|
-- | Searches for a player by exact match on name
|
|
|
|
playerSearchExact
|
|
|
|
:: String
|
|
|
|
-- ^ The player's name
|
|
|
|
-> [Player]
|
|
|
|
-- ^ The list of players to search
|
|
|
|
-> Maybe (Int, Player)
|
|
|
|
-- ^ The player's index and value
|
|
|
|
playerSearchExact sStr =
|
|
|
|
listToMaybe .
|
|
|
|
filter (match sStr) .
|
|
|
|
zip [0..]
|
|
|
|
where match sStr (_, p) = p^.pName == sStr
|
2019-09-25 02:28:48 -04:00
|
|
|
|
|
|
|
-- | Modifies a player with a given name
|
|
|
|
modifyPlayer
|
|
|
|
:: (Player -> Player)
|
|
|
|
-- ^ The modification function
|
|
|
|
-> String
|
|
|
|
-- ^ The player's name
|
|
|
|
-> [Player]
|
|
|
|
-- ^ The list of players to modify
|
|
|
|
-> [Player]
|
|
|
|
-- ^ The modified list
|
|
|
|
modifyPlayer f n = map
|
|
|
|
(\p -> if p^.pName == n
|
|
|
|
then f p
|
|
|
|
else p)
|
2019-10-01 00:58:15 -04:00
|
|
|
|
|
|
|
-- | Provides a short summary string for a player
|
|
|
|
playerSummary :: Player -> String
|
2019-10-02 01:31:07 -04:00
|
|
|
playerSummary p =
|
|
|
|
p^.pName ++ " (" ++ show (p^.pNumber) ++ ") " ++ p^.pPosition
|
2019-10-09 00:24:34 -04:00
|
|
|
|
2019-10-15 00:51:42 -04:00
|
|
|
-- | Determines whether or not a player has been active in the current
|
|
|
|
-- season/year
|
|
|
|
playerIsActive :: Player -> Bool
|
|
|
|
playerIsActive = do
|
|
|
|
stats <- (^.pYtd)
|
|
|
|
return
|
|
|
|
$ stats^.psGoals /= 0
|
|
|
|
|| stats^.psAssists /= 0
|
|
|
|
|| stats^.psPMin /= 0
|
|
|
|
|
2019-10-09 00:35:35 -04:00
|
|
|
-- | Calculates a player's points
|
|
|
|
psPoints :: PlayerStats -> Int
|
|
|
|
psPoints s = s^.psGoals + s^.psAssists
|
|
|
|
|
2019-10-09 00:24:34 -04:00
|
|
|
-- | Adds two 'PlayerStats' together
|
|
|
|
addPlayerStats :: PlayerStats -> PlayerStats -> PlayerStats
|
|
|
|
addPlayerStats s1 s2 = newPlayerStats
|
|
|
|
& psGoals .~ s1^.psGoals + s2^.psGoals
|
|
|
|
& psAssists .~ s1^.psAssists + s2^.psAssists
|
|
|
|
& psPMin .~ s1^.psPMin + s2^.psPMin
|