makes it compile as is

This commit is contained in:
2019-11-11 15:49:39 +01:00
parent 5725d81236
commit 3aee238cd9
6 changed files with 794 additions and 845 deletions

View File

@@ -1,15 +1,21 @@
module Api exposing (..)
module Api exposing (Update(..), Msg(..)
, HttpResult
, Player, Wealth, fetchPlayer, blankPlayer
, Item, Loot, fetchLoot
, Claim, Claims, fetchClaims
, ActionMode(..), confirmAction
)
import Http
import Json.Decode as D exposing (Decoder, field, int, string, succeed)
import Json.Encode as E
import Modes
type alias HttpResult a =
Result Http.Error a
-- Format of the server's response
type alias Response =
{ value : Maybe String
, notification : Maybe String
@@ -27,8 +33,7 @@ type Update
type Msg
= GotPlayer (HttpResult Player)
| GotActionResult (HttpResult Response)
= GotActionResult (HttpResult Response)
@@ -95,10 +100,10 @@ claimDecoder =
(D.field "loot_id" int)
fetchClaims : (Result Http.Error Claims -> msg) -> Cmd msg
fetchClaims toMsg =
fetchClaims : (Result Http.Error Claims -> msg) -> Int -> Cmd msg
fetchClaims toMsg playerId =
Http.get
{ url = "http://localhost:8088/api/claims"
{ url = "http://localhost:8088/api/claims" -- TODO: ++ playerId
, expect =
valueDecoder (D.list claimDecoder)
|> Http.expectJson toMsg
@@ -109,11 +114,12 @@ fetchClaims toMsg =
--
fetchPlayer : Int -> Cmd Msg
fetchPlayer id =
fetchPlayer : (Result Http.Error Player -> msg) -> Int -> Cmd msg
fetchPlayer toMsg id =
Http.get
{ url = "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/"
, expect = Http.expectJson GotPlayer (valueDecoder playerDecoder)
, expect = Http.expectJson toMsg (valueDecoder playerDecoder)
}
@@ -214,16 +220,23 @@ undoLastAction id =
}
buildPayload : Modes.Model -> List Item -> E.Value
type ActionMode
= Sell
| Buy
| Grab
| Add
| NoMode
buildPayload : ActionMode -> List Item -> E.Value
buildPayload mode items =
case mode of
Modes.Buy ->
Buy ->
E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
, ( "global_mod", E.null )
]
Modes.Sell ->
Sell ->
E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
, ( "global_mod", E.null )
@@ -231,43 +244,43 @@ buildPayload mode items =
]
-- API expects the list of claimed loot ids
Modes.Grab ->
Grab ->
items |> E.list (\i -> E.int i.id)
Modes.Add ->
Add ->
E.object
[ ( "items", items |> E.list (\i -> E.int i.id) )
]
Modes.None -> E.null
NoMode -> E.null
sendRequest : Modes.Model -> String -> List Item -> Cmd Msg
sendRequest mode id items =
confirmAction : ActionMode -> String -> List Item -> Cmd Msg
confirmAction mode id items =
let
( endpoint, method ) =
case mode of
Modes.Add ->
Add ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "POST"
)
Modes.Buy ->
Buy ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "PUT"
)
Modes.Sell ->
Sell ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "DELETE"
)
Modes.Grab ->
Grab ->
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
, "POST"
)
-- TODO: ???
Modes.None -> ("", "GET")
NoMode -> ("", "GET")
in
Http.request
{ method = method