* @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(); $system = array(); $systemWaypoints = array(); $paginatedWaypoints = array(); $marketDetails = array(); $shipyardDetails = array(); $selectedShipSymbol = ''; $selectedWaypointSymbol = ''; $waypointPageSize = 15; $waypointPage = isset( $_GET['waypoint_page'] ) ? max( 1, (int) $_GET['waypoint_page'] ) : 1; $waypointTotalPages = 1; if (! isset( $tokenError ) ) { $client = new SpacetradersApi( trim( $token ), $config['api_base_url'], (int) $config['api_timeout'], $storage, (int) $config['cache_ttl'] ); if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['navigate_ship'] ) && isset( $_POST['ship_symbol'] ) && isset( $_POST['waypoint_symbol'] )) { $selectedShipSymbol = trim( (string) $_POST['ship_symbol'] ); $selectedWaypointSymbol = trim( (string) $_POST['waypoint_symbol'] ); if ($selectedShipSymbol !== '' && $selectedWaypointSymbol !== '' ) { try { $selectedShipResponse = $client->getShip( $selectedShipSymbol ); $selectedShipData = $selectedShipResponse['data'] ?? array(); $shipNavStatus = (string) ( $selectedShipData['nav']['status'] ?? '' ); if ($shipNavStatus === 'IN_TRANSIT' ) { throw new SpacetradersApiException( 'Selected ship is currently in transit and cannot navigate yet.' ); } if ($shipNavStatus === 'DOCKED' ) { $client->orbitShip( $selectedShipSymbol ); } $client->navigateShip( $selectedShipSymbol, $selectedWaypointSymbol ); $storage->clearAllCache(); $statusMessage = 'Navigation started for ' . $selectedShipSymbol . '.'; } catch (SpacetradersApiException $e) { $errorMessage = 'Unable to navigate ship: ' . $e->getMessage(); } } } 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; $currentSystemSymbol = ''; 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 !== '' ) { $systemResponse = $client->getSystem( $currentSystemSymbol ); $system = $systemResponse['data'] ?? $systemResponse; $page = 1; $total = 0; do { $waypointsResponse = $client->listWaypoints( $currentSystemSymbol, array( 'page' => $page, 'limit' => 20, ) ); $pageData = $waypointsResponse['data'] ?? array(); if (! is_array( $pageData ) || empty( $pageData ) ) { break; } $systemWaypoints = array_merge( $systemWaypoints, $pageData ); $total = (int) ( $waypointsResponse['meta']['total'] ?? count( $systemWaypoints ) ); $page++; } while (count( $systemWaypoints ) < $total); foreach ( $systemWaypoints as $waypoint ) { $waypointSymbol = (string) ( $waypoint['symbol'] ?? '' ); $traits = $waypoint['traits'] ?? array(); $hasMarket = false; $hasShipyard = false; foreach ( $traits as $trait ) { $traitSymbol = (string) ( $trait['symbol'] ?? '' ); if ($traitSymbol === 'MARKETPLACE' ) { $hasMarket = true; } if ($traitSymbol === 'SHIPYARD' ) { $hasShipyard = true; } } if ($hasMarket ) { $marketRecord = array( 'waypoint' => $waypoint, 'data' => array(), 'error' => '', ); try { $marketResponse = $client->getWaypointMarket( $currentSystemSymbol, $waypointSymbol ); $marketRecord['data'] = $marketResponse['data'] ?? array(); } catch (SpacetradersApiException $e) { $marketRecord['error'] = $e->getMessage(); } $marketDetails[] = $marketRecord; } if ($hasShipyard ) { $shipyardRecord = array( 'waypoint' => $waypoint, 'data' => array(), 'error' => '', ); try { $shipyardResponse = $client->getWaypointShipyard( $currentSystemSymbol, $waypointSymbol ); $shipyardRecord['data'] = $shipyardResponse['data'] ?? array(); } catch (SpacetradersApiException $e) { $shipyardRecord['error'] = $e->getMessage(); } $shipyardDetails[] = $shipyardRecord; } } $waypointCount = count( $systemWaypoints ); if ($waypointCount > 0 ) { $waypointTotalPages = (int) ceil( $waypointCount / $waypointPageSize ); $waypointPage = min( $waypointPage, $waypointTotalPages ); $offset = ( $waypointPage - 1 ) * $waypointPageSize; $paginatedWaypoints = array_slice( $systemWaypoints, $offset, $waypointPageSize ); } } } } 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 ); } ?> Spacetraders - Dashboard

Open Configuration Page
Buy Ships | Mining Fleet | Configuration

Spacetraders Agent and Ships

Agent:
Credits:

Headquarters:
Faction:
Ship Count:
System:

System Details

Current System: ()

0 ? ( ( $waypointPage - 1 ) * $waypointPageSize ) + 1 : 0; $waypointEnd = min( $waypointPage * $waypointPageSize, $waypointCount ); ?>

Showing - of waypoints

Symbol Type Coordinates Traits
,

Ships

Name Role Type Status Flight Mode Route
N/A N/A

Contracts

Contract : -

Delivery Details: units delivered to

Payment: on Accept, on Fulfill

Deadline To Accept:

Deadline:

Status: Accept?