diff --git a/src/Chest.elm b/src/Chest.elm index 427a017..e410e41 100644 --- a/src/Chest.elm +++ b/src/Chest.elm @@ -188,17 +188,17 @@ update msg model = ( View <| Table.renderRowLevel renderItem (\_ -> []), Cmd.none ) IntoBuy -> - ( Buy <| Selection.init Nothing True, Cmd.none ) + ( Buy <| Selection.init Nothing (Just .base_price), Cmd.none ) IntoSell -> - ( Sell <| Selection.init Nothing True, Cmd.none ) + ( Sell <| Selection.init Nothing (Just (\i -> i.base_price // 2)), Cmd.none ) IntoClaim claims -> let initialSelection = List.map .loot_id claims in - ( Claim <| Selection.init (Just initialSelection) False, Cmd.none ) + ( Claim <| Selection.init (Just initialSelection) Nothing, Cmd.none ) IntoAdd -> ( New NewFromInventory.init, Cmd.none ) diff --git a/src/Chest/Selection.elm b/src/Chest/Selection.elm index 91d6c9b..06793b4 100644 --- a/src/Chest/Selection.elm +++ b/src/Chest/Selection.elm @@ -15,14 +15,18 @@ type alias Selection = type Data a = NoData - | Data (Dict Int a) + | Data (Dict Int a) PriceExtractor + + +type alias PriceExtractor = + Item -> Int type Model = Model Selection (Data Int) -init : Maybe (List Int) -> Bool -> Model +init : Maybe (List Int) -> Maybe PriceExtractor -> Model init maybeInitial hasData = Model (case maybeInitial of @@ -33,10 +37,10 @@ init maybeInitial hasData = Set.empty ) (case hasData of - True -> - Data Dict.empty + Just getPrice -> + Data Dict.empty getPrice - False -> + Nothing -> NoData ) @@ -49,12 +53,16 @@ view (Model selection data) loot = renderRight item = case data of - Data inner -> + Data inner getPrice -> let maybeMod = Dict.get item.id inner in - renderItemWithPrice .base_price isSelected maybeMod item + renderItemWithPrice + (getPrice item) + (isSelected item) + maybeMod + item.id NoData -> [] @@ -69,12 +77,13 @@ view (Model selection data) loot = loot -renderItemWithPrice toPrice isSelected maybeMod item = +renderItemWithPrice : Int -> Bool -> Maybe Int -> Int -> List (Html Msg) +renderItemWithPrice price isSelected maybeMod itemId = [ viewPriceWithModApplied (Maybe.map (\i -> toFloatingMod i) maybeMod) - (toFloat item.base_price) - , if isSelected item then - viewPriceModifier item.id <| + (toFloat price) + , if isSelected then + viewPriceModifier itemId <| case maybeMod of Just mod -> String.fromInt mod @@ -143,7 +152,7 @@ selected (Model selection data) loot = modifiers : Model -> Loot -> List (Maybe Float) modifiers (Model selection data) items = case data of - Data inner -> + Data inner _ -> List.map (\item -> Dict.get item.id inner @@ -166,11 +175,19 @@ totalSelectedPrice model loot = (Model selection data) = model + getPrice = + case data of + Data _ getPrice_ -> + getPrice_ + + NoData -> + .base_price + modifier item = Maybe.withDefault 1.0 <| Maybe.map toFloatingMod <| case data of - Data inner -> + Data inner _ -> Dict.get item.id inner NoData -> @@ -178,7 +195,7 @@ totalSelectedPrice model loot = in Just <| List.foldl (+) 0.0 <| - List.map (\item -> toFloat item.base_price * modifier item) items + List.map (\item -> toFloat (getPrice item) * modifier item) items itemInSelection : Selection -> Item -> Bool @@ -215,7 +232,7 @@ update msg (Model selection data) = PriceModifierChanged id value -> ( Model selection <| case data of - Data inner -> + Data inner getPrice -> Data (Dict.insert id @@ -228,6 +245,7 @@ update msg (Model selection data) = ) inner ) + getPrice NoData -> data