adds conflicts information on claims
This commit is contained in:
64
src/Api.elm
64
src/Api.elm
@@ -139,14 +139,16 @@ type alias Claim =
|
||||
{ id : Int
|
||||
, player_id : Int
|
||||
, loot_id : Int
|
||||
, conflicts : Bool
|
||||
}
|
||||
|
||||
|
||||
claimDecoder =
|
||||
D.map3 Claim
|
||||
D.map4 Claim
|
||||
(D.field "id" int)
|
||||
(D.field "player_id" int)
|
||||
(D.field "loot_id" int)
|
||||
(D.succeed False)
|
||||
|
||||
|
||||
|
||||
@@ -432,20 +434,64 @@ getLoot id =
|
||||
}
|
||||
|
||||
|
||||
getClaims id =
|
||||
let
|
||||
path =
|
||||
if id == 0 then
|
||||
"api/claims"
|
||||
|
||||
else
|
||||
"api/players/" ++ String.fromInt id ++ "/claims"
|
||||
-- Updates the conflicts field on every claims
|
||||
|
||||
|
||||
groupByLoot : List Claims -> Claims -> List Claims
|
||||
groupByLoot acc claims =
|
||||
case claims of
|
||||
last :: [] ->
|
||||
groupByLoot ([ last ] :: acc) []
|
||||
|
||||
first :: rest ->
|
||||
let
|
||||
( newGroup, left ) =
|
||||
List.partition (\c -> c.loot_id == first.loot_id) rest
|
||||
in
|
||||
groupByLoot ((first :: newGroup) :: acc) left
|
||||
|
||||
[] ->
|
||||
acc
|
||||
|
||||
|
||||
updateConflicts : Claims -> Claims
|
||||
updateConflicts claims =
|
||||
groupByLoot [] claims
|
||||
|> List.concatMap
|
||||
(\group ->
|
||||
let
|
||||
_ =
|
||||
Debug.log "updating group" group
|
||||
in
|
||||
case group of
|
||||
[] ->
|
||||
[]
|
||||
|
||||
[ c ] ->
|
||||
[ { c | conflicts = False } ]
|
||||
|
||||
xs ->
|
||||
List.map (\c -> { c | conflicts = True }) xs
|
||||
)
|
||||
|
||||
|
||||
parseClaims : Int -> Claims -> Task x Claims
|
||||
parseClaims id claims =
|
||||
-- Filter claims relevant to player
|
||||
Task.succeed <|
|
||||
List.filter (\claim -> id == 0 || claim.player_id == id) <|
|
||||
updateConflicts claims
|
||||
|
||||
|
||||
getClaims : Int -> Task Http.Error Claims
|
||||
getClaims id =
|
||||
send
|
||||
{ method = "GET"
|
||||
, path = path
|
||||
, path = "api/claims"
|
||||
, decoder = valueDecoder (D.list claimDecoder)
|
||||
}
|
||||
|> Task.andThen (parseClaims id)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -94,3 +94,7 @@ levelItem =
|
||||
|
||||
isInfo =
|
||||
class "is-info"
|
||||
|
||||
|
||||
isError =
|
||||
class "is-error"
|
||||
|
||||
@@ -167,21 +167,37 @@ update msg model =
|
||||
IntoViewWithClaims claims ->
|
||||
let
|
||||
isClaimed item =
|
||||
List.any (\claim_ -> claim_.loot_id == item.id) claims
|
||||
case List.filter (\c -> c.loot_id == item.id) claims of
|
||||
[ c ] ->
|
||||
Just c
|
||||
|
||||
[] ->
|
||||
Nothing
|
||||
|
||||
_ ->
|
||||
Debug.log "Warning ! Duplicated claim found" Nothing
|
||||
|
||||
renderItem item =
|
||||
[ p [ B.levelItem ] [ text item.name ]
|
||||
, if isClaimed item then
|
||||
B.tag [ B.levelItem, B.isInfo ]
|
||||
, case isClaimed item of
|
||||
Just c ->
|
||||
B.tag
|
||||
[ B.levelItem
|
||||
, if c.conflicts then
|
||||
B.isError
|
||||
|
||||
else
|
||||
B.isInfo
|
||||
]
|
||||
[ B.icon
|
||||
{ icon = "fas fa-praying-hands"
|
||||
, size = Just "is-small"
|
||||
, ratio = Just "fa-1x"
|
||||
}
|
||||
, text "time left..."
|
||||
, p [] [ text "en attente..." ]
|
||||
]
|
||||
|
||||
else
|
||||
Nothing ->
|
||||
text ""
|
||||
]
|
||||
in
|
||||
|
||||
Reference in New Issue
Block a user