impls item price modifiers
This commit is contained in:
52
src/Api.elm
52
src/Api.elm
@@ -320,26 +320,60 @@ type ActionMode
|
||||
|
||||
|
||||
type RequestData
|
||||
= SellPayload Loot (Maybe Float) (List Float) (List Int)
|
||||
| BuyPayload Loot (Maybe Float) (List Float)
|
||||
= SellPayload Loot (Maybe Float) (List (Maybe Float)) (List Int)
|
||||
| BuyPayload Loot (Maybe Float) (List (Maybe Float))
|
||||
| GrabPayload Loot
|
||||
| AddPayload String Loot
|
||||
|
||||
|
||||
zip xs ys =
|
||||
List.map2 Tuple.pair xs ys
|
||||
|
||||
|
||||
itemsWithMods items mods =
|
||||
zip items mods
|
||||
|> E.list
|
||||
(\( item, mod ) ->
|
||||
E.list identity
|
||||
[ E.int item.id
|
||||
, case mod of
|
||||
Just m ->
|
||||
E.float m
|
||||
|
||||
Nothing ->
|
||||
E.null
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
buildPayload : RequestData -> E.Value
|
||||
buildPayload data =
|
||||
case data of
|
||||
BuyPayload items _ _ ->
|
||||
BuyPayload items gMod iMods ->
|
||||
E.object
|
||||
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
|
||||
, ( "global_mod", E.null )
|
||||
[ ( "items", itemsWithMods items iMods )
|
||||
, ( "global_mod"
|
||||
, case gMod of
|
||||
Nothing ->
|
||||
E.null
|
||||
|
||||
Just f ->
|
||||
E.float f
|
||||
)
|
||||
]
|
||||
|
||||
SellPayload items _ _ _ ->
|
||||
SellPayload items gMod iMods players ->
|
||||
E.object
|
||||
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
|
||||
, ( "global_mod", E.null )
|
||||
, ( "players", E.null )
|
||||
[ ( "items", itemsWithMods items iMods )
|
||||
, ( "global_mod"
|
||||
, case gMod of
|
||||
Nothing ->
|
||||
E.null
|
||||
|
||||
Just f ->
|
||||
E.float f
|
||||
)
|
||||
, ( "players", E.list (\id -> E.int id) players )
|
||||
]
|
||||
|
||||
-- API expects the list of claimed items ids
|
||||
|
||||
@@ -12,6 +12,7 @@ import Api
|
||||
, confirmAction
|
||||
)
|
||||
import Browser.Navigation as Nav
|
||||
import Dict exposing (Dict)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (onCheck, onClick, onInput)
|
||||
@@ -31,6 +32,9 @@ type alias State =
|
||||
, error : Maybe String
|
||||
, notification : Maybe String
|
||||
|
||||
-- Buy/Sell loot
|
||||
, priceModifiers : Dict Int Int
|
||||
|
||||
-- AddLoot
|
||||
, showModal : Bool
|
||||
, autoComplete : Loot
|
||||
@@ -69,6 +73,7 @@ init (Player navKey playerId) =
|
||||
View
|
||||
Nothing
|
||||
Nothing
|
||||
Dict.empty
|
||||
False
|
||||
[]
|
||||
Nothing
|
||||
@@ -274,6 +279,10 @@ showWealthField name value =
|
||||
-- VIEW
|
||||
|
||||
|
||||
type alias ItemRenderer =
|
||||
Item -> List (Html Msg)
|
||||
|
||||
|
||||
view : Model -> List (Html Msg)
|
||||
view model =
|
||||
let
|
||||
@@ -303,23 +312,83 @@ view model =
|
||||
canSelect =
|
||||
canSelectIn model.state.mode
|
||||
|
||||
rowRenderer =
|
||||
rowRenderer item =
|
||||
case model.state.mode of
|
||||
View ->
|
||||
case model.shown of
|
||||
-- Claim controls for Group chest
|
||||
GroupLoot ->
|
||||
let
|
||||
isClaimed =
|
||||
itemInClaims model.claims
|
||||
in
|
||||
-- Claim controls for Group chest
|
||||
Just (claimedItemRenderer isClaimed)
|
||||
case isClaimed item of
|
||||
True ->
|
||||
[ renderIcon
|
||||
{ icon = "fas fa-praying-hands"
|
||||
, size = "small"
|
||||
, ratio = "1x"
|
||||
}
|
||||
]
|
||||
|
||||
False ->
|
||||
[]
|
||||
|
||||
_ ->
|
||||
Nothing
|
||||
[]
|
||||
|
||||
activeMode ->
|
||||
Just (rowRendererForMode activeMode)
|
||||
Buy ->
|
||||
let
|
||||
maybeMod =
|
||||
Dict.get item.id model.state.priceModifiers
|
||||
in
|
||||
[ viewPriceWithModApplied
|
||||
(Maybe.map (\i -> toFloatingMod i) maybeMod)
|
||||
(toFloat item.base_price)
|
||||
, if isSelected item then
|
||||
viewPriceModifier item.id <|
|
||||
case Dict.get item.id model.state.priceModifiers of
|
||||
Just mod ->
|
||||
String.fromInt mod
|
||||
|
||||
Nothing ->
|
||||
"0"
|
||||
|
||||
else
|
||||
text ""
|
||||
]
|
||||
|
||||
Sell ->
|
||||
let
|
||||
maybeMod =
|
||||
Dict.get item.id model.state.priceModifiers
|
||||
in
|
||||
[ viewPriceWithModApplied
|
||||
(Debug.log
|
||||
"maybeMod"
|
||||
(Maybe.map (\i -> toFloatingMod i) maybeMod)
|
||||
)
|
||||
(toFloat item.base_price / 2)
|
||||
, if isSelected item then
|
||||
viewPriceModifier item.id <|
|
||||
case maybeMod of
|
||||
Just mod ->
|
||||
String.fromInt mod
|
||||
|
||||
Nothing ->
|
||||
"0"
|
||||
|
||||
else
|
||||
text ""
|
||||
]
|
||||
|
||||
Grab ->
|
||||
[ p [ class "level-item" ] [ text "Grab" ]
|
||||
]
|
||||
|
||||
Add ->
|
||||
[ p [ class "level-item" ] [ text <| "Valeur : " ++ String.fromInt item.base_price ++ "po" ]
|
||||
]
|
||||
|
||||
filteredItems =
|
||||
shownItems
|
||||
@@ -380,12 +449,12 @@ view model =
|
||||
-- VIEW LOOT
|
||||
|
||||
|
||||
viewLoot : Maybe (Item -> Html Msg) -> Bool -> (Item -> Bool) -> Loot -> Html Msg
|
||||
viewLoot maybeRowRenderer canSelect isSelected items =
|
||||
viewLoot : ItemRenderer -> Bool -> (Item -> Bool) -> Loot -> Html Msg
|
||||
viewLoot rowRenderer canSelect isSelected items =
|
||||
table [ class "table is-fullwidth is-striped is-hoverable" ]
|
||||
[ thead [ class "table-header" ]
|
||||
[ th [] [ text "Nom" ] ]
|
||||
, tbody [] <| List.map (viewItemTableRow isSelected canSelect maybeRowRenderer) items
|
||||
, tbody [] <| List.map (viewItemTableRow isSelected canSelect rowRenderer) items
|
||||
]
|
||||
|
||||
|
||||
@@ -408,55 +477,55 @@ viewSearchBar textValue =
|
||||
]
|
||||
|
||||
|
||||
toFloatingMod : Int -> Float
|
||||
toFloatingMod percent =
|
||||
(100 + Debug.log "toFloat" (toFloat percent)) / 100
|
||||
|
||||
|
||||
|
||||
-- Renderers : Item -> Html Msg
|
||||
|
||||
|
||||
claimedItemRenderer : (Item -> Bool) -> Item -> Html Msg
|
||||
claimedItemRenderer isClaimed item =
|
||||
case isClaimed item of
|
||||
True ->
|
||||
renderIcon
|
||||
{ icon = "fas fa-praying-hands"
|
||||
, size = "small"
|
||||
, ratio = "1x"
|
||||
}
|
||||
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
|
||||
]
|
||||
|
||||
False ->
|
||||
text ""
|
||||
Nothing ->
|
||||
p [ class "level-item" ] [ (String.fromFloat basePrice ++ "po") |> text ]
|
||||
|
||||
|
||||
rowRendererForMode : ActionMode -> Item -> Html Msg
|
||||
rowRendererForMode mode item =
|
||||
case mode of
|
||||
Buy ->
|
||||
p [ class "level-item" ] [ text (String.fromInt item.base_price ++ "po") ]
|
||||
|
||||
Sell ->
|
||||
p [ class "level-item" ] [ text (String.fromFloat (toFloat item.base_price / 2) ++ "po") ]
|
||||
|
||||
Grab ->
|
||||
p [ class "level-item" ] [ text "Grab" ]
|
||||
|
||||
Add ->
|
||||
p [ class "level-item" ] [ text <| "Valeur : " ++ String.fromInt item.base_price ++ "po" ]
|
||||
|
||||
View ->
|
||||
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" ] [] ]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
viewItemTableRow : (Item -> Bool) -> Bool -> Maybe (Item -> Html Msg) -> Item -> Html Msg
|
||||
viewItemTableRow : (Item -> Bool) -> Bool -> ItemRenderer -> Item -> Html Msg
|
||||
viewItemTableRow isSelected canSelect rowRenderer item =
|
||||
let
|
||||
rightLevel =
|
||||
div [ class "level-right" ]
|
||||
[ case rowRenderer of
|
||||
Just render ->
|
||||
render item
|
||||
|
||||
Nothing ->
|
||||
text ""
|
||||
, if canSelect then
|
||||
div [ class "level-right" ] <|
|
||||
(if canSelect then
|
||||
input
|
||||
[ class "checkbox level-item"
|
||||
, type_ "checkbox"
|
||||
@@ -467,7 +536,8 @@ viewItemTableRow isSelected canSelect rowRenderer item =
|
||||
|
||||
else
|
||||
text ""
|
||||
]
|
||||
)
|
||||
:: rowRenderer item
|
||||
in
|
||||
tr [ classList [ ( "is-selected", isSelected item ) ] ]
|
||||
[ td []
|
||||
@@ -756,6 +826,7 @@ type Msg
|
||||
| FromListChanged String
|
||||
| FromListConfirmed
|
||||
| NewItemsFromList Loot (Maybe String)
|
||||
| PriceModifierChanged Int String
|
||||
|
||||
|
||||
insensitiveContains : String -> String -> Bool
|
||||
@@ -766,6 +837,30 @@ insensitiveContains substring string =
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
case msg of
|
||||
PriceModifierChanged id value ->
|
||||
let
|
||||
state =
|
||||
model.state
|
||||
in
|
||||
( { model
|
||||
| state =
|
||||
{ state
|
||||
| priceModifiers =
|
||||
Dict.insert
|
||||
id
|
||||
(case String.toInt value of
|
||||
Just i ->
|
||||
i
|
||||
|
||||
Nothing ->
|
||||
0
|
||||
)
|
||||
model.state.priceModifiers
|
||||
}
|
||||
}
|
||||
, Cmd.none
|
||||
)
|
||||
|
||||
SwitchMenuOpen ->
|
||||
let
|
||||
state =
|
||||
@@ -996,10 +1091,28 @@ update msg model =
|
||||
(selectContent model)
|
||||
|
||||
Buy ->
|
||||
Just <| Api.BuyPayload items Nothing []
|
||||
let
|
||||
modList =
|
||||
List.map
|
||||
(\item ->
|
||||
Dict.get item.id model.state.priceModifiers
|
||||
|> Maybe.map (\i -> toFloatingMod i)
|
||||
)
|
||||
items
|
||||
in
|
||||
Just <| Api.BuyPayload items Nothing modList
|
||||
|
||||
Sell ->
|
||||
Just <| Api.SellPayload items Nothing [] []
|
||||
let
|
||||
modList =
|
||||
List.map
|
||||
(\item ->
|
||||
Dict.get item.id model.state.priceModifiers
|
||||
|> Maybe.map (\i -> toFloatingMod i)
|
||||
)
|
||||
items
|
||||
in
|
||||
Just <| Api.SellPayload items Nothing modList []
|
||||
|
||||
Grab ->
|
||||
Just <| Api.GrabPayload items
|
||||
|
||||
Reference in New Issue
Block a user