257 lines
6.3 KiB
Elm
257 lines
6.3 KiB
Elm
module Chest.Selection exposing (Model, Msg, init, modifiers, selected, totalSelectedPrice, update, view)
|
|
|
|
import Api exposing (Item, Loot)
|
|
import Dict exposing (Dict)
|
|
import Html exposing (..)
|
|
import Html.Attributes exposing (..)
|
|
import Html.Events exposing (..)
|
|
import Set exposing (Set)
|
|
import Table
|
|
|
|
|
|
type alias Selection =
|
|
Set Int
|
|
|
|
|
|
type Data a
|
|
= NoData
|
|
| Data (Dict Int a) PriceExtractor
|
|
|
|
|
|
type alias PriceExtractor =
|
|
Item -> Int
|
|
|
|
|
|
type Model
|
|
= Model Selection (Data Int)
|
|
|
|
|
|
init : Maybe (List Int) -> Maybe PriceExtractor -> Model
|
|
init maybeInitial hasData =
|
|
Model
|
|
(case maybeInitial of
|
|
Just initial ->
|
|
Set.fromList initial
|
|
|
|
Nothing ->
|
|
Set.empty
|
|
)
|
|
(case hasData of
|
|
Just getPrice ->
|
|
Data Dict.empty getPrice
|
|
|
|
Nothing ->
|
|
NoData
|
|
)
|
|
|
|
|
|
view : Model -> Loot -> Html Msg
|
|
view (Model selection data) loot =
|
|
let
|
|
isSelected =
|
|
itemInSelection selection
|
|
|
|
renderRight item =
|
|
case data of
|
|
Data inner getPrice ->
|
|
let
|
|
maybeMod =
|
|
Dict.get item.id inner
|
|
in
|
|
renderItemWithPrice
|
|
(getPrice item)
|
|
(isSelected item)
|
|
maybeMod
|
|
item.id
|
|
|
|
NoData ->
|
|
[]
|
|
in
|
|
Table.view
|
|
(Table.renderSelectableRow
|
|
(\item -> [ p [] [ text item.name ] ])
|
|
renderRight
|
|
(\item _ -> SwitchSelectionState item.id)
|
|
isSelected
|
|
)
|
|
loot
|
|
|
|
|
|
renderItemWithPrice : Int -> Bool -> Maybe Int -> Int -> List (Html Msg)
|
|
renderItemWithPrice price isSelected maybeMod itemId =
|
|
[ viewPriceWithModApplied
|
|
(Maybe.map (\i -> toFloatingMod i) maybeMod)
|
|
(toFloat price)
|
|
, if isSelected then
|
|
viewPriceModifier itemId <|
|
|
case maybeMod of
|
|
Just mod ->
|
|
String.fromInt mod
|
|
|
|
Nothing ->
|
|
"0"
|
|
|
|
else
|
|
text ""
|
|
]
|
|
|
|
|
|
toFloatingMod : Int -> Float
|
|
toFloatingMod percent =
|
|
(100 + Debug.log "toFloat" (toFloat percent)) / 100
|
|
|
|
|
|
|
|
-- Renderers : Item -> Html Msg
|
|
|
|
|
|
viewPriceWithModApplied : Maybe Float -> Float -> Html Msg
|
|
viewPriceWithModApplied maybeMod basePrice =
|
|
case maybeMod of
|
|
Just mod ->
|
|
p [ class "level-item has-text-weight-bold" ]
|
|
[ (Debug.log "withMod" (String.fromFloat (basePrice * mod)) ++ "po")
|
|
|> text
|
|
]
|
|
|
|
Nothing ->
|
|
p [ class "level-item" ] [ (String.fromFloat basePrice ++ "po") |> text ]
|
|
|
|
|
|
viewPriceModifier : Int -> String -> Html Msg
|
|
viewPriceModifier id modValue =
|
|
div [ class "level-item field has-addons" ]
|
|
[ div [ class "control has-icons-left" ]
|
|
[ input
|
|
[ type_ "number"
|
|
, value modValue
|
|
, class "input is-small"
|
|
, size 3
|
|
, style "width" "6em"
|
|
, Html.Attributes.min "-50"
|
|
, Html.Attributes.max "50"
|
|
, step "5"
|
|
, onInput (PriceModifierChanged id)
|
|
]
|
|
[]
|
|
, span [ class "icon is-left" ] [ i [ class "fas fa-percent" ] [] ]
|
|
]
|
|
]
|
|
|
|
|
|
|
|
-- Selection
|
|
-- Get list of selected items
|
|
|
|
|
|
selected : Model -> Loot -> Loot
|
|
selected (Model selection data) loot =
|
|
List.filter (itemInSelection selection) loot
|
|
|
|
|
|
modifiers : Model -> Loot -> List (Maybe Float)
|
|
modifiers (Model selection data) items =
|
|
case data of
|
|
Data inner _ ->
|
|
List.map
|
|
(\item ->
|
|
Dict.get item.id inner
|
|
|> Maybe.map toFloatingMod
|
|
)
|
|
items
|
|
|
|
NoData ->
|
|
[]
|
|
|
|
|
|
totalSelectedPrice : Model -> Loot -> Maybe Float
|
|
totalSelectedPrice model loot =
|
|
case selected model loot of
|
|
[] ->
|
|
Nothing
|
|
|
|
items ->
|
|
let
|
|
(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 _ ->
|
|
Dict.get item.id inner
|
|
|
|
NoData ->
|
|
Nothing
|
|
in
|
|
Just <|
|
|
List.foldl (+) 0.0 <|
|
|
List.map (\item -> toFloat (getPrice item) * modifier item) items
|
|
|
|
|
|
itemInSelection : Selection -> Item -> Bool
|
|
itemInSelection selection item =
|
|
Set.member item.id selection
|
|
|
|
|
|
|
|
{-
|
|
itemInClaims : Claims -> Item -> Bool
|
|
itemInClaims claims item =
|
|
List.any (\c -> c.loot_id == item.id) claims
|
|
-}
|
|
|
|
|
|
switchSelectionState : Int -> Selection -> Selection
|
|
switchSelectionState id selection =
|
|
case Set.member id selection of
|
|
True ->
|
|
Set.remove id selection
|
|
|
|
False ->
|
|
Set.insert id selection
|
|
|
|
|
|
type Msg
|
|
= SwitchSelectionState Int
|
|
| PriceModifierChanged Int String
|
|
|
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
update msg (Model selection data) =
|
|
case msg of
|
|
PriceModifierChanged id value ->
|
|
( Model selection <|
|
|
case data of
|
|
Data inner getPrice ->
|
|
Data
|
|
(Dict.insert
|
|
id
|
|
(case String.toInt value of
|
|
Just i ->
|
|
i
|
|
|
|
Nothing ->
|
|
0
|
|
)
|
|
inner
|
|
)
|
|
getPrice
|
|
|
|
NoData ->
|
|
data
|
|
, Cmd.none
|
|
)
|
|
|
|
SwitchSelectionState id ->
|
|
( Model (switchSelectionState id selection) data, Cmd.none )
|