Merge pull request #24 from mtlstats/gs-totals
Calculate and display total player stats for game
This commit is contained in:
commit
146e2e42a1
|
@ -37,26 +37,26 @@ report
|
|||
-> ProgState
|
||||
-- ^ The program state
|
||||
-> String
|
||||
report width s = unlines $ fromMaybe [] $ do
|
||||
report width s
|
||||
= standingsReport width s
|
||||
++ "\n"
|
||||
++ gameStatsReport width s
|
||||
|
||||
standingsReport :: Int -> ProgState -> String
|
||||
standingsReport width s = unlines $ fromMaybe [] $ do
|
||||
let
|
||||
db = s^.database
|
||||
gs = s^.progMode.gameStateL
|
||||
gNum = db^.dbGames
|
||||
date = gameDate gs
|
||||
hTeam = homeTeam gs
|
||||
aTeam = awayTeam gs
|
||||
hStats = db^.dbHomeGameStats
|
||||
aStats = db^.dbAwayGameStats
|
||||
tStats = addGameStats hStats aStats
|
||||
players = db^.dbPlayers
|
||||
db = s^.database
|
||||
gs = s^.progMode.gameStateL
|
||||
gNum = db^.dbGames
|
||||
date = gameDate gs
|
||||
hTeam = homeTeam gs
|
||||
aTeam = awayTeam gs
|
||||
hStats = db^.dbHomeGameStats
|
||||
aStats = db^.dbAwayGameStats
|
||||
tStats = addGameStats hStats aStats
|
||||
hScore <- gs^.homeScore
|
||||
aScore <- gs^.awayScore
|
||||
pStats <- mapM
|
||||
(\(n, stats) -> do
|
||||
player <- nth n players
|
||||
Just (player, stats))
|
||||
(M.toList $ gs^.gamePlayerStats)
|
||||
Just $
|
||||
Just
|
||||
[ overlay
|
||||
("GAME NUMBER " ++ padNum 2 gNum)
|
||||
(centre width
|
||||
|
@ -86,12 +86,26 @@ report width s = unlines $ fromMaybe [] $ do
|
|||
, centre width
|
||||
$ left 11 "TOTALS"
|
||||
++ showStats tStats
|
||||
, ""
|
||||
, centre width "GAME STATISTICS"
|
||||
]
|
||||
|
||||
gameStatsReport :: Int -> ProgState -> String
|
||||
gameStatsReport width s = unlines $ fromMaybe [] $ do
|
||||
pStats <- mapM
|
||||
(\(pid, stats) -> do
|
||||
p <- nth pid $ s^.database.dbPlayers
|
||||
Just (p, stats))
|
||||
(M.toList $ s^.progMode.gameStateL.gamePlayerStats)
|
||||
let
|
||||
nameWidth = succ $ maximum $ 10 : map
|
||||
(length . (^.pName) . fst)
|
||||
pStats
|
||||
tStats = foldr (addPlayerStats . snd) newPlayerStats pStats
|
||||
Just $
|
||||
[ centre width "GAME STATISTICS"
|
||||
, ""
|
||||
, centre width
|
||||
$ "NO. "
|
||||
++ left 20 "PLAYER"
|
||||
++ left nameWidth "PLAYER"
|
||||
++ right 3 "G"
|
||||
++ right 6 "A"
|
||||
++ right 6 "P"
|
||||
|
@ -100,12 +114,25 @@ report width s = unlines $ fromMaybe [] $ do
|
|||
(\(p, stats) -> centre width
|
||||
$ right 2 (show $ p^.pNumber)
|
||||
++ " "
|
||||
++ left 20 (p^.pName)
|
||||
++ left nameWidth (p^.pName)
|
||||
++ right 3 (show $ stats^.psGoals)
|
||||
++ right 6 (show $ stats^.psAssists)
|
||||
++ right 6 (show $ pPoints stats)
|
||||
++ right 6 (show $ psPoints stats)
|
||||
++ right 6 (show $ stats^.psPMin))
|
||||
pStats
|
||||
pStats ++
|
||||
[ centre width
|
||||
$ replicate (4 + nameWidth) ' '
|
||||
++ replicate (3 + 3 * 6) '-'
|
||||
, overlay
|
||||
"GAME 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)
|
||||
)
|
||||
]
|
||||
|
||||
gameDate :: GameState -> String
|
||||
gameDate gs = fromMaybe "" $ do
|
||||
|
|
|
@ -126,11 +126,13 @@ module Mtlstats.Types (
|
|||
gmsPoints,
|
||||
addGameStats,
|
||||
-- ** Player Helpers
|
||||
pPoints,
|
||||
playerSearch,
|
||||
playerSearchExact,
|
||||
modifyPlayer,
|
||||
playerSummary
|
||||
playerSummary,
|
||||
-- ** PlayerStats Helpers
|
||||
psPoints,
|
||||
addPlayerStats
|
||||
) where
|
||||
|
||||
import Control.Monad.Trans.State (StateT)
|
||||
|
@ -675,10 +677,6 @@ addGameStats s1 s2 = GameStats
|
|||
, _gmsGoalsAgainst = s1^.gmsGoalsAgainst + s2^.gmsGoalsAgainst
|
||||
}
|
||||
|
||||
-- | Calculates a player's points
|
||||
pPoints :: PlayerStats -> Int
|
||||
pPoints s = s^.psGoals + s^.psAssists
|
||||
|
||||
-- | Searches through a list of players
|
||||
playerSearch
|
||||
:: String
|
||||
|
@ -725,3 +723,14 @@ modifyPlayer f n = map
|
|||
playerSummary :: Player -> String
|
||||
playerSummary p =
|
||||
p^.pName ++ " (" ++ show (p^.pNumber) ++ ") " ++ p^.pPosition
|
||||
|
||||
-- | Calculates a player's points
|
||||
psPoints :: PlayerStats -> Int
|
||||
psPoints s = s^.psGoals + s^.psAssists
|
||||
|
||||
-- | 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
|
||||
|
|
|
@ -54,11 +54,12 @@ spec = describe "Mtlstats.Types" $ do
|
|||
gmsGamesSpec
|
||||
gmsPointsSpec
|
||||
addGameStatsSpec
|
||||
pPointsSpec
|
||||
playerSearchSpec
|
||||
playerSearchExactSpec
|
||||
modifyPlayerSpec
|
||||
playerSummarySpec
|
||||
psPointsSpec
|
||||
addPlayerStatsSpec
|
||||
Menu.spec
|
||||
|
||||
playerSpec :: Spec
|
||||
|
@ -509,24 +510,6 @@ addGameStatsSpec = describe "addGameStats" $
|
|||
|
||||
in addGameStats s1 s2 `shouldBe` expected
|
||||
|
||||
pPointsSpec :: Spec
|
||||
pPointsSpec = describe "pPoints" $ mapM_
|
||||
(\(goals, assists, points) -> let
|
||||
desc = "goals: " ++ show goals ++
|
||||
", assists: " ++ show assists
|
||||
stats = newPlayerStats &
|
||||
psGoals .~ goals &
|
||||
psAssists .~ assists
|
||||
in context desc $
|
||||
it ("should be " ++ show points) $
|
||||
pPoints stats `shouldBe` points)
|
||||
-- goals, assists, points
|
||||
[ ( 0, 0, 0 )
|
||||
, ( 1, 0, 1 )
|
||||
, ( 0, 1, 1 )
|
||||
, ( 2, 3, 5 )
|
||||
]
|
||||
|
||||
playerSearchSpec :: Spec
|
||||
playerSearchSpec = describe "playerSearch" $ mapM_
|
||||
(\(sStr, expected) -> context sStr $
|
||||
|
@ -584,6 +567,51 @@ playerSummarySpec = describe "playerSummary" $
|
|||
it "should be \"Joe (2) center\"" $
|
||||
playerSummary joe `shouldBe` "Joe (2) center"
|
||||
|
||||
psPointsSpec :: Spec
|
||||
psPointsSpec = describe "psPoints" $ mapM_
|
||||
(\(goals, assists, points) -> let
|
||||
desc = "goals: " ++ show goals ++
|
||||
", assists: " ++ show assists
|
||||
stats = newPlayerStats &
|
||||
psGoals .~ goals &
|
||||
psAssists .~ assists
|
||||
in context desc $
|
||||
it ("should be " ++ show points) $
|
||||
psPoints stats `shouldBe` points)
|
||||
-- goals, assists, points
|
||||
[ ( 0, 0, 0 )
|
||||
, ( 1, 0, 1 )
|
||||
, ( 0, 1, 1 )
|
||||
, ( 2, 3, 5 )
|
||||
]
|
||||
|
||||
addPlayerStatsSpec :: Spec
|
||||
addPlayerStatsSpec = describe "addPlayerStats" $ do
|
||||
let
|
||||
s1
|
||||
= newPlayerStats
|
||||
& psGoals .~ 1
|
||||
& psAssists .~ 2
|
||||
& psPMin .~ 3
|
||||
s2
|
||||
= newPlayerStats
|
||||
& psGoals .~ 4
|
||||
& psAssists .~ 5
|
||||
& psPMin .~ 6
|
||||
s3 = addPlayerStats s1 s2
|
||||
|
||||
describe "psGoals" $
|
||||
it "should be 5" $
|
||||
s3^.psGoals `shouldBe` 5
|
||||
|
||||
describe "psAssists" $
|
||||
it "should be 7" $
|
||||
s3^.psAssists `shouldBe` 7
|
||||
|
||||
describe "psPMin" $
|
||||
it "should be 9" $
|
||||
s3^.psPMin `shouldBe` 9
|
||||
|
||||
joe :: Player
|
||||
joe = newPlayer 2 "Joe" "center"
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user