Unify regex/filtering code

This commit is contained in:
2024-10-26 00:01:12 +01:00
parent 46ee29f646
commit dc34172691
6 changed files with 316 additions and 306 deletions
+88 -54
View File
@@ -1,4 +1,4 @@
--{-# OPTIONS_GHC -fno-full-laziness #-}
-- {-# OPTIONS_GHC -fno-full-laziness #-}
module Dodge.DisplayInventory (
toggleCombineInv,
updateInventoryPositioning,
@@ -42,31 +42,36 @@ updateCombineSections ::
IM.IntMap (SelectionSection CombinableItem) ->
IM.IntMap (SelectionSection CombinableItem)
updateCombineSections w cfig =
updateSectionsPositioning
mselpos
availablelines
[(0, showncombs), (-1, filtinv)]
updateSectionsPositioning
(w ^? hud . hudElement . subInventory . ciSelection . _Just)
(getAvailableListLines secondColumnParams cfig)
[(0, sclose), (-1, sfclose)]
where
mselpos = w ^? hud . hudElement . subInventory . ciSelection . _Just
availablelines = getAvailableListLines secondColumnParams cfig
(sfclose,sclose) = filterSectionsPair (flip (andOrRegex $ regexCombs invitms))
allcombs "COMBINATIONS" $ mstr
invitms = fromMaybe mempty $ w ^? hud . hudElement . diSections . ix 0 . ssItems
allcombs = IM.fromDistinctAscList $ zip [0 ..] $ combineList w
filtcombs = fromMaybe allcombs $ do
str <- mstr
invitms <- w ^? hud . hudElement . diSections . ix 0 . ssItems
return $ IM.filter (flip (andOrRegex $ regexCombs invitms) str) allcombs
showncombs
| null filtcombs =
IM.singleton 0 $
SelectionInfo ["No possible combinations"] 1 False white 0
| otherwise = filtcombs
filtinv = fromMaybe mempty $ do
str <- mstr
return $
IM.singleton
0
(SelectionInfo ["COMBINATIONS FILTER: " ++ str, numfiltitems] 2 True white 0)
-- filtcombs = fromMaybe allcombs $ do
-- str <- mstr
-- invitms <- w ^? hud . hudElement . diSections . ix 0 . ssItems
-- return $ IM.filter (flip (andOrRegex $ regexCombs invitms) str) allcombs
-- showncombs
-- | null filtcombs =
-- IM.singleton 0 $
-- SelectionInfo ["No possible combinations"] 1 False white 0
-- | otherwise = filtcombs
-- filterdisplay = fromMaybe mempty $ do
-- str <- mstr
-- return $
-- IM.singleton 0 $
-- SelectionInfo
-- ["COMBINATIONS FILTER: " ++ str, numfiltitems]
-- 2
-- True
-- white
-- 0
mstr = w ^? hud . hudElement . subInventory . ciFilter . _Just
numfiltitems = " " ++ show (length allcombs - length filtcombs) ++ " FILTERED"
-- numfiltitems = " " ++ show (length allcombs - length filtcombs) ++ " FILTERED"
regexCombs :: IM.IntMap (SelectionItem ()) -> SelectionItem CombinableItem -> String -> Bool
regexCombs inv ci str = case str of
@@ -96,12 +101,11 @@ updateDisplaySections ::
Configuration ->
IM.IntMap (SelectionSection ()) ->
IM.IntMap (SelectionSection ())
updateDisplaySections w cfig sss =
updateSectionsPositioning
mselpos
availablelines
addorder
sss
updateDisplaySections w cfig =
updateSectionsPositioning
mselpos
(getAvailableListLines (invDisplayParams w) cfig)
addorder
where
mselpos = w ^? hud . hudElement . diSelection . _Just
addorder = case mselpos of
@@ -110,40 +114,70 @@ updateDisplaySections w cfig sss =
Just (1, _) -> reverse [filtinv, filtclose, invx, youx, closex]
Just (2, _) -> reverse [filtinv, filtclose, closex, invx, youx]
_ -> reverse [filtinv, filtclose, closex, invx, youx]
(filtinv, invx) = filtpair (-1) invitems' "INV. "
(filtclose, closex) = filtpair 2 coitems' "NEARBY "
(sfinv,sinv) = filterSectionsPair plainRegex invitems "INVTORY" $
w ^? hud . hudElement . diInvFilter . _Just
(filtinv, invx) = ((-1,sfinv),(0,sinv))
-- filtpair (-1) invitems "INV. " $
-- w ^? hud . hudElement . diInvFilter . _Just
(sfclose,sclose) = filterSectionsPair plainRegex coitems "NEARBY" $
w ^? hud . hudElement . diCloseFilter . _Just
(filtclose, closex) = ((2,sfclose),(3,sclose))
-- filtpair 2 coitems "NEARBY " $
-- w ^? hud . hudElement . diCloseFilter . _Just
youx = (1, youitems)
youitems = IM.singleton 0 $ SelectionItem [thetext] 1 True invDimColor 2 ()
thetext = displayFreeSlots nfreeslots
availablelines = getAvailableListLines (invDisplayParams w) cfig
coitems' =
coitems =
IM.fromDistinctAscList . zip [0 ..] $
map closeObjectToSelectionItem (w ^. hud . closeObjects)
invitems' =
invitems =
IM.mapWithKey
(\k (y, x) -> invSelectionItem cr k (x ^. locLDT . ldtValue) y)
invlocs
(\k (y, x) -> invSelectionItem y cr k (x ^. locLDT . ldtValue))
(allInvLocs $ _crInv cr)
cr = you w
invlocs = allInvLocs $ _crInv (you w)
nfreeslots = crNumFreeSlots cr
filtpair i itms filtdescription =
( (i, filtsis)
, (i + 1, itms')
)
where
mstr = case i of
(-1) -> w ^? hud . hudElement . diInvFilter . _Just
2 -> w ^? hud . hudElement . diCloseFilter . _Just
filtsis = fromMaybe mempty $ do
str <- mstr
return $
IM.singleton
-- filtpair i itms filtdescription mstr =
-- ( (i, filtsis)
-- , (i + 1, itms')
-- )
-- where
-- filtsis = fromMaybe mempty $ do
-- str <- mstr
-- return $
-- IM.singleton
-- 0
-- (SelectionInfo [filtdescription ++ "FILTER: " ++ str, numfiltitems] 2 True white 0)
-- itms' = fromMaybe itms $ do
-- str <- mstr
-- return $ IM.filter (plainRegex str) itms
-- numfiltitems = " " ++ show (length itms - length itms') ++ " FILTERED"
filterSectionsPair ::
(String -> SelectionItem a -> Bool) ->
IM.IntMap (SelectionItem a) ->
String ->
Maybe String ->
(IM.IntMap (SelectionItem a), IM.IntMap (SelectionItem a))
filterSectionsPair filtfn itms filtdescription mfilt =
( filtsis
, itms'
)
where
filtsis = fromMaybe mempty $ do
str <- mfilt
return $
IM.singleton
0
$ SelectionInfo
[filtdescription ++ " FILTER: " ++ str, numfiltitems]
2
True
white
0
(SelectionInfo [filtdescription ++ "FILTER: " ++ str, numfiltitems] 2 True white 0)
itms' = fromMaybe itms $ do
str <- mstr
return $ IM.filter (plainRegex str) itms
numfiltitems = " " ++ show (length itms - length itms') ++ " FILTERED"
itms' = fromMaybe itms $ do
str <- mfilt
return $ IM.filter (filtfn str) itms
numfiltitems = " " ++ show (length itms - length itms') ++ " FILTERED"
invDimColor :: Color
invDimColor = greyN 0.7