summaryrefslogtreecommitdiff
path: root/haskell/app/Main.hs
blob: 08e412dc759a47367dc8a8c8e512e7c171f1b203 (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
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Control.Exception
import qualified Database.SQLite.Simple as S
import qualified Data.Text as T

import SpaceTraders.APIClient.Agent
import SpaceTraders.Database
import SpaceTraders.Database.Agents
import SpaceTraders.Database.Contracts
import SpaceTraders.Database.Tokens

main :: IO ()
main = do
  conn <- open
  t <- getToken conn `catch` registerNow conn
  ma <- myAgent t
  print ma
  close conn
  where
    registerNow :: S.Connection -> SomeException -> IO (T.Text)
    registerNow conn _ = do
      r <- register "ADYXAX" "COSMIC"
      case r of
        Right r' -> do
          setAgent conn $ agent r'
          addContract conn $ contract r'
          let t = token r'
          setToken conn $ t
          return t
        Left e' -> throwIO e'