adds admin, pratices ELM refactoring with 'wealth'

This commit is contained in:
2019-11-21 12:11:33 +01:00
parent 27d7ca63b1
commit 75968f73c1
5 changed files with 234 additions and 101 deletions

View File

@@ -8,7 +8,6 @@ import Api
, Item
, Loot
, RequestData(..)
, Wealth
, confirmAction
)
import Browser.Navigation as Nav
@@ -16,8 +15,8 @@ import Dict exposing (Dict)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onCheck, onClick, onInput)
import Page.Chest.Wealth as Wealth
import Route exposing (ChestContent(..))
import Session exposing (Session(..))
import Set exposing (Set)
import Utils exposing (..)
@@ -42,9 +41,6 @@ type alias State =
, itemList : Maybe (List String)
-- , inventoryItems : Loot
, editWealth : Bool
, wealthAmount : String
-- Fetched on init
, player : Api.Player
, playerLoot : Loot
@@ -64,11 +60,12 @@ type alias Model =
, shown : Route.ChestContent
, selection : Maybe Selection
, searchText : String
, wealth : Wealth.Model
, claims : Claims
}
init (Player navKey playerId) =
init navKey playerId =
( Model
navKey
(State
@@ -81,8 +78,6 @@ init (Player navKey playerId) =
Nothing
Nothing
Nothing
False
"0.0"
Api.blankPlayer
[]
[]
@@ -92,6 +87,9 @@ init (Player navKey playerId) =
Route.PlayerLoot
Nothing
""
(Wealth.init
Api.blankPlayer.wealth
)
[]
, Cmd.batch
[ Api.fetchPlayer GotPlayer playerId
@@ -105,7 +103,7 @@ init (Player navKey playerId) =
viewNotification : Model -> Html Msg
viewNotification model =
div []
div [ class "section" ]
[ case model.state.notification of
Just t ->
div [ class "notification is-success" ]
@@ -131,22 +129,13 @@ viewNotification model =
-- PLAYER BAR
viewPlayerBar : Api.Player -> List (Html Msg) -> ( Bool, String ) -> Html Msg
viewPlayerBar player actionControls ( editing, amount ) =
viewPlayerBar : Api.Player -> List (Html Msg) -> Wealth.Model -> Html Msg
viewPlayerBar player actionControls wealthModel =
section [ class "hero is-dark is-bold" ]
[ div [ class "hero-body" ]
[ div [ class "level container is-mobile" ]
[ div [ class "level-left" ]
(div [ class "level-item" ]
[ span [ class "icon is-large" ] [ i [ class "fas fa-2x fa-piggy-bank" ] [] ]
, span [ class "icon", onClick EditWealth ] [ i [ class "fas fa-tools" ] [] ]
]
:: (if editing then
viewUpdateWealth amount
else
viewWealth player.wealth
)
(Wealth.view player.wealth wealthModel
++ (if player.debt > 0 then
[ div [ class "level-item" ]
[ p [ class "heading is-size-4 has-text-danger" ]
@@ -158,46 +147,13 @@ viewPlayerBar player actionControls ( editing, amount ) =
[]
)
)
|> Html.map WealthMsg
, div [ class "level-right" ] actionControls
]
]
]
viewUpdateWealth amount =
let
isAmountValid =
case String.toFloat amount of
Just _ ->
True
Nothing ->
False
in
[ input [ class "level-item", class "input", classList [ ( "is-danger", not isAmountValid ) ], value amount, onInput AmountChanged ] []
, button [ class "level-item button", onClick ConfirmEditWealth ] [ text "Ok" ]
]
viewWealth : Wealth -> List (Html Msg)
viewWealth wealth =
[ showWealthField "pp" <| String.fromInt wealth.pp
, showWealthField "gp" <| String.padLeft 2 '0' <| String.fromInt wealth.gp
, showWealthField "sp" <| String.fromInt wealth.sp
, showWealthField "cp" <| String.fromInt wealth.cp
]
showWealthField : String -> String -> Html Msg
showWealthField name value =
div [ class "level-item" ]
[ p [ class "has-text-right" ]
[ strong [ class "heading is-marginless has-text-white" ] [ text name ]
, span [ class <| "is-size-4" ] [ text value ]
]
]
-- VIEW
@@ -305,10 +261,7 @@ view model =
Dict.get item.id model.state.priceModifiers
in
[ viewPriceWithModApplied
(Debug.log
"maybeMod"
(Maybe.map (\i -> toFloatingMod i) maybeMod)
)
(Maybe.map (\i -> toFloatingMod i) maybeMod)
(toFloat item.base_price / 2)
, if isSelected item then
viewPriceModifier item.id <|
@@ -336,7 +289,7 @@ view model =
|> List.filter
(\i -> String.toLower i.name |> String.contains (String.toLower model.searchText))
in
[ viewPlayerBar model.state.player renderControls ( model.state.editWealth, model.state.wealthAmount )
[ viewPlayerBar model.state.player renderControls model.wealth
, main_
[ class "container" ]
[ viewNotification model
@@ -775,10 +728,14 @@ type Msg
| AddMsg AddMsg
-- Buy/Sell modes
| PriceModifierChanged Int String
-- Edit wealth
| EditWealth
| AmountChanged String
| ConfirmEditWealth
| WealthMsg Wealth.Msg
-- Edit wealth
--| EditWealth
--| AmountChanged String
--| ConfirmEditWealth
insensitiveContains : String -> String -> Bool
@@ -799,28 +756,27 @@ setWealthAmount state amount =
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
EditWealth ->
( { model | state = switchEditWealth model.state }, Cmd.none )
AmountChanged amount ->
( { model | state = setWealthAmount model.state amount }, Cmd.none )
ConfirmEditWealth ->
let
amount =
case String.toFloat model.state.wealthAmount of
WealthMsg wealthMsg ->
case wealthMsg of
Wealth.ConfirmEdit ->
let
amount =
Wealth.editValue model.wealth
in
( { model | wealth = Wealth.update Wealth.QuitEdit model.wealth }
, case amount of
Just a ->
a
Cmd.map ApiMsg <|
Api.confirmAction
(String.fromInt model.state.player.id)
(Api.WealthPayload a)
Nothing ->
0.0
in
( { model | state = setWealthAmount model.state "0" |> switchEditWealth }
, Cmd.map ApiMsg <|
Api.confirmAction
(String.fromInt model.state.player.id)
(Api.WealthPayload amount)
)
Cmd.none
)
_ ->
( { model | wealth = Wealth.update wealthMsg model.wealth }, Cmd.none )
PriceModifierChanged id value ->
let
@@ -1221,7 +1177,7 @@ applyUpdate u model =
| player =
{ player
| wealth =
Wealth
Api.Wealth
(wealth.cp + diff.cp)
(wealth.sp + diff.sp)
(wealth.gp + diff.gp)