diff --git a/index.php b/index.php index 9909ca8..2b1bdd7 100644 --- a/index.php +++ b/index.php @@ -40,6 +40,10 @@ $systemWaypoints = array(); $paginatedWaypoints = array(); $marketDetails = array(); $shipyardDetails = array(); +$activeContracts = array(); +$pendingContracts = array(); +$deliveryReadyByTradeSymbol = array(); +$contractNegotiationShipSymbol = ''; $selectedShipSymbol = ''; $selectedWaypointSymbol = ''; $waypointPageSize = 15; @@ -103,6 +107,161 @@ try { $ships = $shipsResponse['data'] ?? $shipsResponse; $contracts = $contractsResponse['data'] ?? $contractsResponse; + if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['contract_action'] ) ) { + $contractAction = (string) $_POST['contract_action']; + + if ($contractAction === 'fulfill_contract' ) { + $contractId = trim( (string) ( $_POST['contract_id'] ?? '' ) ); + if ($contractId !== '' ) { + $client->fulfillContract( $contractId ); + $storage->clearAllCache(); + $statusMessage = 'Contract ' . $contractId . ' fulfilled.'; + } + } elseif ($contractAction === 'deliver_contract' ) { + $contractId = trim( (string) ( $_POST['contract_id'] ?? '' ) ); + if ($contractId !== '' ) { + $contractToDeliver = null; + foreach ( (array) $contracts as $contract ) { + if ((string) ( $contract['id'] ?? '' ) === $contractId ) { + $contractToDeliver = $contract; + break; + } + } + + if (! is_array( $contractToDeliver ) ) { + throw new SpacetradersApiException( 'Contract not found.' ); + } + + $deliveries = (array) ( $contractToDeliver['terms']['deliver'] ?? array() ); + $deliveryAttempts = 0; + + foreach ( $deliveries as $delivery ) { + $tradeSymbol = (string) ( $delivery['tradeSymbol'] ?? '' ); + $destinationSymbol = (string) ( $delivery['destinationSymbol'] ?? '' ); + $unitsRequired = (int) ( $delivery['unitsRequired'] ?? 0 ); + $unitsFulfilled = (int) ( $delivery['unitsFulfilled'] ?? 0 ); + $unitsRemaining = max( 0, $unitsRequired - $unitsFulfilled ); + + if ($tradeSymbol === '' || $destinationSymbol === '' || $unitsRemaining <= 0 ) { + continue; + } + + foreach ( $ships as $ship ) { + if ($unitsRemaining <= 0 ) { + break; + } + + $shipSymbol = (string) ( $ship['symbol'] ?? '' ); + $shipStatus = (string) ( $ship['nav']['status'] ?? '' ); + $shipWaypoint = (string) ( $ship['nav']['waypointSymbol'] ?? '' ); + + if ($shipSymbol === '' || $shipStatus === 'IN_TRANSIT' || $shipWaypoint !== $destinationSymbol ) { + continue; + } + + $shipResponse = $client->getShip( $shipSymbol ); + $shipData = $shipResponse['data'] ?? array(); + + $currentStatus = (string) ( $shipData['nav']['status'] ?? '' ); + if ($currentStatus === 'IN_TRANSIT' ) { + continue; + } + + if ($currentStatus !== 'DOCKED' ) { + $client->dockShip( $shipSymbol ); + } + + $availableUnits = 0; + $inventory = (array) ( $shipData['cargo']['inventory'] ?? array() ); + foreach ( $inventory as $item ) { + if ((string) ( $item['symbol'] ?? '' ) === $tradeSymbol ) { + $availableUnits = (int) ( $item['units'] ?? 0 ); + break; + } + } + + if ($availableUnits <= 0 ) { + continue; + } + + $deliverUnits = min( $availableUnits, $unitsRemaining ); + if ($deliverUnits <= 0 ) { + continue; + } + + $client->deliverContractCargo( $contractId, $shipSymbol, $tradeSymbol, $deliverUnits ); + $unitsRemaining -= $deliverUnits; + $deliveryAttempts++; + } + } + + if ($deliveryAttempts > 0 ) { + $storage->clearAllCache(); + $statusMessage = 'Delivered contract cargo using ' . $deliveryAttempts . ' shipment(s) for contract ' . $contractId . '.'; + } else { + $errorMessage = 'No eligible cargo/ships available to deliver for contract ' . $contractId . '.'; + } + } + } elseif ($contractAction === 'negotiate_contract' ) { + $negotiateShipSymbol = trim( (string) ( $_POST['negotiate_ship_symbol'] ?? '' ) ); + if ($negotiateShipSymbol === '' ) { + $negotiateShipSymbol = (string) ( $ships[0]['symbol'] ?? '' ); + } + + if ($negotiateShipSymbol === '' ) { + throw new SpacetradersApiException( 'No available ship found to negotiate a contract.' ); + } + + $client->negotiateContract( $negotiateShipSymbol ); + $storage->clearAllCache(); + $statusMessage = 'Negotiated a new contract using ' . $negotiateShipSymbol . '.'; + } + + if ($statusMessage !== '' || $errorMessage !== '' ) { + $shipsResponse = $client->listMyShips(); + $contractsResponse = $client->listMyContracts(); + $ships = $shipsResponse['data'] ?? $shipsResponse; + $contracts = $contractsResponse['data'] ?? $contractsResponse; + } + } + + $activeContracts = array_values( + array_filter( + (array) $contracts, + static function ( array $contract ): bool { + return (bool) ( $contract['accepted'] ?? false ) && ! (bool) ( $contract['fulfilled'] ?? false ); + } + ) + ); + + $pendingContracts = array_values( + array_filter( + (array) $contracts, + static function ( array $contract ): bool { + return ! (bool) ( $contract['accepted'] ?? false ) && ! (bool) ( $contract['fulfilled'] ?? false ); + } + ) + ); + + foreach ( $ships as $ship ) { + $inventory = (array) ( $ship['cargo']['inventory'] ?? array() ); + foreach ( $inventory as $item ) { + $tradeSymbol = (string) ( $item['symbol'] ?? '' ); + $units = (int) ( $item['units'] ?? 0 ); + if ($tradeSymbol === '' || $units <= 0 ) { + continue; + } + + if (! isset( $deliveryReadyByTradeSymbol[ $tradeSymbol ] ) ) { + $deliveryReadyByTradeSymbol[ $tradeSymbol ] = 0; + } + + $deliveryReadyByTradeSymbol[ $tradeSymbol ] += $units; + } + } + + $contractNegotiationShipSymbol = (string) ( $ships[0]['symbol'] ?? '' ); + $currentSystemSymbol = ''; if (isset( $ships[0]['nav']['systemSymbol'] ) && is_string( $ships[0]['nav']['systemSymbol'] ) ) { @@ -468,7 +627,7 @@ try { - +

Contracts

-
- -
-

Contract : -

- -

- Delivery Details: units delivered to -

- -

- Payment: on Accept, on Fulfill -

- -

Deadline To Accept:

-

Deadline:

- -

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

+

Active Contracts

+ +

No active contracts.

+ +
+ + + +
+

+ Uses ship:

+ + +
+ + +
+

Contract:

+

Type:

+

+ Deadline: + +

+

+ Payment: + + + + +

+
+ + + +
+
+ + + +
+ +

Deliveries:

+
    + + +
  • + : + / + to + | Ready: + $deliverableNow ) : ?> + ( in cargo) + +
  • + +
+ +
+
- - +
+ +
+

Pending Contracts

+
+ +
+ +

Contract:

+

Type:

+

Deadline To Accept:

+ +

Requirements:

+
    + + +
  • + : + / + to +
  • + +
+ + + Accept Contract + +
+ +
+
+ + + + + + +

Spacetraders - Shop

+ + +
+ +
+ + + + + + +
+ +
+ + + +
+ +
+ + +

+ Credits: + | Stationed Ships: +

+ +
+
+ + +
+ +
+ + +
+ No stationed ships are available for shopping. +
+ +
+

+ Selected Ship: + +

+

+ Location: + +

+

+ Cargo: + + / + +

+
+ + +
+ +
+ + + +
+ No trade goods available for this market yet. +
+ + + + + + + + + + + + + + + + + + + + + +
SymbolTypeSupplyPurchase PriceSell PriceBuy
+ 0 ) : ?> +
+ + + + + +
+ + N/A + +
+ + + + +