✨feature: Add cooldown/navigation timers, move credits display to header
This commit is contained in:
346
mining-fleet.php
346
mining-fleet.php
@@ -31,6 +31,70 @@ function filterMiningShips( array $ships ): array {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build contract delivery targets for outstanding delivery requirements.
|
||||
*
|
||||
* @param array<int,array<string,mixed>> $activeContracts
|
||||
*
|
||||
* @return array<int,array<string,mixed>>
|
||||
*/
|
||||
function buildContractDeliveryTargets( array $activeContracts ): array {
|
||||
$targetsByKey = array();
|
||||
|
||||
foreach ( $activeContracts as $contract ) {
|
||||
$contractId = (string) ( $contract['id'] ?? '' );
|
||||
$deliveries = (array) ( $contract['terms']['deliver'] ?? array() );
|
||||
|
||||
if ($contractId === '' || empty( $deliveries ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $deliveries as $delivery ) {
|
||||
$tradeSymbol = (string) ( $delivery['tradeSymbol'] ?? '' );
|
||||
$destinationSymbol = (string) ( $delivery['destinationSymbol'] ?? '' );
|
||||
$unitsRequired = (int) ( $delivery['unitsRequired'] ?? 0 );
|
||||
$unitsFulfilled = (int) ( $delivery['unitsFulfilled'] ?? 0 );
|
||||
$remainingUnits = max( 0, $unitsRequired - $unitsFulfilled );
|
||||
|
||||
if ($tradeSymbol === '' || $destinationSymbol === '' || $remainingUnits <= 0 ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = $contractId . '|' . $destinationSymbol . '|' . $tradeSymbol;
|
||||
|
||||
if (! isset( $targetsByKey[ $key ] ) ) {
|
||||
$targetsByKey[ $key ] = array(
|
||||
'key' => $key,
|
||||
'contractId' => $contractId,
|
||||
'destinationSymbol' => $destinationSymbol,
|
||||
'tradeSymbol' => $tradeSymbol,
|
||||
'remainingUnits' => 0,
|
||||
);
|
||||
}
|
||||
|
||||
$targetsByKey[ $key ]['remainingUnits'] += $remainingUnits;
|
||||
}
|
||||
}
|
||||
|
||||
$targets = array_values( $targetsByKey );
|
||||
|
||||
usort(
|
||||
$targets,
|
||||
static function ( array $left, array $right ): int {
|
||||
$leftSort = (string) ( $left['destinationSymbol'] ?? '' ) . '|' .
|
||||
(string) ( $left['tradeSymbol'] ?? '' ) . '|' .
|
||||
(string) ( $left['contractId'] ?? '' );
|
||||
$rightSort = (string) ( $right['destinationSymbol'] ?? '' ) . '|' .
|
||||
(string) ( $right['tradeSymbol'] ?? '' ) . '|' .
|
||||
(string) ( $right['contractId'] ?? '' );
|
||||
|
||||
return strcmp( $leftSort, $rightSort );
|
||||
}
|
||||
);
|
||||
|
||||
return $targets;
|
||||
}
|
||||
|
||||
$config = require __DIR__ . '/lib/project-config.php';
|
||||
|
||||
$storage = new SpacetradersStorage( $config['db_path'] );
|
||||
@@ -44,7 +108,9 @@ $ships = array();
|
||||
$miningShips = array();
|
||||
$activeContracts = array();
|
||||
$marketWaypoints = array();
|
||||
$contractDeliveryTargets = array();
|
||||
$selectedMarketWaypoint = '';
|
||||
$selectedContractDeliveryKey = '';
|
||||
|
||||
if (! is_string( $token ) || trim( $token ) === '' ) {
|
||||
$envToken = getenv( 'SPACETRADERS_TOKEN' );
|
||||
@@ -135,10 +201,24 @@ try {
|
||||
$selectedMarketWaypoint = isset( $_POST['market_waypoint'] ) ?
|
||||
trim( (string) $_POST['market_waypoint'] ) :
|
||||
( $marketWaypoints[0] ?? '' );
|
||||
$contractDeliveryTargets = buildContractDeliveryTargets( $activeContracts );
|
||||
$selectedContractDeliveryKey = isset( $_POST['contract_delivery_key'] ) ?
|
||||
trim( (string) $_POST['contract_delivery_key'] ) :
|
||||
(string) ( $contractDeliveryTargets[0]['key'] ?? '' );
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['fleet_action'] ) ) {
|
||||
$fleetAction = (string) $_POST['fleet_action'];
|
||||
$successCount = 0;
|
||||
$selectedContractDeliveryTarget = null;
|
||||
$remainingContractUnits = 0;
|
||||
|
||||
foreach ( $contractDeliveryTargets as $target ) {
|
||||
if ((string) ( $target['key'] ?? '' ) === $selectedContractDeliveryKey ) {
|
||||
$selectedContractDeliveryTarget = $target;
|
||||
$remainingContractUnits = (int) ( $target['remainingUnits'] ?? 0 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $miningShips as $fleetShip ) {
|
||||
$shipSymbol = (string) ( $fleetShip['symbol'] ?? '' );
|
||||
@@ -277,6 +357,93 @@ try {
|
||||
|
||||
$actionResults[] = $shipSymbol . ': jettisoned ' . $jettisonedItems . ' cargo item type(s).';
|
||||
$successCount++;
|
||||
} elseif ($fleetAction === 'navigate_contract_delivery' ) {
|
||||
if (! is_array( $selectedContractDeliveryTarget ) ) {
|
||||
$actionResults[] = $shipSymbol . ': no contract delivery target selected.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($shipStatus === 'IN_TRANSIT' ) {
|
||||
$actionResults[] = $shipSymbol . ': already in transit.';
|
||||
continue;
|
||||
}
|
||||
|
||||
$destinationSymbol = (string) ( $selectedContractDeliveryTarget['destinationSymbol'] ?? '' );
|
||||
if ($destinationSymbol === '' ) {
|
||||
$actionResults[] = $shipSymbol . ': delivery destination is missing.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($waypointSymbol !== $destinationSymbol ) {
|
||||
if ($shipStatus === 'DOCKED' ) {
|
||||
$client->orbitShip( $shipSymbol );
|
||||
}
|
||||
|
||||
$client->navigateShip( $shipSymbol, $destinationSymbol );
|
||||
$actionResults[] = $shipSymbol . ': navigating to contract delivery waypoint ' . $destinationSymbol . '.';
|
||||
$successCount++;
|
||||
} else {
|
||||
$actionResults[] = $shipSymbol . ': already at contract delivery waypoint.';
|
||||
}
|
||||
} elseif ($fleetAction === 'deliver_contract_goods' ) {
|
||||
if (! is_array( $selectedContractDeliveryTarget ) ) {
|
||||
$actionResults[] = $shipSymbol . ': no contract delivery target selected.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($shipStatus === 'IN_TRANSIT' ) {
|
||||
$actionResults[] = $shipSymbol . ': skipped (in transit).';
|
||||
continue;
|
||||
}
|
||||
|
||||
$contractId = (string) ( $selectedContractDeliveryTarget['contractId'] ?? '' );
|
||||
$destinationSymbol = (string) ( $selectedContractDeliveryTarget['destinationSymbol'] ?? '' );
|
||||
$tradeSymbol = (string) ( $selectedContractDeliveryTarget['tradeSymbol'] ?? '' );
|
||||
|
||||
if ($contractId === '' || $destinationSymbol === '' || $tradeSymbol === '' ) {
|
||||
$actionResults[] = $shipSymbol . ': selected contract delivery target is incomplete.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($remainingContractUnits <= 0 ) {
|
||||
$actionResults[] = $shipSymbol . ': nothing remaining to deliver for this target.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($waypointSymbol !== $destinationSymbol ) {
|
||||
$actionResults[] = $shipSymbol . ': not at delivery waypoint (' . $destinationSymbol . ').';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($shipStatus !== 'DOCKED' ) {
|
||||
$client->dockShip( $shipSymbol );
|
||||
}
|
||||
|
||||
$inventory = (array) ( $shipData['cargo']['inventory'] ?? array() );
|
||||
$availableUnits = 0;
|
||||
foreach ( $inventory as $item ) {
|
||||
if ((string) ( $item['symbol'] ?? '' ) === $tradeSymbol ) {
|
||||
$availableUnits = (int) ( $item['units'] ?? 0 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($availableUnits <= 0 ) {
|
||||
$actionResults[] = $shipSymbol . ': no ' . $tradeSymbol . ' in cargo.';
|
||||
continue;
|
||||
}
|
||||
|
||||
$deliverUnits = min( $availableUnits, $remainingContractUnits );
|
||||
|
||||
if ($deliverUnits <= 0 ) {
|
||||
$actionResults[] = $shipSymbol . ': no units available to deliver.';
|
||||
continue;
|
||||
}
|
||||
|
||||
$client->deliverContractCargo( $contractId, $shipSymbol, $tradeSymbol, $deliverUnits );
|
||||
$remainingContractUnits -= $deliverUnits;
|
||||
$actionResults[] = $shipSymbol . ': delivered ' . $deliverUnits . ' of ' . $tradeSymbol . ' for contract ' . $contractId . '.';
|
||||
$successCount++;
|
||||
}
|
||||
} catch (SpacetradersApiException $e) {
|
||||
$actionResults[] = $shipSymbol . ': ' . $e->getMessage();
|
||||
@@ -302,6 +469,10 @@ try {
|
||||
}
|
||||
)
|
||||
);
|
||||
$contractDeliveryTargets = buildContractDeliveryTargets( $activeContracts );
|
||||
$selectedContractDeliveryKey = isset( $_POST['contract_delivery_key'] ) ?
|
||||
trim( (string) $_POST['contract_delivery_key'] ) :
|
||||
(string) ( $contractDeliveryTargets[0]['key'] ?? '' );
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['ship_action'] ) && in_array( (string) $_POST['ship_action'], array( 'sell_ship_cargo', 'jettison_ship_cargo' ), true ) && isset( $_POST['ship_symbol'] )) {
|
||||
@@ -362,6 +533,34 @@ try {
|
||||
} catch (SpacetradersApiException $e) {
|
||||
$errorMessage = $e->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a string by replacing underscores with spaces and capitalizing the first letter.
|
||||
*
|
||||
* @param string $value The string to format.
|
||||
* @return string The formatted string.
|
||||
*/
|
||||
function formatString( $value ): string {
|
||||
return ucfirst( strtolower( str_replace( '_', ' ', $value ) ) );
|
||||
}
|
||||
|
||||
$deliveryReadyByTradeSymbol = array();
|
||||
foreach ( $miningShips as $miningShip ) {
|
||||
$inventory = (array) ( $miningShip['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;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
@@ -413,6 +612,32 @@ try {
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="contract_delivery_key" class="block text-sm mb-1">Contract Delivery Target</label>
|
||||
<select id="contract_delivery_key" name="contract_delivery_key" class="px-3 py-2 rounded text-black min-w-80">
|
||||
<?php if (empty( $contractDeliveryTargets ) ) : ?>
|
||||
<option value="">No outstanding delivery targets</option>
|
||||
<?php else : ?>
|
||||
<?php foreach ( $contractDeliveryTargets as $target ) : ?>
|
||||
<?php
|
||||
$targetKey = (string) ( $target['key'] ?? '' );
|
||||
$targetLabel = 'Contract ' .
|
||||
(string) ( $target['contractId'] ?? '' ) .
|
||||
' | ' .
|
||||
(string) ( formatString( $target['tradeSymbol'] ?? '' ) ) .
|
||||
' | Remaining ' .
|
||||
number_format( (int) ( $target['remainingUnits'] ?? 0 ) ) .
|
||||
' | To ' .
|
||||
(string) ( $target['destinationSymbol'] ?? '' );
|
||||
?>
|
||||
<option value="<?php echo htmlspecialchars( $targetKey ); ?>" <?php echo $targetKey === $selectedContractDeliveryKey ? 'selected' : ''; ?>>
|
||||
<?php echo htmlspecialchars( $targetLabel ); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button type="submit" name="fleet_action" value="excavate_all" class="px-4 py-2 bg-emerald-700 rounded hover:bg-emerald-600">
|
||||
Command All: Excavate
|
||||
</button>
|
||||
@@ -428,6 +653,12 @@ try {
|
||||
<button type="submit" name="fleet_action" value="jettison_cargo_all" class="px-4 py-2 bg-red-800 rounded hover:bg-red-700">
|
||||
Command All: Jettison Cargo
|
||||
</button>
|
||||
<button type="submit" name="fleet_action" value="navigate_contract_delivery" class="px-4 py-2 bg-indigo-700 rounded hover:bg-indigo-600">
|
||||
Command All: Navigate to Contract Delivery
|
||||
</button>
|
||||
<button type="submit" name="fleet_action" value="deliver_contract_goods" class="px-4 py-2 bg-violet-700 rounded hover:bg-violet-600">
|
||||
Command All: Deliver Contract Goods
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -458,8 +689,8 @@ try {
|
||||
$deliveries = (array) ( $contract['terms']['deliver'] ?? array() );
|
||||
?>
|
||||
<div class="border border-gray-500 rounded p-3">
|
||||
<p><span class="font-bold">Contract:</span> <?php echo htmlspecialchars( (string) ( $contract['id'] ?? '' ) ); ?></p>
|
||||
<p><span class="font-bold">Type:</span> <?php echo htmlspecialchars( $contractType ); ?></p>
|
||||
<p><span class="font-bold">Contract:</span> <?php echo htmlspecialchars( (string) ( formatString( $contract['id'] ?? '' ) ) ); ?></p>
|
||||
<p><span class="font-bold">Type:</span> <?php echo htmlspecialchars( formatString( $contractType ) ); ?></p>
|
||||
<p>
|
||||
<span class="font-bold">Deadline:</span>
|
||||
<?php echo htmlspecialchars( (string) date( 'Y-m-d H:i:s', strtotime( (string) ( $contract['terms']['deadline'] ?? '' ) ) ) ); ?>
|
||||
@@ -475,13 +706,21 @@ try {
|
||||
<ul class="list-disc list-inside text-sm text-gray-300">
|
||||
<?php foreach ( $deliveries as $delivery ) : ?>
|
||||
<?php
|
||||
$tradeSymbol = (string) ( $delivery['tradeSymbol'] ?? '' );
|
||||
$unitsRequired = (int) ( $delivery['unitsRequired'] ?? 0 );
|
||||
$unitsFulfilled = (int) ( $delivery['unitsFulfilled'] ?? 0 );
|
||||
$remainingUnits = max( 0, $unitsRequired - $unitsFulfilled );
|
||||
$readyUnits = (int) ( $deliveryReadyByTradeSymbol[ $tradeSymbol ] ?? 0 );
|
||||
$deliverableNow = min( $readyUnits, $remainingUnits );
|
||||
?>
|
||||
<li>
|
||||
<?php echo htmlspecialchars( (string) ( $delivery['tradeSymbol'] ?? '' ) ); ?>:
|
||||
<?php echo htmlspecialchars( formatString( $tradeSymbol ) ); ?>:
|
||||
<?php echo number_format( $unitsFulfilled ); ?>/<?php echo number_format( $unitsRequired ); ?>
|
||||
to <?php echo htmlspecialchars( (string) ( $delivery['destinationSymbol'] ?? '' ) ); ?>
|
||||
| Ready: <?php echo number_format( $deliverableNow ); ?>
|
||||
<?php if ($readyUnits > $deliverableNow ) : ?>
|
||||
(<?php echo number_format( $readyUnits ); ?> in cargo)
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
@@ -504,18 +743,32 @@ try {
|
||||
$fuelCapacity = (int) ( $ship['fuel']['capacity'] ?? 0 );
|
||||
$cargoUnits = (int) ( $ship['cargo']['units'] ?? 0 );
|
||||
$cargoCapacity = (int) ( $ship['cargo']['capacity'] ?? 0 );
|
||||
$cooldownRemaining = (int) ( $ship['cooldown']['remainingSeconds'] ?? 0 );
|
||||
$shipStatus = (string) ( $ship['nav']['status'] ?? '' );
|
||||
$arrivalIso = (string) ( $ship['nav']['route']['arrival'] ?? '' );
|
||||
?>
|
||||
<div class="border border-gray-600 rounded p-4">
|
||||
<h3 class="text-lg font-bold mb-2">
|
||||
<a href="ship-details.php?ship=<?php echo urlencode( $shipSymbol ); ?>" class="text-blue-400 hover:underline">
|
||||
<?php echo htmlspecialchars( $shipSymbol ); ?>
|
||||
<?php echo htmlspecialchars( formatString( $shipSymbol ) ); ?>
|
||||
</a>
|
||||
</h3>
|
||||
<p><span class="font-bold">Name:</span> <?php echo htmlspecialchars( (string) ( $ship['registration']['name'] ?? '' ) ); ?></p>
|
||||
<p><span class="font-bold">Status:</span> <?php echo htmlspecialchars( (string) ( $ship['nav']['status'] ?? '' ) ); ?></p>
|
||||
<p><span class="font-bold">Status:</span> <?php echo htmlspecialchars( formatString( $shipStatus ) ); ?></p>
|
||||
<p><span class="font-bold">Waypoint:</span> <?php echo htmlspecialchars( (string) ( $ship['nav']['waypointSymbol'] ?? '' ) ); ?></p>
|
||||
<p><span class="font-bold">Fuel:</span> <?php echo number_format( $fuelCurrent ); ?>/<?php echo number_format( $fuelCapacity ); ?></p>
|
||||
<p><span class="font-bold">Cargo:</span> <?php echo number_format( $cargoUnits ); ?>/<?php echo number_format( $cargoCapacity ); ?></p>
|
||||
<p>
|
||||
<span class="font-bold">Navigation Timer:</span>
|
||||
<span
|
||||
class="ship-nav-timer"
|
||||
data-status="<?php echo htmlspecialchars( $shipStatus ); ?>"
|
||||
data-arrival="<?php echo htmlspecialchars( $arrivalIso ); ?>"
|
||||
></span>
|
||||
</p>
|
||||
<p>
|
||||
<span class="font-bold">Cooldown:</span>
|
||||
<span class="ship-cooldown" data-seconds="<?php echo htmlspecialchars( (string) max( 0, $cooldownRemaining ) ); ?>"></span>
|
||||
</p>
|
||||
|
||||
<?php $inventory = (array) ( $ship['cargo']['inventory'] ?? array() ); ?>
|
||||
<?php if (! empty( $inventory ) ) : ?>
|
||||
@@ -523,7 +776,7 @@ try {
|
||||
<ul class="list-disc list-inside text-sm text-gray-300">
|
||||
<?php foreach ( $inventory as $item ) : ?>
|
||||
<li>
|
||||
<?php echo htmlspecialchars( (string) ( $item['symbol'] ?? '' ) ); ?>:
|
||||
<?php echo htmlspecialchars( (string) ( formatString( $item['symbol'] ?? '' ) ) ); ?>:
|
||||
<?php echo number_format( (int) ( $item['units'] ?? 0 ) ); ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
@@ -543,5 +796,84 @@ try {
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
const cooldownNodes = document.querySelectorAll('.ship-cooldown');
|
||||
const navTimerNodes = document.querySelectorAll('.ship-nav-timer');
|
||||
|
||||
function formatCooldown(seconds) {
|
||||
if (seconds <= 0) {
|
||||
return 'Ready';
|
||||
}
|
||||
|
||||
const total = Math.max(0, Math.floor(seconds));
|
||||
const mins = Math.floor(total / 60);
|
||||
const secs = total % 60;
|
||||
const paddedSecs = secs.toString().padStart(2, '0');
|
||||
return mins > 0 ? `${mins}:${paddedSecs}` : `${secs}s`;
|
||||
}
|
||||
|
||||
function formatNavTimer(seconds) {
|
||||
if (seconds <= 0) {
|
||||
return 'Arriving';
|
||||
}
|
||||
|
||||
const total = Math.max(0, Math.floor(seconds));
|
||||
const hours = Math.floor(total / 3600);
|
||||
const minutes = Math.floor((total % 3600) / 60);
|
||||
const secs = total % 60;
|
||||
|
||||
if (hours > 0) {
|
||||
return `${hours}:${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
return `${minutes}:${String(secs).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function render() {
|
||||
cooldownNodes.forEach((node) => {
|
||||
const seconds = Number(node.dataset.seconds || '0');
|
||||
node.textContent = formatCooldown(seconds);
|
||||
});
|
||||
|
||||
const nowMs = Date.now();
|
||||
navTimerNodes.forEach((node) => {
|
||||
const status = node.dataset.status || '';
|
||||
const arrival = node.dataset.arrival || '';
|
||||
|
||||
if (status !== 'IN_TRANSIT') {
|
||||
node.textContent = 'Ready';
|
||||
return;
|
||||
}
|
||||
|
||||
const arrivalMs = Date.parse(arrival);
|
||||
if (!Number.isFinite(arrivalMs)) {
|
||||
node.textContent = 'In transit';
|
||||
return;
|
||||
}
|
||||
|
||||
const remainingSeconds = Math.max(0, Math.floor((arrivalMs - nowMs) / 1000));
|
||||
node.textContent = formatNavTimer(remainingSeconds);
|
||||
});
|
||||
}
|
||||
|
||||
render();
|
||||
|
||||
if (cooldownNodes.length === 0 && navTimerNodes.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.setInterval(() => {
|
||||
cooldownNodes.forEach((node) => {
|
||||
const current = Number(node.dataset.seconds || '0');
|
||||
const next = Math.max(0, current - 1);
|
||||
node.dataset.seconds = String(next);
|
||||
});
|
||||
|
||||
render();
|
||||
}, 1000);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user