✨feature: Add markets page
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Spacetraders PHP Implementation
|
||||
* Spacetraders dashboard page.
|
||||
*
|
||||
* @package SpacetradersAPI
|
||||
* @author Keith Solomon <keith@keithsolomon.net>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<div class="mb-6">
|
||||
<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="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>
|
||||
|
||||
244
market.php
Normal file
244
market.php
Normal file
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
/**
|
||||
* Spacetraders markets at stationed ship waypoints.
|
||||
*
|
||||
* @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();
|
||||
|
||||
$errorMessage = '';
|
||||
$agent = array();
|
||||
$ships = array();
|
||||
$marketRecords = array();
|
||||
|
||||
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.';
|
||||
}
|
||||
|
||||
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();
|
||||
$shipsResponse = $client->listMyShips();
|
||||
|
||||
$agent = $agentResponse['data'] ?? $agentResponse;
|
||||
$ships = $shipsResponse['data'] ?? $shipsResponse;
|
||||
|
||||
$waypointShipMap = array();
|
||||
|
||||
foreach ( $ships as $ship ) {
|
||||
$shipSymbol = (string) ( $ship['symbol'] ?? '' );
|
||||
$status = (string) ( $ship['nav']['status'] ?? '' );
|
||||
$systemSymbol = (string) ( $ship['nav']['systemSymbol'] ?? '' );
|
||||
$waypointSymbol = (string) ( $ship['nav']['waypointSymbol'] ?? '' );
|
||||
|
||||
if ($shipSymbol === '' || $systemSymbol === '' || $waypointSymbol === '' ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($status === 'IN_TRANSIT' ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! isset( $waypointShipMap[ $waypointSymbol ] ) ) {
|
||||
$waypointShipMap[ $waypointSymbol ] = array(
|
||||
'systemSymbol' => $systemSymbol,
|
||||
'shipSymbols' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
$waypointShipMap[ $waypointSymbol ]['shipSymbols'][] = $shipSymbol;
|
||||
}
|
||||
|
||||
foreach ( $waypointShipMap as $waypointSymbol => $stationedData ) {
|
||||
$record = array(
|
||||
'waypointSymbol' => (string) $waypointSymbol,
|
||||
'systemSymbol' => (string) ( $stationedData['systemSymbol'] ?? '' ),
|
||||
'shipSymbols' => (array) ( $stationedData['shipSymbols'] ?? array() ),
|
||||
'data' => array(),
|
||||
'error' => '',
|
||||
);
|
||||
|
||||
if ($record['systemSymbol'] === '' ) {
|
||||
$record['error'] = 'Missing system symbol for this waypoint.';
|
||||
$marketRecords[] = $record;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$marketResponse = $client->getWaypointMarket(
|
||||
$record['systemSymbol'],
|
||||
$record['waypointSymbol']
|
||||
);
|
||||
$record['data'] = (array) ( $marketResponse['data'] ?? array() );
|
||||
} catch (SpacetradersApiException $e) {
|
||||
$record['error'] = $e->getMessage();
|
||||
}
|
||||
|
||||
$marketRecords[] = $record;
|
||||
}
|
||||
|
||||
usort(
|
||||
$marketRecords,
|
||||
static function ( array $left, array $right ): int {
|
||||
return strcmp(
|
||||
(string) ( $left['waypointSymbol'] ?? '' ),
|
||||
(string) ( $right['waypointSymbol'] ?? '' )
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
} catch (SpacetradersApiException $e) {
|
||||
$errorMessage = $e->getMessage();
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Spacetraders - Markets</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 require __DIR__ . '/main-menu.php'; ?>
|
||||
|
||||
<h1 class="text-3xl font-bold mb-6 underline decoration-gray-300 w-full"><a href="market.php">Spacetraders - Markets</a></h1>
|
||||
|
||||
<?php if (isset( $tokenError ) ) : ?>
|
||||
<div class="mb-6 border border-red-500 p-4 rounded text-red-300">
|
||||
<?php echo htmlspecialchars( $tokenError ); ?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
<?php exit; ?>
|
||||
<?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; ?>
|
||||
|
||||
<p class="mb-6">
|
||||
Credits: <span class="font-semibold"><?php echo number_format( (int) ( $agent['credits'] ?? 0 ) ); ?></span>
|
||||
| Ships: <span class="font-semibold"><?php echo number_format( count( $ships ) ); ?></span>
|
||||
| Stationed Waypoints: <span class="font-semibold"><?php echo number_format( count( $marketRecords ) ); ?></span>
|
||||
</p>
|
||||
|
||||
<?php if (empty( $marketRecords ) ) : ?>
|
||||
<div class="border border-gray-600 p-4 rounded">
|
||||
No stationed ships found at waypoints, or no market data is available yet.
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php foreach ( $marketRecords as $record ) : ?>
|
||||
<?php
|
||||
$imports = (array) ( $record['data']['imports'] ?? array() );
|
||||
$exports = (array) ( $record['data']['exports'] ?? array() );
|
||||
$exchange = (array) ( $record['data']['exchange'] ?? array() );
|
||||
?>
|
||||
<div class="mb-6 border border-gray-600 p-4 rounded">
|
||||
<h2 class="text-2xl font-bold mb-2">
|
||||
<?php echo htmlspecialchars( (string) ( $record['waypointSymbol'] ?? '' ) ); ?>
|
||||
</h2>
|
||||
<p class="text-sm text-gray-300 mb-3">
|
||||
System: <?php echo htmlspecialchars( (string) ( $record['systemSymbol'] ?? '' ) ); ?>
|
||||
| Ships Here: <?php echo htmlspecialchars( implode( ', ', (array) ( $record['shipSymbols'] ?? array() ) ) ); ?>
|
||||
</p>
|
||||
|
||||
<?php if ((string) ( $record['error'] ?? '' ) !== '' ) : ?>
|
||||
<p class="text-red-300"><?php echo htmlspecialchars( (string) $record['error'] ); ?></p>
|
||||
<?php else : ?>
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||
<div>
|
||||
<h3 class="font-bold mb-2">Imports</h3>
|
||||
<?php if (empty( $imports ) ) : ?>
|
||||
<p class="text-gray-400 text-sm">None</p>
|
||||
<?php else : ?>
|
||||
<ul class="list-disc list-inside text-sm">
|
||||
<?php foreach ( $imports as $item ) : ?>
|
||||
<li>
|
||||
<?php echo htmlspecialchars( (string) ( $item['symbol'] ?? '' ) ); ?>
|
||||
<?php if ((string) ( $item['name'] ?? '' ) !== '' ) : ?>
|
||||
(<?php echo htmlspecialchars( (string) $item['name'] ); ?>)
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3 class="font-bold mb-2">Exports</h3>
|
||||
<?php if (empty( $exports ) ) : ?>
|
||||
<p class="text-gray-400 text-sm">None</p>
|
||||
<?php else : ?>
|
||||
<ul class="list-disc list-inside text-sm">
|
||||
<?php foreach ( $exports as $item ) : ?>
|
||||
<li>
|
||||
<?php echo htmlspecialchars( (string) ( $item['symbol'] ?? '' ) ); ?>
|
||||
<?php if ((string) ( $item['name'] ?? '' ) !== '' ) : ?>
|
||||
(<?php echo htmlspecialchars( (string) $item['name'] ); ?>)
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3 class="font-bold mb-2">Exchange</h3>
|
||||
<?php if (empty( $exchange ) ) : ?>
|
||||
<p class="text-gray-400 text-sm">None</p>
|
||||
<?php else : ?>
|
||||
<ul class="list-disc list-inside text-sm">
|
||||
<?php foreach ( $exchange as $item ) : ?>
|
||||
<li>
|
||||
<?php echo htmlspecialchars( (string) ( $item['symbol'] ?? '' ) ); ?>
|
||||
<?php if ((string) ( $item['name'] ?? '' ) !== '' ) : ?>
|
||||
(<?php echo htmlspecialchars( (string) $item['name'] ); ?>)
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user