summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders.hs
blob: 19d55d0502c9954a39bb199100372197e217d2df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module SpaceTraders
  ( SpaceTradersT
  , microSinceEpoch
  , newEnv
  , runSpaceTradersT
  , Env(..)
  , HasDatabaseConn
  , HasRequest
  , ask
  , getConn
  , getLastAPICall
  , getRequest
  , liftIO
  ) where

import           Control.Monad.IO.Class
import           Control.Monad.Reader
import           Data.IORef
import           Data.Time.Clock
import           Data.Time.Clock.POSIX
import qualified Database.SQLite.Simple as S
import           Network.HTTP.Simple

type SpaceTradersT a = ReaderT Env IO a

newEnv :: S.Connection -> Request -> IO Env
newEnv conn req = do
  r <- newIORef 0
  return $ Env conn r req

runSpaceTradersT :: SpaceTradersT a -> Env -> IO a
runSpaceTradersT = runReaderT

microSinceEpoch :: IO Integer
microSinceEpoch = do
  t <- getCurrentTime
  return $ floor . (1e6 *) . nominalDiffTimeToSeconds $ utcTimeToPOSIXSeconds t

data Env = Env { envConn        :: S.Connection
               , envLastAPICall :: IORef Integer
               , envRequest     :: Request }

class HasDatabaseConn a where
  getConn :: a -> S.Connection
instance HasDatabaseConn S.Connection where
  getConn = id
instance HasDatabaseConn Env where
  getConn = envConn

class HasRequest a where
  getLastAPICall :: a -> IORef Integer
  getRequest :: a -> Request
instance HasRequest Env where
  getLastAPICall = envLastAPICall
  getRequest = envRequest