cleaning up

This commit is contained in:
2019-11-08 15:56:07 +01:00
parent b784137d15
commit eb29c5a24f
5 changed files with 464 additions and 466 deletions

View File

@@ -3,7 +3,7 @@ module Api exposing (..)
import Http
import Json.Decode as D exposing (Decoder, field, int, string, succeed)
import Json.Encode as E
import Modes exposing (ViewMode)
import Modes
type alias HttpResult a =
@@ -28,8 +28,6 @@ type Update
type Msg
= GotPlayer (HttpResult Player)
| GotClaims Int (HttpResult Claims)
| GotLoot ToChest (HttpResult Loot)
| GotActionResult (HttpResult Response)
@@ -97,13 +95,13 @@ claimDecoder =
(D.field "loot_id" int)
fetchClaims : Int -> Cmd Msg
fetchClaims playerId =
fetchClaims : (Result Http.Error Claims -> msg) -> Cmd msg
fetchClaims toMsg =
Http.get
{ url = "http://localhost:8088/api/claims"
, expect =
valueDecoder (D.list claimDecoder)
|> Http.expectJson (GotClaims playerId)
|> Http.expectJson toMsg
}
@@ -142,12 +140,6 @@ wealthDecoder =
-- Location of a loot
type ToChest
= OfPlayer Int
| OfGroup
| OfShop
itemDecoder =
D.map3 Item
(D.field "id" int)
@@ -160,23 +152,11 @@ lootDecoder =
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"
in
fetchLoot : String -> (Result Http.Error Loot -> msg) -> Cmd msg
fetchLoot url toMsg =
Http.get
{ url = url
, expect = Http.expectJson (GotLoot dest) (valueDecoder lootDecoder)
, expect = Http.expectJson toMsg (valueDecoder lootDecoder)
}
@@ -234,7 +214,7 @@ undoLastAction id =
}
buildPayload : ViewMode -> List Item -> E.Value
buildPayload : Modes.Model -> List Item -> E.Value
buildPayload mode items =
case mode of
Modes.Buy ->
@@ -259,8 +239,10 @@ buildPayload mode items =
[ ( "items", items |> E.list (\i -> E.int i.id) )
]
Modes.None -> E.null
sendRequest : ViewMode -> String -> List Item -> Cmd Msg
sendRequest : Modes.Model -> String -> List Item -> Cmd Msg
sendRequest mode id items =
let
( endpoint, method ) =
@@ -284,6 +266,8 @@ sendRequest mode id items =
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
, "POST"
)
-- TODO: ???
Modes.None -> ("", "GET")
in
Http.request
{ method = method