summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders
diff options
context:
space:
mode:
authorJulien Dessaux2024-03-23 15:28:28 +0100
committerJulien Dessaux2024-03-27 15:21:37 +0100
commit51705b930f8408c1a3f4706d0b172eb7970f20ba (patch)
treed262a4617b084e3c03c647eb64c51679016700c0 /haskell/src/SpaceTraders
parent[haskell] only dock or orbit when needed (diff)
downloadspacetraders-51705b930f8408c1a3f4706d0b172eb7970f20ba.tar.gz
spacetraders-51705b930f8408c1a3f4706d0b172eb7970f20ba.tar.bz2
spacetraders-51705b930f8408c1a3f4706d0b172eb7970f20ba.zip
[haskell] implemented ship refueling
Diffstat (limited to 'haskell/src/SpaceTraders')
-rw-r--r--haskell/src/SpaceTraders/APIClient/Ships.hs28
-rw-r--r--haskell/src/SpaceTraders/Model/Ship.hs8
2 files changed, 36 insertions, 0 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Ships.hs b/haskell/src/SpaceTraders/APIClient/Ships.hs
index d396911..912f6e8 100644
--- a/haskell/src/SpaceTraders/APIClient/Ships.hs
+++ b/haskell/src/SpaceTraders/APIClient/Ships.hs
@@ -6,6 +6,7 @@ module SpaceTraders.APIClient.Ships
( dock
, myShips
, orbit
+ , refuel
) where
import Data.Aeson.Types
@@ -16,7 +17,10 @@ import Network.HTTP.Simple
import SpaceTraders
import SpaceTraders.APIClient.Client
import SpaceTraders.APIClient.Pagination
+import SpaceTraders.Database.Agents
import SpaceTraders.Database.Ships
+import qualified SpaceTraders.Model.Agent
+import SpaceTraders.Model.Fuel
import SpaceTraders.Model.Nav
import SpaceTraders.Model.Ship
@@ -62,3 +66,27 @@ orbit ship = if isInOrbit ship then pure (Right ship) else orbit'
let s = ship{SpaceTraders.Model.Ship.nav=n}
setShip s
return $ Right s
+
+data RefuelMessage = RefuelMessage { agent :: SpaceTraders.Model.Agent.Agent
+ , fuel :: Fuel
+ } deriving (FromJSON, Generic, Show)
+
+refuel :: Ship -> SpaceTradersT (APIResponse Ship)
+refuel ship = if overNinetyPercentFuel ship then pure (Right ship) else refuel'
+ where
+ refuel' = do
+ resp <- dock ship
+ case resp of
+ (Left e) -> return $ Left e
+ (Right ship') -> refuel'' ship'
+ refuel'' ship' = do
+ -- TODO check if we are at a marketplace, that sells fuel
+ resp' <- send $ setRequestPath (T.encodeUtf8 $ mconcat ["/v2/my/ships/", symbol ship, "/refuel"])
+ . setRequestMethod "POST" :: SpaceTradersT (APIResponse RefuelMessage)
+ case resp' of
+ Left e -> return $ Left e
+ Right (RefuelMessage a f) -> do
+ setAgent a
+ let s = ship'{SpaceTraders.Model.Ship.fuel=f}
+ setShip s
+ return $ Right s
diff --git a/haskell/src/SpaceTraders/Model/Ship.hs b/haskell/src/SpaceTraders/Model/Ship.hs
index f42d558..f30679c 100644
--- a/haskell/src/SpaceTraders/Model/Ship.hs
+++ b/haskell/src/SpaceTraders/Model/Ship.hs
@@ -6,6 +6,7 @@ module SpaceTraders.Model.Ship
( Ship(..)
, isDocked
, isInOrbit
+ , overNinetyPercentFuel
) where
import Data.Aeson
@@ -40,3 +41,10 @@ isDocked ship = status (nav ship) == "DOCKED"
isInOrbit :: Ship -> Bool
isInOrbit = not . isDocked
+
+overNinetyPercentFuel :: Ship -> Bool
+overNinetyPercentFuel ship = curr * 10 > capa * 9
+ where
+ curr = current f
+ capa = SpaceTraders.Model.Fuel.capacity f
+ f = fuel ship