From 52b66eed970153a7efd7a232efae0d0c130ba81e Mon Sep 17 00:00:00 2001 From: Artus Date: Tue, 5 Nov 2019 15:06:55 +0100 Subject: [PATCH] works on claims --- main.js | 237 +++++++++++++++++++++++++++++++++++++++++---------- src/Main.elm | 93 +++++++++++++++++--- 2 files changed, 271 insertions(+), 59 deletions(-) diff --git a/main.js b/main.js index 078834f..bc03ae7 100644 --- a/main.js +++ b/main.js @@ -5334,9 +5334,9 @@ var $elm$core$Task$perform = F2( A2($elm$core$Task$map, toMessage, task))); }); var $elm$browser$Browser$application = _Browser_application; -var $author$project$Main$Model = F5( - function (state, player, loot, groupLoot, merchantItems) { - return {groupLoot: groupLoot, loot: loot, merchantItems: merchantItems, player: player, state: state}; +var $author$project$Main$Model = F7( + function (state, player, claims, notification, loot, groupLoot, merchantItems) { + return {claims: claims, groupLoot: groupLoot, loot: loot, merchantItems: merchantItems, notification: notification, player: player, state: state}; }); var $author$project$Main$PlayerChest = {$: 'PlayerChest'}; var $author$project$Main$State = F6( @@ -6186,6 +6186,31 @@ var $author$project$Main$fetchShopInventory = $elm$http$Http$get( $author$project$Main$valueDecoder($author$project$Main$lootDecoder)), url: 'http://localhost:8088/api/items' }); +var $author$project$Main$GotClaims = F2( + function (a, b) { + return {$: 'GotClaims', a: a, b: b}; + }); +var $author$project$Main$Claim = F3( + function (id, player_id, loot_id) { + return {id: id, loot_id: loot_id, player_id: player_id}; + }); +var $author$project$Main$claimDecoder = A4( + $elm$json$Json$Decode$map3, + $author$project$Main$Claim, + A2($elm$json$Json$Decode$field, 'id', $elm$json$Json$Decode$int), + A2($elm$json$Json$Decode$field, 'player_id', $elm$json$Json$Decode$int), + A2($elm$json$Json$Decode$field, 'loot_id', $elm$json$Json$Decode$int)); +var $author$project$Main$fetchClaims = function (playerId) { + return $elm$http$Http$get( + { + expect: A2( + $elm$http$Http$expectJson, + $author$project$Main$GotClaims(playerId), + $author$project$Main$valueDecoder( + $elm$json$Json$Decode$list($author$project$Main$claimDecoder))), + url: 'http://localhost:8088/api/claims' + }); +}; var $author$project$Main$OfPlayer = {$: 'OfPlayer'}; var $author$project$Main$fetchLoot = function (id) { return $elm$http$Http$get( @@ -6230,7 +6255,8 @@ var $author$project$Main$initPlayer = function (id) { _List_fromArray( [ $author$project$Main$fetchPlayer(id), - $author$project$Main$fetchLoot(id) + $author$project$Main$fetchLoot(id), + $author$project$Main$fetchClaims(id) ])); }; var $author$project$Main$fetchInitialData = function (playerId) { @@ -6487,10 +6513,12 @@ var $author$project$Main$init = F3( } }(); return _Utils_Tuple2( - A5( + A7( $author$project$Main$Model, A6($author$project$Main$State, key, route, '', false, $elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing), $author$project$Main$blankPlayer, + _List_Nil, + $elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing), @@ -6903,6 +6931,12 @@ var $author$project$Main$setError = F2( {error: error}) }); }); +var $author$project$Main$setNotification = F2( + function (notification, model) { + return _Utils_update( + model, + {notification: notification}); + }); var $elm$core$Set$remove = F2( function (key, _v0) { var dict = _v0.a; @@ -7044,6 +7078,32 @@ var $author$project$Main$update = F2( model), $elm$core$Platform$Cmd$none); } + case 'GotClaims': + var id = msg.a; + var result = msg.b; + if (result.$ === 'Ok') { + var claims = result.a; + return _Utils_Tuple2( + _Utils_update( + model, + { + claims: A2( + $elm$core$List$filter, + function (c) { + return _Utils_eq(c.player_id, id); + }, + claims) + }), + $elm$core$Platform$Cmd$none); + } else { + var error = result.a; + return _Utils_Tuple2( + A2( + $author$project$Main$setError, + 'Fetching claims...' + $elm$core$Debug$toString(error), + model), + $elm$core$Platform$Cmd$none); + } case 'GotLoot': var dest = msg.a; var result = msg.b; @@ -7115,11 +7175,15 @@ var $author$project$Main$update = F2( return $elm$core$Maybe$Nothing; } else { if (newMode.a.$ === 'Grab') { - var _v8 = newMode.a; + var _v9 = newMode.a; return $elm$core$Maybe$Just( $elm$core$Set$fromList( - _List_fromArray( - [34, 38]))); + A2( + $elm$core$List$map, + function (c) { + return c.loot_id; + }, + model.claims))); } else { var others = newMode.a; return $elm$core$Maybe$Just($elm$core$Set$empty); @@ -7148,15 +7212,23 @@ var $author$project$Main$update = F2( tracker: $elm$core$Maybe$Nothing, url: 'http://localhost:8088/api/players/' + (playerId + '/events/last') })); - default: + case 'GotActionResult': var response = msg.a; if (response.$ === 'Ok') { var result = response.a; var updates = A2($elm$core$Maybe$withDefault, _List_Nil, result.updates); + var notification = result.notification; + var errors = A2($elm$core$Maybe$withDefault, '', result.errors); return A2( $author$project$Main$update, $author$project$Main$ModeSwitched($elm$core$Maybe$Nothing), - A3($elm$core$List$foldl, $author$project$Main$applyUpdate, model, updates)); + A2( + $author$project$Main$setError, + errors, + A2( + $author$project$Main$setNotification, + notification, + A3($elm$core$List$foldl, $author$project$Main$applyUpdate, model, updates)))); } else { var r = response.a; return _Utils_Tuple2( @@ -7166,6 +7238,12 @@ var $author$project$Main$update = F2( model), $elm$core$Platform$Cmd$none); } + default: + return _Utils_Tuple2( + _Utils_update( + model, + {notification: $elm$core$Maybe$Nothing}), + $elm$core$Platform$Cmd$none); } }); var $author$project$Main$UndoLastAction = {$: 'UndoLastAction'}; @@ -7304,16 +7382,64 @@ var $author$project$Main$controlsWhenRoute = function (route) { } }; var $elm$html$Html$hr = _VirtualDom_node('hr'); -var $author$project$Main$renderId = function (item) { - return A2( - $elm$html$Html$p, - _List_Nil, - _List_fromArray( - [ - $elm$html$Html$text( - $elm$core$String$fromInt(item.id)) - ])); -}; +var $elm$core$List$any = F2( + function (isOkay, list) { + any: + while (true) { + if (!list.b) { + return false; + } else { + var x = list.a; + var xs = list.b; + if (isOkay(x)) { + return true; + } else { + var $temp$isOkay = isOkay, + $temp$list = xs; + isOkay = $temp$isOkay; + list = $temp$list; + continue any; + } + } + } + }); +var $author$project$Main$itemInClaims = F2( + function (claims, item) { + return A2( + $elm$core$List$any, + function (c) { + return _Utils_eq(c.loot_id, item.id); + }, + claims); + }); +var $author$project$Main$renderIcon = F2( + function (name, size) { + return A2( + $elm$html$Html$span, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('icon is-medium') + ]), + _List_fromArray( + [ + A2( + $elm$html$Html$i, + _List_fromArray( + [ + $elm$html$Html$Attributes$class(name + (' fa-' + size)) + ]), + _List_Nil) + ])); + }); +var $author$project$Main$renderIfClaimed = F2( + function (isClaimed, item) { + var _v0 = isClaimed(item); + if (_v0) { + return A2($author$project$Main$renderIcon, 'fas fa-praying-hands', '1x'); + } else { + return $elm$html$Html$text(''); + } + }); var $author$project$Main$LootViewItemSwitched = function (a) { return {$: 'LootViewItemSwitched', a: a}; }; @@ -7563,7 +7689,7 @@ var $author$project$Main$stackedIcon = function (name) { $elm$html$Html$span, _List_fromArray( [ - $elm$html$Html$Attributes$class('icon is-large has-text-dark') + $elm$html$Html$Attributes$class('icon is-medium') ]), _List_fromArray( [ @@ -7589,7 +7715,7 @@ var $author$project$Main$stackedIcon = function (name) { $elm$html$Html$Attributes$class(name + ' fa-inverse fa-stack-1x') ]), _List_Nil), - $elm$html$Html$text(name) + $elm$html$Html$text('') ])) ])); }; @@ -7717,6 +7843,17 @@ var $author$project$Main$viewDebugSection = function (model) { $elm$html$Html$text( 'Selection : ' + $elm$core$Debug$toString(model.state.selection)) ])), + A2( + $elm$html$Html$p, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('panel-block') + ]), + _List_fromArray( + [ + $elm$html$Html$text( + 'Claims : ' + $elm$core$Debug$toString(model.claims)) + ])), A2($elm$html$Html$p, _List_Nil, $author$project$Main$debugSandbox) ])); }; @@ -7735,25 +7872,6 @@ var $elm$html$Html$Attributes$href = function (url) { _VirtualDom_noJavaScriptUri(url)); }; var $elm$html$Html$nav = _VirtualDom_node('nav'); -var $author$project$Main$renderIcon = F2( - function (name, size) { - return A2( - $elm$html$Html$span, - _List_fromArray( - [ - $elm$html$Html$Attributes$class('icon is-medium') - ]), - _List_fromArray( - [ - A2( - $elm$html$Html$i, - _List_fromArray( - [ - $elm$html$Html$Attributes$class(name + (' fa-' + size)) - ]), - _List_Nil) - ])); - }); var $author$project$Main$viewHeaderBar = function (model) { return A2( $elm$html$Html$nav, @@ -7879,6 +7997,32 @@ var $author$project$Main$viewHeaderBar = function (model) { ])) ])); }; +var $author$project$Main$ClearNotification = {$: 'ClearNotification'}; +var $author$project$Main$viewNotification = function (notification) { + if (notification.$ === 'Just') { + var t = notification.a; + return A2( + $elm$html$Html$div, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('notification is-success is-marginless') + ]), + _List_fromArray( + [ + A2( + $elm$html$Html$button, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('delete'), + $elm$html$Html$Events$onClick($author$project$Main$ClearNotification) + ]), + _List_Nil), + $elm$html$Html$text(t) + ])); + } else { + return $elm$html$Html$text(''); + } +}; var $elm$core$String$cons = _String_cons; var $elm$core$String$fromChar = function (_char) { return A2($elm$core$String$cons, _char, ''); @@ -7973,8 +8117,8 @@ var $author$project$Main$viewWealth = function (wealth) { $elm$core$String$fromInt(wealth.cp)) ]); }; -var $author$project$Main$viewPlayerBar = F2( - function (player, actionControls) { +var $author$project$Main$viewPlayerBar = F3( + function (player, notification, actionControls) { return A2( $elm$html$Html$section, _List_fromArray( @@ -8043,6 +8187,7 @@ var $author$project$Main$viewPlayerBar = F2( ])) ])) ]) : _List_Nil))), + $author$project$Main$viewNotification(notification), A2( $elm$html$Html$div, _List_fromArray( @@ -8099,7 +8244,9 @@ var $author$project$Main$view = function (model) { function () { var _v4 = model.state.route; if (_v4.$ === 'GroupLoot') { - return $elm$core$Maybe$Just($author$project$Main$renderId); + return $elm$core$Maybe$Just( + $author$project$Main$renderIfClaimed( + $author$project$Main$itemInClaims(model.claims))); } else { return $elm$core$Maybe$Nothing; } @@ -8112,7 +8259,7 @@ var $author$project$Main$view = function (model) { body: _List_fromArray( [ $author$project$Main$viewHeaderBar(model), - A2($author$project$Main$viewPlayerBar, model.player, headerControls), + A3($author$project$Main$viewPlayerBar, model.player, model.notification, headerControls), A2( $elm$html$Html$article, _List_fromArray( diff --git a/src/Main.elm b/src/Main.elm index aeca8a5..6d1507a 100644 --- a/src/Main.elm +++ b/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 ]