From f2cfe3b37b1140829322c06529736e81b36c21b7 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sun, 25 Aug 2019 09:25:34 -0400 Subject: [PATCH 1/4] missing type signature --- test/TypesSpec.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index a97554a..5415634 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -130,6 +130,7 @@ gameTypeLSpec = describe "gameTypeL" $ do in m ^. gameTypeL `shouldBe` Just t) [HomeGame, AwayGame] +homeScoreLSpec :: Spec homeScoreLSpec = describe "homeScoreL" $ do context "getter" $ do From 2a2189c7d1f538f346e677647be552120ad4d426 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 24 Aug 2019 21:15:50 -0400 Subject: [PATCH 2/4] implenented strPrompt --- src/Mtlstats/Prompt.hs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Mtlstats/Prompt.hs b/src/Mtlstats/Prompt.hs index 5c69ec4..cd66019 100644 --- a/src/Mtlstats/Prompt.hs +++ b/src/Mtlstats/Prompt.hs @@ -23,6 +23,7 @@ module Mtlstats.Prompt ( -- * Prompt Functions drawPrompt, promptHandler, + strPrompt, numPrompt, -- * Individual prompts homeScorePrompt, @@ -63,6 +64,20 @@ promptHandler p (C.EventSpecialKey (C.KeyFunction k)) = promptFunctionKey p k promptHandler _ _ = return () +-- | Builds a string prompt +strPrompt + :: String + -- ^ The prompt string + -> (String -> Action ()) + -- ^ The callback function for the result + -> Prompt +strPrompt pStr act = Prompt + { promptDrawer = drawSimplePrompt pStr + , promptCharCheck = const True + , promptAction = act + , promptFunctionKey = const $ return () + } + -- | Builds a numeric prompt numPrompt :: String @@ -71,7 +86,7 @@ numPrompt -- ^ The callback function for the result -> Prompt numPrompt pStr act = Prompt - { promptDrawer = \s -> C.drawString $ pStr ++ s ^. inputBuffer + { promptDrawer = drawSimplePrompt pStr , promptCharCheck = isDigit , promptAction = \inStr -> forM_ (readMaybe inStr) act , promptFunctionKey = const $ return () @@ -84,3 +99,6 @@ homeScorePrompt = numPrompt "Home score: " $ awayScorePrompt :: Prompt awayScorePrompt = numPrompt "Away score: " $ modify . (progMode . awayScoreL ?~) + +drawSimplePrompt :: String -> ProgState -> C.Update () +drawSimplePrompt pStr s = C.drawString $ pStr ++ s ^. inputBuffer From 0f9f76ce4eabfc4e57de65a13441e3356ef739ae Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sun, 25 Aug 2019 09:23:59 -0400 Subject: [PATCH 3/4] added _otherTeam with its associated lenses --- src/Mtlstats/Types.hs | 14 ++++++++++++++ test/TypesSpec.hs | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 2184303..547e7be 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -41,10 +41,12 @@ module Mtlstats.Types ( inputBuffer, -- ** GameState Lenses gameType, + otherTeam, homeScore, awayScore, -- ** ProgMode Lenses gameTypeL, + otherTeamL, homeScoreL, awayScoreL, -- ** Database Lenses @@ -123,6 +125,8 @@ data ProgState = ProgState data GameState = GameState { _gameType :: Maybe GameType -- ^ The type of game (home/away) + , _otherTeam :: String + -- ^ The name of the other team , _homeScore :: Maybe Int -- ^ The home team's score , _awayScore :: Maybe Int @@ -341,6 +345,15 @@ gameTypeL = lens NewGame gs -> NewGame $ gs & gameType .~ gt _ -> NewGame $ newGameState & gameType .~ gt) +otherTeamL :: Lens' ProgMode String +otherTeamL = lens + (\case + NewGame gs -> gs ^. otherTeam + _ -> "") + (\m ot -> case m of + NewGame gs -> NewGame $ gs & otherTeam .~ ot + _ -> NewGame $ newGameState & otherTeam .~ ot) + homeScoreL :: Lens' ProgMode (Maybe Int) homeScoreL = lens (\case @@ -371,6 +384,7 @@ newProgState = ProgState newGameState :: GameState newGameState = GameState { _gameType = Nothing + , _otherTeam = "" , _homeScore = Nothing , _awayScore = Nothing } diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index 5415634..3b4f003 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -39,6 +39,7 @@ spec = describe "Mtlstats.Types" $ do goalieSpec databaseSpec gameTypeLSpec + otherTeamLSpec homeScoreLSpec awayScoreLSpec teamScoreSpec @@ -130,6 +131,32 @@ gameTypeLSpec = describe "gameTypeL" $ do in m ^. gameTypeL `shouldBe` Just t) [HomeGame, AwayGame] +otherTeamLSpec :: Spec +otherTeamLSpec = describe "otherTeamL" $ do + + context "getter" $ do + + context "unexpected mode" $ + it "should return an empty string" $ + MainMenu ^. otherTeamL `shouldBe` "" + + context "expected mode" $ + it "should return \"foo\"" $ let + m = NewGame $ newGameState & otherTeam .~ "foo" + in m ^. otherTeamL `shouldBe` "foo" + + context "setter" $ do + + context "unexpected mode" $ + it "should set the value" $ let + m = MainMenu & otherTeamL .~ "foo" + in m ^. otherTeamL `shouldBe` "foo" + + context "expected mode" $ + it "should set the value" $ let + m = NewGame newGameState & otherTeamL .~ "foo" + in m ^. otherTeamL `shouldBe` "foo" + homeScoreLSpec :: Spec homeScoreLSpec = describe "homeScoreL" $ do From 9ba91f1f0171ef23ebc0f52b0210cf2e0c17ea86 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sun, 25 Aug 2019 10:04:37 -0400 Subject: [PATCH 4/4] prompt for other team --- src/Mtlstats/Events.hs | 3 +++ src/Mtlstats/Prompt.hs | 5 +++++ src/Mtlstats/UI.hs | 1 + 3 files changed, 9 insertions(+) diff --git a/src/Mtlstats/Events.hs b/src/Mtlstats/Events.hs index 52ad1cc..feeff2a 100644 --- a/src/Mtlstats/Events.hs +++ b/src/Mtlstats/Events.hs @@ -45,6 +45,9 @@ handleEvent e = gets (view progMode) >>= \case | null $ gs ^. gameType -> do menuHandler gameTypeMenu e return True + | null $ gs ^. otherTeam -> do + promptHandler otherTeamPrompt e + return True | null $ gs ^. homeScore -> do promptHandler homeScorePrompt e return True diff --git a/src/Mtlstats/Prompt.hs b/src/Mtlstats/Prompt.hs index cd66019..e979ceb 100644 --- a/src/Mtlstats/Prompt.hs +++ b/src/Mtlstats/Prompt.hs @@ -26,6 +26,7 @@ module Mtlstats.Prompt ( strPrompt, numPrompt, -- * Individual prompts + otherTeamPrompt, homeScorePrompt, awayScorePrompt ) where @@ -92,6 +93,10 @@ numPrompt pStr act = Prompt , promptFunctionKey = const $ return () } +otherTeamPrompt :: Prompt +otherTeamPrompt = strPrompt "Other team: " $ + modify . (progMode . otherTeamL .~) + homeScorePrompt :: Prompt homeScorePrompt = numPrompt "Home score: " $ modify . (progMode . homeScoreL ?~) diff --git a/src/Mtlstats/UI.hs b/src/Mtlstats/UI.hs index 42d296f..95940f2 100644 --- a/src/Mtlstats/UI.hs +++ b/src/Mtlstats/UI.hs @@ -41,6 +41,7 @@ draw s = do NewSeason -> drawMenu newSeasonMenu NewGame gs | null $ gs ^. gameType -> drawMenu gameTypeMenu + | null $ gs ^. otherTeam -> drawPrompt otherTeamPrompt s | null $ gs ^. homeScore -> drawPrompt homeScorePrompt s | null $ gs ^. awayScore -> drawPrompt awayScorePrompt s | otherwise -> undefined