cleans Actions API and modes

This commit is contained in:
2019-11-13 15:46:15 +01:00
parent ca1ff2c778
commit 18f661ddf3
2 changed files with 225 additions and 107 deletions

View File

@@ -1,10 +1,21 @@
module Api exposing (Update(..), Msg(..)
, HttpResult
, Player, Wealth, fetchPlayer, blankPlayer
, Item, Loot, fetchLoot
, Claim, Claims, fetchClaims
, ActionMode(..), confirmAction
)
module Api exposing
( ActionMode(..)
, Claim
, Claims
, HttpResult
, Item
, Loot
, Msg(..)
, Player
, RequestData(..)
, Update(..)
, Wealth
, blankPlayer
, confirmAction
, fetchClaims
, fetchLoot
, fetchPlayer
)
import Http
import Json.Decode as D exposing (Decoder, field, int, string, succeed)
@@ -15,7 +26,10 @@ type alias HttpResult a =
Result Http.Error a
-- Format of the server's response
type alias Response =
{ value : Maybe String
, notification : Maybe String
@@ -110,11 +124,11 @@ fetchClaims toMsg playerId =
}
-- PLAYERS
--
fetchPlayer : (Result Http.Error Player -> msg) -> Int -> Cmd msg
fetchPlayer toMsg id =
Http.get
@@ -153,6 +167,14 @@ itemDecoder =
(D.field "base_price" int)
itemEncoder item =
E.object
[ ( "id", E.int item.id )
, ( "name", E.string item.name )
, ( "base_price", E.int item.base_price )
]
lootDecoder : Decoder Loot
lootDecoder =
D.list itemDecoder
@@ -168,10 +190,6 @@ fetchLoot url toMsg =
-- CLAIMS
-- API Response
--
@@ -220,73 +238,86 @@ undoLastAction id =
}
{- ACTIONS
Actions that can be taken on a selection of items
-}
type ActionMode
= Sell
= View
| Sell
| Buy
| Grab
| Add
| NoMode
buildPayload : ActionMode -> List Item -> E.Value
buildPayload mode items =
case mode of
Buy ->
type RequestData
= SellPayload Loot (Maybe Float) (List Float) (List Int)
| BuyPayload Loot (Maybe Float) (List Float)
| GrabPayload Loot
| AddPayload String Loot
buildPayload : RequestData -> E.Value
buildPayload data =
case data of
BuyPayload items _ _ ->
E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
, ( "global_mod", E.null )
]
Sell ->
SellPayload items _ _ _ ->
E.object
[ ( "items", items |> E.list (\i -> E.list identity [ E.int i.id, E.null ]) )
, ( "global_mod", E.null )
, ( "players", E.null )
]
-- API expects the list of claimed loot ids
Grab ->
-- API expects the list of claimed items ids
GrabPayload items ->
items |> E.list (\i -> E.int i.id)
Add ->
AddPayload sourceName items ->
E.object
[ ( "items", items |> E.list (\i -> E.int i.id) )
[ ( "items", items |> E.list itemEncoder )
, ( "source_name", E.string sourceName )
]
NoMode -> E.null
confirmAction : ActionMode -> String -> List Item -> Cmd Msg
confirmAction mode id items =
confirmAction : String -> RequestData -> Cmd Msg
confirmAction id data =
let
( endpoint, method ) =
case mode of
Add ->
case data of
AddPayload _ _ ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "POST"
)
Buy ->
BuyPayload _ _ _ ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "PUT"
)
Sell ->
SellPayload _ _ _ _ ->
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "DELETE"
)
Grab ->
GrabPayload _ ->
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
, "POST"
)
-- TODO: ???
NoMode -> ("", "GET")
in
Http.request
{ method = method
, headers = []
, url = endpoint
, body = Http.jsonBody <| buildPayload mode items
, body = Http.jsonBody <| buildPayload data
, expect = Http.expectJson GotActionResult apiResponseDecoder
, timeout = Nothing
, tracker = Nothing