until it compiles...
This commit is contained in:
98
src/Api.elm
98
src/Api.elm
@@ -2,9 +2,11 @@ module Api exposing (..)
|
||||
|
||||
import Http
|
||||
import Json.Decode as D
|
||||
import Json.Decode exposing (Decoder, int, string)
|
||||
import Json.Decode exposing (Decoder, int, string, field, succeed)
|
||||
import Json.Encode as E
|
||||
|
||||
import Modes exposing (ViewMode)
|
||||
|
||||
type alias HttpResult a = (Result Http.Error a)
|
||||
|
||||
type alias Response =
|
||||
@@ -28,6 +30,51 @@ type Msg
|
||||
| GotLoot ToChest (HttpResult Loot)
|
||||
| GotActionResult (HttpResult Response)
|
||||
|
||||
|
||||
---
|
||||
-- MODELS
|
||||
---
|
||||
|
||||
-- Player
|
||||
|
||||
type alias Player =
|
||||
{ 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
|
||||
}
|
||||
|
||||
-- Loot
|
||||
|
||||
type alias Loot = List Item
|
||||
|
||||
type alias Item =
|
||||
{ id: Int
|
||||
, name: String
|
||||
, base_price: Int
|
||||
}
|
||||
|
||||
-- Claims
|
||||
|
||||
type alias Claims = List Claim
|
||||
|
||||
type alias Claim =
|
||||
{ id: Int
|
||||
, player_id: Int
|
||||
, loot_id: Int
|
||||
}
|
||||
|
||||
-- PLAYERS
|
||||
--
|
||||
|
||||
@@ -35,7 +82,7 @@ fetchPlayer : Int -> Cmd Msg
|
||||
fetchPlayer id =
|
||||
Http.get
|
||||
{ url = "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/"
|
||||
, expect = Http.expectJson Main.GotPlayer (valueDecoder playerDecoder )
|
||||
, expect = Http.expectJson GotPlayer (valueDecoder playerDecoder )
|
||||
}
|
||||
|
||||
playerDecoder : Decoder Player
|
||||
@@ -72,13 +119,13 @@ lootDecoder : Decoder Loot
|
||||
lootDecoder =
|
||||
Json.Decode.list itemDecoder
|
||||
|
||||
fetchLoot : Main.ToChest -> Cmd Msg
|
||||
fetchLoot : ToChest -> Cmd Msg
|
||||
fetchLoot dest =
|
||||
let
|
||||
url = case dest of
|
||||
Main.OfPlayer id -> "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/loot"
|
||||
Main.OfShop -> "http://localhost:8088/api/items"
|
||||
Main.OfGroup -> "http://localhost:8088/api/players/0/loot"
|
||||
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
|
||||
@@ -109,7 +156,7 @@ valueDecoder thenDecoder =
|
||||
-- TODO: update server to produce better json
|
||||
-- like an object with list of updates of the same type
|
||||
-- { ItemRemoved : [..], Wealth : [ .. ], .. }
|
||||
updatesDecoder : Decoder DbUpdate
|
||||
updatesDecoder : Decoder Update
|
||||
updatesDecoder =
|
||||
-- We expect one update but do not know it's kind
|
||||
Json.Decode.oneOf
|
||||
@@ -121,9 +168,9 @@ updatesDecoder =
|
||||
]
|
||||
|
||||
|
||||
apiResponseDecoder : Decoder ApiResponse
|
||||
apiResponseDecoder : Decoder Response
|
||||
apiResponseDecoder =
|
||||
Json.Decode.map4 ApiResponse
|
||||
Json.Decode.map4 Response
|
||||
(D.maybe (field "value" string))
|
||||
(Json.Decode.maybe (field "notification" string))
|
||||
(Json.Decode.maybe (field "updates" (Json.Decode.list updatesDecoder)))
|
||||
@@ -139,35 +186,40 @@ undoLastAction id = Http.request
|
||||
, tracker = Nothing
|
||||
}
|
||||
|
||||
sendRequest : Maybe ViewMode -> Model -> Cmd Msg
|
||||
sendRequest activeMode model =
|
||||
case activeMode of
|
||||
Nothing -> Cmd.none
|
||||
Just mode ->
|
||||
sendRequest : ViewMode -> String -> E.Value -> Cmd Msg
|
||||
sendRequest mode id payload =
|
||||
let
|
||||
(endpoint, method) = case mode of
|
||||
Add ->
|
||||
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/loot"
|
||||
Modes.Add ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "POST"
|
||||
)
|
||||
Buy ->
|
||||
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/loot"
|
||||
Modes.Buy ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "PUT"
|
||||
)
|
||||
Sell ->
|
||||
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/loot"
|
||||
Modes.Sell ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "DELETE"
|
||||
)
|
||||
Grab ->
|
||||
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/claims"
|
||||
Modes.Grab ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
|
||||
, "POST")
|
||||
in
|
||||
Http.request
|
||||
{ method = method
|
||||
, headers = []
|
||||
, url = endpoint
|
||||
, body = Http.jsonBody <| buildPayload mode model
|
||||
, body = Http.jsonBody payload
|
||||
, expect = Http.expectJson GotActionResult apiResponseDecoder
|
||||
, timeout = Nothing
|
||||
, tracker = Nothing
|
||||
}
|
||||
|
||||
|
||||
|
||||
printError : Http.Error -> String
|
||||
printError error =
|
||||
case error of
|
||||
Http.NetworkError -> "Le serveur ne répond pas"
|
||||
_ -> "Erreur inconnue"
|
||||
|
||||
Reference in New Issue
Block a user