Skip to content

Commit

Permalink
Code style refactor, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed May 3, 2024
1 parent 1f2543f commit 7030a7b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
5 changes: 3 additions & 2 deletions app/Factories/GameMissionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class GameMissionFactory
* @return array<GameMission>
* @throws BindingResolutionException
*/
public static function getAllMissions(): array {
public static function getAllMissions(): array
{
/*
{
"1": "Attack",
Expand All @@ -35,4 +36,4 @@ public static function getAllMissions(): array {
app()->make(ColonisationMission::class),
];
}
}
}
2 changes: 1 addition & 1 deletion app/GameMissions/Abstracts/GameMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function getTypeId(): int
* @param UnitCollection $units
* @return MissionPossibleStatus
*/
public abstract function isMissionPossible(PlanetService $planet, ?PlanetService $targetPlanet, UnitCollection $units): MissionPossibleStatus;
abstract public function isMissionPossible(PlanetService $planet, ?PlanetService $targetPlanet, UnitCollection $units): MissionPossibleStatus;

/**
* Cancel an already started mission.
Expand Down
5 changes: 2 additions & 3 deletions app/GameMissions/ColonisationMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function isMissionPossible(PlanetService $planet, ?PlanetService $targetP
// Check if a colony ship is present in the fleet
if ($units->getAmountByMachineName('colony_ship') > 0) {
return new MissionPossibleStatus(true);
}
else {
} else {
// Return error message
return new MissionPossibleStatus(false, __('You need a colony ship to colonize a planet.'));
}
Expand All @@ -46,4 +45,4 @@ protected function processReturn(FleetMission $mission): void
{
// TODO: Implement the return logic
}
}
}
13 changes: 8 additions & 5 deletions app/Http/Controllers/FleetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OGame\Models\Resources;
use OGame\Services\FleetMissionService;
use OGame\Services\ObjectService;
use OGame\Services\PlanetService;
use OGame\Services\PlayerService;
use OGame\ViewModels\UnitViewModel;

Expand Down Expand Up @@ -82,7 +83,8 @@ public function movement(): View
/**
* @throws BindingResolutionException
*/
public function dispatchCheckTarget(PlayerService $currentPlayer, ObjectService $objects) : JsonResponse {
public function dispatchCheckTarget(PlayerService $currentPlayer, ObjectService $objects): JsonResponse
{
$currentPlanet = $currentPlayer->planets->current();

// Return ships data for this planet taking into account the current planet's properties and research levels.
Expand Down Expand Up @@ -130,8 +132,7 @@ public function dispatchCheckTarget(PlayerService $currentPlayer, ObjectService
$possible = $mission->isMissionPossible($currentPlanet, $targetPlanet, $units);
if ($possible->possible) {
$enabledMissions[] = $mission::getTypeId();
}
else if (!empty($possible->error)) {
} elseif (!empty($possible->error)) {
// If the mission is not possible and has an error message, return error message in JSON.
$errors[] = [
'message' => $possible->error,
Expand Down Expand Up @@ -190,8 +191,9 @@ public function dispatchCheckTarget(PlayerService $currentPlayer, ObjectService
/**
* Handles the dispatch of a fleet.
*
* @param PlayerService $player
* @return JsonResponse
* @throws Exception
* @throws BindingResolutionException
*/
public function dispatchSendFleet(PlayerService $player): JsonResponse
{
Expand Down Expand Up @@ -286,7 +288,8 @@ public function dispatchRecallFleet(): JsonResponse
* @return UnitCollection
* @throws Exception
*/
private function getUnitsFromRequest(PlanetService $planet): UnitCollection {
private function getUnitsFromRequest(PlanetService $planet): UnitCollection
{
$units = new UnitCollection();
foreach (request()->all() as $key => $value) {
if (str_starts_with($key, 'am')) {
Expand Down
3 changes: 1 addition & 2 deletions tests/AccountTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ protected function getNearbyForeignPlanet(): PlanetService

if ($planet_id == null) {
$this->fail('Failed to find a nearby foreign planet for testing.');
}
else {
} else {
// Create and return a new PlanetService instance for the found planet.
$planetServiceFactory = app()->make(PlanetServiceFactory::class);
return $planetServiceFactory->make($planet_id[0]);
Expand Down
12 changes: 5 additions & 7 deletions tests/FleetDispatchSelfTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ protected function basicSetup(): void
$this->planetAddUnit('small_cargo', 5);
}

protected abstract function messageCheckMissionArrival(): void;
protected abstract function messageCheckMissionReturn(): void;
abstract protected function messageCheckMissionArrival(): void;
abstract protected function messageCheckMissionReturn(): void;

/**
* Verify that dispatching a fleet deducts correct amount of units from planet.
Expand Down Expand Up @@ -184,7 +184,7 @@ public function testDispatchFleetReturnTrip(): void
$response->assertStatus(200);

// Assert that the fleet mission is processed.
$fleetMission = $fleetMissionService->getFleetMissionById($fleetMissionId, false);
$fleetMission = $fleetMissionService->getFleetMissionById($fleetMissionId, false);
$this->assertTrue($fleetMission->processed == 1, 'Fleet mission is not processed after fleet has arrived at destination.');

// Check that message has been received by calling extended method
Expand Down Expand Up @@ -214,8 +214,7 @@ public function testDispatchFleetReturnTrip(): void
$this->assertObjectLevelOnPage($response, 'small_cargo', 5, 'Small Cargo ships are not at 5 units after return trip.');

$this->messageCheckMissionReturn();
}
else {
} else {
// Assert that NO return trip has been launched by checking the active missions for the current planet.
$this->assertCount(0, $activeMissions, 'Return trip launched after fleet with deployment mission has arrived at destination.');
}
Expand Down Expand Up @@ -254,8 +253,7 @@ public function testDispatchFleetReturnShown(): void
// Assert that we see both rows in the event list.
$response->assertSee('data-return-flight="false"', false);
$response->assertSee('data-return-flight="true"', false);
}
else {
} else {
// If the mission does not have a return mission, we should only see the parent mission.
$response->assertSee($this->missionName);
$response->assertDontSee($this->missionName . ' (R)');
Expand Down
12 changes: 7 additions & 5 deletions tests/FleetDispatchTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use OGame\GameObjects\Models\UnitCollection;
use OGame\Models\Resources;
use OGame\Services\PlanetService;
use OGame\Services\PlayerService;

/**
* Base class to test that fleet missions work as expected.
Expand Down Expand Up @@ -41,7 +40,7 @@ abstract class FleetDispatchTestCase extends AccountTestCase
* @return void
* @throws BindingResolutionException
*/
protected abstract function basicSetup(): void;
abstract protected function basicSetup(): void;

/**
* Send a fleet to the second planet of the test user.
Expand All @@ -51,7 +50,8 @@ protected abstract function basicSetup(): void;
* @param int $assertStatus
* @return void
*/
protected function sendMissionToSecondPlanet(UnitCollection $units, Resources $resources, int $assertStatus = 200) : void {
protected function sendMissionToSecondPlanet(UnitCollection $units, Resources $resources, int $assertStatus = 200): void
{
// Convert units to array.
$unitsArray = [];
foreach ($units->units as $unit) {
Expand Down Expand Up @@ -84,7 +84,8 @@ protected function sendMissionToSecondPlanet(UnitCollection $units, Resources $r
*
* @throws BindingResolutionException
*/
protected function sendMissionToOtherPlayer(UnitCollection $units, Resources $resources, int $assertStatus = 200): PlanetService {
protected function sendMissionToOtherPlayer(UnitCollection $units, Resources $resources, int $assertStatus = 200): PlanetService
{
// Convert units to array.
$unitsArray = [];
foreach ($units->units as $unit) {
Expand Down Expand Up @@ -125,7 +126,8 @@ protected function sendMissionToOtherPlayer(UnitCollection $units, Resources $re
* @param int $assertStatus
* @return void
*/
protected function sendMissionToEmptyPosition(UnitCollection $units, Resources $resources, int $assertStatus = 200): void {
protected function sendMissionToEmptyPosition(UnitCollection $units, Resources $resources, int $assertStatus = 200): void
{
// Convert units to array.
$unitsArray = [];
foreach ($units->units as $unit) {
Expand Down

0 comments on commit 7030a7b

Please sign in to comment.