blob: 8e90fcaa571beff42a425fb4b1afb3301b048ee4 (
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
56
57
58
|
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.Automation.Init
( deinitST
, initST
) where
import Control.Exception
import qualified Database.SQLite.Simple as S
import qualified Data.Text as T
import System.Directory
import SpaceTraders
import qualified SpaceTraders.APIClient.Agent as STAA
import SpaceTraders.APIClient.Errors
import SpaceTraders.Database
import SpaceTraders.Database.Agents
import SpaceTraders.Database.Contracts
import SpaceTraders.Database.Ships
import SpaceTraders.Database.Tokens
deinitST :: Config -> IO ()
deinitST config = do
close $ conn config
initST :: IO Config
initST = do
c <- open
t <- getToken c `catch` handleNoToken c
ma <- runSpaceTradersT STAA.myAgent (Config c t)
case ma of
Left (APIResetHappened _) -> wipe c
Left e -> throwIO e
_ -> return $ Config c t
where
handleNoToken :: S.Connection -> SomeException -> IO T.Text
handleNoToken c _ = register c
register :: S.Connection -> IO (T.Text)
register c = do
r <- STAA.register "ADYXAX" "COSMIC"
case r of
Right r' -> do
setAgent c $ STAA.agent r'
addContract c $ STAA.contract r'
addShip c $ STAA.ship r'
let t = STAA.token r'
setToken c $ t
return t
Left e' -> throwIO e'
wipe :: S.Connection -> IO Config
wipe c = do
close c
removeFile "spacetraders.db"
conn' <- open
t <- register conn'
return $ Config conn' t
|