Files
Spacetraders/agent-info.php
2026-02-08 23:25:50 -06:00

223 lines
9.4 KiB
PHP

<?php
/**
* Simple Spacetraders API wrapper usage example.
*
* Displays authenticated Agent and Ship data.
*
* @package SpacetradersApi
* @author Keith Solomon <keith@keithsolomon.net>
* @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 );
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spacetraders - Agent Information</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="container mx-auto px-4 py-8 bg-stone-800 text-gray-200">
<?php if (isset( $tokenError ) ) : ?>
<div class="mb-6 border border-red-500 p-4 rounded">
<p class="mb-3 text-red-300"><?php echo htmlspecialchars( $tokenError ); ?></p>
<a href="config.php" class="text-blue-400 hover:underline">Open Configuration Page</a>
</div>
</body>
</html>
<?php exit; ?>
<?php endif; ?>
<div class="mb-6">
<a href="config.php" class="text-blue-400 hover:underline">Configuration</a>
</div>
<?php if ($statusMessage !== '' ) : ?>
<div class="mb-6 border border-green-500 p-4 rounded text-green-300">
<?php echo htmlspecialchars( $statusMessage ); ?>
</div>
<?php endif; ?>
<?php if ($errorMessage !== '' ) : ?>
<div class="mb-6 border border-red-500 p-4 rounded text-red-300">
<?php echo htmlspecialchars( $errorMessage ); ?>
</div>
<?php endif; ?>
<h1 class="text-3xl font-bold mb-6 underline decoration-gray-300 w-full">Spacetraders Agent and Ships</h1>
<h2 class="text-2xl font-bold mb-2">
Agent: <?php echo htmlspecialchars( ucfirst( strtolower( $agent['symbol'] ) ) ); ?><br>
Credits: <span class="font-normal"><?php echo number_format( $agent['credits'] ); ?></span>
</h2>
<h3 class="text-xl font-bold mb-4">
Headquarters: <span class="font-normal"><?php echo htmlspecialchars( $agent['headquarters'] ); ?></span><br>
Faction: <span class="font-normal"><?php echo htmlspecialchars( ucfirst( strtolower( $agent['startingFaction'] ) ) ); ?></span><br>
Ship Count: <span class="font-normal"><?php echo htmlspecialchars( $agent['shipCount'] ); ?></span><br>
System: <span class="font-normal"><?php echo htmlspecialchars( $ships[0]['nav']['systemSymbol'] ); ?></span>
</h3>
<h2 class="text-2xl font-bold my-4">Ships</h2>
<table class="table-auto border-collapse border border-gray-300">
<tr>
<th class="border border-gray-300 px-4 py-2">Name</th>
<th class="border border-gray-300 px-4 py-2">Role</th>
<th class="border border-gray-300 px-4 py-2">Type</th>
<th class="border border-gray-300 px-4 py-2">Status</th>
<th class="border border-gray-300 px-4 py-2">Flight Mode</th>
<th class="border border-gray-300 px-4 py-2">Route</th>
</tr>
<?php foreach ( $ships as $ship ) : ?>
<tr>
<td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars( ucfirst( strtolower( $ship['registration']['name'] ) ) ); ?></td>
<td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars( ucfirst( strtolower( $ship['registration']['role'] ) ) ); ?></td>
<td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars( $ship['frame']['name'] ); ?></td>
<td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars( ucfirst( strtolower( $ship['nav']['status'] ) ) ); ?></td>
<?php if ($ship['nav']['status'] !== 'DOCKED' ) : ?>
<td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars( $ship['nav']['flightMode'] ); ?></td>
<td class="border border-gray-300 px-4 py-2"><?php echo htmlspecialchars( $ship['nav']['route']['origin']['symbol'] . ' - ' . $ship['nav']['route']['destination']['symbol'] ); ?></td>
<?php else : ?>
<td class="border border-gray-300 px-4 py-2">N/A</td>
<td class="border border-gray-300 px-4 py-2">N/A</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</table>
<h2 class="text-2xl font-bold my-4">Contracts</h2>
<div class="flex flex-col lg:flex-row gap-4">
<?php
$i = 1;
foreach ( $contracts as $contract ) :
$type = ucfirst( strtolower( str_replace( '_', ' ', $contract['type'] ) ) ) ?? null;
$delivery = ucfirst( strtolower( str_replace( '_', ' ', $contract['terms']['deliver'][0]['tradeSymbol'] ) ) ) ?? null;
$accepted = $contract['accepted'] ? 'Accepted' : false;
$fulfilled = $contract['fulfilled'] ? 'Fulfilled' : false;
if (! $accepted && ! $fulfilled) {
$status = 'Not Accepted';
} elseif ($accepted && ! $fulfilled) {
$status = 'Accepted';
} elseif ($accepted && $fulfilled) {
$status = 'Fulfilled';
} else {
$status = 'Unknown';
}
?>
<div class="mb-4 border border-gray-300 p-4 rounded">
<h3 class="text-xl font-bold mb-2 w-full underline decoration-gray-300">Contract <?php echo $i; ?>: <?php echo htmlspecialchars( $type ); ?> - <?php echo htmlspecialchars( $delivery ); ?></h3>
<p class="mb-0 font-bold">
Delivery Details: <span class="font-normal"><?php echo number_format( $contract['terms']['deliver'][0]['unitsRequired'] ); ?> units delivered to <?php echo htmlspecialchars( $contract['terms']['deliver'][0]['destinationSymbol'] ); ?></span>
</p>
<p class="mb-2 font-bold">
Payment: <span class="font-normal"><?php echo number_format( $contract['terms']['payment']['onAccepted'] ); ?> on Accept, <?php echo number_format( $contract['terms']['payment']['onFulfilled'] ); ?> on Fulfill</span>
</p>
<p class="mb-0 font-bold">Deadline To Accept: <span class="font-normal"><?php echo htmlspecialchars( date( 'Y-m-d H:i:s', strtotime( $contract['deadlineToAccept'] ) ) ); ?></span></p>
<p class="mb-2 font-bold">Deadline: <span class="font-normal"><?php echo htmlspecialchars( date( 'Y-m-d H:i:s', strtotime( $contract['terms']['deadline'] ) ) ); ?></span></p>
<p class="mb-0 font-bold">
Status: <span class="font-normal"><?php echo htmlspecialchars( $status ); ?></span>
<?php if ($status === 'Not Accepted' ) : ?>
<span class="ml-2">
<a
href="agent-info.php?accept_contract=<?php echo urlencode( $contract['id'] ); ?>"
class="font-normal text-blue-400 hover:underline"
>
Accept?
</a>
</span>
<?php endif; ?>
</p>
</div>
<?php
$i++;
endforeach;
?>
</div>
</body>
</html>