84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
/**
|
|
* Spacetraders navigation.
|
|
*
|
|
* @package SpacetradersAPI
|
|
* @author Keith Solomon <keith@keithsolomon.net>
|
|
* @license MIT License
|
|
* @version GIT: <git_id>
|
|
* @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();
|
|
|
|
if (! isset( $tokenError ) ) {
|
|
$client = new SpacetradersApi(
|
|
trim( $token ),
|
|
$config['api_base_url'],
|
|
(int) $config['api_timeout'],
|
|
$storage,
|
|
(int) $config['cache_ttl']
|
|
);
|
|
}
|
|
|
|
try {
|
|
if (! isset( $tokenError ) ) {
|
|
$agentResponse = $client->getMyAgent();
|
|
$agent = $agentResponse['data'] ?? $agentResponse;
|
|
}
|
|
} catch (SpacetradersApiException $e) {
|
|
$error = array(
|
|
'error' => $e->getMessage(),
|
|
'code' => $e->getCode(),
|
|
'payload' => $e->getErrorPayload(),
|
|
);
|
|
|
|
http_response_code( 500 );
|
|
header( 'Content-Type: application/json; charset=utf-8' );
|
|
echo json_encode( $error, JSON_PRETTY_PRINT );
|
|
}
|
|
?>
|
|
|
|
<div class="mb-6 flex items-center justify-between">
|
|
<div>
|
|
<a href="index.php" class="text-blue-400 hover:underline">Dashboard</a>
|
|
<span class="mx-2">|</span>
|
|
<a href="market.php" class="text-blue-400 hover:underline">Markets</a>
|
|
<span class="mx-2">|</span>
|
|
<a href="all-markets.php" class="text-blue-400 hover:underline">All Markets</a>
|
|
<span class="mx-2">|</span>
|
|
<a href="buy-ships.php" class="text-blue-400 hover:underline">Buy Ships</a>
|
|
<span class="mx-2">|</span>
|
|
<a href="mining-fleet.php" class="text-blue-400 hover:underline">Mining Fleet</a>
|
|
<span class="mx-2">|</span>
|
|
<a href="config.php" class="text-blue-400 hover:underline">Configuration</a>
|
|
</div>
|
|
|
|
<div>
|
|
<span class="font-bold">Credits: <span class="font-normal"><?php echo number_format( $agent['credits'] ); ?></span></span>
|
|
</div>
|
|
</div>
|