diff --git a/src/Mtlstats/Prompt.hs b/src/Mtlstats/Prompt.hs index d02ccb0..c6c74bc 100644 --- a/src/Mtlstats/Prompt.hs +++ b/src/Mtlstats/Prompt.hs @@ -45,6 +45,7 @@ module Mtlstats.Prompt ( selectPlayerPrompt, selectActivePlayerPrompt, selectGoaliePrompt, + selectActiveGoaliePrompt, selectPositionPrompt, playerToEditPrompt ) where @@ -316,18 +317,21 @@ selectActivePlayerPrompt -> Prompt selectActivePlayerPrompt = selectPlayerPromptWith activePlayerSearch --- | Selects a goalie (creating one if necessary) -selectGoaliePrompt - :: String +-- | Selects a goalie with a specified search criteria (creating the +-- goalie if necessary) +selectGoaliePromptWith + :: (String -> [Goalie] -> [(Int, Goalie)]) + -- ^ The search criteria + -> String -- ^ The prompt string -> (Maybe Int -> Action ()) -- ^ The callback to run (takes the index number of the goalie as -- input) -> Prompt -selectGoaliePrompt pStr callback = selectPrompt SelectParams +selectGoaliePromptWith criteria pStr callback = selectPrompt SelectParams { spPrompt = pStr , spSearchHeader = "Goalie select:" - , spSearch = \sStr db -> goalieSearch sStr (db^.dbGoalies) + , spSearch = \sStr db -> criteria sStr (db^.dbGoalies) , spSearchExact = \sStr db -> fst <$> goalieSearchExact sStr (db^.dbGoalies) , spElemDesc = goalieSummary , spProcessChar = capitalizeName @@ -345,6 +349,26 @@ selectGoaliePrompt pStr callback = selectPrompt SelectParams modify $ progMode .~ CreateGoalie cgs } +-- | Selects a goalie (creating one if necessary) +selectGoaliePrompt + :: String + -- ^ The prompt string + -> (Maybe Int -> Action ()) + -- ^ The callback to run (takes the index number of the goalie as + -- input) + -> Prompt +selectGoaliePrompt = selectGoaliePromptWith goalieSearch + +-- | Selects an active goalie (creating one if necessary) +selectActiveGoaliePrompt + :: String + -- ^ The prompt string + -> (Maybe Int -> Action ()) + -- ^ The callback to run (takes the index number of the goalie as + -- input) + -> Prompt +selectActiveGoaliePrompt = selectGoaliePromptWith activeGoalieSearch + -- | Selects (or creates) a player position selectPositionPrompt :: String diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 1bee699..b5ad653 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -186,6 +186,7 @@ module Mtlstats.Types ( addPlayerStats, -- ** Goalie Helpers goalieSearch, + activeGoalieSearch, goalieSearchExact, goalieSummary, goalieIsActive, @@ -1094,6 +1095,23 @@ addPlayerStats s1 s2 = newPlayerStats & psAssists .~ s1^.psAssists + s2^.psAssists & psPMin .~ s1^.psPMin + s2^.psPMin +-- | Searches a list of goalies with a search criteria +goalieSearchWith + :: (Goalie -> Bool) + -- ^ The search criteria + -> String + -- ^ The search string + -> [Goalie] + -- ^ The list to search + -> [(Int, Goalie)] + -- ^ The search results with their corresponding index numbers +goalieSearchWith criteria sStr = + filter match . zip [0..] + where + match (_, g) + = map toUpper sStr `isInfixOf` map toUpper (g^.gName) + && criteria g + -- | Searches a list of goalies goalieSearch :: String @@ -1102,9 +1120,17 @@ goalieSearch -- ^ The list to search -> [(Int, Goalie)] -- ^ The search results with their corresponding index numbers -goalieSearch sStr = - filter match . zip [0..] - where match (_, g) = map toUpper sStr `isInfixOf` map toUpper (g^.gName) +goalieSearch = goalieSearchWith $ const True + +-- | Searches a list of goalies for an active goalie +activeGoalieSearch + :: String + -- ^ The search string + -> [Goalie] + -- ^ The list to search + -> [(Int, Goalie)] + -- ^ The search results with their corresponding index numbers +activeGoalieSearch = goalieSearchWith (^.gActive) -- | Searches a list of goalies for an exact match goalieSearchExact diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index 1104e51..e6d6340 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -81,6 +81,7 @@ spec = describe "Mtlstats.Types" $ do psPointsSpec addPlayerStatsSpec goalieSearchSpec + activeGoalieSearchSpec goalieSearchExactSpec goalieSummarySpec goalieIsActiveSpec @@ -792,6 +793,28 @@ goalieSearchSpec = describe "goalieSearch" $ do it "should return Bob" $ goalieSearch "bob" goalies `shouldBe` [result 1] +activeGoalieSearchSpec :: Spec +activeGoalieSearchSpec = describe "activeGoalieSearch" $ do + let + goalies = + [ newGoalie 2 "Joe" + , newGoalie 3 "Bob" + , newGoalie 5 "Steve" & gActive .~ False + ] + result n = (n, goalies!!n) + + context "partial match" $ + it "should return Joe" $ + activeGoalieSearch "e" goalies `shouldBe` [result 0] + + context "no match" $ + it "should return an empty list" $ + activeGoalieSearch "x" goalies `shouldBe` [] + + context "exact match" $ + it "should return Bob" $ + activeGoalieSearch "bob" goalies `shouldBe` [result 1] + goalieSearchExactSpec :: Spec goalieSearchExactSpec = describe "goalieSearchExact" $ do let