Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9534218797 | ||
|
|
d7d3d1a4fd | ||
|
|
86c4fe316e | ||
|
|
d5ac42268f | ||
|
|
df26e9d265 | ||
|
|
cb5f2d7d15 | ||
|
|
152ea76bda | ||
|
|
36ab31a17c | ||
|
|
768cb47fac | ||
|
|
427ad12603 | ||
|
|
1ca2ffc378 | ||
|
|
9e6b71c464 | ||
|
|
2f4e963e41 | ||
|
|
05af939963 | ||
|
|
8af7974c8f | ||
|
|
f7cfd5d835 | ||
|
|
cc495fa589 | ||
|
|
9c5d166f31 | ||
|
|
a9e12d11a9 | ||
|
|
08be4154b3 | ||
|
|
4e25db12f1 | ||
|
|
50389b4f4c | ||
|
|
dcbb809ae1 | ||
|
|
be54198960 | ||
|
|
e3d5af5f88 | ||
|
|
de67628df0 | ||
|
|
4848e54d81 | ||
|
|
3b6f77ba21 | ||
|
|
e7606c8a5e | ||
|
|
3560aa7595 | ||
|
|
5979856578 | ||
|
|
4941e0e64f | ||
|
|
eedeaed8fc | ||
|
|
d0f237e707 | ||
|
|
8795cb46a9 | ||
|
|
f1f7077c8c | ||
|
|
a407a01339 | ||
|
|
3e1218f6ff | ||
|
|
7ff16b8ac2 | ||
|
|
d7879a92af | ||
|
|
9b9feefa4f | ||
|
|
26a90a5ed9 | ||
|
|
e8b850c23a | ||
|
|
0efac07a33 |
11
ChangeLog.md
11
ChangeLog.md
@@ -1,5 +1,16 @@
|
||||
# Changelog for mtlstats
|
||||
|
||||
## 0.8.0
|
||||
- Bugfix: removed quotation marks from goalie names in report
|
||||
- Allow lower case player names
|
||||
- Don't show players without points in game report
|
||||
- Removed unnecessary goalie statistics from game report
|
||||
- Fixed goalie average calculation
|
||||
|
||||
## 0.7.0
|
||||
- Shortened views to fit within 25 lines
|
||||
- Implemented goalie reports
|
||||
|
||||
## 0.6.0
|
||||
- Generate lifetime statistics report
|
||||
- Implemented goalie editing
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: mtlstats
|
||||
version: 0.6.0
|
||||
version: 0.8.0
|
||||
github: "mtlstats/mtlstats"
|
||||
license: GPL-3
|
||||
author: "Jonathan Lamothe"
|
||||
|
||||
@@ -28,6 +28,7 @@ module Mtlstats.Actions.NewGame
|
||||
, awardAssist
|
||||
, resetGoalData
|
||||
, assignPMins
|
||||
, awardShutouts
|
||||
) where
|
||||
|
||||
import qualified Data.Map as M
|
||||
@@ -171,3 +172,20 @@ assignPMins mins s = fromMaybe s $ do
|
||||
(psPMin +~ mins)
|
||||
)
|
||||
. (gameSelectedPlayer .~ Nothing)
|
||||
|
||||
-- | Awards a shutout to any 'Goalie' who played and didn't allow any
|
||||
-- goals
|
||||
awardShutouts :: ProgState -> ProgState
|
||||
awardShutouts s = foldl
|
||||
(\s' (gid, stats) -> if stats^.gsGoalsAllowed == 0
|
||||
then s'
|
||||
& database.dbGoalies %~ modifyNth gid
|
||||
( ( gYtd.gsShutouts %~ succ )
|
||||
. ( gLifetime.gsShutouts %~ succ )
|
||||
)
|
||||
& progMode.gameStateL.gameGoalieStats %~ M.adjust
|
||||
(gsShutouts %~ succ)
|
||||
gid
|
||||
else s')
|
||||
s
|
||||
(M.toList $ s^.progMode.gameStateL.gameGoalieStats)
|
||||
|
||||
@@ -43,7 +43,7 @@ dispatch :: ProgState -> Controller
|
||||
dispatch s = case s^.progMode of
|
||||
MainMenu -> mainMenuC
|
||||
NewSeason -> newSeasonC
|
||||
NewGame _ -> newGameC s
|
||||
NewGame gs -> newGameC gs
|
||||
CreatePlayer cps
|
||||
| null $ cps^.cpsNumber -> getPlayerNumC
|
||||
| null $ cps^.cpsName -> getPlayerNameC
|
||||
|
||||
@@ -27,6 +27,7 @@ import Data.Maybe (fromMaybe)
|
||||
import Lens.Micro ((^.))
|
||||
import UI.NCurses as C
|
||||
|
||||
import Mtlstats.Helpers.Goalie
|
||||
import Mtlstats.Menu
|
||||
import Mtlstats.Menu.EditGoalie
|
||||
import Mtlstats.Prompt
|
||||
@@ -118,20 +119,4 @@ header :: ProgState -> C.Update ()
|
||||
header s = C.drawString $ fromMaybe "" $ do
|
||||
gid <- s^.progMode.editGoalieStateL.egsSelectedGoalie
|
||||
g <- nth gid $ s^.database.dbGoalies
|
||||
Just $ unlines
|
||||
[ " Goalie number: " ++ show (g^.gNumber)
|
||||
, " Goalie name: " ++ g^.gName
|
||||
, " YTD games played: " ++ show (g^.gYtd.gsGames)
|
||||
, " YTD mins played: " ++ show (g^.gYtd.gsMinsPlayed)
|
||||
, " YTD goals allowed: " ++ show (g^.gYtd.gsGoalsAllowed)
|
||||
, " YTD wins: " ++ show (g^.gYtd.gsWins)
|
||||
, " YTD losses: " ++ show (g^.gYtd.gsLosses)
|
||||
, " YTD ties: " ++ show (g^.gYtd.gsTies)
|
||||
, " Lifetime games played: " ++ show (g^.gLifetime.gsGames)
|
||||
, " Lifetime mins played: " ++ show (g^.gLifetime.gsMinsPlayed)
|
||||
, "Lifetime goals allowed: " ++ show (g^.gLifetime.gsGoalsAllowed)
|
||||
, " Lifetime wins: " ++ show (g^.gLifetime.gsWins)
|
||||
, " Lifetime losses: " ++ show (g^.gLifetime.gsLosses)
|
||||
, " Lifetime ties: " ++ show (g^.gLifetime.gsTies)
|
||||
, ""
|
||||
]
|
||||
Just $ goalieDetails g ++ "\n"
|
||||
|
||||
@@ -25,7 +25,9 @@ import Data.Maybe (fromMaybe)
|
||||
import Lens.Micro ((^.))
|
||||
import qualified UI.NCurses as C
|
||||
|
||||
import Mtlstats.Helpers.Player
|
||||
import Mtlstats.Menu
|
||||
import Mtlstats.Menu.EditPlayer
|
||||
import Mtlstats.Prompt
|
||||
import Mtlstats.Prompt.EditPlayer
|
||||
import Mtlstats.Types
|
||||
@@ -40,6 +42,8 @@ editPlayerC eps
|
||||
EPNumber -> numberC
|
||||
EPName -> nameC
|
||||
EPPosition -> positionC
|
||||
EPYtd -> ytdC
|
||||
EPLifetime -> lifetimeC
|
||||
EPYtdGoals -> ytdGoalsC
|
||||
EPYtdAssists -> ytdAssistsC
|
||||
EPYtdPMin -> ytdPMinC
|
||||
@@ -48,96 +52,46 @@ editPlayerC eps
|
||||
EPLtPMin -> ltPMinC
|
||||
|
||||
selectPlayerC :: Controller
|
||||
selectPlayerC = Controller
|
||||
{ drawController = drawPrompt playerToEditPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler playerToEditPrompt e
|
||||
return True
|
||||
}
|
||||
selectPlayerC = promptController playerToEditPrompt
|
||||
|
||||
menuC :: Controller
|
||||
menuC = Controller
|
||||
{ drawController = \s -> do
|
||||
let
|
||||
header = fromMaybe "" $ do
|
||||
pid <- s^.progMode.editPlayerStateL.epsSelectedPlayer
|
||||
p <- nth pid $ s^.database.dbPlayers
|
||||
Just $ playerDetails p ++ "\n"
|
||||
C.drawString header
|
||||
drawMenu editPlayerMenu
|
||||
, handleController = \e -> do
|
||||
menuHandler editPlayerMenu e
|
||||
return True
|
||||
}
|
||||
menuC = menuControllerWith header editPlayerMenu
|
||||
|
||||
numberC :: Controller
|
||||
numberC = Controller
|
||||
{ drawController = drawPrompt editPlayerNumPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerNumPrompt e
|
||||
return True
|
||||
}
|
||||
numberC = promptController editPlayerNumPrompt
|
||||
|
||||
nameC :: Controller
|
||||
nameC = Controller
|
||||
{ drawController = drawPrompt editPlayerNamePrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerNamePrompt e
|
||||
return True
|
||||
}
|
||||
nameC = promptController editPlayerNamePrompt
|
||||
|
||||
positionC :: Controller
|
||||
positionC = Controller
|
||||
{ drawController = drawPrompt editPlayerPosPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerPosPrompt e
|
||||
return True
|
||||
}
|
||||
positionC = promptController editPlayerPosPrompt
|
||||
|
||||
ytdC :: Controller
|
||||
ytdC = menuControllerWith header editPlayerYtdMenu
|
||||
|
||||
lifetimeC :: Controller
|
||||
lifetimeC = menuControllerWith header editPlayerLtMenu
|
||||
|
||||
ytdGoalsC :: Controller
|
||||
ytdGoalsC = Controller
|
||||
{ drawController = drawPrompt editPlayerYtdGoalsPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerYtdGoalsPrompt e
|
||||
return True
|
||||
}
|
||||
ytdGoalsC = promptController editPlayerYtdGoalsPrompt
|
||||
|
||||
ytdAssistsC :: Controller
|
||||
ytdAssistsC = Controller
|
||||
{ drawController = drawPrompt editPlayerYtdAssistsPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerYtdAssistsPrompt e
|
||||
return True
|
||||
}
|
||||
ytdAssistsC = promptController editPlayerYtdAssistsPrompt
|
||||
|
||||
ytdPMinC :: Controller
|
||||
ytdPMinC = Controller
|
||||
{ drawController = drawPrompt editPlayerYtdPMinPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerYtdPMinPrompt e
|
||||
return True
|
||||
}
|
||||
ytdPMinC = promptController editPlayerYtdPMinPrompt
|
||||
|
||||
ltGoalsC :: Controller
|
||||
ltGoalsC = Controller
|
||||
{ drawController = drawPrompt editPlayerLtGoalsPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerLtGoalsPrompt e
|
||||
return True
|
||||
}
|
||||
ltGoalsC = promptController editPlayerLtGoalsPrompt
|
||||
|
||||
ltAssistsC :: Controller
|
||||
ltAssistsC = Controller
|
||||
{ drawController = drawPrompt editPlayerLtAssistsPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerLtAssistsPrompt e
|
||||
return True
|
||||
}
|
||||
ltAssistsC = promptController editPlayerLtAssistsPrompt
|
||||
|
||||
ltPMinC :: Controller
|
||||
ltPMinC = Controller
|
||||
{ drawController = drawPrompt editPlayerLtPMinPrompt
|
||||
, handleController = \e -> do
|
||||
promptHandler editPlayerLtPMinPrompt e
|
||||
return True
|
||||
}
|
||||
ltPMinC = promptController editPlayerLtPMinPrompt
|
||||
|
||||
header :: ProgState -> C.Update ()
|
||||
header s = C.drawString $ fromMaybe "" $ do
|
||||
pid <- s^.progMode.editPlayerStateL.epsSelectedPlayer
|
||||
player <- nth pid $ s^.database.dbPlayers
|
||||
Just $ playerDetails player ++ "\n"
|
||||
|
||||
@@ -39,95 +39,43 @@ import Mtlstats.Types
|
||||
import Mtlstats.Util
|
||||
|
||||
-- | Dispatcher for a new game
|
||||
newGameC :: ProgState -> Controller
|
||||
newGameC s = let
|
||||
gs = s^.progMode.gameStateL
|
||||
in if null $ gs^.gameYear then gameYearC
|
||||
else if null $ gs^.gameMonth then gameMonthC
|
||||
else if null $ gs^.gameDay then gameDayC
|
||||
else if null $ gs^.gameType then gameTypeC
|
||||
else if null $ gs^.otherTeam then otherTeamC
|
||||
else if null $ gs^.homeScore then homeScoreC
|
||||
else if null $ gs^.awayScore then awayScoreC
|
||||
else if null $ gs^.overtimeFlag then overtimeFlagC
|
||||
else if not $ gs^.dataVerified then verifyDataC
|
||||
else if fromJust (unaccountedPoints gs) then goalInput gs
|
||||
else if isJust $ gs^.gameSelectedPlayer then getPMinsC
|
||||
else if not $ gs^.gamePMinsRecorded then pMinPlayerC
|
||||
else if not $ gs^.gameGoalieAssigned then goalieInputC s
|
||||
else reportC
|
||||
newGameC :: GameState -> Controller
|
||||
newGameC gs
|
||||
| null $ gs^.gameYear = gameYearC
|
||||
| null $ gs^.gameMonth = gameMonthC
|
||||
| null $ gs^.gameDay = gameDayC
|
||||
| null $ gs^.gameType = gameTypeC
|
||||
| null $ gs^.otherTeam = otherTeamC
|
||||
| null $ gs^.homeScore = homeScoreC
|
||||
| null $ gs^.awayScore = awayScoreC
|
||||
| null $ gs^.overtimeFlag = overtimeFlagC
|
||||
| not $ gs^.dataVerified = verifyDataC
|
||||
| fromJust (unaccountedPoints gs) = goalInput gs
|
||||
| isJust $ gs^.gameSelectedPlayer = getPMinsC
|
||||
| not $ gs^.gamePMinsRecorded = pMinPlayerC
|
||||
| not $ gs^.gameGoalieAssigned = goalieInputC gs
|
||||
| otherwise = reportC
|
||||
|
||||
gameYearC :: Controller
|
||||
gameYearC = Controller
|
||||
{ drawController = \s -> do
|
||||
header s
|
||||
drawPrompt gameYearPrompt s
|
||||
, handleController = \e -> do
|
||||
promptHandler gameYearPrompt e
|
||||
return True
|
||||
}
|
||||
gameYearC = promptControllerWith header gameYearPrompt
|
||||
|
||||
gameMonthC :: Controller
|
||||
gameMonthC = Controller
|
||||
{ drawController = \s -> do
|
||||
header s
|
||||
drawMenu gameMonthMenu
|
||||
, handleController = \e -> do
|
||||
menuHandler gameMonthMenu e
|
||||
return True
|
||||
}
|
||||
gameMonthC = menuControllerWith header gameMonthMenu
|
||||
|
||||
gameDayC :: Controller
|
||||
gameDayC = Controller
|
||||
{ drawController = \s -> do
|
||||
header s
|
||||
drawPrompt gameDayPrompt s
|
||||
, handleController = \e -> do
|
||||
promptHandler gameDayPrompt e
|
||||
modify validateGameDate
|
||||
return True
|
||||
}
|
||||
gameDayC = promptControllerWith header gameDayPrompt
|
||||
|
||||
gameTypeC :: Controller
|
||||
gameTypeC = Controller
|
||||
{ drawController = \s -> do
|
||||
header s
|
||||
drawMenu gameTypeMenu
|
||||
, handleController = \e -> do
|
||||
menuHandler gameTypeMenu e
|
||||
return True
|
||||
}
|
||||
gameTypeC = menuControllerWith header gameTypeMenu
|
||||
|
||||
otherTeamC :: Controller
|
||||
otherTeamC = Controller
|
||||
{ drawController = \s -> do
|
||||
header s
|
||||
drawPrompt otherTeamPrompt s
|
||||
, handleController = \e -> do
|
||||
promptHandler otherTeamPrompt e
|
||||
return True
|
||||
}
|
||||
otherTeamC = promptControllerWith header otherTeamPrompt
|
||||
|
||||
homeScoreC :: Controller
|
||||
homeScoreC = Controller
|
||||
{ drawController = \s -> do
|
||||
header s
|
||||
drawPrompt homeScorePrompt s
|
||||
, handleController = \e -> do
|
||||
promptHandler homeScorePrompt e
|
||||
return True
|
||||
}
|
||||
homeScoreC = promptControllerWith header homeScorePrompt
|
||||
|
||||
awayScoreC :: Controller
|
||||
awayScoreC = Controller
|
||||
{ drawController = \s -> do
|
||||
header s
|
||||
drawPrompt awayScorePrompt s
|
||||
, handleController = \e -> do
|
||||
promptHandler awayScorePrompt e
|
||||
modify overtimeCheck
|
||||
return True
|
||||
}
|
||||
awayScoreC = promptControllerWith header awayScorePrompt
|
||||
|
||||
overtimeFlagC :: Controller
|
||||
overtimeFlagC = Controller
|
||||
@@ -146,19 +94,22 @@ verifyDataC = Controller
|
||||
let gs = s^.progMode.gameStateL
|
||||
header s
|
||||
C.drawString "\n"
|
||||
C.drawString $ " Date: " ++ gameDate gs ++ "\n"
|
||||
C.drawString $ " Game type: " ++ show (fromJust $ gs^.gameType) ++ "\n"
|
||||
C.drawString $ "Other team: " ++ gs^.otherTeam ++ "\n"
|
||||
C.drawString $ "Home score: " ++ show (fromJust $ gs^.homeScore) ++ "\n"
|
||||
C.drawString $ "Away score: " ++ show (fromJust $ gs^.awayScore) ++ "\n"
|
||||
C.drawString $ " Overtime: " ++ show (fromJust $ gs^.overtimeFlag) ++ "\n\n"
|
||||
C.drawString "Is the above information correct? (Y/N)"
|
||||
C.drawString $ unlines $ labelTable
|
||||
[ ( "Date", gameDate gs )
|
||||
, ( "Game type", show $ fromJust $ gs^.gameType )
|
||||
, ( "Other team", gs^.otherTeam )
|
||||
, ( "Home score", show $ fromJust $ gs^.homeScore )
|
||||
, ( "Away score", show $ fromJust $ gs^.awayScore )
|
||||
, ( "Overtime", show $ fromJust $ gs^.overtimeFlag )
|
||||
]
|
||||
C.drawString "\nIs the above information correct? (Y/N)"
|
||||
return C.CursorInvisible
|
||||
, handleController = \e -> do
|
||||
case ynHandler e of
|
||||
Just True -> do
|
||||
modify $ progMode.gameStateL.dataVerified .~ True
|
||||
modify updateGameStats
|
||||
Just True -> modify
|
||||
$ (progMode.gameStateL.dataVerified .~ True)
|
||||
. updateGameStats
|
||||
. awardShutouts
|
||||
Just False -> modify $ progMode.gameStateL .~ newGameState
|
||||
Nothing -> return ()
|
||||
return True
|
||||
|
||||
@@ -33,16 +33,12 @@ import Mtlstats.Types
|
||||
import Mtlstats.Util
|
||||
|
||||
-- | The dispatcher for handling goalie input
|
||||
goalieInputC :: ProgState -> Controller
|
||||
goalieInputC s = let
|
||||
gs = s^.progMode.gameStateL
|
||||
in if gs^.gameGoaliesRecorded
|
||||
then selectGameGoalieC s
|
||||
else if null $ gs^.gameSelectedGoalie
|
||||
then selectGoalieC
|
||||
else if null $ gs^.gameGoalieMinsPlayed
|
||||
then minsPlayedC
|
||||
else goalsAllowedC
|
||||
goalieInputC :: GameState -> Controller
|
||||
goalieInputC gs
|
||||
| gs^.gameGoaliesRecorded = selectGameGoalieC
|
||||
| null $ gs^.gameSelectedGoalie = selectGoalieC
|
||||
| null $ gs^.gameGoalieMinsPlayed = minsPlayedC
|
||||
| otherwise = goalsAllowedC
|
||||
|
||||
selectGoalieC :: Controller
|
||||
selectGoalieC = promptController selectGameGoaliePrompt
|
||||
@@ -53,8 +49,8 @@ minsPlayedC = promptControllerWith header goalieMinsPlayedPrompt
|
||||
goalsAllowedC :: Controller
|
||||
goalsAllowedC = promptControllerWith header goalsAllowedPrompt
|
||||
|
||||
selectGameGoalieC :: ProgState -> Controller
|
||||
selectGameGoalieC = menuController . gameGoalieMenu
|
||||
selectGameGoalieC :: Controller
|
||||
selectGameGoalieC = menuStateController gameGoalieMenu
|
||||
|
||||
header :: ProgState -> C.Update ()
|
||||
header s = C.drawString $ unlines
|
||||
|
||||
@@ -19,6 +19,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
-}
|
||||
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
|
||||
module Mtlstats.Format
|
||||
( padNum
|
||||
, left
|
||||
@@ -26,8 +28,18 @@ module Mtlstats.Format
|
||||
, centre
|
||||
, overlay
|
||||
, month
|
||||
, labelTable
|
||||
, numTable
|
||||
, tableWith
|
||||
, complexTable
|
||||
, overlayLast
|
||||
, showFloating
|
||||
) where
|
||||
|
||||
import Data.List (transpose)
|
||||
|
||||
import Mtlstats.Types
|
||||
|
||||
-- | Pad an 'Int' with leading zeroes to fit a certain character width
|
||||
padNum
|
||||
:: Int
|
||||
@@ -101,3 +113,84 @@ month 10 = "OCT"
|
||||
month 11 = "NOV"
|
||||
month 12 = "DEC"
|
||||
month _ = ""
|
||||
|
||||
-- | Creates a two-column table with labels
|
||||
labelTable :: [(String, String)] -> [String]
|
||||
labelTable xs = let
|
||||
labelWidth = maximum $ map (length . fst) xs
|
||||
in map
|
||||
(\(label, val) -> right labelWidth label ++ ": " ++ val)
|
||||
xs
|
||||
|
||||
-- | Creates a variable column table of numbers with two axes
|
||||
numTable
|
||||
:: [String]
|
||||
-- ^ The top column labels
|
||||
-> [(String, [Int])]
|
||||
-- ^ The rows with their labels
|
||||
-> [String]
|
||||
numTable headers rows = tableWith right $ header : body
|
||||
where
|
||||
header = "" : headers
|
||||
body = map
|
||||
(\(label, row) ->
|
||||
label : map show row)
|
||||
rows
|
||||
|
||||
-- | Creates a table from a two-dimensional list with a specified
|
||||
-- padding function
|
||||
tableWith
|
||||
:: (Int -> String -> String)
|
||||
-- ^ The padding function
|
||||
-> [[String]]
|
||||
-- ^ The cells
|
||||
-> [String]
|
||||
tableWith pFunc tData = complexTable
|
||||
(repeat pFunc)
|
||||
(map (map CellText) tData)
|
||||
|
||||
-- | Creates a complex table
|
||||
complexTable
|
||||
:: [Int -> String -> String]
|
||||
-- ^ The padding function for each column
|
||||
-> [[TableCell]]
|
||||
-- ^ The table cells (an array of rows)
|
||||
-> [String]
|
||||
complexTable pFuncs tData = let
|
||||
widths = map
|
||||
(map $ \case
|
||||
CellText str -> length str
|
||||
CellFill _ -> 0)
|
||||
tData
|
||||
colWidths = map maximum $ transpose widths
|
||||
|
||||
bFunc = \case
|
||||
[] -> ""
|
||||
[(f, len, CellText str)] -> f len str
|
||||
[(_, len, CellFill ch)] -> replicate len ch
|
||||
(f, len, CellText str) : cells -> f len str ++ " " ++ bFunc cells
|
||||
(_, len, CellFill ch) : cells -> replicate (succ len) ch ++ bFunc cells
|
||||
|
||||
in map
|
||||
(bFunc . zip3 pFuncs colWidths)
|
||||
tData
|
||||
|
||||
-- | Places an overlay on the last line of an report
|
||||
overlayLast
|
||||
:: String
|
||||
-- ^ The text to overlay
|
||||
-> [String]
|
||||
-- ^ The report to modify
|
||||
-> [String]
|
||||
-- ^ The resulting report
|
||||
overlayLast _ [] = []
|
||||
overlayLast str [l] = [overlay str l]
|
||||
overlayLast str (l:ls) = l : overlayLast str ls
|
||||
|
||||
-- | Converts a non-integer into a string
|
||||
showFloating :: RealFrac n => n -> String
|
||||
showFloating n = let
|
||||
i = round $ n * 100
|
||||
whole = i `div` 100
|
||||
fraction = i `mod` 100
|
||||
in show whole ++ "." ++ padNum 2 fraction
|
||||
|
||||
47
src/Mtlstats/Helpers/Goalie.hs
Normal file
47
src/Mtlstats/Helpers/Goalie.hs
Normal file
@@ -0,0 +1,47 @@
|
||||
{- |
|
||||
|
||||
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/>.
|
||||
|
||||
-}
|
||||
|
||||
module Mtlstats.Helpers.Goalie (goalieDetails) where
|
||||
|
||||
import Lens.Micro ((^.))
|
||||
|
||||
import Mtlstats.Format
|
||||
import Mtlstats.Types
|
||||
|
||||
-- | Provides a detailed 'String' describing a 'Goalie'
|
||||
goalieDetails :: Goalie -> String
|
||||
goalieDetails g = let
|
||||
header = unlines $ labelTable
|
||||
[ ( "Number", show $ g^.gNumber )
|
||||
, ( "Name", g^.gName )
|
||||
]
|
||||
|
||||
body = unlines $ numTable ["YTD", "Lifetime"] $ map
|
||||
(\(label, lens) -> (label, [g^.gYtd.lens, g^.gLifetime.lens]))
|
||||
[ ( "Games played", gsGames )
|
||||
, ( "Mins played", gsMinsPlayed )
|
||||
, ( "Goals allowed", gsGoalsAllowed )
|
||||
, ( "Wins", gsWins )
|
||||
, ( "Losses", gsLosses )
|
||||
, ( "Ties", gsTies )
|
||||
]
|
||||
|
||||
in header ++ "\n" ++ body
|
||||
45
src/Mtlstats/Helpers/Player.hs
Normal file
45
src/Mtlstats/Helpers/Player.hs
Normal file
@@ -0,0 +1,45 @@
|
||||
{- |
|
||||
|
||||
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/>.
|
||||
|
||||
-}
|
||||
|
||||
module Mtlstats.Helpers.Player (playerDetails) where
|
||||
|
||||
import Lens.Micro ((^.))
|
||||
|
||||
import Mtlstats.Format
|
||||
import Mtlstats.Types
|
||||
|
||||
-- | Provides a detailed string describing a 'Player'
|
||||
playerDetails :: Player -> String
|
||||
playerDetails p = unlines $ top ++ [""] ++ table
|
||||
where
|
||||
top = labelTable
|
||||
[ ( "Number", show $ p^.pNumber )
|
||||
, ( "Name", p^.pName )
|
||||
, ( "Position", p^.pPosition )
|
||||
]
|
||||
|
||||
table = numTable ["YTD", "Lifetime"] $ map
|
||||
(\(label, lens) ->
|
||||
(label, [p^.pYtd.lens, p^.pLifetime.lens]))
|
||||
[ ( "Goals", psGoals )
|
||||
, ( "Assists", psAssists )
|
||||
, ( "Penalty mins", psPMin )
|
||||
]
|
||||
@@ -23,6 +23,7 @@ module Mtlstats.Menu (
|
||||
-- * Menu Functions
|
||||
menuController,
|
||||
menuControllerWith,
|
||||
menuStateController,
|
||||
drawMenu,
|
||||
menuHandler,
|
||||
-- * Menus
|
||||
@@ -30,7 +31,6 @@ module Mtlstats.Menu (
|
||||
newSeasonMenu,
|
||||
gameMonthMenu,
|
||||
gameTypeMenu,
|
||||
editPlayerMenu,
|
||||
gameGoalieMenu
|
||||
) where
|
||||
|
||||
@@ -40,7 +40,7 @@ import Data.Aeson (encodeFile)
|
||||
import Data.Char (toUpper)
|
||||
import qualified Data.Map as M
|
||||
import Data.Maybe (mapMaybe)
|
||||
import Lens.Micro ((^.), (.~), (?~))
|
||||
import Lens.Micro ((^.), (?~))
|
||||
import Lens.Micro.Extras (view)
|
||||
import System.EasyFile
|
||||
( createDirectoryIfMissing
|
||||
@@ -77,6 +77,21 @@ menuControllerWith header menu = Controller
|
||||
return True
|
||||
}
|
||||
|
||||
-- | Generate and create a controller for a menu based on the current
|
||||
-- 'ProgState'
|
||||
menuStateController
|
||||
:: (ProgState -> Menu ())
|
||||
-- ^ The function to generate the menu
|
||||
-> Controller
|
||||
-- ^ The resulting controller
|
||||
menuStateController menuFunc = Controller
|
||||
{ drawController = drawMenu . menuFunc
|
||||
, handleController = \e -> do
|
||||
menu <- gets menuFunc
|
||||
menuHandler menu e
|
||||
return True
|
||||
}
|
||||
|
||||
-- | The draw function for a 'Menu'
|
||||
drawMenu :: Menu a -> C.Update C.CursorMode
|
||||
drawMenu m = do
|
||||
@@ -157,24 +172,6 @@ gameTypeMenu = Menu "Game type:" ()
|
||||
modify $ progMode.gameStateL.gameType ?~ AwayGame
|
||||
]
|
||||
|
||||
-- | The player edit menu
|
||||
editPlayerMenu :: Menu ()
|
||||
editPlayerMenu = Menu "*** EDIT PLAYER ***" () $ map
|
||||
(\(ch, label, mode) -> MenuItem ch label $ case mode of
|
||||
Nothing -> modify $ progMode .~ MainMenu
|
||||
Just m -> modify $ progMode.editPlayerStateL.epsMode .~ m)
|
||||
[ ( '1', "Change number", Just EPNumber )
|
||||
, ( '2', "Change name", Just EPName )
|
||||
, ( '3', "Change position", Just EPPosition )
|
||||
, ( '4', "YTD goals", Just EPYtdGoals )
|
||||
, ( '5', "YTD assists", Just EPYtdAssists )
|
||||
, ( '6', "YTD penalty mins", Just EPYtdPMin )
|
||||
, ( '7', "Lifetime goals", Just EPLtGoals )
|
||||
, ( '8', "Lifetime assists", Just EPLtAssists )
|
||||
, ( '9', "Lifetime penalty mins", Just EPLtPMin )
|
||||
, ( '0', "Finished editing", Nothing )
|
||||
]
|
||||
|
||||
-- | Game goalie selection menu
|
||||
gameGoalieMenu :: ProgState -> Menu ()
|
||||
gameGoalieMenu s = let
|
||||
|
||||
74
src/Mtlstats/Menu/EditPlayer.hs
Normal file
74
src/Mtlstats/Menu/EditPlayer.hs
Normal file
@@ -0,0 +1,74 @@
|
||||
{- |
|
||||
|
||||
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/>.
|
||||
|
||||
-}
|
||||
|
||||
module Mtlstats.Menu.EditPlayer
|
||||
( editPlayerMenu
|
||||
, editPlayerYtdMenu
|
||||
, editPlayerLtMenu
|
||||
) where
|
||||
|
||||
import Control.Monad.Trans.State (modify)
|
||||
import Lens.Micro ((.~))
|
||||
|
||||
import Mtlstats.Types
|
||||
import Mtlstats.Types.Menu
|
||||
|
||||
-- | The 'Player' edit menu
|
||||
editPlayerMenu :: Menu ()
|
||||
editPlayerMenu = Menu "*** EDIT PLAYER ***" () $ map
|
||||
(\(ch, label, mode) -> MenuItem ch label $ case mode of
|
||||
Nothing -> modify $ progMode .~ MainMenu
|
||||
Just m -> modify $ progMode.editPlayerStateL.epsMode .~ m)
|
||||
-- key, label, value
|
||||
[ ( '1', "Edit number", Just EPNumber )
|
||||
, ( '2', "Edit name", Just EPName )
|
||||
, ( '3', "Edit position", Just EPPosition )
|
||||
, ( '4', "Edit YTD stats", Just EPYtd )
|
||||
, ( '5', "Edit lifetime stats", Just EPLifetime )
|
||||
, ( 'R', "Finished editing", Nothing )
|
||||
]
|
||||
|
||||
-- | The 'Player' YTD stats edit menu
|
||||
editPlayerYtdMenu :: Menu ()
|
||||
editPlayerYtdMenu = editMenu
|
||||
"*** EDIT PLAYER YEAR-TO-DATE ***"
|
||||
-- key, label, value
|
||||
[ ( '1', "Edit YTD goals", EPYtdGoals )
|
||||
, ( '2', "Edit YTD assists", EPYtdAssists )
|
||||
, ( '3', "Edit YTD penalty mins", EPYtdPMin )
|
||||
, ( 'R', "Return to player edit menu", EPMenu )
|
||||
]
|
||||
|
||||
-- | The 'Player' lifetime stats edit menu
|
||||
editPlayerLtMenu :: Menu ()
|
||||
editPlayerLtMenu = editMenu
|
||||
"*** EDIT PLAYER LIFETIME ***"
|
||||
-- key, label, value
|
||||
[ ( '1', "Edit lifetime goals", EPLtGoals )
|
||||
, ( '2', "Edit lifetime assits", EPLtAssists )
|
||||
, ( '3', "Edit lifetime penalty mins", EPLtPMin )
|
||||
, ( 'R', "Return to edit player menu", EPMenu )
|
||||
]
|
||||
|
||||
editMenu :: String -> [(Char, String, EditPlayerMode)] -> Menu ()
|
||||
editMenu title = Menu title () . map
|
||||
(\(key, label, val) -> MenuItem key label $
|
||||
modify $ progMode.editPlayerStateL.epsMode .~ val)
|
||||
@@ -28,6 +28,7 @@ module Mtlstats.Prompt (
|
||||
promptControllerWith,
|
||||
promptController,
|
||||
strPrompt,
|
||||
ucStrPrompt,
|
||||
numPrompt,
|
||||
selectPrompt,
|
||||
-- * Individual prompts
|
||||
@@ -46,7 +47,7 @@ import Control.Monad.Extra (whenJust)
|
||||
import Control.Monad.Trans.State (gets, modify)
|
||||
import Data.Char (isDigit, toUpper)
|
||||
import Data.Foldable (forM_)
|
||||
import Lens.Micro ((^.), (&), (.~), (?~))
|
||||
import Lens.Micro ((^.), (&), (.~), (?~), (%~))
|
||||
import Lens.Micro.Extras (view)
|
||||
import Text.Read (readMaybe)
|
||||
import qualified UI.NCurses as C
|
||||
@@ -68,10 +69,8 @@ promptHandler p (C.EventCharacter '\n') = do
|
||||
val <- gets $ view inputBuffer
|
||||
modify $ inputBuffer .~ ""
|
||||
promptAction p val
|
||||
promptHandler p (C.EventCharacter c) = let
|
||||
c' = toUpper c
|
||||
in when (promptCharCheck p c') $
|
||||
modify $ addChar c'
|
||||
promptHandler p (C.EventCharacter c) =
|
||||
modify $ inputBuffer %~ promptProcessChar p c
|
||||
promptHandler _ (C.EventSpecialKey C.KeyBackspace) =
|
||||
modify removeChar
|
||||
promptHandler p (C.EventSpecialKey k) =
|
||||
@@ -112,11 +111,21 @@ strPrompt
|
||||
-> Prompt
|
||||
strPrompt pStr act = Prompt
|
||||
{ promptDrawer = drawSimplePrompt pStr
|
||||
, promptCharCheck = const True
|
||||
, promptProcessChar = \ch -> (++ [ch])
|
||||
, promptAction = act
|
||||
, promptSpecialKey = const $ return ()
|
||||
}
|
||||
|
||||
-- | Creates an upper case string prompt
|
||||
ucStrPrompt
|
||||
:: String
|
||||
-- ^ The prompt string
|
||||
-> (String -> Action ())
|
||||
-- ^ The callback function for the result
|
||||
-> Prompt
|
||||
ucStrPrompt pStr act = (strPrompt pStr act)
|
||||
{ promptProcessChar = \ch -> (++ [toUpper ch]) }
|
||||
|
||||
-- | Builds a numeric prompt
|
||||
numPrompt
|
||||
:: String
|
||||
@@ -126,7 +135,9 @@ numPrompt
|
||||
-> Prompt
|
||||
numPrompt pStr act = Prompt
|
||||
{ promptDrawer = drawSimplePrompt pStr
|
||||
, promptCharCheck = isDigit
|
||||
, promptProcessChar = \ch str -> if isDigit ch
|
||||
then str ++ [ch]
|
||||
else str
|
||||
, promptAction = \inStr -> forM_ (readMaybe inStr) act
|
||||
, promptSpecialKey = const $ return ()
|
||||
}
|
||||
@@ -146,7 +157,7 @@ selectPrompt params = Prompt
|
||||
in "F" ++ show n ++ ") " ++ desc)
|
||||
results
|
||||
C.moveCursor row col
|
||||
, promptCharCheck = const True
|
||||
, promptProcessChar = \ch -> (++[ch])
|
||||
, promptAction = \sStr -> if null sStr
|
||||
then spCallback params Nothing
|
||||
else do
|
||||
@@ -180,7 +191,7 @@ playerNamePrompt = strPrompt "Player name: " $
|
||||
|
||||
-- | Prompts for a new player's position
|
||||
playerPosPrompt :: Prompt
|
||||
playerPosPrompt = strPrompt "Player position: " $
|
||||
playerPosPrompt = ucStrPrompt "Player position: " $
|
||||
modify . (progMode.createPlayerStateL.cpsPosition .~)
|
||||
|
||||
-- | Prompts tor the goalie's number
|
||||
|
||||
@@ -51,7 +51,7 @@ editPlayerNamePrompt = strPrompt "Player name: " $
|
||||
|
||||
-- | Prompt to edit a player's position
|
||||
editPlayerPosPrompt :: Prompt
|
||||
editPlayerPosPrompt = strPrompt "Player position: " $
|
||||
editPlayerPosPrompt = ucStrPrompt "Player position: " $
|
||||
editPlayer . (pPosition .~)
|
||||
|
||||
-- | Prompt to edit a player's year-to-date goals
|
||||
|
||||
@@ -55,7 +55,7 @@ gameDayPrompt = numPrompt "Day of month: " $
|
||||
|
||||
-- | Prompts for the other team name
|
||||
otherTeamPrompt :: Prompt
|
||||
otherTeamPrompt = strPrompt "Other team: " $
|
||||
otherTeamPrompt = ucStrPrompt "Other team: " $
|
||||
modify . (progMode.gameStateL.otherTeam .~)
|
||||
|
||||
-- | Prompts for the home score
|
||||
@@ -65,8 +65,9 @@ homeScorePrompt = numPrompt "Home score: " $
|
||||
|
||||
-- | Prompts for the away score
|
||||
awayScorePrompt :: Prompt
|
||||
awayScorePrompt = numPrompt "Away score: " $
|
||||
modify . (progMode.gameStateL.awayScore ?~)
|
||||
awayScorePrompt = numPrompt "Away score: " $ \score -> modify
|
||||
$ overtimeCheck
|
||||
. (progMode.gameStateL.awayScore ?~ score)
|
||||
|
||||
-- | Prompts for the player who scored the goal
|
||||
recordGoalPrompt
|
||||
|
||||
@@ -19,10 +19,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
-}
|
||||
|
||||
module Mtlstats.Report (report, gameDate, playerNameColWidth) where
|
||||
module Mtlstats.Report (report, gameDate) where
|
||||
|
||||
import qualified Data.Map as M
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Data.Maybe (fromMaybe, mapMaybe)
|
||||
import Lens.Micro ((^.))
|
||||
|
||||
import Mtlstats.Config
|
||||
@@ -60,7 +60,8 @@ standingsReport width s = fromMaybe [] $ do
|
||||
tStats = addGameStats hStats aStats
|
||||
hScore <- gs^.homeScore
|
||||
aScore <- gs^.awayScore
|
||||
Just
|
||||
let
|
||||
rHeader =
|
||||
[ overlay
|
||||
("GAME NUMBER " ++ padNum 2 gNum)
|
||||
(centre width
|
||||
@@ -69,45 +70,96 @@ standingsReport width s = fromMaybe [] $ do
|
||||
, date
|
||||
, centre width "STANDINGS"
|
||||
, ""
|
||||
, centre width
|
||||
$ left 11 myTeam
|
||||
++ right 2 "G"
|
||||
++ right 4 "W"
|
||||
++ right 4 "L"
|
||||
++ right 4 "OT"
|
||||
++ right 4 "GF"
|
||||
++ right 4 "GA"
|
||||
++ right 4 "P"
|
||||
, centre width
|
||||
$ left 11 "HOME"
|
||||
++ showStats hStats
|
||||
, centre width
|
||||
$ left 11 "ROAD"
|
||||
++ showStats aStats
|
||||
, centre width
|
||||
$ replicate 11 ' '
|
||||
++ replicate (2 + 4 * 6) '-'
|
||||
, centre width
|
||||
$ left 11 "TOTALS"
|
||||
++ showStats tStats
|
||||
]
|
||||
|
||||
tHeader =
|
||||
[ CellText myTeam
|
||||
, CellText " G"
|
||||
, CellText " W"
|
||||
, CellText " L"
|
||||
, CellText " OT"
|
||||
, CellText " GF"
|
||||
, CellText " GA"
|
||||
, CellText " P"
|
||||
]
|
||||
|
||||
rowCells stats =
|
||||
[ CellText $ show $ gmsGames stats
|
||||
, CellText $ show $ stats^.gmsWins
|
||||
, CellText $ show $ stats^.gmsLosses
|
||||
, CellText $ show $ stats^.gmsOvertime
|
||||
, CellText $ show $ stats^.gmsGoalsFor
|
||||
, CellText $ show $ stats^.gmsGoalsAgainst
|
||||
, CellText $ show $ gmsPoints stats
|
||||
]
|
||||
|
||||
body =
|
||||
[ CellText "HOME" : rowCells hStats
|
||||
, CellText "ROAD" : rowCells aStats
|
||||
]
|
||||
|
||||
separator = CellText "" : replicate 7 (CellFill '-')
|
||||
totals = CellText "TOTALS" : rowCells tStats
|
||||
|
||||
table = map (centre width) $
|
||||
complexTable
|
||||
(left : repeat right)
|
||||
(tHeader : body ++ [separator, totals])
|
||||
|
||||
Just $ rHeader ++ table
|
||||
|
||||
gameStatsReport :: Int -> ProgState -> [String]
|
||||
gameStatsReport width s = playerReport width "GAME" $
|
||||
fromMaybe [] $ mapM
|
||||
gameStatsReport width s = let
|
||||
gs = s^.progMode.gameStateL
|
||||
db = s^.database
|
||||
|
||||
playerStats = mapMaybe
|
||||
(\(pid, stats) -> do
|
||||
p <- nth pid $ s^.database.dbPlayers
|
||||
p <- nth pid $ db^.dbPlayers
|
||||
Just (p, stats))
|
||||
(M.toList $ s^.progMode.gameStateL.gamePlayerStats)
|
||||
(M.toList $ gs^.gamePlayerStats)
|
||||
|
||||
goalieStats = mapMaybe
|
||||
(\(gid, stats) -> do
|
||||
g <- nth gid $ db^.dbGoalies
|
||||
Just (g, stats))
|
||||
(M.toList $ gs^.gameGoalieStats)
|
||||
|
||||
criteria (_, ps) = psPoints ps > 0
|
||||
|
||||
in filteredPlayerReport width "GAME" criteria playerStats
|
||||
++ [""]
|
||||
++ gameGoalieReport width goalieStats
|
||||
|
||||
yearToDateStatsReport :: Int -> ProgState -> [String]
|
||||
yearToDateStatsReport width s = playerReport width "YEAR TO DATE" $
|
||||
map (\p -> (p, p^.pYtd)) $
|
||||
filter playerIsActive $ s^.database.dbPlayers
|
||||
yearToDateStatsReport width s = let
|
||||
db = s^.database
|
||||
|
||||
playerStats = map (\p -> (p, p^.pYtd))
|
||||
$ filter playerIsActive
|
||||
$ db^.dbPlayers
|
||||
|
||||
goalieStats = map (\g -> (g, g^.gYtd))
|
||||
$ filter goalieIsActive
|
||||
$ db^.dbGoalies
|
||||
|
||||
in playerReport width "YEAR TO DATE" playerStats
|
||||
++ [""]
|
||||
++ goalieReport width goalieStats
|
||||
|
||||
lifetimeStatsReport :: Int -> ProgState -> [String]
|
||||
lifetimeStatsReport width s = playerReport width "LIFETIME" $
|
||||
map (\p -> (p, p^.pLifetime)) $ s^.database.dbPlayers
|
||||
lifetimeStatsReport width s = let
|
||||
db = s^.database
|
||||
|
||||
playerStats = map (\p -> (p, p^.pYtd))
|
||||
$ db^.dbPlayers
|
||||
|
||||
goalieStats = map (\g -> (g, g^.gYtd))
|
||||
$ db^.dbGoalies
|
||||
|
||||
in playerReport width "LIFETIME" playerStats
|
||||
++ [""]
|
||||
++ goalieReport width goalieStats
|
||||
|
||||
gameDate :: GameState -> String
|
||||
gameDate gs = fromMaybe "" $ do
|
||||
@@ -117,54 +169,124 @@ gameDate gs = fromMaybe "" $ do
|
||||
Just $ m ++ " " ++ d ++ " " ++ y
|
||||
|
||||
playerReport :: Int -> String -> [(Player, PlayerStats)] -> [String]
|
||||
playerReport width label ps = let
|
||||
nameWidth = playerNameColWidth $ map fst ps
|
||||
tStats = foldr (addPlayerStats . snd) newPlayerStats ps
|
||||
in
|
||||
playerReport width label ps =
|
||||
filteredPlayerReport width label (const True) ps
|
||||
|
||||
filteredPlayerReport
|
||||
:: Int
|
||||
-> String
|
||||
-> ((Player, PlayerStats) -> Bool)
|
||||
-> [(Player, PlayerStats)]
|
||||
-> [String]
|
||||
filteredPlayerReport width label criteria ps = let
|
||||
tStats = foldl addPlayerStats newPlayerStats $ map snd ps
|
||||
fps = filter criteria ps
|
||||
|
||||
rHeader =
|
||||
[ centre width (label ++ " STATISTICS")
|
||||
, ""
|
||||
, centre width
|
||||
$ "NO. "
|
||||
++ left nameWidth "PLAYER"
|
||||
++ right 3 "G"
|
||||
++ right 6 "A"
|
||||
++ right 6 "P"
|
||||
++ right 6 "PM"
|
||||
] ++ map
|
||||
(\(p, stats) -> centre width
|
||||
$ right 2 (show $ p^.pNumber)
|
||||
++ " "
|
||||
++ left nameWidth (p^.pName)
|
||||
++ right 3 (show $ stats^.psGoals)
|
||||
++ right 6 (show $ stats^.psAssists)
|
||||
++ right 6 (show $ psPoints stats)
|
||||
++ right 6 (show $ stats^.psPMin))
|
||||
ps ++
|
||||
[ centre width
|
||||
$ replicate (4 + nameWidth) ' '
|
||||
++ replicate (3 + 3 * 6) '-'
|
||||
, overlay
|
||||
(label ++ " TOTALS")
|
||||
( centre width
|
||||
$ replicate (4 + nameWidth) ' '
|
||||
++ right 3 (show $ tStats^.psGoals)
|
||||
++ right 6 (show $ tStats^.psAssists)
|
||||
++ right 6 (show $ psPoints tStats)
|
||||
++ right 6 (show $ tStats^.psPMin)
|
||||
)
|
||||
]
|
||||
|
||||
playerNameColWidth :: [Player] -> Int
|
||||
playerNameColWidth = foldr
|
||||
(\player current -> max current $ succ $ length $ player^.pName)
|
||||
10
|
||||
tHeader =
|
||||
[ CellText "NO."
|
||||
, CellText "Player"
|
||||
, CellText " G"
|
||||
, CellText " A"
|
||||
, CellText " P"
|
||||
, CellText " PM"
|
||||
]
|
||||
|
||||
showStats :: GameStats -> String
|
||||
showStats gs
|
||||
= right 2 (show $ gmsGames gs)
|
||||
++ right 4 (show $ gs^.gmsWins)
|
||||
++ right 4 (show $ gs^.gmsLosses)
|
||||
++ right 4 (show $ gs^.gmsOvertime)
|
||||
++ right 4 (show $ gs^.gmsGoalsFor)
|
||||
++ right 4 (show $ gs^.gmsGoalsAgainst)
|
||||
++ right 4 (show $ gmsPoints gs)
|
||||
statsCells stats =
|
||||
[ CellText $ show $ stats^.psGoals
|
||||
, CellText $ show $ stats^.psAssists
|
||||
, CellText $ show $ psPoints stats
|
||||
, CellText $ show $ stats^.psPMin
|
||||
]
|
||||
|
||||
body = map
|
||||
(\(p, stats) ->
|
||||
[ CellText $ show (p^.pNumber) ++ " "
|
||||
, CellText $ p^.pName
|
||||
] ++ statsCells stats)
|
||||
fps
|
||||
|
||||
separator = replicate 2 (CellText "") ++ replicate 4 (CellFill '-')
|
||||
|
||||
totals =
|
||||
[ CellText ""
|
||||
, CellText ""
|
||||
] ++ statsCells tStats
|
||||
|
||||
table = overlayLast (label ++ " TOTALS")
|
||||
$ map (centre width)
|
||||
$ complexTable ([right, left] ++ repeat right)
|
||||
$ tHeader : body ++ [separator, totals]
|
||||
|
||||
in rHeader ++ table
|
||||
|
||||
goalieReport :: Int -> [(Goalie, GoalieStats)] -> [String]
|
||||
goalieReport width goalieData = let
|
||||
olayText = "GOALTENDING TOTALS"
|
||||
|
||||
tData = foldl addGoalieStats newGoalieStats
|
||||
$ map snd goalieData
|
||||
|
||||
header =
|
||||
[ CellText "NO."
|
||||
, CellText $ left (length olayText) "GOALTENDER"
|
||||
, CellText "GP"
|
||||
, CellText " MIN"
|
||||
, CellText " GA"
|
||||
, CellText " SO"
|
||||
, CellText "AVE"
|
||||
]
|
||||
|
||||
rowCells stats =
|
||||
[ CellText $ show $ stats^.gsGames
|
||||
, CellText $ show $ stats^.gsMinsPlayed
|
||||
, CellText $ show $ stats^.gsGoalsAllowed
|
||||
, CellText $ show $ stats^.gsShutouts
|
||||
, CellText $ showFloating $ gsAverage stats
|
||||
]
|
||||
|
||||
body = map
|
||||
(\(goalie, stats) ->
|
||||
[ CellText $ show (goalie^.gNumber) ++ " "
|
||||
, CellText $ goalie^.gName
|
||||
] ++ rowCells stats)
|
||||
goalieData
|
||||
|
||||
separator
|
||||
= replicate 2 (CellText "")
|
||||
++ replicate 5 (CellFill '-')
|
||||
|
||||
summary = replicate 2 (CellText "") ++ rowCells tData
|
||||
|
||||
in map (centre width)
|
||||
$ overlayLast olayText
|
||||
$ complexTable ([right, left] ++ repeat right)
|
||||
$ header : body ++ [separator, summary]
|
||||
|
||||
gameGoalieReport :: Int -> [(Goalie, GoalieStats)] -> [String]
|
||||
gameGoalieReport width goalieData = let
|
||||
header =
|
||||
[ CellText "NO."
|
||||
, CellText "GOALTENDER"
|
||||
, CellText " MIN"
|
||||
, CellText " GA"
|
||||
, CellText " AVE"
|
||||
]
|
||||
|
||||
body = map
|
||||
(\(goalie, stats) ->
|
||||
[ CellText $ show (goalie^.gNumber) ++ " "
|
||||
, CellText $ goalie^.gName
|
||||
, CellText $ show $ stats^.gsMinsPlayed
|
||||
, CellText $ show $ stats^.gsGoalsAllowed
|
||||
, CellText $ showFloating $ gsAverage stats
|
||||
])
|
||||
goalieData
|
||||
|
||||
in map (centre width)
|
||||
$ complexTable ([right, left] ++ repeat right)
|
||||
$ header : body
|
||||
|
||||
@@ -43,6 +43,7 @@ module Mtlstats.Types (
|
||||
GameStats (..),
|
||||
Prompt (..),
|
||||
SelectParams (..),
|
||||
TableCell (..),
|
||||
-- * Lenses
|
||||
-- ** ProgState Lenses
|
||||
database,
|
||||
@@ -120,6 +121,7 @@ module Mtlstats.Types (
|
||||
gsGames,
|
||||
gsMinsPlayed,
|
||||
gsGoalsAllowed,
|
||||
gsShutouts,
|
||||
gsWins,
|
||||
gsLosses,
|
||||
gsTies,
|
||||
@@ -161,7 +163,6 @@ module Mtlstats.Types (
|
||||
playerSearchExact,
|
||||
modifyPlayer,
|
||||
playerSummary,
|
||||
playerDetails,
|
||||
playerIsActive,
|
||||
-- ** PlayerStats Helpers
|
||||
psPoints,
|
||||
@@ -169,7 +170,11 @@ module Mtlstats.Types (
|
||||
-- ** Goalie Helpers
|
||||
goalieSearch,
|
||||
goalieSearchExact,
|
||||
goalieSummary
|
||||
goalieSummary,
|
||||
goalieIsActive,
|
||||
-- ** GoalieStats Helpers
|
||||
addGoalieStats,
|
||||
gsAverage
|
||||
) where
|
||||
|
||||
import Control.Monad.Trans.State (StateT)
|
||||
@@ -183,8 +188,11 @@ import Data.Aeson
|
||||
, toJSON
|
||||
, withObject
|
||||
, (.:)
|
||||
, (.:?)
|
||||
, (.!=)
|
||||
, (.=)
|
||||
)
|
||||
import Data.Char (toUpper)
|
||||
import Data.List (isInfixOf)
|
||||
import qualified Data.Map as M
|
||||
import Data.Maybe (listToMaybe)
|
||||
@@ -335,6 +343,8 @@ data EditPlayerMode
|
||||
| EPNumber
|
||||
| EPName
|
||||
| EPPosition
|
||||
| EPYtd
|
||||
| EPLifetime
|
||||
| EPYtdGoals
|
||||
| EPYtdAssists
|
||||
| EPYtdPMin
|
||||
@@ -512,6 +522,8 @@ data GoalieStats = GoalieStats
|
||||
-- ^ The number of minutes played
|
||||
, _gsGoalsAllowed :: Int
|
||||
-- ^ The number of goals allowed
|
||||
, _gsShutouts :: Int
|
||||
-- ^ The number of shutouts the goalie has accumulated
|
||||
, _gsWins :: Int
|
||||
-- ^ The number of wins
|
||||
, _gsLosses :: Int
|
||||
@@ -522,26 +534,29 @@ data GoalieStats = GoalieStats
|
||||
|
||||
instance FromJSON GoalieStats where
|
||||
parseJSON = withObject "GoalieStats" $ \v -> GoalieStats
|
||||
<$> v .: "games"
|
||||
<*> v .: "mins_played"
|
||||
<*> v .: "goals_allowed"
|
||||
<*> v .: "wins"
|
||||
<*> v .: "losses"
|
||||
<*> v .: "ties"
|
||||
<$> v .:? "games" .!= 0
|
||||
<*> v .:? "mins_played" .!= 0
|
||||
<*> v .:? "goals_allowed" .!= 0
|
||||
<*> v .:? "shutouts" .!= 0
|
||||
<*> v .:? "wins" .!= 0
|
||||
<*> v .:? "losses" .!= 0
|
||||
<*> v .:? "ties" .!= 0
|
||||
|
||||
instance ToJSON GoalieStats where
|
||||
toJSON (GoalieStats g m a w l t) = object
|
||||
toJSON (GoalieStats g m a s w l t) = object
|
||||
[ "games" .= g
|
||||
, "mins_played" .= m
|
||||
, "goals_allowed" .= a
|
||||
, "shutouts" .= s
|
||||
, "wins" .= w
|
||||
, "losses" .= l
|
||||
, "ties" .= t
|
||||
]
|
||||
toEncoding (GoalieStats g m a w l t) = pairs $
|
||||
toEncoding (GoalieStats g m a s w l t) = pairs $
|
||||
"games" .= g <>
|
||||
"mins_played" .= m <>
|
||||
"goals_allowed" .= a <>
|
||||
"shutouts" .= s <>
|
||||
"wins" .= w <>
|
||||
"losses" .= l <>
|
||||
"ties" .= t
|
||||
@@ -587,8 +602,8 @@ instance ToJSON GameStats where
|
||||
data Prompt = Prompt
|
||||
{ promptDrawer :: ProgState -> C.Update ()
|
||||
-- ^ Draws the prompt to the screen
|
||||
, promptCharCheck :: Char -> Bool
|
||||
-- ^ Determines whether or not the character is valid
|
||||
, promptProcessChar :: Char -> String -> String
|
||||
-- ^ Modifies the string based on the character entered
|
||||
, promptAction :: String -> Action ()
|
||||
-- ^ Action to perform when the value is entered
|
||||
, promptSpecialKey :: C.Key -> Action ()
|
||||
@@ -613,6 +628,14 @@ data SelectParams a = SelectParams
|
||||
-- ^ The function to call when the selection doesn't exist
|
||||
}
|
||||
|
||||
-- | Describes a table cell
|
||||
data TableCell
|
||||
= CellText String
|
||||
-- ^ A cell with text
|
||||
| CellFill Char
|
||||
-- ^ A cell filled with the given character
|
||||
deriving (Eq, Show)
|
||||
|
||||
makeLenses ''ProgState
|
||||
makeLenses ''GameState
|
||||
makeLenses ''CreatePlayerState
|
||||
@@ -785,6 +808,7 @@ newGoalieStats = GoalieStats
|
||||
{ _gsGames = 0
|
||||
, _gsMinsPlayed = 0
|
||||
, _gsGoalsAllowed = 0
|
||||
, _gsShutouts = 0
|
||||
, _gsWins = 0
|
||||
, _gsLosses = 0
|
||||
, _gsTies = 0
|
||||
@@ -881,7 +905,7 @@ playerSearch
|
||||
-- ^ The matching players with their index numbers
|
||||
playerSearch sStr =
|
||||
filter match . zip [0..]
|
||||
where match (_, p) = sStr `isInfixOf` (p^.pName)
|
||||
where match (_, p) = map toUpper sStr `isInfixOf` map toUpper (p^.pName)
|
||||
|
||||
-- | Searches for a player by exact match on name
|
||||
playerSearchExact
|
||||
@@ -915,20 +939,6 @@ playerSummary :: Player -> String
|
||||
playerSummary p =
|
||||
p^.pName ++ " (" ++ show (p^.pNumber) ++ ") " ++ p^.pPosition
|
||||
|
||||
-- | Provides a detailed string describing a 'Player'
|
||||
playerDetails :: Player -> String
|
||||
playerDetails p = unlines
|
||||
[ " Number: " ++ show (p^.pNumber)
|
||||
, " Name: " ++ p^.pName
|
||||
, " Position: " ++ p^.pPosition
|
||||
, " YTD goals: " ++ show (p^.pYtd.psGoals)
|
||||
, " YTD assists: " ++ show (p^.pYtd.psAssists)
|
||||
, " YTD penalty mins: " ++ show (p^.pYtd.psPMin)
|
||||
, " Lifetime goals: " ++ show (p^.pLifetime.psGoals)
|
||||
, " Lifetime assists: " ++ show (p^.pLifetime.psAssists)
|
||||
, "Lifetime penalty mins: " ++ show (p^.pLifetime.psPMin)
|
||||
]
|
||||
|
||||
-- | Determines whether or not a player has been active in the current
|
||||
-- season/year
|
||||
playerIsActive :: Player -> Bool
|
||||
@@ -958,8 +968,9 @@ goalieSearch
|
||||
-- ^ The list to search
|
||||
-> [(Int, Goalie)]
|
||||
-- ^ The search results with their corresponding index numbers
|
||||
goalieSearch sStr = filter (\(_, goalie) -> sStr `isInfixOf` (goalie^.gName)) .
|
||||
zip [0..]
|
||||
goalieSearch sStr =
|
||||
filter match . zip [0..]
|
||||
where match (_, g) = map toUpper sStr `isInfixOf` map toUpper (g^.gName)
|
||||
|
||||
-- | Searches a list of goalies for an exact match
|
||||
goalieSearchExact
|
||||
@@ -979,3 +990,30 @@ goalieSearchExact sStr goalies = let
|
||||
-- | Provides a description string for a 'Goalie'
|
||||
goalieSummary :: Goalie -> String
|
||||
goalieSummary g = g^.gName ++ " (" ++ show (g^.gNumber) ++ ")"
|
||||
|
||||
-- | Determines whether or not a goalie has been active in the current
|
||||
-- season
|
||||
goalieIsActive :: Goalie -> Bool
|
||||
goalieIsActive g = g^.gYtd.gsMinsPlayed /= 0
|
||||
|
||||
-- | Adds two sets of 'GoalieStats'
|
||||
addGoalieStats :: GoalieStats -> GoalieStats -> GoalieStats
|
||||
addGoalieStats g1 g2 = GoalieStats
|
||||
{ _gsGames = g1^.gsGames + g2^.gsGames
|
||||
, _gsMinsPlayed = g1^.gsMinsPlayed + g2^.gsMinsPlayed
|
||||
, _gsGoalsAllowed = g1^.gsGoalsAllowed + g2^.gsGoalsAllowed
|
||||
, _gsShutouts = g1^.gsShutouts + g2^.gsShutouts
|
||||
, _gsWins = g1^.gsWins + g2^.gsWins
|
||||
, _gsLosses = g1^.gsLosses + g2^.gsLosses
|
||||
, _gsTies = g1^.gsTies + g2^.gsTies
|
||||
}
|
||||
|
||||
-- | Determines a goalie's average goals allowed per game.
|
||||
gsAverage :: GoalieStats -> Rational
|
||||
gsAverage gs = let
|
||||
allowed = fromIntegral $ gs^.gsGoalsAllowed
|
||||
mins = fromIntegral $ gs^.gsMinsPlayed
|
||||
gLen = fromIntegral gameLength
|
||||
in if mins == 0
|
||||
then 0
|
||||
else allowed / mins * gLen
|
||||
|
||||
@@ -44,6 +44,7 @@ spec = describe "NewGame" $ do
|
||||
awardAssistSpec
|
||||
resetGoalDataSpec
|
||||
assignPMinsSpec
|
||||
awardShutoutsSpec
|
||||
GoalieInput.spec
|
||||
|
||||
overtimeCheckSpec :: Spec
|
||||
@@ -481,3 +482,45 @@ assignPMinsSpec = describe "assignPMins" $ let
|
||||
, ( Just 2, 4, 3, 2, 6, 5, 0 )
|
||||
, ( Nothing, 4, 3, 2, 6, 5, 0 )
|
||||
]
|
||||
|
||||
awardShutoutsSpec :: Spec
|
||||
awardShutoutsSpec = describe "awardShutouts" $ let
|
||||
joe = newGoalie 2 "Joe"
|
||||
& gYtd.gsShutouts .~ 1
|
||||
& gLifetime.gsShutouts .~ 2
|
||||
|
||||
bob = newGoalie 3 "Bob"
|
||||
& gYtd.gsShutouts .~ 3
|
||||
& gLifetime.gsShutouts .~ 4
|
||||
|
||||
steve = newGoalie 5 "Steve"
|
||||
& gYtd.gsShutouts .~ 5
|
||||
& gLifetime.gsShutouts .~ 6
|
||||
|
||||
ps = newProgState
|
||||
& database.dbGoalies .~ [joe, bob, steve]
|
||||
& progMode.gameStateL.gameGoalieStats .~ M.fromList
|
||||
[ ( 0, newGoalieStats & gsGoalsAllowed .~ 1 )
|
||||
, ( 1, newGoalieStats )
|
||||
]
|
||||
& awardShutouts
|
||||
|
||||
in mapM_
|
||||
(\(name, gid, expectedGame, expectedYtd, expectedLt) -> context name $ let
|
||||
game = M.findWithDefault newGoalieStats gid $
|
||||
ps^.progMode.gameStateL.gameGoalieStats
|
||||
goalie = (ps^.database.dbGoalies) !! gid
|
||||
in mapM_
|
||||
(\(label, actual, expected) -> context label $
|
||||
it ("should be " ++ show actual) $
|
||||
actual `shouldBe` expected)
|
||||
-- label, actual, expected
|
||||
[ ( "Game", game^.gsShutouts, expectedGame )
|
||||
, ( "YTD", goalie^.gYtd.gsShutouts, expectedYtd )
|
||||
, ( "lifetime", goalie^.gLifetime.gsShutouts, expectedLt )
|
||||
])
|
||||
-- goalie, goalie ID, Game, YTD, lifetime
|
||||
[ ( "Joe", 0, 0, 1, 2 )
|
||||
, ( "Bob", 1, 1, 4, 5 )
|
||||
, ( "Steve", 2, 0, 5, 6 )
|
||||
]
|
||||
|
||||
@@ -21,9 +21,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
module FormatSpec (spec) where
|
||||
|
||||
import Data.Ratio ((%))
|
||||
import Test.Hspec (Spec, context, describe, it, shouldBe)
|
||||
|
||||
import Mtlstats.Format
|
||||
import Mtlstats.Types
|
||||
|
||||
spec :: Spec
|
||||
spec = describe "Mtlstats.Format" $ do
|
||||
@@ -33,6 +35,12 @@ spec = describe "Mtlstats.Format" $ do
|
||||
centreSpec
|
||||
overlaySpec
|
||||
monthSpec
|
||||
labelTableSpec
|
||||
numTableSpec
|
||||
tableWithSpec
|
||||
complexTableSpec
|
||||
overlayLastSpec
|
||||
showFloatingSpec
|
||||
|
||||
padNumSpec :: Spec
|
||||
padNumSpec = describe "padNum" $ do
|
||||
@@ -111,3 +119,120 @@ monthSpec = describe "month" $ do
|
||||
context "invalid" $
|
||||
it "should return an empty string" $
|
||||
month 0 `shouldBe` ""
|
||||
|
||||
labelTableSpec :: Spec
|
||||
labelTableSpec = describe "labelTable" $
|
||||
it "should format the table" $ let
|
||||
input =
|
||||
[ ( "foo", "bar" )
|
||||
, ( "baz", "quux" )
|
||||
, ( "longer", "x" )
|
||||
]
|
||||
|
||||
expected =
|
||||
[ " foo: bar"
|
||||
, " baz: quux"
|
||||
, "longer: x"
|
||||
]
|
||||
|
||||
in labelTable input `shouldBe` expected
|
||||
|
||||
numTableSpec :: Spec
|
||||
numTableSpec = describe "numTable" $
|
||||
it "should format the table" $ let
|
||||
headers = ["foo", "bar", "baz"]
|
||||
|
||||
rows =
|
||||
[ ( "quux", [ 1, 2, 3 ] )
|
||||
, ( "xyzzy", [ 9, 99, 999 ] )
|
||||
]
|
||||
|
||||
expected =
|
||||
[ " foo bar baz"
|
||||
, " quux 1 2 3"
|
||||
, "xyzzy 9 99 999"
|
||||
]
|
||||
|
||||
in numTable headers rows `shouldBe` expected
|
||||
|
||||
tableWithSpec :: Spec
|
||||
tableWithSpec = describe "tableWith" $ let
|
||||
vals =
|
||||
[ [ "foo", "bar", "baz" ]
|
||||
, [ "quux", "xyzzy", "x" ]
|
||||
]
|
||||
|
||||
in mapM_
|
||||
(\(label, func, expected) -> context label $
|
||||
it "should format the table" $
|
||||
tableWith func vals `shouldBe` expected)
|
||||
[ ( "align left"
|
||||
, left
|
||||
, [ "foo bar baz"
|
||||
, "quux xyzzy x "
|
||||
]
|
||||
)
|
||||
, ( "align right"
|
||||
, right
|
||||
, [ " foo bar baz"
|
||||
, "quux xyzzy x"
|
||||
]
|
||||
)
|
||||
]
|
||||
|
||||
complexTableSpec :: Spec
|
||||
complexTableSpec = describe "complexTable" $ mapM_
|
||||
(\(label, pFuncs, cells, expected) -> context label $
|
||||
it "should format correctly" $
|
||||
complexTable pFuncs cells `shouldBe` expected)
|
||||
[ ( "no fill"
|
||||
, [left, right]
|
||||
, [ [ CellText "foo", CellText "bar" ]
|
||||
, [ CellText "baaz", CellText "quux" ]
|
||||
]
|
||||
, [ "foo bar"
|
||||
, "baaz quux"
|
||||
]
|
||||
)
|
||||
, ( "with fill"
|
||||
, [left, left, left]
|
||||
, [ [ CellText "foo", CellText "bar", CellText "baz" ]
|
||||
, [ CellText "quux", CellFill '-', CellFill '@' ]
|
||||
]
|
||||
, [ "foo bar baz"
|
||||
, "quux ----@@@"
|
||||
]
|
||||
)
|
||||
]
|
||||
|
||||
overlayLastSpec :: Spec
|
||||
overlayLastSpec = describe "overlayLast" $ let
|
||||
text = "foo"
|
||||
|
||||
sample =
|
||||
[ "line 1"
|
||||
, "line 2"
|
||||
]
|
||||
|
||||
edited =
|
||||
[ "line 1"
|
||||
, "fooe 2"
|
||||
]
|
||||
|
||||
in mapM_
|
||||
(\(label, input, expected) -> context label $
|
||||
it ("should be " ++ show expected) $
|
||||
overlayLast text input `shouldBe` expected)
|
||||
|
||||
-- label, input, expected
|
||||
[ ( "empty list", [], [] )
|
||||
, ( "non-empty list", sample, edited )
|
||||
]
|
||||
|
||||
showFloatingSpec :: Spec
|
||||
showFloatingSpec = describe "showFloating" $ let
|
||||
input = 3 % 2 :: Rational
|
||||
expected = "1.50"
|
||||
|
||||
in it ("should be " ++ expected) $
|
||||
showFloating input `shouldBe` expected
|
||||
|
||||
66
test/Helpers/GoalieSpec.hs
Normal file
66
test/Helpers/GoalieSpec.hs
Normal file
@@ -0,0 +1,66 @@
|
||||
{-
|
||||
|
||||
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/>.
|
||||
|
||||
-}
|
||||
|
||||
module Helpers.GoalieSpec (spec) where
|
||||
|
||||
import Lens.Micro ((&), (.~), (%~))
|
||||
import Test.Hspec (Spec, describe, it, shouldBe)
|
||||
|
||||
import Mtlstats.Helpers.Goalie
|
||||
import Mtlstats.Types
|
||||
|
||||
spec :: Spec
|
||||
spec = describe "Goalie"
|
||||
goalieDetailsSpec
|
||||
|
||||
goalieDetailsSpec :: Spec
|
||||
goalieDetailsSpec = describe "goalieDetails" $ let
|
||||
input = newGoalie 1 "Joe"
|
||||
& gYtd
|
||||
%~ ( gsGames .~ 2 )
|
||||
. ( gsMinsPlayed .~ 3 )
|
||||
. ( gsGoalsAllowed .~ 4 )
|
||||
. ( gsWins .~ 5 )
|
||||
. ( gsLosses .~ 6 )
|
||||
. ( gsTies .~ 7 )
|
||||
& gLifetime
|
||||
%~ ( gsGames .~ 8 )
|
||||
. ( gsMinsPlayed .~ 9 )
|
||||
. ( gsGoalsAllowed .~ 10 )
|
||||
. ( gsWins .~ 11 )
|
||||
. ( gsLosses .~ 12 )
|
||||
. ( gsTies .~ 13 )
|
||||
|
||||
expected = unlines
|
||||
[ "Number: 1"
|
||||
, " Name: Joe"
|
||||
, ""
|
||||
, " YTD Lifetime"
|
||||
, " Games played 2 8"
|
||||
, " Mins played 3 9"
|
||||
, "Goals allowed 4 10"
|
||||
, " Wins 5 11"
|
||||
, " Losses 6 12"
|
||||
, " Ties 7 13"
|
||||
]
|
||||
|
||||
in it "should format the output correctly" $
|
||||
goalieDetails input `shouldBe` expected
|
||||
61
test/Helpers/PlayerSpec.hs
Normal file
61
test/Helpers/PlayerSpec.hs
Normal file
@@ -0,0 +1,61 @@
|
||||
{-
|
||||
|
||||
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/>.
|
||||
|
||||
-}
|
||||
|
||||
module Helpers.PlayerSpec (spec) where
|
||||
|
||||
import Lens.Micro ((&), (.~))
|
||||
import Test.Hspec (Spec, describe, it, shouldBe)
|
||||
|
||||
import Mtlstats.Helpers.Player
|
||||
import Mtlstats.Types
|
||||
|
||||
spec :: Spec
|
||||
spec = describe "Player"
|
||||
playerDetailsSpec
|
||||
|
||||
playerDetailsSpec :: Spec
|
||||
playerDetailsSpec = describe "playerDetails" $
|
||||
it "should give a detailed description" $ let
|
||||
|
||||
p = newPlayer 1 "Joe" "centre"
|
||||
& pYtd .~ PlayerStats
|
||||
{ _psGoals = 2
|
||||
, _psAssists = 3
|
||||
, _psPMin = 4
|
||||
}
|
||||
& pLifetime .~ PlayerStats
|
||||
{ _psGoals = 5
|
||||
, _psAssists = 6
|
||||
, _psPMin = 7
|
||||
}
|
||||
|
||||
expected = unlines
|
||||
[ " Number: 1"
|
||||
, " Name: Joe"
|
||||
, "Position: centre"
|
||||
, ""
|
||||
, " YTD Lifetime"
|
||||
, " Goals 2 5"
|
||||
, " Assists 3 6"
|
||||
, "Penalty mins 4 7"
|
||||
]
|
||||
|
||||
in playerDetails p `shouldBe` expected
|
||||
32
test/HelpersSpec.hs
Normal file
32
test/HelpersSpec.hs
Normal file
@@ -0,0 +1,32 @@
|
||||
{-
|
||||
|
||||
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/>.
|
||||
|
||||
-}
|
||||
|
||||
module HelpersSpec (spec) where
|
||||
|
||||
import Test.Hspec (Spec, describe)
|
||||
|
||||
import qualified Helpers.GoalieSpec as Goalie
|
||||
import qualified Helpers.PlayerSpec as Player
|
||||
|
||||
spec :: Spec
|
||||
spec = describe "Helper" $ do
|
||||
Player.spec
|
||||
Goalie.spec
|
||||
@@ -28,9 +28,8 @@ import Mtlstats.Report
|
||||
import Mtlstats.Types
|
||||
|
||||
spec :: Spec
|
||||
spec = describe "Mtlstats.Report" $ do
|
||||
spec = describe "Mtlstats.Report"
|
||||
gameDateSpec
|
||||
playerNameColWidthSpec
|
||||
|
||||
gameDateSpec :: Spec
|
||||
gameDateSpec = describe "gameDate" $ do
|
||||
@@ -46,20 +45,3 @@ gameDateSpec = describe "gameDate" $ do
|
||||
context "invalid date" $
|
||||
it "should return an empty string" $
|
||||
gameDate newGameState `shouldBe` ""
|
||||
|
||||
playerNameColWidthSpec :: Spec
|
||||
playerNameColWidthSpec = describe "playerNameColWidth" $ do
|
||||
let
|
||||
short1 = newPlayer 1 "short" "foo"
|
||||
short2 = newPlayer 2 "shorty" "bar"
|
||||
long = newPlayer 3 "123456789012345" "baz"
|
||||
|
||||
mapM_
|
||||
(\(label, players, expected) -> context label $
|
||||
it ("should be " ++ show expected) $
|
||||
playerNameColWidth players `shouldBe` expected)
|
||||
-- label, players, expected
|
||||
[ ( "empty list", [], 10 )
|
||||
, ( "short names", [short1, short2], 10 )
|
||||
, ( "long name", [short1, long], 16 )
|
||||
]
|
||||
|
||||
@@ -24,6 +24,7 @@ import Test.Hspec (hspec)
|
||||
import qualified ActionsSpec as Actions
|
||||
import qualified FormatSpec as Format
|
||||
import qualified HandlersSpec as Handlers
|
||||
import qualified HelpersSpec as Helpers
|
||||
import qualified ReportSpec as Report
|
||||
import qualified TypesSpec as Types
|
||||
import qualified UtilSpec as Util
|
||||
@@ -31,6 +32,7 @@ import qualified UtilSpec as Util
|
||||
main :: IO ()
|
||||
main = hspec $ do
|
||||
Types.spec
|
||||
Helpers.spec
|
||||
Actions.spec
|
||||
Format.spec
|
||||
Handlers.spec
|
||||
|
||||
@@ -34,6 +34,7 @@ import Control.Monad (replicateM)
|
||||
import Data.Aeson (FromJSON, ToJSON, decode, encode, toJSON)
|
||||
import Data.Aeson.Types (Value (Object))
|
||||
import qualified Data.HashMap.Strict as HM
|
||||
import Data.Ratio ((%))
|
||||
import Lens.Micro (Lens', (&), (^.), (.~), (?~))
|
||||
import System.Random (randomRIO)
|
||||
import Test.Hspec (Spec, context, describe, it, shouldBe)
|
||||
@@ -72,13 +73,15 @@ spec = describe "Mtlstats.Types" $ do
|
||||
playerSearchExactSpec
|
||||
modifyPlayerSpec
|
||||
playerSummarySpec
|
||||
playerDetailsSpec
|
||||
playerIsActiveSpec
|
||||
psPointsSpec
|
||||
addPlayerStatsSpec
|
||||
goalieSearchSpec
|
||||
goalieSearchExactSpec
|
||||
goalieSummarySpec
|
||||
goalieIsActiveSpec
|
||||
addGoalieStatsSpec
|
||||
gsAverageSpec
|
||||
Menu.spec
|
||||
|
||||
playerSpec :: Spec
|
||||
@@ -311,18 +314,20 @@ goalieStats n = newGoalieStats
|
||||
& gsGames .~ n
|
||||
& gsMinsPlayed .~ n + 1
|
||||
& gsGoalsAllowed .~ n + 2
|
||||
& gsWins .~ n + 3
|
||||
& gsLosses .~ n + 4
|
||||
& gsTies .~ n + 5
|
||||
& gsShutouts .~ n + 3
|
||||
& gsWins .~ n + 4
|
||||
& gsLosses .~ n + 5
|
||||
& gsTies .~ n + 6
|
||||
|
||||
goalieStatsJSON :: Int -> Value
|
||||
goalieStatsJSON n = Object $ HM.fromList
|
||||
[ ( "games", toJSON n )
|
||||
, ( "mins_played", toJSON $ n + 1 )
|
||||
, ( "goals_allowed", toJSON $ n + 2 )
|
||||
, ( "wins", toJSON $ n + 3 )
|
||||
, ( "losses", toJSON $ n + 4 )
|
||||
, ( "ties", toJSON $ n + 5 )
|
||||
, ( "shutouts", toJSON $ n + 3 )
|
||||
, ( "wins", toJSON $ n + 4 )
|
||||
, ( "losses", toJSON $ n + 5 )
|
||||
, ( "ties", toJSON $ n + 6 )
|
||||
]
|
||||
|
||||
gameStats :: Int -> GameStats
|
||||
@@ -586,7 +591,7 @@ playerSearchSpec = describe "playerSearch" $ mapM_
|
||||
ps = [joe, bob, steve]
|
||||
in playerSearch sStr ps `shouldBe` expected)
|
||||
-- search, result
|
||||
[ ( "Joe", [(0, joe)] )
|
||||
[ ( "joe", [(0, joe)] )
|
||||
, ( "o", [(0, joe), (1, bob)] )
|
||||
, ( "e", [(0, joe), (2, steve)] )
|
||||
, ( "x", [] )
|
||||
@@ -636,36 +641,6 @@ playerSummarySpec = describe "playerSummary" $
|
||||
it "should be \"Joe (2) center\"" $
|
||||
playerSummary joe `shouldBe` "Joe (2) center"
|
||||
|
||||
playerDetailsSpec :: Spec
|
||||
playerDetailsSpec = describe "playerDetails" $
|
||||
it "should give a detailed description" $ let
|
||||
|
||||
p = newPlayer 1 "Joe" "centre"
|
||||
& pYtd .~ PlayerStats
|
||||
{ _psGoals = 2
|
||||
, _psAssists = 3
|
||||
, _psPMin = 4
|
||||
}
|
||||
& pLifetime .~ PlayerStats
|
||||
{ _psGoals = 5
|
||||
, _psAssists = 6
|
||||
, _psPMin = 7
|
||||
}
|
||||
|
||||
expected = unlines
|
||||
[ " Number: 1"
|
||||
, " Name: Joe"
|
||||
, " Position: centre"
|
||||
, " YTD goals: 2"
|
||||
, " YTD assists: 3"
|
||||
, " YTD penalty mins: 4"
|
||||
, " Lifetime goals: 5"
|
||||
, " Lifetime assists: 6"
|
||||
, "Lifetime penalty mins: 7"
|
||||
]
|
||||
|
||||
in playerDetails p `shouldBe` expected
|
||||
|
||||
playerIsActiveSpec :: Spec
|
||||
playerIsActiveSpec = describe "playerIsActive" $ do
|
||||
let
|
||||
@@ -750,8 +725,8 @@ goalieSearchSpec = describe "goalieSearch" $ do
|
||||
goalieSearch "x" goalies `shouldBe` []
|
||||
|
||||
context "exact match" $
|
||||
it "should return Steve" $
|
||||
goalieSearch "Bob" goalies `shouldBe` [result 1]
|
||||
it "should return Bob" $
|
||||
goalieSearch "bob" goalies `shouldBe` [result 1]
|
||||
|
||||
goalieSearchExactSpec :: Spec
|
||||
goalieSearchExactSpec = describe "goalieSearchExact" $ do
|
||||
@@ -782,6 +757,77 @@ goalieSummarySpec = describe "goalieSummary" $
|
||||
it "should provide a summary string" $
|
||||
goalieSummary (newGoalie 2 "Joe") `shouldBe` "Joe (2)"
|
||||
|
||||
goalieIsActiveSpec :: Spec
|
||||
goalieIsActiveSpec = describe "goalieIsActive" $ mapM_
|
||||
(\(label, input, expected) -> context label $
|
||||
it ("should be " ++ show expected) $
|
||||
goalieIsActive input `shouldBe` expected)
|
||||
|
||||
-- label, input, expected
|
||||
[ ( "inactive", inactive, False )
|
||||
, ( "active", active, True )
|
||||
]
|
||||
|
||||
where
|
||||
inactive = newGoalie 1 "Joe"
|
||||
& gLifetime.gsMinsPlayed .~ 1
|
||||
|
||||
active = inactive
|
||||
& gYtd.gsMinsPlayed .~ 1
|
||||
|
||||
addGoalieStatsSpec :: Spec
|
||||
addGoalieStatsSpec = describe "addGoalieStats" $ let
|
||||
g1 = GoalieStats
|
||||
{ _gsGames = 1
|
||||
, _gsMinsPlayed = 2
|
||||
, _gsGoalsAllowed = 3
|
||||
, _gsShutouts = 4
|
||||
, _gsWins = 5
|
||||
, _gsLosses = 6
|
||||
, _gsTies = 7
|
||||
}
|
||||
|
||||
g2 = GoalieStats
|
||||
{ _gsGames = 8
|
||||
, _gsMinsPlayed = 9
|
||||
, _gsGoalsAllowed = 10
|
||||
, _gsShutouts = 11
|
||||
, _gsWins = 12
|
||||
, _gsLosses = 13
|
||||
, _gsTies = 14
|
||||
}
|
||||
|
||||
expected = GoalieStats
|
||||
{ _gsGames = 9
|
||||
, _gsMinsPlayed = 11
|
||||
, _gsGoalsAllowed = 13
|
||||
, _gsShutouts = 15
|
||||
, _gsWins = 17
|
||||
, _gsLosses = 19
|
||||
, _gsTies = 21
|
||||
}
|
||||
|
||||
actual = g1 `addGoalieStats` g2
|
||||
|
||||
in it ("should be " ++ show expected) $
|
||||
actual `shouldBe` expected
|
||||
|
||||
gsAverageSpec :: Spec
|
||||
gsAverageSpec = describe "gsAverage" $ mapM_
|
||||
(\(label, stats, expected) -> context label $
|
||||
it ("should be " ++ show expected) $
|
||||
gsAverage stats `shouldBe` expected)
|
||||
|
||||
-- label, stats, expected
|
||||
[ ( "with minutes", gs, 3 % 2 )
|
||||
, ( "no minutes", newGoalieStats , 0 )
|
||||
]
|
||||
|
||||
where
|
||||
gs = newGoalieStats
|
||||
& gsMinsPlayed .~ 2 * gameLength
|
||||
& gsGoalsAllowed .~ 3
|
||||
|
||||
joe :: Player
|
||||
joe = newPlayer 2 "Joe" "center"
|
||||
|
||||
@@ -824,7 +870,7 @@ makeGoalieStats = GoalieStats
|
||||
<*> makeNum
|
||||
<*> makeNum
|
||||
<*> makeNum
|
||||
|
||||
<*> makeNum
|
||||
|
||||
makeNum :: IO Int
|
||||
makeNum = randomRIO (1, 10)
|
||||
|
||||
Reference in New Issue
Block a user