Skip to content

Commit

Permalink
Fix planet abandon sanity check order and error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Jan 6, 2025
1 parent 4a511e4 commit 91f596e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
18 changes: 16 additions & 2 deletions app/Http/Controllers/PlanetAbandonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,23 @@ public function abandon(PlayerService $player): JsonResponse
]);
}

// Abandon the planet.
$planetToDelete->abandonPlanet();
try {
// Abandon the planet.
$planetToDelete->abandonPlanet();
}
catch (Exception $e) {
// Exception occured, return error.
return response()->json([
'status' => 'error',
'errorbox' => [
'type' => 'fadeBox',
'text' => $e->getMessage(),
'failed' => true,
],
]);
}

// Return success message.
return response()->json([
'status' => 'error',
'errorbox' => [
Expand Down
9 changes: 5 additions & 4 deletions app/Services/PlanetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public function isValidPlanetName(string $name): bool
*/
public function abandonPlanet(): void
{
// Sanity check: disallow abandoning the last remaining planet of user.
if ($this->isPlanet() && $this->player->planets->planetCount() < 2) {
throw new RuntimeException('Cannot abandon only remaining planet.');
}

// If this is a planet and has a moon, delete the moon first
if ($this->isPlanet() && $this->hasMoon()) {
$this->moon()->abandonPlanet();
Expand All @@ -187,10 +192,6 @@ public function abandonPlanet(): void
// Building queues
BuildingQueue::where('planet_id', $this->planet->id)->delete();

if ($this->isPlanet() && $this->player->planets->planetCount() < 2) {
throw new RuntimeException('Cannot abandon only remaining planet.');
}

// Update the player's current planet if it is the planet being abandoned.
if ($this->getPlayer()->getCurrentPlanetId() === $this->planet->id) {
$this->getPlayer()->setCurrentPlanetId(0);
Expand Down

0 comments on commit 91f596e

Please sign in to comment.