summaryrefslogtreecommitdiff
path: root/haskell
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-29 00:32:36 +0200
committerJulien Dessaux2023-07-29 00:32:36 +0200
commit961625c363193905e58221bdc895a19f9f153cce (patch)
tree173f8f284b68be4046f8ef22db654195bfcd7da2 /haskell
parent[haskell] implemented ships's availability tracking in the database (diff)
downloadspacetraders-961625c363193905e58221bdc895a19f9f153cce.tar.gz
spacetraders-961625c363193905e58221bdc895a19f9f153cce.tar.bz2
spacetraders-961625c363193905e58221bdc895a19f9f153cce.zip
[haskell] implemented dock and orbit api calls
Diffstat (limited to 'haskell')
-rw-r--r--haskell/app/Main.hs5
-rw-r--r--haskell/src/SpaceTraders/APIClient/Ships.hs33
2 files changed, 35 insertions, 3 deletions
diff --git a/haskell/app/Main.hs b/haskell/app/Main.hs
index 85b07da..a7774e5 100644
--- a/haskell/app/Main.hs
+++ b/haskell/app/Main.hs
@@ -9,6 +9,7 @@ import System.Posix.Process
import SpaceTraders
import SpaceTraders.Automation.Init
import SpaceTraders.APIClient.Errors
+import SpaceTraders.APIClient.Ships
import SpaceTraders.APIClient.Systems
import SpaceTraders.Database.Agents
import SpaceTraders.Database.Contracts
@@ -28,5 +29,7 @@ main = do
Left e -> throwIO e
Right s' -> print $ length s'
runSpaceTradersT getContracts env >>= print
- runSpaceTradersT getShips env >>= print
+ ss <- runSpaceTradersT getShips env
+ runSpaceTradersT (dock $ head ss) env >>= print
+ runSpaceTradersT (orbit $ head ss) env >>= print
deinitST env
diff --git a/haskell/src/SpaceTraders/APIClient/Ships.hs b/haskell/src/SpaceTraders/APIClient/Ships.hs
index e2cf15c..f4b7290 100644
--- a/haskell/src/SpaceTraders/APIClient/Ships.hs
+++ b/haskell/src/SpaceTraders/APIClient/Ships.hs
@@ -3,16 +3,34 @@
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.APIClient.Ships
- ( myShips
+ ( dock
+ , myShips
+ , orbit
) where
+import Data.Aeson.Types
+import qualified Data.Text.Encoding as T
+import GHC.Generics
import Network.HTTP.Simple
import SpaceTraders
import SpaceTraders.APIClient.Client
import SpaceTraders.APIClient.Pagination
import SpaceTraders.Database.Ships
-import SpaceTraders.Model.Ship(Ship)
+import SpaceTraders.Model.Nav
+import SpaceTraders.Model.Ship
+
+data NavMessage = NavMessage { nav :: Nav } deriving (FromJSON, Generic, Show)
+dock :: Ship -> SpaceTradersT (APIResponse Ship)
+dock ship = do
+ resp <- send $ setRequestPath (T.encodeUtf8 $ mconcat ["/v2/my/ships/", symbol ship, "/dock"])
+ . setRequestMethod "POST" :: SpaceTradersT (APIResponse NavMessage)
+ case resp of
+ Left e -> return $ Left e
+ Right (NavMessage n) -> do
+ let s = ship{SpaceTraders.Model.Ship.nav=n}
+ updateShip s Nothing
+ return $ Right s
myShips :: SpaceTradersT (APIResponse [Ship])
myShips = do
@@ -28,3 +46,14 @@ myShips = do
if (limit p' * page p' < total p') then listShips' (nextPage p')
else Right <$> getShips
_ -> undefined
+
+orbit :: Ship -> SpaceTradersT (APIResponse Ship)
+orbit ship = do
+ resp <- send $ setRequestPath (T.encodeUtf8 $ mconcat ["/v2/my/ships/", symbol ship, "/orbit"])
+ . setRequestMethod "POST" :: SpaceTradersT (APIResponse NavMessage)
+ case resp of
+ Left e -> return $ Left e
+ Right (NavMessage n) -> do
+ let s = ship{SpaceTraders.Model.Ship.nav=n}
+ updateShip s Nothing
+ return $ Right s