This commit is contained in:
parent
96d8588130
commit
c0ba436847
6 changed files with 113 additions and 65 deletions
|
@ -1,5 +1,4 @@
|
|||
import argv
|
||||
import dot_env
|
||||
import gleam/io
|
||||
import gleam/list
|
||||
import gleam_community/ansi
|
||||
|
@ -13,7 +12,6 @@ import spacetraders_sdk
|
|||
const usage = "\nusage: gleam -m spacetraders_register AGENT_SYMBOL FACTION_SYMBOL"
|
||||
|
||||
pub fn main() {
|
||||
dot_env.load_default()
|
||||
let assert Ok(account_token) = env.get_account_token()
|
||||
as "no valid account token provided"
|
||||
let args = argv.load().arguments
|
||||
|
|
|
@ -11,14 +11,14 @@ gleam_stdlib = ">= 0.60.0 and < 1.0.0"
|
|||
gleam_json = ">= 3.0.1 and < 4.0.0"
|
||||
gleam_http = ">= 4.0.0 and < 5.0.0"
|
||||
gleam_httpc = ">= 4.1.1 and < 5.0.0"
|
||||
birl = ">= 1.8.0 and < 2.0.0"
|
||||
dot_env = ">= 1.2.0 and < 2.0.0"
|
||||
argv = ">= 1.0.2 and < 2.0.0"
|
||||
gleam_erlang = ">= 1.0.0 and < 2.0.0"
|
||||
gleam_otp = ">= 1.0.0 and < 2.0.0"
|
||||
gleam_community_ansi = ">= 1.4.3 and < 2.0.0"
|
||||
shore = ">= 1.1.0 and < 2.0.0"
|
||||
spacetraders_sdk = ">= 1.5.6 and < 2.0.0"
|
||||
envoy = ">= 1.0.2 and < 2.0.0"
|
||||
gleam_time = ">= 1.2.0 and < 2.0.0"
|
||||
spacetraders_sdk = ">= 1.6.0 and < 2.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.5.1 and < 2.0.0"
|
||||
|
|
|
@ -3,9 +3,7 @@
|
|||
|
||||
packages = [
|
||||
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||
{ name = "birl", version = "1.8.0", build_tools = ["gleam"], requirements = ["gleam_regexp", "gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "2AC7BA26F998E3DFADDB657148BD5DDFE966958AD4D6D6957DD0D22E5B56C400" },
|
||||
{ name = "dot_env", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "simplifile"], otp_app = "dot_env", source = "hex", outer_checksum = "F2B4815F1B5AF8F20A6EADBB393E715C4C35203EBD5BE8200F766EA83A0B18DE" },
|
||||
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
|
||||
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
|
||||
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||
{ name = "gleam_community_colour", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "F0ACE69E3A47E913B03D3D0BB23A5563A91A4A7D20956916286068F4A9F817FE" },
|
||||
{ name = "gleam_erlang", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "7E6A5234F927C4B24F8054AB1E4572206C41F9E6D5C6C02273CB7531E7E5CED0" },
|
||||
|
@ -15,18 +13,15 @@ packages = [
|
|||
{ name = "gleam_otp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "7020E652D18F9ABAC9C877270B14160519FA0856EE80126231C505D719AD68DA" },
|
||||
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
|
||||
{ name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" },
|
||||
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||
{ name = "gleam_time", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_time", source = "hex", outer_checksum = "D71F1AFF7FEB534FF55E5DC58E534E9201BA75A444619788A2E4DEA4EBD87D16" },
|
||||
{ name = "gleeunit", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D33B7736CF0766ED3065F64A1EBB351E72B2E8DE39BAFC8ADA0E35E92A6A934F" },
|
||||
{ name = "ranger", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_yielder"], otp_app = "ranger", source = "hex", outer_checksum = "C8988E8F8CDBD3E7F4D8F2E663EF76490390899C2B2885A6432E942495B3E854" },
|
||||
{ name = "shore", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "shore", source = "hex", outer_checksum = "B5929F807459EAE243E4664D41F02696B5D3E9CE314971E8C2ECB57007CA9210" },
|
||||
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
|
||||
{ name = "spacetraders_sdk", version = "1.5.6", build_tools = ["gleam"], requirements = ["birl", "gleam_http", "gleam_httpc", "gleam_json", "gleam_stdlib"], otp_app = "spacetraders_sdk", source = "hex", outer_checksum = "C5B5EB49730AB3C662242FA700D3CAE0F319223DF0D76A6BE2FFC9B8C5D63058" },
|
||||
{ name = "spacetraders_sdk", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_httpc", "gleam_json", "gleam_stdlib", "gleam_time"], otp_app = "spacetraders_sdk", source = "hex", outer_checksum = "B6DF94B8453A7D812B3B805062972CB0DA58B8A45DD884B59E85B2D47CCF3339" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
argv = { version = ">= 1.0.2 and < 2.0.0" }
|
||||
birl = { version = ">= 1.8.0 and < 2.0.0" }
|
||||
dot_env = { version = ">= 1.2.0 and < 2.0.0" }
|
||||
envoy = { version = ">= 1.0.2 and < 2.0.0" }
|
||||
gleam_community_ansi = { version = ">= 1.4.3 and < 2.0.0" }
|
||||
gleam_erlang = { version = ">= 1.0.0 and < 2.0.0" }
|
||||
gleam_http = { version = ">= 4.0.0 and < 5.0.0" }
|
||||
|
@ -34,6 +29,7 @@ gleam_httpc = { version = ">= 4.1.1 and < 5.0.0" }
|
|||
gleam_json = { version = ">= 3.0.1 and < 4.0.0" }
|
||||
gleam_otp = { version = ">= 1.0.0 and < 2.0.0" }
|
||||
gleam_stdlib = { version = ">= 0.60.0 and < 1.0.0" }
|
||||
gleam_time = { version = ">= 1.2.0 and < 2.0.0" }
|
||||
gleeunit = { version = ">= 1.5.1 and < 2.0.0" }
|
||||
shore = { version = ">= 1.1.0 and < 2.0.0" }
|
||||
spacetraders_sdk = { version = ">= 1.5.6 and < 2.0.0" }
|
||||
spacetraders_sdk = { version = ">= 1.6.0 and < 2.0.0" }
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import birl.{type Time}
|
||||
import gleam/erlang/process
|
||||
import gleam/io
|
||||
import gleam/list
|
||||
import gleam/option.{type Option, None, Some}
|
||||
import gleam/time/timestamp.{type Timestamp}
|
||||
import shore
|
||||
import shore/key
|
||||
import shore/layout
|
||||
|
@ -74,7 +74,7 @@ pub opaque type Model {
|
|||
Model(
|
||||
env: Env,
|
||||
errors: List(ApiError),
|
||||
last_request: Option(Time),
|
||||
last_request: Option(Timestamp),
|
||||
last_server_status: LastServerStatus,
|
||||
)
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ pub opaque type Msg {
|
|||
}
|
||||
|
||||
fn init() -> #(Model, List(fn() -> Msg)) {
|
||||
let assert Ok(env) = env.load_dotenv()
|
||||
let assert Ok(env) = env.load_env()
|
||||
let model =
|
||||
Model(env:, errors: [], last_request: None, last_server_status: Loading)
|
||||
let cmds = [fn() { GetServerStatus }]
|
||||
|
@ -111,7 +111,7 @@ fn update(model: Model, msg: Msg) -> #(Model, List(fn() -> Msg)) {
|
|||
#(
|
||||
Model(
|
||||
..model,
|
||||
last_request: Some(birl.now()),
|
||||
last_request: Some(timestamp.system_time()),
|
||||
last_server_status: case model.last_server_status {
|
||||
Loading -> Loaded(response)
|
||||
Loaded(prev_response)
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import dot_env
|
||||
import dot_env/env
|
||||
import envoy
|
||||
import gleam/result
|
||||
import spacetraders_sdk.{type AccountToken, type AgentToken}
|
||||
import spacetraders_sdk.{
|
||||
type AccountToken, type AgentToken, type TokenParseError,
|
||||
}
|
||||
|
||||
pub type Env {
|
||||
Env(account_token: AccountToken, agent_token: AgentToken)
|
||||
|
@ -9,25 +10,27 @@ pub type Env {
|
|||
|
||||
pub type EnvError {
|
||||
MissingVar(name: String)
|
||||
InvalidVar(name: String)
|
||||
InvalidTokenVar(name: String, error: TokenParseError)
|
||||
}
|
||||
|
||||
fn get(name: String) -> Result(String, EnvError) {
|
||||
envoy.get(name)
|
||||
|> result.replace_error(MissingVar(name:))
|
||||
}
|
||||
|
||||
fn get_token(name: String, parse: fn(String) -> Result(token, TokenParseError)) {
|
||||
get(name)
|
||||
|> result.then(fn(value) {
|
||||
parse(value) |> result.map_error(InvalidTokenVar(name:, error: _))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_account_token() -> Result(AccountToken, EnvError) {
|
||||
env.get_string("ACCOUNT_TOKEN")
|
||||
|> result.replace_error(MissingVar("ACCOUNT_TOKEN"))
|
||||
|> result.then(fn(value) {
|
||||
spacetraders_sdk.parse_account_token(value)
|
||||
|> result.replace_error(InvalidVar("ACCOUNT_TOKEN"))
|
||||
})
|
||||
get_token("ACCOUNT_TOKEN", spacetraders_sdk.parse_account_token)
|
||||
}
|
||||
|
||||
pub fn get_agent_token() -> Result(AgentToken, EnvError) {
|
||||
env.get_string("AGENT_TOKEN")
|
||||
|> result.replace_error(MissingVar("AGENT_TOKEN"))
|
||||
|> result.then(fn(value) {
|
||||
spacetraders_sdk.parse_agent_token(value)
|
||||
|> result.replace_error(InvalidVar("AGENT_TOKEN"))
|
||||
})
|
||||
get_token("AGENT_TOKEN", spacetraders_sdk.parse_agent_token)
|
||||
}
|
||||
|
||||
pub fn load_env() -> Result(Env, EnvError) {
|
||||
|
@ -36,11 +39,6 @@ pub fn load_env() -> Result(Env, EnvError) {
|
|||
Ok(Env(account_token:, agent_token:))
|
||||
}
|
||||
|
||||
pub fn load_dotenv() -> Result(Env, EnvError) {
|
||||
dot_env.load_default()
|
||||
load_env()
|
||||
}
|
||||
|
||||
pub fn load_env_unsafe() -> Env {
|
||||
let assert Ok(account_token) = get_account_token()
|
||||
as "Missing or invalid account token"
|
||||
|
@ -48,8 +46,3 @@ pub fn load_env_unsafe() -> Env {
|
|||
as "Missing or invalid agent token"
|
||||
Env(account_token:, agent_token:)
|
||||
}
|
||||
|
||||
pub fn load_dotenv_unsafe() -> Env {
|
||||
dot_env.load_default()
|
||||
load_env_unsafe()
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
import birl
|
||||
import gleam/int
|
||||
import gleam/io
|
||||
import gleam/list
|
||||
import gleam/option.{None, Some}
|
||||
import gleam/order
|
||||
import gleam/string
|
||||
import gleam/time/calendar.{Date, TimeOfDay}
|
||||
import gleam/time/duration
|
||||
import gleam/time/timestamp.{type Timestamp}
|
||||
import gleam/uri
|
||||
import gleam_community/ansi
|
||||
import spacetraders_api.{type ServerStatus}
|
||||
|
@ -27,6 +29,56 @@ import spacetraders_models/trade_symbol
|
|||
import spacetraders_models/waypoint_symbol
|
||||
import spacetraders_models/waypoint_type
|
||||
|
||||
fn legible_timestamp(timestamp: Timestamp) -> String {
|
||||
let #(
|
||||
Date(year:, month:, day:),
|
||||
TimeOfDay(hours:, minutes:, seconds:, nanoseconds: _),
|
||||
) = timestamp.to_calendar(timestamp, calendar.local_offset())
|
||||
int.to_string(hours)
|
||||
<> ":"
|
||||
<> int.to_string(minutes)
|
||||
<> ":"
|
||||
<> int.to_string(seconds)
|
||||
<> " "
|
||||
<> int.to_string(day)
|
||||
<> "/"
|
||||
<> int.to_string(calendar.month_to_int(month))
|
||||
<> "/"
|
||||
<> int.to_string(year)
|
||||
}
|
||||
|
||||
fn legible_difference(left: Timestamp, right: Timestamp) -> String {
|
||||
let #(value, unit) = timestamp.difference(left, right) |> duration.approximate
|
||||
case value == 0 {
|
||||
True -> "now"
|
||||
False -> {
|
||||
let s =
|
||||
int.to_string(value)
|
||||
<> " "
|
||||
<> case unit {
|
||||
duration.Year -> "year"
|
||||
duration.Month -> "month"
|
||||
duration.Week -> "week"
|
||||
duration.Day -> "day"
|
||||
duration.Hour -> "hour"
|
||||
duration.Minute -> "minute"
|
||||
duration.Second -> "second"
|
||||
duration.Millisecond -> "millisecond"
|
||||
duration.Microsecond -> "microsecond"
|
||||
duration.Nanosecond -> "nanosecond"
|
||||
}
|
||||
<> case value {
|
||||
1 | -1 -> ""
|
||||
_ -> "s"
|
||||
}
|
||||
case value > 0 {
|
||||
True -> "in " <> s
|
||||
False -> s <> " ago"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn account(account: Account) -> Nil {
|
||||
io.println(ansi.bold(ansi.underline(ansi.blue("Account"))))
|
||||
io.println(ansi.bold("Id:\t\t") <> account_id.to_string(account.id))
|
||||
|
@ -34,7 +86,9 @@ pub fn account(account: Account) -> Nil {
|
|||
Some(email) -> io.println(ansi.bold("Email:\t\t") <> email)
|
||||
None -> Nil
|
||||
}
|
||||
io.println(ansi.bold("Created At:\t") <> birl.to_iso8601(account.created_at))
|
||||
io.println(
|
||||
ansi.bold("Created At:\t") <> legible_timestamp(account.created_at),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn agent(agent: Agent) -> Nil {
|
||||
|
@ -56,17 +110,24 @@ pub fn server_status(
|
|||
print_links: Bool,
|
||||
print_leaderboards: Bool,
|
||||
) -> Nil {
|
||||
let now = birl.now()
|
||||
let now = timestamp.system_time()
|
||||
io.println(ansi.bold(ansi.underline(ansi.green("Server"))))
|
||||
io.println(ansi.bold("Version:\t") <> server_status.version)
|
||||
io.println(ansi.bold("Status:\t\t") <> server_status.status)
|
||||
io.println(
|
||||
ansi.bold("Last Reset:\t")
|
||||
<> birl.legible_difference(now, server_status.reset_date),
|
||||
<> legible_difference(
|
||||
now,
|
||||
timestamp.from_calendar(
|
||||
server_status.reset_date,
|
||||
TimeOfDay(0, 0, 0, 0),
|
||||
calendar.utc_offset,
|
||||
),
|
||||
),
|
||||
)
|
||||
io.println(
|
||||
ansi.bold("Next Reset:\t")
|
||||
<> birl.legible_difference(now, server_status.server_resets.next)
|
||||
<> legible_difference(now, server_status.server_resets.next)
|
||||
<> " ("
|
||||
<> ansi.italic(server_status.server_resets.frequency)
|
||||
<> ")",
|
||||
|
@ -75,7 +136,7 @@ pub fn server_status(
|
|||
Some(last_market_update) ->
|
||||
io.println(
|
||||
ansi.bold("Market Updated:\t")
|
||||
<> birl.legible_difference(now, last_market_update),
|
||||
<> legible_difference(now, last_market_update),
|
||||
)
|
||||
None -> Nil
|
||||
}
|
||||
|
@ -123,7 +184,7 @@ pub fn server_status(
|
|||
}
|
||||
|
||||
pub fn contract(contract: Contract) -> Nil {
|
||||
let now = birl.now()
|
||||
let now = timestamp.system_time()
|
||||
io.println(ansi.bold(ansi.underline(ansi.yellow("Contract"))))
|
||||
io.println(ansi.bold("Id:\t\t") <> contract_id.to_string(contract.id))
|
||||
io.println(
|
||||
|
@ -156,16 +217,16 @@ pub fn contract(contract: Contract) -> Nil {
|
|||
<> case contract.deadline_to_accept {
|
||||
Some(deadline) ->
|
||||
"Accept by "
|
||||
<> birl.to_iso8601(deadline)
|
||||
<> legible_timestamp(deadline)
|
||||
<> " ("
|
||||
<> ansi.italic(birl.legible_difference(now, deadline))
|
||||
<> ansi.italic(legible_difference(now, deadline))
|
||||
<> ")"
|
||||
None -> "No deadline to accept"
|
||||
}
|
||||
<> " / Fulfill by "
|
||||
<> birl.to_iso8601(contract.terms.deadline)
|
||||
<> legible_timestamp(contract.terms.deadline)
|
||||
<> " ("
|
||||
<> ansi.italic(birl.legible_difference(now, contract.terms.deadline))
|
||||
<> ansi.italic(legible_difference(now, contract.terms.deadline))
|
||||
<> ")",
|
||||
)
|
||||
io.println(
|
||||
|
@ -187,7 +248,7 @@ pub fn contract(contract: Contract) -> Nil {
|
|||
}
|
||||
|
||||
pub fn ship(ship: Ship) -> Nil {
|
||||
let now = birl.now()
|
||||
let now = timestamp.system_time()
|
||||
io.println(ansi.bold(ansi.underline(ansi.blue("Ship"))))
|
||||
io.println(ansi.bold("Symbol:\t\t") <> ship_symbol.to_string(ship.symbol))
|
||||
io.println(
|
||||
|
@ -209,7 +270,7 @@ pub fn ship(ship: Ship) -> Nil {
|
|||
"consumed "
|
||||
<> int.to_string(consumed.amount)
|
||||
<> " on "
|
||||
<> birl.to_iso8601(consumed.timestamp)
|
||||
<> legible_timestamp(consumed.timestamp)
|
||||
})
|
||||
<> ")",
|
||||
)
|
||||
|
@ -237,7 +298,7 @@ pub fn ship(ship: Ship) -> Nil {
|
|||
<> case ship.cooldown.expiration {
|
||||
Some(expiration) ->
|
||||
"Expires at "
|
||||
<> birl.to_iso8601(expiration)
|
||||
<> legible_timestamp(expiration)
|
||||
<> " ("
|
||||
<> ansi.italic(
|
||||
"in " <> int.to_string(ship.cooldown.remaining_seconds) <> " seconds",
|
||||
|
@ -259,7 +320,7 @@ pub fn ship(ship: Ship) -> Nil {
|
|||
)
|
||||
io.println(
|
||||
ansi.bold("Origin:\t\t")
|
||||
<> case birl.compare(now, ship.nav.route.departure_time) {
|
||||
<> case timestamp.compare(now, ship.nav.route.departure_time) {
|
||||
order.Lt -> "Departing"
|
||||
_ -> "Departed"
|
||||
}
|
||||
|
@ -274,14 +335,14 @@ pub fn ship(ship: Ship) -> Nil {
|
|||
<> int.to_string(ship.nav.route.origin.y),
|
||||
)
|
||||
<> ") at "
|
||||
<> birl.to_iso8601(ship.nav.route.departure_time)
|
||||
<> legible_timestamp(ship.nav.route.departure_time)
|
||||
<> " ("
|
||||
<> ansi.italic(birl.legible_difference(now, ship.nav.route.departure_time))
|
||||
<> ansi.italic(legible_difference(now, ship.nav.route.departure_time))
|
||||
<> ")",
|
||||
)
|
||||
io.println(
|
||||
ansi.bold("Destination:\t")
|
||||
<> case birl.compare(now, ship.nav.route.arrival) {
|
||||
<> case timestamp.compare(now, ship.nav.route.arrival) {
|
||||
order.Lt -> "Arriving"
|
||||
_ -> "Arrived"
|
||||
}
|
||||
|
@ -296,9 +357,9 @@ pub fn ship(ship: Ship) -> Nil {
|
|||
<> int.to_string(ship.nav.route.destination.y),
|
||||
)
|
||||
<> ") at "
|
||||
<> birl.to_iso8601(ship.nav.route.arrival)
|
||||
<> legible_timestamp(ship.nav.route.arrival)
|
||||
<> " ("
|
||||
<> ansi.italic(birl.legible_difference(now, ship.nav.route.arrival))
|
||||
<> ansi.italic(legible_difference(now, ship.nav.route.arrival))
|
||||
<> ")",
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue