implemented playerSearch

This commit is contained in:
Jonathan Lamothe 2019-09-18 01:55:38 -04:00
parent 2ff8cff1c8
commit ed9e437a1a
2 changed files with 34 additions and 1 deletions

View File

@ -120,7 +120,8 @@ module Mtlstats.Types (
gmsPoints,
addGameStats,
-- ** Player Helpers
pPoints
pPoints,
playerSearch
) where
import Control.Monad.Trans.State (StateT)
@ -136,6 +137,7 @@ import Data.Aeson
, (.:)
, (.=)
)
import Data.List (isInfixOf)
import Lens.Micro (Lens', lens, (&), (^.), (.~))
import Lens.Micro.TH (makeLenses)
import qualified UI.NCurses as C
@ -636,3 +638,16 @@ addGameStats s1 s2 = GameStats
-- | Calculates a player's points
pPoints :: PlayerStats -> Int
pPoints s = s^.psGoals + s^.psAssists
-- | Searches through a list of players
playerSearch
:: String
-- ^ The search string
-> [Player]
-- ^ The list of players to search
-> [(Int, Player)]
-- ^ The matching players with their index numbers
playerSearch sStr =
filter (match sStr) .
zip [0..]
where match sStr (_, p) = sStr `isInfixOf` (p^.pName)

View File

@ -55,6 +55,7 @@ spec = describe "Mtlstats.Types" $ do
gmsPointsSpec
addGameStatsSpec
pPointsSpec
playerSearchSpec
Menu.spec
playerSpec :: Spec
@ -512,3 +513,20 @@ pPointsSpec = describe "pPoints" $ mapM_
, ( 0, 1, 1 )
, ( 2, 3, 5 )
]
playerSearchSpec :: Spec
playerSearchSpec = describe "playerSearch" $ mapM_
(\(sStr, expected) -> context sStr $
it ("should return " ++ show expected) $ let
ps = [joe, bob, steve]
in playerSearch sStr ps `shouldBe` expected)
-- search, result
[ ( "Joe", [(0, joe)] )
, ( "o", [(0, joe), (1, bob)] )
, ( "e", [(0, joe), (2, steve)] )
, ( "x", [] )
]
where
joe = newPlayer 2 "Joe" "center"
bob = newPlayer 3 "Bob" "defense"
steve = newPlayer 5 "Steve" "forward"