restores all functionnality after refactoring

ready to go on !
This commit is contained in:
2019-11-06 21:50:49 +01:00
parent 081ef1a89f
commit 8a604279db
6 changed files with 694 additions and 394 deletions

View File

@@ -1,17 +1,18 @@
module Api exposing (..)
import Http
import Json.Decode as D
import Json.Decode exposing (Decoder, int, string, field, succeed)
import Json.Decode as D exposing (Decoder, field, int, string, succeed)
import Json.Encode as E
import Modes exposing (ViewMode)
type alias HttpResult a = (Result Http.Error a)
type alias HttpResult a =
Result Http.Error a
type alias Response =
{ value: Maybe String
, notification: Maybe String
{ value : Maybe String
, notification : Maybe String
, updates : Maybe (List Update)
, errors : Maybe String
}
@@ -24,6 +25,7 @@ type Update
| ClaimAdded ()
| ClaimRemoved ()
type Msg
= GotPlayer (HttpResult Player)
| GotClaims Int (HttpResult Claims)
@@ -31,67 +33,84 @@ type Msg
| GotActionResult (HttpResult Response)
---
-- MODELS
---
-- Player
type alias Player =
{ id: Int
, name: String
, debt: Int
, wealth: Wealth
{ id : Int
, name : String
, debt : Int
, wealth : Wealth
}
blankPlayer =
Player 0 "Loot-a-lot" 0 (Wealth 0 0 0 0)
type alias Wealth =
{ cp: Int
, sp: Int
, gp: Int
, pp: Int
{ cp : Int
, sp : Int
, gp : Int
, pp : Int
}
-- Loot
type alias Loot = List Item
type alias Loot =
List Item
type alias Item =
{ id: Int
, name: String
, base_price: Int
{ id : Int
, name : String
, base_price : Int
}
-- Claims
type alias Claims = List Claim
type alias Claims =
List Claim
type alias Claim =
{ id: Int
, player_id: Int
, loot_id: Int
{ id : Int
, player_id : Int
, loot_id : Int
}
-- PLAYERS
--
fetchPlayer : Int -> Cmd Msg
fetchPlayer id =
Http.get
{ url = "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/"
, expect = Http.expectJson GotPlayer (valueDecoder playerDecoder )
{ url = "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/"
, expect = Http.expectJson GotPlayer (valueDecoder playerDecoder)
}
playerDecoder : Decoder Player
playerDecoder =
D.map4 Player
(D.field "id" int)
(D.field "name" string)
(D.field "debt" int)
wealthDecoder
(D.field "id" int)
(D.field "name" string)
(D.field "debt" int)
wealthDecoder
wealthDecoder : Decoder Wealth
wealthDecoder =
@@ -101,127 +120,172 @@ wealthDecoder =
(D.field "gp" int)
(D.field "pp" int)
-- LOOT
-- LOOT
-- Location of a loot
type ToChest
= OfPlayer Int
| OfGroup
| OfShop
itemDecoder =
D.map3 Item
(D.field "id" int)
(D.field "name" string)
(D.field "base_price" int)
lootDecoder : Decoder Loot
lootDecoder =
Json.Decode.list itemDecoder
D.list itemDecoder
fetchLoot : ToChest -> Cmd Msg
fetchLoot dest =
let
url = case dest of
OfPlayer id -> "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/loot"
OfShop -> "http://localhost:8088/api/items"
OfGroup -> "http://localhost:8088/api/players/0/loot"
url =
case dest of
OfPlayer id ->
"http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot"
OfShop ->
"http://localhost:8088/api/items"
OfGroup ->
"http://localhost:8088/api/players/0/loot"
in
Http.get
{ url = url
, expect = Http.expectJson (GotLoot dest) (valueDecoder lootDecoder)}
, expect = Http.expectJson (GotLoot dest) (valueDecoder lootDecoder)
}
-- CLAIMS
claimDecoder =
D.map3 Claim
(D.field "id" int)
(D.field "player_id" int)
(D.field "loot_id" int)
fetchClaims : Int -> Cmd Msg
fetchClaims playerId =
Http.get
{ url = "http://localhost:8088/api/claims"
, expect = valueDecoder (D.list claimDecoder)
|> Http.expectJson (GotClaims playerId)
, expect =
valueDecoder (D.list claimDecoder)
|> Http.expectJson (GotClaims playerId)
}
-- API Response
--
valueDecoder : Decoder a -> Decoder a
valueDecoder thenDecoder =
D.field "value" thenDecoder
-- TODO: update server to produce better json
-- like an object with list of updates of the same type
-- { ItemRemoved : [..], Wealth : [ .. ], .. }
updatesDecoder : Decoder Update
updatesDecoder =
-- We expect one update but do not know it's kind
Json.Decode.oneOf
[ (field "ItemRemoved" (itemDecoder |> Json.Decode.andThen (\i -> succeed <| ItemRemoved i)))
, (field "ItemAdded" (itemDecoder |> Json.Decode.andThen (\i -> succeed <| ItemAdded i)))
, (field "Wealth" (wealthDecoder |> Json.Decode.andThen (\i -> succeed <| WealthUpdated i)))
, (field "ClaimRemoved" (succeed () |> Json.Decode.andThen (\i -> succeed <| ClaimRemoved i)))
, (field "ClaimAdded" (succeed () |> Json.Decode.andThen (\i -> succeed <| ClaimAdded i)))
]
D.oneOf
[ field "ItemRemoved" (itemDecoder |> D.andThen (\i -> succeed <| ItemRemoved i))
, field "ItemAdded" (itemDecoder |> D.andThen (\i -> succeed <| ItemAdded i))
, field "Wealth" (wealthDecoder |> D.andThen (\i -> succeed <| WealthUpdated i))
, field "ClaimRemoved" (succeed () |> D.andThen (\i -> succeed <| ClaimRemoved i))
, field "ClaimAdded" (succeed () |> D.andThen (\i -> succeed <| ClaimAdded i))
]
apiResponseDecoder : Decoder Response
apiResponseDecoder =
Json.Decode.map4 Response
D.map4 Response
(D.maybe (field "value" string))
(Json.Decode.maybe (field "notification" string))
(Json.Decode.maybe (field "updates" (Json.Decode.list updatesDecoder)))
(Json.Decode.maybe (field "errors" string))
(D.maybe (field "notification" string))
(D.maybe (field "updates" (D.list updatesDecoder)))
(D.maybe (field "errors" string))
undoLastAction id =
Http.request
{ url = "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/events/last"
, method = "DELETE"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson GotActionResult apiResponseDecoder
, timeout = Nothing
, tracker = Nothing
}
undoLastAction id = Http.request
{ url = "http://localhost:8088/api/players/" ++ String.fromInt id ++"/events/last"
, method = "DELETE"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson GotActionResult apiResponseDecoder
, timeout = Nothing
, tracker = Nothing
}
buildPayload : ViewMode -> List Item -> E.Value
buildPayload mode items =
case mode of
Modes.Buy -> E.object
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
, ("global_mod", E.null )
]
Modes.Sell -> E.object
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
, ("global_mod", E.null )
]
Modes.Grab -> E.object
[ ( "items", items |> E.list (\i -> E.int i.id))
, ("global_mod", E.null )
]
Modes.Add -> E.object
[ ( "items", items |> E.list (\i -> E.int i.id))
, ("global_mod", E.null )
]
case mode of
Modes.Buy ->
E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
, ( "global_mod", E.null )
]
Modes.Sell ->
E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
, ( "global_mod", E.null )
]
Modes.Grab ->
E.object
[ ( "items", items |> E.list (\i -> E.int i.id) )
, ( "global_mod", E.null )
]
Modes.Add ->
E.object
[ ( "items", items |> E.list (\i -> E.int i.id) )
, ( "global_mod", E.null )
]
sendRequest : ViewMode -> String -> List Item -> Cmd Msg
sendRequest mode id items =
let
(endpoint, method) = case mode of
Modes.Add ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "POST" )
Modes.Buy ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "PUT" )
Modes.Sell ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "DELETE" )
Modes.Grab ->
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
, "POST" )
( endpoint, method ) =
case mode of
Modes.Add ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "POST"
)
Modes.Buy ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "PUT"
)
Modes.Sell ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "DELETE"
)
Modes.Grab ->
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
, "POST"
)
in
Http.request
{ method = method
@@ -234,9 +298,11 @@ sendRequest mode id items =
}
printError : Http.Error -> String
printError error =
case error of
Http.NetworkError -> "Le serveur ne répond pas"
_ -> "Erreur inconnue"
Http.NetworkError ->
"Le serveur ne répond pas"
_ ->
"Erreur inconnue"