implemented searchActiveGoaliePrompt

This commit is contained in:
Jonathan Lamothe 2020-04-06 15:01:26 -04:00
parent 9b6dfc4be9
commit 4f147cd5a4
3 changed files with 81 additions and 8 deletions

View File

@ -45,6 +45,7 @@ module Mtlstats.Prompt (
selectPlayerPrompt, selectPlayerPrompt,
selectActivePlayerPrompt, selectActivePlayerPrompt,
selectGoaliePrompt, selectGoaliePrompt,
selectActiveGoaliePrompt,
selectPositionPrompt, selectPositionPrompt,
playerToEditPrompt playerToEditPrompt
) where ) where
@ -316,18 +317,21 @@ selectActivePlayerPrompt
-> Prompt -> Prompt
selectActivePlayerPrompt = selectPlayerPromptWith activePlayerSearch selectActivePlayerPrompt = selectPlayerPromptWith activePlayerSearch
-- | Selects a goalie (creating one if necessary) -- | Selects a goalie with a specified search criteria (creating the
selectGoaliePrompt -- goalie if necessary)
:: String selectGoaliePromptWith
:: (String -> [Goalie] -> [(Int, Goalie)])
-- ^ The search criteria
-> String
-- ^ The prompt string -- ^ The prompt string
-> (Maybe Int -> Action ()) -> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the goalie as -- ^ The callback to run (takes the index number of the goalie as
-- input) -- input)
-> Prompt -> Prompt
selectGoaliePrompt pStr callback = selectPrompt SelectParams selectGoaliePromptWith criteria pStr callback = selectPrompt SelectParams
{ spPrompt = pStr { spPrompt = pStr
, spSearchHeader = "Goalie select:" , 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) , spSearchExact = \sStr db -> fst <$> goalieSearchExact sStr (db^.dbGoalies)
, spElemDesc = goalieSummary , spElemDesc = goalieSummary
, spProcessChar = capitalizeName , spProcessChar = capitalizeName
@ -345,6 +349,26 @@ selectGoaliePrompt pStr callback = selectPrompt SelectParams
modify $ progMode .~ CreateGoalie cgs 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 -- | Selects (or creates) a player position
selectPositionPrompt selectPositionPrompt
:: String :: String

View File

@ -186,6 +186,7 @@ module Mtlstats.Types (
addPlayerStats, addPlayerStats,
-- ** Goalie Helpers -- ** Goalie Helpers
goalieSearch, goalieSearch,
activeGoalieSearch,
goalieSearchExact, goalieSearchExact,
goalieSummary, goalieSummary,
goalieIsActive, goalieIsActive,
@ -1094,6 +1095,23 @@ addPlayerStats s1 s2 = newPlayerStats
& psAssists .~ s1^.psAssists + s2^.psAssists & psAssists .~ s1^.psAssists + s2^.psAssists
& psPMin .~ s1^.psPMin + s2^.psPMin & 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 -- | Searches a list of goalies
goalieSearch goalieSearch
:: String :: String
@ -1102,9 +1120,17 @@ goalieSearch
-- ^ The list to search -- ^ The list to search
-> [(Int, Goalie)] -> [(Int, Goalie)]
-- ^ The search results with their corresponding index numbers -- ^ The search results with their corresponding index numbers
goalieSearch sStr = goalieSearch = goalieSearchWith $ const True
filter match . zip [0..]
where match (_, g) = map toUpper sStr `isInfixOf` map toUpper (g^.gName) -- | 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 -- | Searches a list of goalies for an exact match
goalieSearchExact goalieSearchExact

View File

@ -81,6 +81,7 @@ spec = describe "Mtlstats.Types" $ do
psPointsSpec psPointsSpec
addPlayerStatsSpec addPlayerStatsSpec
goalieSearchSpec goalieSearchSpec
activeGoalieSearchSpec
goalieSearchExactSpec goalieSearchExactSpec
goalieSummarySpec goalieSummarySpec
goalieIsActiveSpec goalieIsActiveSpec
@ -792,6 +793,28 @@ goalieSearchSpec = describe "goalieSearch" $ do
it "should return Bob" $ it "should return Bob" $
goalieSearch "bob" goalies `shouldBe` [result 1] 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 :: Spec
goalieSearchExactSpec = describe "goalieSearchExact" $ do goalieSearchExactSpec = describe "goalieSearchExact" $ do
let let