From 44e44c70bfca58dc1f2f7ffc9a70991b78f4d958 Mon Sep 17 00:00:00 2001 From: Artus Date: Mon, 18 Nov 2019 15:46:12 +0100 Subject: [PATCH] works on claims --- src/Api.elm | 20 ++- src/Page/Chest.elm | 306 +++++++++++++++++++++++++-------------------- 2 files changed, 187 insertions(+), 139 deletions(-) diff --git a/src/Api.elm b/src/Api.elm index 196e17b..e1f036d 100644 --- a/src/Api.elm +++ b/src/Api.elm @@ -14,7 +14,7 @@ module Api exposing , blankPlayer , checkList , confirmAction - , fetchClaims + , fetchClaimsOf , fetchLoot , fetchPlayer ) @@ -116,10 +116,20 @@ claimDecoder = (D.field "loot_id" int) -fetchClaims : (Result Http.Error Claims -> msg) -> Int -> Cmd msg -fetchClaims toMsg playerId = +fetchClaimsOf : (Result Http.Error Claims -> msg) -> Int -> Cmd msg +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 - { url = "http://localhost:8088/api/claims" -- TODO: ++ playerId + { url = url , expect = valueDecoder (D.list claimDecoder) |> Http.expectJson toMsg @@ -197,7 +207,7 @@ fetchLoot toMsg dest = "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot" OfShop -> - "http://localhost:8088/api/items" + "http://localhost:8088/api/shop" OfGroup -> "http://localhost:8088/api/players/0/loot" diff --git a/src/Page/Chest.elm b/src/Page/Chest.elm index cd641af..117c4b4 100644 --- a/src/Page/Chest.elm +++ b/src/Page/Chest.elm @@ -42,6 +42,7 @@ type alias State = , sourceName : Maybe String , itemList : Maybe (List String) + -- , inventoryItems : Loot -- Fetched on init , player : Api.Player , playerLoot : Loot @@ -91,7 +92,7 @@ init (Player navKey playerId) = [] , Cmd.batch [ Api.fetchPlayer GotPlayer playerId - , Api.fetchClaims GotClaims playerId + , Api.fetchClaimsOf GotClaims playerId , Api.fetchLoot GotLoot (Api.OfPlayer playerId) , Api.fetchLoot GotLoot Api.OfGroup , Api.fetchLoot GotLoot Api.OfShop @@ -233,12 +234,11 @@ viewPlayerBar player actionControls = ([ div [ class "level-item" ] [ p [ class "title is-3" ] [ text player.name ] ] , div [ class "level-item" ] - [ span [ class "icon is-large" ] - [ i [ class "fas fa-2x fa-piggy-bank" ] [] ] + [ span [ class "icon is-large" ] [ i [ class "fas fa-2x fa-piggy-bank" ] [] ] + , span [ class "icon" ] [ i [ class "fas fa-tools" ] [] ] ] ] ++ viewWealth player.wealth - ++ [ span [ class "icon has-text-danger" ] [ i [ class "fas fa-tools" ] [] ] ] ++ (if player.debt > 0 then [ div [ class "level-item" ] [ 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 = [ showWealthField "pp" <| String.fromInt wealth.pp @@ -315,9 +321,27 @@ view model = rowRenderer item = case model.state.mode of View -> - case model.shown of - -- Claim controls for Group chest - GroupLoot -> + case ( model.state.player.id, model.shown ) of + ( 0, PlayerLoot ) -> + -- 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 isClaimed = itemInClaims model.claims @@ -404,7 +428,7 @@ view model = [ class "section" ] (case model.state.mode of Add -> - [ viewAddLoot model + [ Html.map AddMsg (viewAddLoot model) , 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 = div [ class "dropdown" @@ -593,7 +617,7 @@ viewCompletionDropdown shown results = ] -viewAddLoot : Model -> Html Msg +viewAddLoot : Model -> Html AddMsg viewAddLoot model = let autoResults = @@ -802,22 +826,8 @@ viewControls mode content = -- UPDATE -type Msg - = ApiMsg Api.Msg - | 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 +type AddMsg + = NewItemAdded Item | NewItemNameChanged String | NewItemPriceChanged String | SourceNameChanged String @@ -826,6 +836,29 @@ type Msg | FromListChanged String | FromListConfirmed | 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 @@ -870,121 +903,126 @@ update msg model = , Cmd.none ) - NewItemsFromList newLoot maybeErrors -> - let - state = - 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 -> + AddMsg addMsg -> + case addMsg of + NewItemsFromList newLoot maybeErrors -> let - newItem = - case model.state.newItem of - Just item -> - { item | base_price = newPrice } + state = + 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 -> - Item 0 "" newPrice + Nothing in - update (SetNewItem newItem) model + ( { model + | state = + { state + | itemList = Nothing + , newLoot = newLoot ++ model.state.newLoot + , error = error + } + } + , Cmd.none + ) - Nothing -> - ( model, Cmd.none ) + FromListChanged newText -> + let + state = + model.state - NewItemNameChanged itemName -> - let - state = - model.state + itemList = + String.lines newText + in + ( { model | state = { state | itemList = Just itemList } } + , Cmd.none + ) - -- Recalculate auto-completion results - matches = - if itemName == "" then - [] + FromListConfirmed -> + let + state = + model.state - 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 (SetNewItem <| Item 0 itemName 0) + itemList = + Maybe.withDefault [] model.state.itemList + in + ( { model | state = { state | showModal = False } } + , Cmd.map AddMsg <| 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 + 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 -> case apiMsg of @@ -1051,7 +1089,7 @@ update msg model = model OnModeExit mode -> - if mode == Add then + if mode == Add || mode == Buy then -- Redirect to PlayerLoot view ( model, Nav.pushUrl model.navKey "/" )