adds replaceShop from items

This commit is contained in:
2019-11-26 21:15:16 +01:00
parent a81d184af6
commit 89b22bb07d
8 changed files with 731 additions and 140 deletions

View File

@@ -1,6 +1,5 @@
module Api exposing
( ActionMode(..)
, Claim
( Claim
, Claims
, HttpResult
, Item
@@ -13,6 +12,7 @@ module Api exposing
, confirmAction
, fetchClaimsOf
, fetchLoot
, replaceShopItems
)
import Api.Player exposing (Player, Wealth)
@@ -57,10 +57,6 @@ type Msg
-- Loot
type alias Loot =
List Item
type alias Item =
{ id : Int
, name : String
@@ -68,6 +64,61 @@ type alias Item =
}
itemDecoder =
D.map3 Item
(D.field "id" int)
(D.field "name" string)
(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 )
]
type alias Loot =
List Item
-- LOOT
-- Location of a loot
lootDecoder : Decoder Loot
lootDecoder =
D.list itemDecoder
type ToChest
= OfPlayer Int
| OfGroup
| OfShop
fetchLoot : (ToChest -> Result Http.Error Loot -> msg) -> ToChest -> Cmd msg
fetchLoot toMsg dest =
let
url =
case dest of
OfPlayer id ->
"http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot"
OfShop ->
"http://localhost:8088/api/shop"
OfGroup ->
"http://localhost:8088/api/players/0/loot"
in
Http.get
{ url = url
, expect = Http.expectJson (toMsg dest) (valueDecoder lootDecoder)
}
-- Claims
@@ -111,57 +162,6 @@ fetchClaimsOf toMsg playerId =
-- LOOT
-- Location of a loot
itemDecoder =
D.map3 Item
(D.field "id" int)
(D.field "name" string)
(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
type ToChest
= OfPlayer Int
| OfGroup
| OfShop
fetchLoot : (ToChest -> Result Http.Error Loot -> msg) -> ToChest -> Cmd msg
fetchLoot toMsg dest =
let
url =
case dest of
OfPlayer id ->
"http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot"
OfShop ->
"http://localhost:8088/api/shop"
OfGroup ->
"http://localhost:8088/api/players/0/loot"
in
Http.get
{ url = url
, expect = Http.expectJson (toMsg dest) (valueDecoder lootDecoder)
}
-- Retrieves items from a list of names
@@ -264,14 +264,6 @@ apiResponseDecoder toValue =
-}
type ActionMode
= View
| Sell
| Buy
| Grab
| Add
type RequestData
= SellPayload Loot (Maybe Float) (List (Maybe Float)) (List Int)
| BuyPayload Loot (Maybe Float) (List (Maybe Float))
@@ -398,6 +390,37 @@ undoLastAction id =
-- ADMIN
--
replaceShopItems : (Maybe () -> msg) -> Loot -> Cmd msg
replaceShopItems toMsg loot =
let
data =
E.list itemEncoder loot
gotResponse : HttpResult (Response ()) -> msg
gotResponse response =
case response of
Ok apiResponse ->
toMsg apiResponse.value
Err error ->
toMsg Nothing
in
Http.request
{ url = "http://localhost:8088/api/shop"
, method = "POST"
, headers = []
, body = Http.jsonBody data
, expect = Http.expectJson gotResponse (apiResponseDecoder <| D.succeed ())
, timeout = Nothing
, tracker = Nothing
}
-- UTILS