implemented homeTeam and awayTeam

This commit is contained in:
Jonathan Lamothe 2019-09-02 18:50:21 -04:00
parent f0e0d644d2
commit 6136151614
3 changed files with 87 additions and 0 deletions

26
src/Mtlstats/Config.hs Normal file
View File

@ -0,0 +1,26 @@
{- |
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.Config (myTeam) where
-- | The name of the team whose stats we're tracking
myTeam :: String
myTeam = "MONTREAL"

View File

@ -97,6 +97,8 @@ module Mtlstats.Types (
-- ** GameState Helpers -- ** GameState Helpers
teamScore, teamScore,
otherScore, otherScore,
homeTeam,
awayTeam,
gameWon, gameWon,
gameLost, gameLost,
gameTied, gameTied,
@ -124,6 +126,8 @@ import Lens.Micro (Lens', lens, (&), (^.), (.~))
import Lens.Micro.TH (makeLenses) import Lens.Micro.TH (makeLenses)
import UI.NCurses (Curses, Update) import UI.NCurses (Curses, Update)
import Mtlstats.Config
-- | Action which maintains program state -- | Action which maintains program state
type Action a = StateT ProgState Curses a type Action a = StateT ProgState Curses a
@ -509,6 +513,22 @@ otherScore s = case s ^. gameType of
Just AwayGame -> s ^. homeScore Just AwayGame -> s ^. homeScore
Nothing -> Nothing Nothing -> Nothing
-- | 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 -> ""
-- | Checks if the game was won -- | Checks if the game was won
gameWon :: GameState -> Maybe Bool gameWon :: GameState -> Maybe Bool
gameWon gs = (>) <$> teamScore gs <*> otherScore gs gameWon gs = (>) <$> teamScore gs <*> otherScore gs

View File

@ -30,6 +30,7 @@ import qualified Data.HashMap.Strict as HM
import Lens.Micro (Lens', (&), (^.), (.~), (?~)) import Lens.Micro (Lens', (&), (^.), (.~), (?~))
import Test.Hspec (Spec, context, describe, it, shouldBe) import Test.Hspec (Spec, context, describe, it, shouldBe)
import Mtlstats.Config
import Mtlstats.Types import Mtlstats.Types
import qualified Types.MenuSpec as Menu import qualified Types.MenuSpec as Menu
@ -43,6 +44,8 @@ spec = describe "Mtlstats.Types" $ do
gameStateLSpec gameStateLSpec
teamScoreSpec teamScoreSpec
otherScoreSpec otherScoreSpec
homeTeamSpec
awayTeamSpec
gameWonSpec gameWonSpec
gameLostSpec gameLostSpec
gameTiedSpec gameTiedSpec
@ -248,6 +251,44 @@ dbJSON = Object $ HM.fromList
, ( "away_game_stats", gameStatsJSON 2 ) , ( "away_game_stats", gameStatsJSON 2 )
] ]
homeTeamSpec :: Spec
homeTeamSpec = describe "homeTeam" $ do
let
gs gt = newGameState
& gameType .~ gt
& otherTeam .~ "foo"
context "unknown game type" $
it "should return an empty string" $
homeTeam (gs Nothing) `shouldBe` ""
context "home game" $
it ("should return " ++ show myTeam) $
homeTeam (gs $ Just HomeGame) `shouldBe` myTeam
context "away game" $
it "should return \"foo\"" $
homeTeam (gs $ Just AwayGame) `shouldBe` "foo"
awayTeamSpec :: Spec
awayTeamSpec = describe "awayTeam" $ do
let
gs gt = newGameState
& gameType .~ gt
& otherTeam .~ "foo"
context "unknown game type" $
it "should return an empty string" $
awayTeam (gs Nothing) `shouldBe` ""
context "home game" $
it "should return \"foo\"" $
awayTeam (gs $ Just HomeGame) `shouldBe` "foo"
context "away game" $
it ("should return " ++ show myTeam) $
awayTeam (gs $ Just AwayGame) `shouldBe` myTeam
gameWonSpec :: Spec gameWonSpec :: Spec
gameWonSpec = describe "gameWon" $ mapM_ gameWonSpec = describe "gameWon" $ mapM_
(\(t, h, a, expected) -> let (\(t, h, a, expected) -> let