Skip to content
Permalink
bff360d1b2
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
81 lines (62 sloc) 1.89 KB
module Epicbot.Index
( DocIndex
, Index
, findById
, fromCards
, new
, random
, search
) where
import Prelude
import Data.Array as Array
import Data.Function.Uncurried (Fn2, runFn2)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Epicbot.Card (Card)
import Epicbot.Card as Card
import Epicbot.Random.Array (takeRandom)
type Index =
{ cards :: Map String Card
, index :: DocIndex
}
type Doc =
{ id :: String
, name :: String
}
type Result =
{ ref :: String
, score :: Number
}
foreign import data DocIndex :: Type
foreign import _addDoc :: EffectFn2 Doc DocIndex DocIndex
foreign import _newDocIndex :: DocIndex
foreign import _searchDoc :: Fn2 String DocIndex (Array Result)
new :: Index
new = { index: _newDocIndex, cards: Map.empty }
fromCards :: Array Card -> Aff Index
fromCards = Array.foldRecM (flip addCard) new
random :: Int -> Index -> Aff (Array Card)
random n { cards } =
takeRandom n <<< Array.filter (not Card.dualSided) <<< Array.fromFoldable <<< Map.values $ cards
search :: String -> Index -> Array Card
search query { index, cards } = Array.mapMaybe resultToCard <<< searchDoc query $ index
where
resultToCard :: Result -> Maybe Card
resultToCard result = Map.lookup result.ref cards
findById :: String -> Index -> Maybe Card
findById id { cards } = Map.lookup id cards
addCard :: Card -> Index -> Aff Index
addCard card { index, cards } = do
index' <- addDoc (cardToDoc card) index
let cards' = Map.insert card.id card cards
pure { index: index', cards: cards' }
addDoc :: Doc -> DocIndex -> Aff DocIndex
addDoc doc index = liftEffect $ runEffectFn2 _addDoc doc index
searchDoc :: String -> DocIndex -> Array Result
searchDoc = runFn2 _searchDoc
cardToDoc :: Card -> Doc
cardToDoc { id, name } = { id, name }