impls item price modifiers

This commit is contained in:
2019-11-17 15:41:48 +01:00
parent 516006f352
commit ebdba97d1e
2 changed files with 206 additions and 59 deletions

View File

@@ -320,26 +320,60 @@ type ActionMode
type RequestData type RequestData
= SellPayload Loot (Maybe Float) (List Float) (List Int) = SellPayload Loot (Maybe Float) (List (Maybe Float)) (List Int)
| BuyPayload Loot (Maybe Float) (List Float) | BuyPayload Loot (Maybe Float) (List (Maybe Float))
| GrabPayload Loot | GrabPayload Loot
| AddPayload String 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 : RequestData -> E.Value
buildPayload data = buildPayload data =
case data of case data of
BuyPayload items _ _ -> BuyPayload items gMod iMods ->
E.object E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) ) [ ( "items", itemsWithMods items iMods )
, ( "global_mod", E.null ) , ( "global_mod"
, case gMod of
Nothing ->
E.null
Just f ->
E.float f
)
] ]
SellPayload items _ _ _ -> SellPayload items gMod iMods players ->
E.object E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) ) [ ( "items", itemsWithMods items iMods )
, ( "global_mod", E.null ) , ( "global_mod"
, ( "players", E.null ) , 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 -- API expects the list of claimed items ids

View File

@@ -12,6 +12,7 @@ import Api
, confirmAction , confirmAction
) )
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Dict exposing (Dict)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (onCheck, onClick, onInput) import Html.Events exposing (onCheck, onClick, onInput)
@@ -31,6 +32,9 @@ type alias State =
, error : Maybe String , error : Maybe String
, notification : Maybe String , notification : Maybe String
-- Buy/Sell loot
, priceModifiers : Dict Int Int
-- AddLoot -- AddLoot
, showModal : Bool , showModal : Bool
, autoComplete : Loot , autoComplete : Loot
@@ -69,6 +73,7 @@ init (Player navKey playerId) =
View View
Nothing Nothing
Nothing Nothing
Dict.empty
False False
[] []
Nothing Nothing
@@ -274,6 +279,10 @@ showWealthField name value =
-- VIEW -- VIEW
type alias ItemRenderer =
Item -> List (Html Msg)
view : Model -> List (Html Msg) view : Model -> List (Html Msg)
view model = view model =
let let
@@ -303,23 +312,83 @@ view model =
canSelect = canSelect =
canSelectIn model.state.mode canSelectIn model.state.mode
rowRenderer = rowRenderer item =
case model.state.mode of case model.state.mode of
View -> View ->
case model.shown of case model.shown of
-- Claim controls for Group chest
GroupLoot -> GroupLoot ->
let let
isClaimed = isClaimed =
itemInClaims model.claims itemInClaims model.claims
in in
-- Claim controls for Group chest case isClaimed item of
Just (claimedItemRenderer isClaimed) True ->
[ renderIcon
{ icon = "fas fa-praying-hands"
, size = "small"
, ratio = "1x"
}
]
False ->
[]
_ -> _ ->
Nothing []
activeMode -> Buy ->
Just (rowRendererForMode activeMode) 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 = filteredItems =
shownItems shownItems
@@ -380,12 +449,12 @@ view model =
-- VIEW LOOT -- VIEW LOOT
viewLoot : Maybe (Item -> Html Msg) -> Bool -> (Item -> Bool) -> Loot -> Html Msg viewLoot : ItemRenderer -> Bool -> (Item -> Bool) -> Loot -> Html Msg
viewLoot maybeRowRenderer canSelect isSelected items = viewLoot rowRenderer canSelect isSelected items =
table [ class "table is-fullwidth is-striped is-hoverable" ] table [ class "table is-fullwidth is-striped is-hoverable" ]
[ thead [ class "table-header" ] [ thead [ class "table-header" ]
[ th [] [ text "Nom" ] ] [ 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 -- Renderers : Item -> Html Msg
claimedItemRenderer : (Item -> Bool) -> Item -> Html Msg viewPriceWithModApplied : Maybe Float -> Float -> Html Msg
claimedItemRenderer isClaimed item = viewPriceWithModApplied maybeMod basePrice =
case isClaimed item of case maybeMod of
True -> Just mod ->
renderIcon p [ class "level-item has-text-weight-bold" ]
{ icon = "fas fa-praying-hands" [ (Debug.log "withMod" (String.fromFloat (basePrice * mod)) ++ "po")
, size = "small" |> text
, ratio = "1x" ]
}
False -> Nothing ->
text "" p [ class "level-item" ] [ (String.fromFloat basePrice ++ "po") |> text ]
rowRendererForMode : ActionMode -> Item -> Html Msg viewPriceModifier : Int -> String -> Html Msg
rowRendererForMode mode item = viewPriceModifier id modValue =
case mode of div [ class "level-item field has-addons" ]
Buy -> [ div [ class "control has-icons-left" ]
p [ class "level-item" ] [ text (String.fromInt item.base_price ++ "po") ] [ input
[ type_ "number"
Sell -> , value modValue
p [ class "level-item" ] [ text (String.fromFloat (toFloat item.base_price / 2) ++ "po") ] , class "input is-small"
, size 3
Grab -> , style "width" "6em"
p [ class "level-item" ] [ text "Grab" ] , Html.Attributes.min "-50"
, Html.Attributes.max "50"
Add -> , step "5"
p [ class "level-item" ] [ text <| "Valeur : " ++ String.fromInt item.base_price ++ "po" ] , onInput (PriceModifierChanged id)
]
View -> []
text "" , 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 = viewItemTableRow isSelected canSelect rowRenderer item =
let let
rightLevel = rightLevel =
div [ class "level-right" ] div [ class "level-right" ] <|
[ case rowRenderer of (if canSelect then
Just render ->
render item
Nothing ->
text ""
, if canSelect then
input input
[ class "checkbox level-item" [ class "checkbox level-item"
, type_ "checkbox" , type_ "checkbox"
@@ -465,9 +534,10 @@ viewItemTableRow isSelected canSelect rowRenderer item =
] ]
[] []
else else
text "" text ""
] )
:: rowRenderer item
in in
tr [ classList [ ( "is-selected", isSelected item ) ] ] tr [ classList [ ( "is-selected", isSelected item ) ] ]
[ td [] [ td []
@@ -756,6 +826,7 @@ type Msg
| FromListChanged String | FromListChanged String
| FromListConfirmed | FromListConfirmed
| NewItemsFromList Loot (Maybe String) | NewItemsFromList Loot (Maybe String)
| PriceModifierChanged Int String
insensitiveContains : String -> String -> Bool insensitiveContains : String -> String -> Bool
@@ -766,6 +837,30 @@ insensitiveContains substring string =
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
update msg model = update msg model =
case msg of 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 -> SwitchMenuOpen ->
let let
state = state =
@@ -996,10 +1091,28 @@ update msg model =
(selectContent model) (selectContent model)
Buy -> 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 -> 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 -> Grab ->
Just <| Api.GrabPayload items Just <| Api.GrabPayload items