diff --git a/.gitignore b/.gitignore index faf4484..21eb6cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Notes/ .vscode/ phpcs-results.txt +data/*.sqlite diff --git a/agent-info.php b/agent-info.php new file mode 100644 index 0000000..95192f3 --- /dev/null +++ b/agent-info.php @@ -0,0 +1,222 @@ + + * @license MIT License + * @link https://git.keithsolomon.net/keith/Spacetraders + */ + +require_once __DIR__ . '/lib/spacetraders-api.php'; +require_once __DIR__ . '/lib/spacetraders-storage.php'; + +$config = require __DIR__ . '/lib/project-config.php'; + +$storage = new SpacetradersStorage( $config['db_path'] ); +$token = $storage->getAgentToken(); +$statusMessage = ''; +$errorMessage = ''; + +if (! is_string( $token ) || trim( $token ) === '') { + $envToken = getenv( 'SPACETRADERS_TOKEN' ); + if (is_string( $envToken ) && trim( $envToken ) !== '' ) { + $token = trim( $envToken ); + $storage->setAgentToken( $token ); + } +} + +if (! is_string( $token ) || trim( $token ) === '') { + $tokenError = 'No token found. Set one in config.php or SPACETRADERS_TOKEN.'; +} + +$agent = array(); +$ships = array(); +$contracts = array(); + +if (! isset( $tokenError ) ) { + $client = new SpacetradersApi( + trim( $token ), + $config['api_base_url'], + (int) $config['api_timeout'], + $storage, + (int) $config['cache_ttl'] + ); + + if (isset( $_GET['accept_contract'] ) && is_string( $_GET['accept_contract'] ) && trim( $_GET['accept_contract'] ) !== '') { + try { + $client->acceptContract( trim( $_GET['accept_contract'] ) ); + $storage->clearAllCache(); + $statusMessage = 'Contract accepted.'; + } catch (SpacetradersApiException $e) { + $errorMessage = 'Unable to accept contract: ' . $e->getMessage(); + } + } +} + +try { + if (! isset( $tokenError ) ) { + $agentResponse = $client->getMyAgent(); + $shipsResponse = $client->listMyShips(); + $contractsResponse = $client->listMyContracts(); + + $agent = $agentResponse['data'] ?? $agentResponse; + $ships = $shipsResponse['data'] ?? $shipsResponse; + $contracts = $contractsResponse['data'] ?? $contractsResponse; + } +} catch (SpacetradersApiException $e) { + $error = array( + 'error' => $e->getMessage(), + 'code' => $e->getCode(), + 'payload' => $e->getErrorPayload(), + ); + + if (PHP_SAPI === 'cli' ) { + fwrite( STDERR, json_encode( $error, JSON_PRETTY_PRINT ) . PHP_EOL ); + exit( 1 ); + } + + http_response_code( 500 ); + header( 'Content-Type: application/json; charset=utf-8' ); + echo json_encode( $error, JSON_PRETTY_PRINT ); +} +?> + + + +
+ + +| Name | +Role | +Type | +Status | +Flight Mode | +Route | +||
|---|---|---|---|---|---|---|---|
| + | + | + | + + | + | + + | N/A | +N/A | + +
+ Delivery Details: units delivered to +
+ ++ Payment: on Accept, on Fulfill +
+ +Deadline To Accept:
+Deadline:
+ ++ Status: + + + + Accept? + + + +
+