File tree Expand file tree Collapse file tree 3 files changed +24
-7
lines changed Expand file tree Collapse file tree 3 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -143,9 +143,22 @@ public function abandon(PlayerService $player): JsonResponse
143
143
]);
144
144
}
145
145
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
+ }
148
160
161
+ // Return success message.
149
162
return response ()->json ([
150
163
'status ' => 'error ' ,
151
164
'errorbox ' => [
Original file line number Diff line number Diff line change @@ -172,6 +172,11 @@ public function isValidPlanetName(string $name): bool
172
172
*/
173
173
public function abandonPlanet (): void
174
174
{
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
+
175
180
// If this is a planet and has a moon, delete the moon first
176
181
if ($ this ->isPlanet () && $ this ->hasMoon ()) {
177
182
$ this ->moon ()->abandonPlanet ();
@@ -187,10 +192,6 @@ public function abandonPlanet(): void
187
192
// Building queues
188
193
BuildingQueue::where ('planet_id ' , $ this ->planet ->id )->delete ();
189
194
190
- if ($ this ->isPlanet () && $ this ->player ->planets ->planetCount () < 2 ) {
191
- throw new RuntimeException ('Cannot abandon only remaining planet. ' );
192
- }
193
-
194
195
// Update the player's current planet if it is the planet being abandoned.
195
196
if ($ this ->getPlayer ()->getCurrentPlanetId () === $ this ->planet ->id ) {
196
197
$ this ->getPlayer ()->setCurrentPlanetId (0 );
Original file line number Diff line number Diff line change @@ -60,6 +60,9 @@ public function testFirstPlanetAbandonFail(): void
60
60
'_token ' => csrf_token (),
61
61
'password ' => 'password ' ,
62
62
]);
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 ());
64
67
}
65
68
}
You can’t perform that action at this time.
0 commit comments