summaryrefslogtreecommitdiff
path: root/haskell/app/Main.hs
blob: 76e4bef1cf57b7727bd6d30268c6c8faf92f8198 (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
{-# 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.Ships
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'
          addShip conn $ ship r'
          let t = token r'
          setToken conn $ t
          return t
        Left e' -> throwIO e'