works on claims

This commit is contained in:
2019-11-18 15:46:12 +01:00
parent ebdba97d1e
commit 44e44c70bf
2 changed files with 187 additions and 139 deletions

View File

@@ -14,7 +14,7 @@ module Api exposing
, blankPlayer , blankPlayer
, checkList , checkList
, confirmAction , confirmAction
, fetchClaims , fetchClaimsOf
, fetchLoot , fetchLoot
, fetchPlayer , fetchPlayer
) )
@@ -116,10 +116,20 @@ claimDecoder =
(D.field "loot_id" int) (D.field "loot_id" int)
fetchClaims : (Result Http.Error Claims -> msg) -> Int -> Cmd msg fetchClaimsOf : (Result Http.Error Claims -> msg) -> Int -> Cmd msg
fetchClaims toMsg playerId = fetchClaimsOf toMsg playerId =
let
url =
case playerId of
-- The 'group' need to see all claims
0 ->
"http://localhost:8088/api/claims"
id ->
"http://localhost:8088/api/players/" ++ String.fromInt playerId ++ "/claims"
in
Http.get Http.get
{ url = "http://localhost:8088/api/claims" -- TODO: ++ playerId { url = url
, expect = , expect =
valueDecoder (D.list claimDecoder) valueDecoder (D.list claimDecoder)
|> Http.expectJson toMsg |> Http.expectJson toMsg
@@ -197,7 +207,7 @@ fetchLoot toMsg dest =
"http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot" "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot"
OfShop -> OfShop ->
"http://localhost:8088/api/items" "http://localhost:8088/api/shop"
OfGroup -> OfGroup ->
"http://localhost:8088/api/players/0/loot" "http://localhost:8088/api/players/0/loot"

View File

@@ -42,6 +42,7 @@ type alias State =
, sourceName : Maybe String , sourceName : Maybe String
, itemList : Maybe (List String) , itemList : Maybe (List String)
-- , inventoryItems : Loot
-- Fetched on init -- Fetched on init
, player : Api.Player , player : Api.Player
, playerLoot : Loot , playerLoot : Loot
@@ -91,7 +92,7 @@ init (Player navKey playerId) =
[] []
, Cmd.batch , Cmd.batch
[ Api.fetchPlayer GotPlayer playerId [ Api.fetchPlayer GotPlayer playerId
, Api.fetchClaims GotClaims playerId , Api.fetchClaimsOf GotClaims playerId
, Api.fetchLoot GotLoot (Api.OfPlayer playerId) , Api.fetchLoot GotLoot (Api.OfPlayer playerId)
, Api.fetchLoot GotLoot Api.OfGroup , Api.fetchLoot GotLoot Api.OfGroup
, Api.fetchLoot GotLoot Api.OfShop , Api.fetchLoot GotLoot Api.OfShop
@@ -233,12 +234,11 @@ viewPlayerBar player actionControls =
([ div [ class "level-item" ] ([ div [ class "level-item" ]
[ p [ class "title is-3" ] [ text player.name ] ] [ p [ class "title is-3" ] [ text player.name ] ]
, div [ class "level-item" ] , div [ class "level-item" ]
[ span [ class "icon is-large" ] [ span [ class "icon is-large" ] [ i [ class "fas fa-2x fa-piggy-bank" ] [] ]
[ i [ class "fas fa-2x fa-piggy-bank" ] [] ] , span [ class "icon" ] [ i [ class "fas fa-tools" ] [] ]
] ]
] ]
++ viewWealth player.wealth ++ viewWealth player.wealth
++ [ span [ class "icon has-text-danger" ] [ i [ class "fas fa-tools" ] [] ] ]
++ (if player.debt > 0 then ++ (if player.debt > 0 then
[ div [ class "level-item" ] [ div [ class "level-item" ]
[ p [ class "heading is-size-4 has-text-danger" ] [ p [ class "heading is-size-4 has-text-danger" ]
@@ -256,6 +256,12 @@ viewPlayerBar player actionControls =
] ]
viewUpdateWealth =
[ input [ class "level-item" ] []
, button [ class "level-item button" ] [ text "Ok" ]
]
viewWealth : Wealth -> List (Html Msg) viewWealth : Wealth -> List (Html Msg)
viewWealth wealth = viewWealth wealth =
[ showWealthField "pp" <| String.fromInt wealth.pp [ showWealthField "pp" <| String.fromInt wealth.pp
@@ -315,9 +321,27 @@ view model =
rowRenderer item = rowRenderer item =
case model.state.mode of case model.state.mode of
View -> View ->
case model.shown of case ( model.state.player.id, model.shown ) of
-- Claim controls for Group chest ( 0, PlayerLoot ) ->
GroupLoot -> -- The group is viewing its chest
let
isClaimed =
itemInClaims model.claims
in
case isClaimed item of
True ->
[ renderIcon
{ icon = "fas fa-praying-hands"
, size = "small"
, ratio = "1x"
}
]
False ->
[]
( _, GroupLoot ) ->
-- A player is viewing group chest
let let
isClaimed = isClaimed =
itemInClaims model.claims itemInClaims model.claims
@@ -404,7 +428,7 @@ view model =
[ class "section" ] [ class "section" ]
(case model.state.mode of (case model.state.mode of
Add -> Add ->
[ viewAddLoot model [ Html.map AddMsg (viewAddLoot model)
, viewLoot rowRenderer canSelect isSelected shownItems , viewLoot rowRenderer canSelect isSelected shownItems
] ]
@@ -571,7 +595,7 @@ fromListModal isActive itemList =
] ]
viewCompletionDropdown : Bool -> Loot -> Html Msg viewCompletionDropdown : Bool -> Loot -> Html AddMsg
viewCompletionDropdown shown results = viewCompletionDropdown shown results =
div div
[ class "dropdown" [ class "dropdown"
@@ -593,7 +617,7 @@ viewCompletionDropdown shown results =
] ]
viewAddLoot : Model -> Html Msg viewAddLoot : Model -> Html AddMsg
viewAddLoot model = viewAddLoot model =
let let
autoResults = autoResults =
@@ -802,22 +826,8 @@ viewControls mode content =
-- UPDATE -- UPDATE
type Msg type AddMsg
= ApiMsg Api.Msg = NewItemAdded Item
| ClearNotification
| SwitchMenuOpen
| SetContent ChestContent
| SetSelection (Maybe Selection)
| SwitchSelectionState Int
| GotLoot Api.ToChest (HttpResult Loot)
| GotClaims (HttpResult Claims)
| GotPlayer (HttpResult Api.Player)
| SearchTextChanged String
| ModeSwitched ActionMode
| OnModeEnter ActionMode
| OnModeExit ActionMode
| ConfirmAction
| NewItemAdded Item
| NewItemNameChanged String | NewItemNameChanged String
| NewItemPriceChanged String | NewItemPriceChanged String
| SourceNameChanged String | SourceNameChanged String
@@ -826,6 +836,29 @@ type Msg
| FromListChanged String | FromListChanged String
| FromListConfirmed | FromListConfirmed
| NewItemsFromList Loot (Maybe String) | NewItemsFromList Loot (Maybe String)
type Msg
= ApiMsg Api.Msg
| GotLoot Api.ToChest (HttpResult Loot)
| GotClaims (HttpResult Claims)
| GotPlayer (HttpResult Api.Player)
-- Chest UI
| ClearNotification
| SwitchMenuOpen
| SetContent ChestContent
| SearchTextChanged String
-- Selection
| SetSelection (Maybe Selection)
| SwitchSelectionState Int
-- Action modes
| ModeSwitched ActionMode
| OnModeEnter ActionMode
| OnModeExit ActionMode
| ConfirmAction
-- Add loot
| AddMsg AddMsg
-- Buy/Sell modes
| PriceModifierChanged Int String | PriceModifierChanged Int String
@@ -870,121 +903,126 @@ update msg model =
, Cmd.none , Cmd.none
) )
NewItemsFromList newLoot maybeErrors -> AddMsg addMsg ->
let case addMsg of
state = NewItemsFromList newLoot maybeErrors ->
model.state
error =
case maybeErrors of
Just errors ->
(String.lines errors
|> String.join ""
)
++ "n'ont pas pu être ajoutés.\n Faites le manuellement !"
|> Just
Nothing ->
Nothing
in
( { model
| state =
{ state
| itemList = Nothing
, newLoot = newLoot ++ model.state.newLoot
, error = error
}
}
, Cmd.none
)
FromListChanged newText ->
let
state =
model.state
itemList =
String.lines newText
in
( { model | state = { state | itemList = Just itemList } }
, Cmd.none
)
FromListConfirmed ->
let
state =
model.state
itemList =
Maybe.withDefault [] model.state.itemList
in
( { model | state = { state | showModal = False } }, Api.checkList NewItemsFromList itemList )
OpenModal ->
let
state =
model.state
in
( { model | state = { state | showModal = True } }, Cmd.none )
NewItemAdded item ->
let
state =
model.state
in
( { model | state = { state | newLoot = item :: state.newLoot } }, Cmd.none )
SetNewItem item ->
let
state =
model.state
in
( { model | state = { state | newItem = Just item } }, Cmd.none )
SourceNameChanged name ->
let
state =
model.state
in
( { model | state = { state | sourceName = Just name } }
, Cmd.none
)
NewItemPriceChanged price ->
case String.toInt price of
Just newPrice ->
let let
newItem = state =
case model.state.newItem of model.state
Just item ->
{ item | base_price = newPrice } error =
case maybeErrors of
Just errors ->
(String.lines errors
|> String.join ""
)
++ "n'ont pas pu être ajoutés.\n Faites le manuellement !"
|> Just
Nothing -> Nothing ->
Item 0 "" newPrice Nothing
in in
update (SetNewItem newItem) model ( { model
| state =
{ state
| itemList = Nothing
, newLoot = newLoot ++ model.state.newLoot
, error = error
}
}
, Cmd.none
)
Nothing -> FromListChanged newText ->
( model, Cmd.none ) let
state =
model.state
NewItemNameChanged itemName -> itemList =
let String.lines newText
state = in
model.state ( { model | state = { state | itemList = Just itemList } }
, Cmd.none
)
-- Recalculate auto-completion results FromListConfirmed ->
matches = let
if itemName == "" then state =
[] model.state
else itemList =
-- TODO: For now, merchantLoot *IS* the inventory Maybe.withDefault [] model.state.itemList
model.state.merchantLoot in
|> List.filter (\i -> insensitiveContains itemName i.name) ( { model | state = { state | showModal = False } }
in , Cmd.map AddMsg <| Api.checkList NewItemsFromList itemList
{ model | state = { state | autoComplete = matches } } )
-- Update newItem field and erase other (outdated) values
|> update (SetNewItem <| Item 0 itemName 0) OpenModal ->
let
state =
model.state
in
( { model | state = { state | showModal = True } }, Cmd.none )
NewItemAdded item ->
let
state =
model.state
in
( { model | state = { state | newLoot = item :: state.newLoot } }, Cmd.none )
SetNewItem item ->
let
state =
model.state
in
( { model | state = { state | newItem = Just item } }, Cmd.none )
SourceNameChanged name ->
let
state =
model.state
in
( { model | state = { state | sourceName = Just name } }
, Cmd.none
)
NewItemPriceChanged price ->
case String.toInt price of
Just newPrice ->
let
newItem =
case model.state.newItem of
Just item ->
{ item | base_price = newPrice }
Nothing ->
Item 0 "" newPrice
in
update (AddMsg (SetNewItem newItem)) model
Nothing ->
( model, Cmd.none )
NewItemNameChanged itemName ->
let
state =
model.state
-- Recalculate auto-completion results
matches =
if itemName == "" then
[]
else
-- TODO: For now, merchantLoot *IS* the inventory
model.state.merchantLoot
|> List.filter (\i -> insensitiveContains itemName i.name)
in
{ model | state = { state | autoComplete = matches } }
-- Update newItem field and erase other (outdated) values
|> update
(AddMsg (SetNewItem <| Item 0 itemName 0))
ApiMsg apiMsg -> ApiMsg apiMsg ->
case apiMsg of case apiMsg of
@@ -1051,7 +1089,7 @@ update msg model =
model model
OnModeExit mode -> OnModeExit mode ->
if mode == Add then if mode == Add || mode == Buy then
-- Redirect to PlayerLoot view -- Redirect to PlayerLoot view
( model, Nav.pushUrl model.navKey "/" ) ( model, Nav.pushUrl model.navKey "/" )