Skip to content

Commit 639b6ec

Browse files
authored
Merge pull request #521 from lanedirt/520-bug-when-abandoning-only-remaining-planet-an-error-is-returned-but-moon-is-deleted
Fix planet abandon sanity check order and error reporting
2 parents 5912715 + e0f9fdd commit 639b6ec

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

app/Http/Controllers/PlanetAbandonController.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,22 @@ public function abandon(PlayerService $player): JsonResponse
143143
]);
144144
}
145145

146-
// Abandon the planet.
147-
$planetToDelete->abandonPlanet();
146+
try {
147+
// Abandon the planet.
148+
$planetToDelete->abandonPlanet();
149+
} catch (Exception $e) {
150+
// Exception occured, return error.
151+
return response()->json([
152+
'status' => 'error',
153+
'errorbox' => [
154+
'type' => 'fadeBox',
155+
'text' => $e->getMessage(),
156+
'failed' => true,
157+
],
158+
]);
159+
}
148160

161+
// Return success message.
149162
return response()->json([
150163
'status' => 'error',
151164
'errorbox' => [

app/Services/PlanetService.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ public function isValidPlanetName(string $name): bool
172172
*/
173173
public function abandonPlanet(): void
174174
{
175+
// Sanity check: disallow abandoning the last remaining planet of user.
176+
if ($this->isPlanet() && $this->player->planets->planetCount() < 2) {
177+
throw new RuntimeException('Cannot abandon only remaining planet.');
178+
}
179+
175180
// If this is a planet and has a moon, delete the moon first
176181
if ($this->isPlanet() && $this->hasMoon()) {
177182
$this->moon()->abandonPlanet();
@@ -187,10 +192,6 @@ public function abandonPlanet(): void
187192
// Building queues
188193
BuildingQueue::where('planet_id', $this->planet->id)->delete();
189194

190-
if ($this->isPlanet() && $this->player->planets->planetCount() < 2) {
191-
throw new RuntimeException('Cannot abandon only remaining planet.');
192-
}
193-
194195
// Update the player's current planet if it is the planet being abandoned.
195196
if ($this->getPlayer()->getCurrentPlanetId() === $this->planet->id) {
196197
$this->getPlayer()->setCurrentPlanetId(0);

tests/Feature/PlanetAbandonTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function testFirstPlanetAbandonFail(): void
6060
'_token' => csrf_token(),
6161
'password' => 'password',
6262
]);
63-
$response->assertStatus(500);
63+
64+
// Assert that response is HTTP 200 but contains error message.
65+
$response->assertStatus(200);
66+
$this->assertStringContainsString('Cannot abandon only remaining planet', (string)$response->getContent());
6467
}
6568
}

0 commit comments

Comments
 (0)