* @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 = ''; $agent = array(); $ships = array(); $stationedShips = array(); $selectedShipSymbol = ''; $selectedShip = array(); $selectedMarketData = array(); $selectedMarketError = ''; $selectedSystemSymbol = ''; $selectedWaypointSymbol = ''; $preferredWaypoint = trim( (string) ( $_GET['waypoint'] ?? '' ) ); 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; foreach ( $ships as $ship ) { $status = (string) ( $ship['nav']['status'] ?? '' ); if ($status === 'IN_TRANSIT' ) { continue; } $stationedShips[] = $ship; } $selectedShipSymbol = trim( (string) ( $_POST['ship_symbol'] ?? '' ) ); if ($selectedShipSymbol === '' ) { $selectedShipSymbol = trim( (string) ( $_GET['ship'] ?? '' ) ); } if ($selectedShipSymbol === '' && $preferredWaypoint !== '' ) { foreach ( $stationedShips as $stationedShip ) { $waypointSymbol = (string) ( $stationedShip['nav']['waypointSymbol'] ?? '' ); if ($waypointSymbol === $preferredWaypoint ) { $selectedShipSymbol = (string) ( $stationedShip['symbol'] ?? '' ); break; } } } if ($selectedShipSymbol === '' && ! empty( $stationedShips ) ) { $selectedShipSymbol = (string) ( $stationedShips[0]['symbol'] ?? '' ); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['buy_cargo'] ) ) { $selectedShipSymbol = trim( (string) ( $_POST['ship_symbol'] ?? '' ) ); $tradeSymbol = trim( (string) ( $_POST['trade_symbol'] ?? '' ) ); $units = (int) ( $_POST['units'] ?? 0 ); if ($selectedShipSymbol === '' || $tradeSymbol === '' || $units <= 0 ) { throw new SpacetradersApiException( 'Ship, trade symbol, and units are required.' ); } $selectedShipResponse = $client->getShip( $selectedShipSymbol ); $selectedShipData = $selectedShipResponse['data'] ?? array(); $shipStatus = (string) ( $selectedShipData['nav']['status'] ?? '' ); if ($shipStatus === 'IN_TRANSIT' ) { throw new SpacetradersApiException( 'Selected ship is currently in transit.' ); } if ($shipStatus !== 'DOCKED' ) { $client->dockShip( $selectedShipSymbol ); } $client->purchaseCargo( $selectedShipSymbol, $tradeSymbol, $units ); $storage->clearAllCache(); $statusMessage = 'Purchased ' . number_format( $units ) . ' units of ' . $tradeSymbol . '.'; $shipsResponse = $client->listMyShips(); $ships = $shipsResponse['data'] ?? $shipsResponse; $stationedShips = array(); foreach ( $ships as $ship ) { $status = (string) ( $ship['nav']['status'] ?? '' ); if ($status === 'IN_TRANSIT' ) { continue; } $stationedShips[] = $ship; } } if ($selectedShipSymbol !== '' ) { $selectedShipResponse = $client->getShip( $selectedShipSymbol ); $selectedShip = $selectedShipResponse['data'] ?? array(); $selectedSystemSymbol = (string) ( $selectedShip['nav']['systemSymbol'] ?? '' ); $selectedWaypointSymbol = (string) ( $selectedShip['nav']['waypointSymbol'] ?? '' ); if ($selectedSystemSymbol !== '' && $selectedWaypointSymbol !== '' ) { try { $marketResponse = $client->getWaypointMarket( $selectedSystemSymbol, $selectedWaypointSymbol ); $selectedMarketData = (array) ( $marketResponse['data'] ?? array() ); } catch (SpacetradersApiException $e) { $selectedMarketError = $e->getMessage(); } } } } } catch (SpacetradersApiException $e) { $errorMessage = $e->getMessage(); } ?> Spacetraders - Shop

Spacetraders - Shop

No stationed ships are available for shopping.

Selected Ship:

Location:

Cargo: /

No trade goods available for this market yet.
Symbol Type Supply Purchase Price Sell Price Buy
0 ) : ?>
N/A