diff --git a/agent-info.php b/agent-info.php deleted file mode 100644 index b4e1046..0000000 --- a/agent-info.php +++ /dev/null @@ -1,565 +0,0 @@ - - * @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 = 25; -$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(), - ); - - 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 ); -} -?> - - - - - - - Spacetraders - Agent Information - - - - - - -
-

- Open Configuration Page -
- - - - - -
- Configuration - | - Buy Ships - | - Mining Fleet -
- - -
- -
- - - -
- -
- - -

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 -

-
- 1 ) : ?> - - Previous - - - - - Next - - -
-
-
- - - - - - - - - - - - - - - -
SymbolTypeCoordinatesTraits
- , - - - -
-
-
- - - - -
- -

Ships

- - - - - - - - - - - - - - - - - - - - - - - - - -
NameRoleTypeStatusFlight ModeRoute
- - - - N/AN/A
- -

Contracts

-
- -
-

Contract : -

- -

- Delivery Details: units delivered to -

- -

- Payment: on Accept, on Fulfill -

- -

Deadline To Accept:

-

Deadline:

- -

- Status: - - - - Accept? - - - -

-
- - -
- - - - diff --git a/buy-ships.php b/buy-ships.php index c6da396..36733b8 100644 --- a/buy-ships.php +++ b/buy-ships.php @@ -160,7 +160,7 @@ try {
- Agent Info + Agent Info Mining Fleet Configuration
diff --git a/config.php b/config.php index 50d422a..f62a0eb 100644 --- a/config.php +++ b/config.php @@ -90,7 +90,7 @@ $hasToken = is_string( $token ) && trim( $token ) !== '';
- Agent Info + Agent Info Buy Ships Mining Fleet
diff --git a/index.php b/index.php index d1ec175..6cd9a32 100644 --- a/index.php +++ b/index.php @@ -1,15 +1,221 @@ - * @license MIT License - * @link https://git.keithsolomon.net/keith/Spacetraders + * @package SpacetradersApi + * @author Keith Solomon + * @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 ); +} ?> @@ -17,26 +223,350 @@ - Spacetraders - Agent Information + Spacetraders - Dashboard - + +
+

+ Open Configuration Page +
+ + + + -

Spacetraders Playground

+
+ Buy Ships + | + Mining Fleet + | + Configuration +
-
    - ' . $file . ''; - } + if ($statusMessage !== '' ) { + $msg = $statusMessage; + $class = 'border-green-500 text-green-300'; + } elseif ($errorMessage !== '' ) { + $msg = $errorMessage; + $class = 'border-red-500 text-red-300'; + } + + if ($msg !== '' ) : ?> -
+
+ +
+ + +

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 +

+
+ 1 ) : ?> + + Previous + + + + + Next + + +
+
+
+ + + + + + + + + + + + + + + +
SymbolTypeCoordinatesTraits
+ , + + + +
+
+
+ + + + +
+ +

Ships

+ + + + + + + + + + + + + + + + + + + + + + + + + +
NameRoleTypeStatusFlight ModeRoute
+ + + + N/AN/A
+ +

Contracts

+
+ +
+

Contract : -

+ +

+ Delivery Details: units delivered to +

+ +

+ Payment: on Accept, on Fulfill +

+ +

Deadline To Accept:

+

Deadline:

+ +

+ Status: + + + + Accept? + + + +

+
+ + +
+ + diff --git a/mining-fleet.php b/mining-fleet.php index 8e89988..21549f6 100644 --- a/mining-fleet.php +++ b/mining-fleet.php @@ -376,7 +376,7 @@ try {
- Agent Info + Agent Info Buy Ships Configuration
diff --git a/ship-details.php b/ship-details.php index 50c492b..7e1e721 100644 --- a/ship-details.php +++ b/ship-details.php @@ -205,7 +205,7 @@ try {
- Agent Info + Agent Info Mining Fleet Buy Ships Configuration