* @license MIT License * @version GIT: * @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 = ''; $ships = array(); $shipyards = array(); $agent = array(); $currentSystemSymbol = ''; 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 ) ) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['ship_type'] ) && isset( $_POST['waypoint_symbol'] )) { $shipType = trim( (string) $_POST['ship_type'] ); $waypointSymbol = trim( (string) $_POST['waypoint_symbol'] ); if ($shipType !== '' && $waypointSymbol !== '' ) { $purchaseResponse = $client->purchaseShip( $shipType, $waypointSymbol ); $shipSymbol = (string) ( $purchaseResponse['data']['ship']['symbol'] ?? '' ); $storage->clearAllCache(); $statusMessage = 'Purchased ' . $shipType . ( $shipSymbol !== '' ? ' (' . $shipSymbol . ')' : '' ) . '.'; } } $agentResponse = $client->getMyAgent(); $shipsResponse = $client->listMyShips(); $agent = $agentResponse['data'] ?? $agentResponse; $ships = $shipsResponse['data'] ?? $shipsResponse; if (isset( $ships[0]['nav']['systemSymbol'] ) && is_string( $ships[0]['nav']['systemSymbol'] ) ) { $currentSystemSymbol = $ships[0]['nav']['systemSymbol']; } if ($currentSystemSymbol === '' && isset( $agent['headquarters'] ) && is_string( $agent['headquarters'] ) ) { $hqParts = explode( '-', $agent['headquarters'] ); if (count( $hqParts ) >= 2 ) { $currentSystemSymbol = $hqParts[0] . '-' . $hqParts[1]; } } if ($currentSystemSymbol !== '' ) { $page = 1; $total = 0; $waypoints = array(); do { $waypointsResponse = $client->listWaypoints( $currentSystemSymbol, array( 'page' => $page, 'limit' => 20, ) ); $pageData = $waypointsResponse['data'] ?? array(); if (! is_array( $pageData ) || empty( $pageData ) ) { break; } $waypoints = array_merge( $waypoints, $pageData ); $total = (int) ( $waypointsResponse['meta']['total'] ?? count( $waypoints ) ); $page++; } while (count( $waypoints ) < $total); foreach ( $waypoints as $waypoint ) { $traits = $waypoint['traits'] ?? array(); $hasShipyard = false; foreach ( $traits as $trait ) { if ((string) ( $trait['symbol'] ?? '' ) === 'SHIPYARD' ) { $hasShipyard = true; break; } } if (! $hasShipyard ) { continue; } $waypointSymbol = (string) ( $waypoint['symbol'] ?? '' ); $record = array( 'waypoint' => $waypoint, 'shipTypes' => array(), 'prices' => array(), 'error' => '', ); try { $shipyardResponse = $client->getWaypointShipyard( $currentSystemSymbol, $waypointSymbol ); $shipyardData = $shipyardResponse['data'] ?? array(); $record['shipTypes'] = (array) ( $shipyardData['shipTypes'] ?? array() ); $transactions = (array) ( $shipyardData['transactions'] ?? array() ); foreach ( $transactions as $transaction ) { $shipType = (string) ( $transaction['shipType'] ?? '' ); $price = (int) ( $transaction['price'] ?? 0 ); if ($shipType !== '' && $price > 0 && ! isset( $record['prices'][ $shipType ] ) ) { $record['prices'][ $shipType ] = $price; } } } catch (SpacetradersApiException $e) { $record['error'] = $e->getMessage(); } $shipyards[] = $record; } } } } catch (SpacetradersApiException $e) { $errorMessage = $e->getMessage(); } ?> Spacetraders - Buy Ships

Spacetraders - Buy Ships

Credits: | Current System:

No shipyards were found in your current system.

Shipyard:

Ship Type Known Price Action
0 ? number_format( $knownPrice ) : 'Unknown'; ?>