works on claims
This commit is contained in:
93
src/Main.elm
93
src/Main.elm
@@ -44,6 +44,8 @@ type alias State =
|
||||
type alias Model =
|
||||
{ state : State
|
||||
, player: Player
|
||||
, claims : Claims
|
||||
, notification : Maybe String
|
||||
, loot: Maybe Loot
|
||||
, groupLoot : Maybe Loot
|
||||
, merchantItems : Maybe Loot
|
||||
@@ -56,7 +58,7 @@ init flags url key =
|
||||
Just r -> r
|
||||
Nothing -> PlayerChest
|
||||
in
|
||||
( Model (State key route "" False Nothing Nothing) blankPlayer Nothing Nothing Nothing, fetchInitialData 0)
|
||||
( Model (State key route "" False Nothing Nothing) blankPlayer [] Nothing Nothing Nothing Nothing, fetchInitialData 0)
|
||||
|
||||
|
||||
fetchInitialData : Int -> Cmd Msg
|
||||
@@ -78,7 +80,7 @@ blankPlayer =
|
||||
Player 0 "Loading" 0 (Wealth 0 0 0 0)
|
||||
|
||||
initPlayer id =
|
||||
Cmd.batch [fetchPlayer id, fetchLoot id]
|
||||
Cmd.batch [fetchPlayer id, fetchLoot id, fetchClaims id]
|
||||
|
||||
fetchPlayer : Int -> Cmd Msg
|
||||
fetchPlayer id =
|
||||
@@ -148,6 +150,28 @@ type ToChest
|
||||
= OfPlayer
|
||||
| OfGroup
|
||||
| OfShop
|
||||
|
||||
type alias Claims = List Claim
|
||||
|
||||
type alias Claim =
|
||||
{ id: Int
|
||||
, player_id: Int
|
||||
, loot_id: Int
|
||||
}
|
||||
|
||||
claimDecoder =
|
||||
Json.Decode.map3 Claim
|
||||
(field "id" int)
|
||||
(field "player_id" int)
|
||||
(field "loot_id" int)
|
||||
|
||||
fetchClaims : Int -> Cmd Msg
|
||||
fetchClaims playerId =
|
||||
Http.get
|
||||
{ url = "http://localhost:8088/api/claims"
|
||||
, expect = valueDecoder (Json.Decode.list claimDecoder)
|
||||
|> Http.expectJson (GotClaims playerId)
|
||||
}
|
||||
-- API Response
|
||||
--
|
||||
valueDecoder : Decoder a -> Decoder a
|
||||
@@ -170,12 +194,14 @@ type Msg
|
||||
| UrlChanged Url.Url
|
||||
| PlayerChanged Int
|
||||
| GotPlayer (HttpResult Player)
|
||||
| GotClaims Int (HttpResult Claims)
|
||||
| GotLoot ToChest (HttpResult Loot)
|
||||
| GotActionResult (HttpResult ApiResponse)
|
||||
| LootViewItemSwitched Int
|
||||
| ModeSwitched (Maybe ViewMode)
|
||||
| ConfirmAction
|
||||
| UndoLastAction
|
||||
| GotActionResult (HttpResult ApiResponse)
|
||||
| ClearNotification
|
||||
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
@@ -220,6 +246,11 @@ update msg model =
|
||||
, Cmd.none
|
||||
)
|
||||
|
||||
GotClaims id result ->
|
||||
case result of
|
||||
Ok claims -> ( { model | claims = List.filter (\c -> c.player_id == id) claims}, Cmd.none )
|
||||
Err error -> ( setError ("Fetching claims..." ++ Debug.toString error) model, Cmd.none)
|
||||
|
||||
GotLoot dest result ->
|
||||
case result of
|
||||
Ok loot ->
|
||||
@@ -250,9 +281,16 @@ update msg model =
|
||||
( { model | state =
|
||||
{ state | activeMode = newMode
|
||||
, selection = case newMode of
|
||||
Nothing -> Nothing
|
||||
Just Grab -> Just (Set.fromList [34, 38])
|
||||
Just others -> Just Set.empty
|
||||
Nothing ->
|
||||
Nothing
|
||||
|
||||
Just Grab -> -- Currently claimed object are initially selected
|
||||
Just ( Set.fromList
|
||||
<| List.map (\c -> c.loot_id) model.claims
|
||||
)
|
||||
|
||||
Just others ->
|
||||
Just Set.empty
|
||||
}}
|
||||
, Cmd.none )
|
||||
|
||||
@@ -280,11 +318,20 @@ update msg model =
|
||||
Ok result ->
|
||||
let
|
||||
updates = Maybe.withDefault [] result.updates
|
||||
notification = result.notification
|
||||
errors = Maybe.withDefault "" result.errors
|
||||
in
|
||||
List.foldl applyUpdate model updates
|
||||
|> setNotification notification
|
||||
|> setError errors
|
||||
|> update (ModeSwitched Nothing)
|
||||
Err r -> (setError (Debug.toString r) model, Cmd.none)
|
||||
ClearNotification ->
|
||||
( { model | notification = Nothing }, Cmd.none )
|
||||
|
||||
setNotification : Maybe String -> Model -> Model
|
||||
setNotification notification model =
|
||||
{ model | notification = notification }
|
||||
|
||||
targetItemsFor : ViewMode -> Model -> List Item
|
||||
targetItemsFor mode model =
|
||||
@@ -511,7 +558,7 @@ view model =
|
||||
:: controlsWhenRoute model.state.route
|
||||
-- Claim controls for Group chest
|
||||
, case model.state.route of
|
||||
GroupLoot -> Just renderId
|
||||
GroupLoot -> Just (renderIfClaimed <| itemInClaims model.claims)
|
||||
_ -> Nothing
|
||||
)
|
||||
|
||||
@@ -523,7 +570,7 @@ view model =
|
||||
{ title = "Loot-a-lot in ELM"
|
||||
, body =
|
||||
[ viewHeaderBar model
|
||||
, viewPlayerBar model.player headerControls
|
||||
, viewPlayerBar model.player model.notification headerControls
|
||||
, article [class "section container"]
|
||||
[ p [class "heading"] [text header]
|
||||
, viewSearchBar
|
||||
@@ -534,6 +581,14 @@ view model =
|
||||
]
|
||||
}
|
||||
|
||||
viewNotification : Maybe String -> Html Msg
|
||||
viewNotification notification =
|
||||
case notification of
|
||||
Just t -> div [ class "notification is-success is-marginless"]
|
||||
[ button [class "delete", onClick ClearNotification ] []
|
||||
, text t ]
|
||||
Nothing -> text ""
|
||||
|
||||
-- LOOT Views
|
||||
|
||||
itemInSelection : Maybe Selection -> Item -> Bool
|
||||
@@ -541,8 +596,15 @@ itemInSelection selection item =
|
||||
Maybe.map (Set.member item.id) selection
|
||||
|> Maybe.withDefault False
|
||||
|
||||
renderId item =
|
||||
p [] [text <| String.fromInt item.id]
|
||||
itemInClaims : List Claim -> Item -> Bool
|
||||
itemInClaims claims item =
|
||||
List.any (\c -> c.loot_id == item.id) claims
|
||||
|
||||
renderIfClaimed : (Item -> Bool) -> Item -> Html Msg
|
||||
renderIfClaimed isClaimed item =
|
||||
case isClaimed item of
|
||||
True -> renderIcon "fas fa-praying-hands" "1x"
|
||||
False -> text ""
|
||||
|
||||
viewChest : (Item -> Bool) -> Maybe (Item -> Html Msg) -> Loot -> Html Msg
|
||||
viewChest isSelected rowControls items =
|
||||
@@ -598,15 +660,17 @@ viewDebugSection model =
|
||||
, p [class "panel-block"] [text ("Route : " ++ Debug.toString model.state.route)]
|
||||
, p [class "panel-block"] [text ("Active Mode : " ++ Debug.toString model.state.activeMode)]
|
||||
, p [class "panel-block"] [text ("Selection : " ++ Debug.toString model.state.selection)]
|
||||
, p [class "panel-block"] [text ("Claims : " ++ Debug.toString model.claims)]
|
||||
, p [] debugSandbox
|
||||
]
|
||||
|
||||
stackedIcon name =
|
||||
span [class "icon is-large has-text-dark"]
|
||||
span [class "icon is-medium"]
|
||||
[ span [ class "fa-stack" ]
|
||||
[ i [ class "fas fa-circle fa-stack-2x" ] []
|
||||
, i [ class (name ++ " fa-inverse fa-stack-1x") ] []
|
||||
, text name ]
|
||||
, text ""
|
||||
]
|
||||
]
|
||||
|
||||
debugSandbox =
|
||||
@@ -678,8 +742,8 @@ viewHeaderBar model =
|
||||
|
||||
-- PLAYER BAR
|
||||
|
||||
viewPlayerBar : Player -> List (Html Msg)-> Html Msg
|
||||
viewPlayerBar player actionControls =
|
||||
viewPlayerBar : Player -> Maybe String -> List (Html Msg)-> Html Msg
|
||||
viewPlayerBar player notification actionControls =
|
||||
section [ class "level container is-mobile box" ]
|
||||
[ div [class "level-left"]
|
||||
([div [ class "level-item" ]
|
||||
@@ -696,6 +760,7 @@ viewPlayerBar player actionControls =
|
||||
[]
|
||||
)
|
||||
)
|
||||
, viewNotification notification
|
||||
, div [class "level-right"] actionControls
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user