implemented selectActivePlayerPrompt

This commit is contained in:
Jonathan Lamothe 2020-04-06 14:46:30 -04:00
parent c20fb30f5b
commit 9b6dfc4be9
3 changed files with 72 additions and 8 deletions

View File

@ -43,6 +43,7 @@ module Mtlstats.Prompt (
goalieNumPrompt, goalieNumPrompt,
goalieNamePrompt, goalieNamePrompt,
selectPlayerPrompt, selectPlayerPrompt,
selectActivePlayerPrompt,
selectGoaliePrompt, selectGoaliePrompt,
selectPositionPrompt, selectPositionPrompt,
playerToEditPrompt playerToEditPrompt
@ -263,18 +264,21 @@ goalieNamePrompt :: Prompt
goalieNamePrompt = namePrompt "Goalie name: " $ goalieNamePrompt = namePrompt "Goalie name: " $
modify . (progMode.createGoalieStateL.cgsName .~) modify . (progMode.createGoalieStateL.cgsName .~)
-- | Selects a player (creating one if necessary) -- | Selects a player using a specified search function (creating the
selectPlayerPrompt -- player if necessary)
:: String selectPlayerPromptWith
:: (String -> [Player] -> [(Int, Player)])
-- ^ The search function
-> String
-- ^ The prompt string -- ^ The prompt string
-> (Maybe Int -> Action ()) -> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the payer as -- ^ The callback to run (takes the index number of the payer as
-- input) -- input)
-> Prompt -> Prompt
selectPlayerPrompt pStr callback = selectPrompt SelectParams selectPlayerPromptWith sFunc pStr callback = selectPrompt SelectParams
{ spPrompt = pStr { spPrompt = pStr
, spSearchHeader = "Player select:" , spSearchHeader = "Player select:"
, spSearch = \sStr db -> playerSearch sStr (db^.dbPlayers) , spSearch = \sStr db -> sFunc sStr (db^.dbPlayers)
, spSearchExact = \sStr db -> fst <$> playerSearchExact sStr (db^.dbPlayers) , spSearchExact = \sStr db -> fst <$> playerSearchExact sStr (db^.dbPlayers)
, spElemDesc = playerSummary , spElemDesc = playerSummary
, spProcessChar = capitalizeName , spProcessChar = capitalizeName
@ -292,6 +296,26 @@ selectPlayerPrompt pStr callback = selectPrompt SelectParams
modify $ progMode .~ CreatePlayer cps modify $ progMode .~ CreatePlayer cps
} }
-- | Selects a player (creating one if necessary)
selectPlayerPrompt
:: String
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the payer as
-- input)
-> Prompt
selectPlayerPrompt = selectPlayerPromptWith playerSearch
-- | Selects an active player (creating one if necessary)
selectActivePlayerPrompt
:: String
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the payer as
-- input)
-> Prompt
selectActivePlayerPrompt = selectPlayerPromptWith activePlayerSearch
-- | Selects a goalie (creating one if necessary) -- | Selects a goalie (creating one if necessary)
selectGoaliePrompt selectGoaliePrompt
:: String :: String

View File

@ -176,6 +176,7 @@ module Mtlstats.Types (
addGameStats, addGameStats,
-- ** Player Helpers -- ** Player Helpers
playerSearch, playerSearch,
activePlayerSearch,
playerSearchExact, playerSearchExact,
modifyPlayer, modifyPlayer,
playerSummary, playerSummary,
@ -1003,6 +1004,23 @@ addGameStats s1 s2 = GameStats
, _gmsGoalsAgainst = s1^.gmsGoalsAgainst + s2^.gmsGoalsAgainst , _gmsGoalsAgainst = s1^.gmsGoalsAgainst + s2^.gmsGoalsAgainst
} }
-- | Searches through a list of players with a specified criteria
playerSearchWith
:: (Player -> Bool)
-- ^ The search criteria
-> String
-- ^ The search string
-> [Player]
-- ^ The list of players to search
-> [(Int, Player)]
-- ^ The matching players with their index numbers
playerSearchWith criteria sStr =
filter match . zip [0..]
where
match (_, p)
= map toUpper sStr `isInfixOf` map toUpper (p^.pName)
&& criteria p
-- | Searches through a list of players -- | Searches through a list of players
playerSearch playerSearch
:: String :: String
@ -1011,9 +1029,17 @@ playerSearch
-- ^ The list of players to search -- ^ The list of players to search
-> [(Int, Player)] -> [(Int, Player)]
-- ^ The matching players with their index numbers -- ^ The matching players with their index numbers
playerSearch sStr = playerSearch = playerSearchWith $ const True
filter match . zip [0..]
where match (_, p) = map toUpper sStr `isInfixOf` map toUpper (p^.pName) -- | Searches through a list of players for an active player
activePlayerSearch
:: String
-- ^ The search string
-> [Player]
-- ^ The list of players to search
-> [(Int, Player)]
-- ^ The matching players with their index numbers
activePlayerSearch = playerSearchWith (^.pActive)
-- | Searches for a player by exact match on name -- | Searches for a player by exact match on name
playerSearchExact playerSearchExact

View File

@ -73,6 +73,7 @@ spec = describe "Mtlstats.Types" $ do
gmsPointsSpec gmsPointsSpec
addGameStatsSpec addGameStatsSpec
playerSearchSpec playerSearchSpec
activePlayerSearchSpec
playerSearchExactSpec playerSearchExactSpec
modifyPlayerSpec modifyPlayerSpec
playerSummarySpec playerSummarySpec
@ -647,6 +648,19 @@ playerSearchSpec = describe "playerSearch" $ mapM_
, ( "x", [] ) , ( "x", [] )
] ]
activePlayerSearchSpec :: Spec
activePlayerSearchSpec = describe "activePlayerSearch" $ mapM_
(\(sStr, expected) -> context sStr $
it ("should return " ++ show expected) $ let
ps = [joe, bob, steve & pActive .~ False]
in activePlayerSearch sStr ps `shouldBe` expected)
-- search, result
[ ( "joe", [(0, joe)] )
, ( "o", [(0, joe), (1, bob)] )
, ( "e", [(0, joe)] )
, ( "x", [] )
]
playerSearchExactSpec :: Spec playerSearchExactSpec :: Spec
playerSearchExactSpec = describe "playerSearchExact" $ mapM_ playerSearchExactSpec = describe "playerSearchExact" $ mapM_
(\(sStr, expected) -> context sStr $ (\(sStr, expected) -> context sStr $