Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky tests and refactor ObjectService #433

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions app/Console/Commands/Tests/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use OGame\Factories\PlayerServiceFactory;
use OGame\Models\Planet\Coordinate;
use OGame\Models\User;
use OGame\Services\ObjectService;
use OGame\Services\PlanetService;
use OGame\Services\PlayerService;

Expand Down Expand Up @@ -70,11 +69,6 @@ abstract class TestCommand extends Command
*/
protected PlayerService $playerService;

/**
* @var ObjectService The objectService.
*/
protected ObjectService $objectService;

/**
* @var PlanetService The current planet service of the test user.
*/
Expand Down Expand Up @@ -117,7 +111,6 @@ protected function setup(): void
$this->info("Test user created with ID: {$user->id}");

$this->playerService = $playerServiceFactory->make($user->id);
$this->objectService = $this->playerService->planets->current()->objects;

// Load current planet.
$this->currentPlanetService = $this->playerService->planets->current();
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/Tests/TestRaceConditionGameMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Validation\ValidationException;
use OGame\Models\FleetMission;
use OGame\Services\ObjectService;

/**
* Class TestRaceConditionGameMission.
Expand Down Expand Up @@ -119,7 +120,7 @@ private function dispatchFleetMissionTransport(): void
'metal' => 0,
'crystal' => 0,
'deuterium' => 0,
'am' . $this->objectService->getUnitObjectByMachineName('small_cargo')->id => '10',
'am' . ObjectService::getUnitObjectByMachineName('small_cargo')->id => '10',
]
]);

Expand Down
9 changes: 5 additions & 4 deletions app/Console/Commands/Tests/TestRaceConditionUnitQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Carbon;
use Illuminate\Validation\ValidationException;
use OGame\Models\Resources;
use OGame\Services\ObjectService;
use OGame\Services\UnitQueueService;

/**
Expand Down Expand Up @@ -65,9 +66,9 @@ private function testSetup(): void
{
// Prepare user for testing the race conditions by adding some entries to the build queues.
$this->currentPlanetService->addResources(new Resources(1000000, 1000000, 1000000, 0));
$this->currentPlanetService->setObjectLevel($this->objectService->getObjectByMachineName('robot_factory')->id, 2);
$this->currentPlanetService->setObjectLevel($this->objectService->getObjectByMachineName('shipyard')->id, 1);
$this->currentPlanetService->setObjectLevel($this->objectService->getObjectByMachineName('research_lab')->id, 1);
$this->currentPlanetService->setObjectLevel(ObjectService::getObjectByMachineName('robot_factory')->id, 2);
$this->currentPlanetService->setObjectLevel(ObjectService::getObjectByMachineName('shipyard')->id, 1);
$this->currentPlanetService->setObjectLevel(ObjectService::getObjectByMachineName('research_lab')->id, 1);
$this->playerService->setResearchLevel('energy_technology', 1);
$this->playerService->setResearchLevel('combustion_drive', 1);

Expand All @@ -77,7 +78,7 @@ private function testSetup(): void

// Add light fighter build job.
$unitQueueService = resolve(UnitQueueService::class);
$unitQueueService->add($this->playerService->planets->current(), $this->objectService->getUnitObjectByMachineName('light_fighter')->id, 10);
$unitQueueService->add($this->playerService->planets->current(), ObjectService::getUnitObjectByMachineName('light_fighter')->id, 10);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions app/GameMessages/Abstracts/GameMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use OGame\Factories\PlayerServiceFactory;
use OGame\Models\Message;
use OGame\Models\Planet\Coordinate;
use OGame\Services\ObjectService;

/**
* GameMessage class which contains unique parsing logic for a specific message type.
Expand Down Expand Up @@ -43,25 +42,21 @@ abstract class GameMessage

protected PlayerServiceFactory $playerServiceFactory;

protected ObjectService $objects;

/**
* GameMessage constructor.
*
* @param Message $message
* @param PlanetServiceFactory $planetServiceFactory
* @param PlayerServiceFactory $playerServiceFactory
* @param ObjectService $objectService
*/
public function __construct(Message $message, PlanetServiceFactory $planetServiceFactory, PlayerServiceFactory $playerServiceFactory, ObjectService $objectService)
public function __construct(Message $message, PlanetServiceFactory $planetServiceFactory, PlayerServiceFactory $playerServiceFactory)
{
// Clone the message to prevent any changes to the original message affecting this object.
// This is important because otherwise mutations such as setting the viewed flag after loading this object
// would affect this object's state as well.
$this->message = clone $message;
$this->planetServiceFactory = $planetServiceFactory;
$this->playerServiceFactory = $playerServiceFactory;
$this->objects = $objectService;
$this->initialize();
}

Expand Down
23 changes: 12 additions & 11 deletions app/GameMessages/BattleReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OGame\Models\Planet\Coordinate;
use OGame\Models\Resources;
use OGame\Services\DebrisFieldService;
use OGame\Services\ObjectService;

class BattleReport extends GameMessage
{
Expand Down Expand Up @@ -163,12 +164,12 @@ private function getBattleReportParams(): array

$attacker_units = new UnitCollection();
foreach ($this->battleReportModel->attacker['units'] as $machine_name => $amount) {
$attacker_units->addUnit($this->objects->getUnitObjectByMachineName($machine_name), $amount);
$attacker_units->addUnit(ObjectService::getUnitObjectByMachineName($machine_name), $amount);
}

$defender_units = new UnitCollection();
foreach ($this->battleReportModel->defender['units'] as $machine_name => $amount) {
$defender_units->addUnit($this->objects->getUnitObjectByMachineName($machine_name), $amount);
$defender_units->addUnit(ObjectService::getUnitObjectByMachineName($machine_name), $amount);
}

// Load rounds and cast to battle result round object.
Expand All @@ -184,32 +185,32 @@ private function getBattleReportParams(): array
$obj->hitsDefender = $round['hits_defender'];
$obj->defenderShips = new UnitCollection();
foreach ($round['defender_ships'] as $machine_name => $amount) {
$unit = $this->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);
$obj->defenderShips->addUnit($unit, $amount);
}
$obj->attackerShips = new UnitCollection();
foreach ($round['attacker_ships'] as $machine_name => $amount) {
$unit = $this->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);
$obj->attackerShips->addUnit($unit, $amount);
}
$obj->defenderLosses = new UnitCollection();
foreach ($round['defender_losses'] as $machine_name => $amount) {
$unit = $this->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);
$obj->defenderLosses->addUnit($unit, $amount);
}
$obj->attackerLosses = new UnitCollection();
foreach ($round['attacker_losses'] as $machine_name => $amount) {
$unit = $this->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);
$obj->attackerLosses->addUnit($unit, $amount);
}
$obj->defenderLossesInThisRound = new UnitCollection();
foreach ($round['defender_losses_in_this_round'] as $machine_name => $amount) {
$unit = $this->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);
$obj->defenderLossesInThisRound->addUnit($unit, $amount);
}
$obj->attackerLossesInThisRound = new UnitCollection();
foreach ($round['attacker_losses_in_this_round'] as $machine_name => $amount) {
$unit = $this->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);
$obj->attackerLossesInThisRound->addUnit($unit, $amount);
}
$rounds[] = $obj;
Expand Down Expand Up @@ -260,9 +261,9 @@ private function getBattleReportParams(): array
'defender_weapons' => $defender_weapons,
'defender_shields' => $defender_shields,
'defender_armor' => $defender_armor,
'military_objects' => $this->objects->getMilitaryShipObjects(),
'civil_objects' => $this->objects->getCivilShipObjects(),
'defense_objects' => $this->objects->getDefenseObjects(),
'military_objects' => ObjectService::getMilitaryShipObjects(),
'civil_objects' => ObjectService::getCivilShipObjects(),
'defense_objects' => ObjectService::getDefenseObjects(),
'attacker_units_start' => $attacker_units,
'defender_units_start' => $defender_units,
'rounds' => $rounds,
Expand Down
9 changes: 5 additions & 4 deletions app/GameMessages/EspionageReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OGame\GameMessages\Abstracts\GameMessage;
use OGame\Models\Planet\Coordinate;
use OGame\Models\Resources;
use OGame\Services\ObjectService;
use OGame\ViewModels\UnitViewModel;

class EspionageReport extends GameMessage
Expand Down Expand Up @@ -134,7 +135,7 @@ private function getEspionageReportParams(): array
if ($this->espionageReportModel->ships !== null) {
foreach ($this->espionageReportModel->ships as $machine_name => $amount) {
// Get object
$unit = $planet->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);

$unitViewModel = new UnitViewModel();
$unitViewModel->amount = $amount;
Expand All @@ -149,7 +150,7 @@ private function getEspionageReportParams(): array
if ($this->espionageReportModel->defense !== null) {
foreach ($this->espionageReportModel->defense as $machine_name => $amount) {
// Get object
$unit = $planet->objects->getUnitObjectByMachineName($machine_name);
$unit = ObjectService::getUnitObjectByMachineName($machine_name);

$unitViewModel = new UnitViewModel();
$unitViewModel->amount = $amount;
Expand All @@ -164,7 +165,7 @@ private function getEspionageReportParams(): array
if ($this->espionageReportModel->buildings !== null) {
foreach ($this->espionageReportModel->buildings as $machine_name => $amount) {
// Get object
$unit = $planet->objects->getObjectByMachineName($machine_name);
$unit = ObjectService::getObjectByMachineName($machine_name);

$unitViewModel = new UnitViewModel();
$unitViewModel->amount = $amount;
Expand All @@ -179,7 +180,7 @@ private function getEspionageReportParams(): array
if ($this->espionageReportModel->research !== null) {
foreach ($this->espionageReportModel->research as $machine_name => $amount) {
// Get object
$unit = $planet->objects->getObjectByMachineName($machine_name);
$unit = ObjectService::getObjectByMachineName($machine_name);

$unitViewModel = new UnitViewModel();
$unitViewModel->amount = $amount;
Expand Down
3 changes: 2 additions & 1 deletion app/GameMissions/ColonisationMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OGame\Models\FleetMission;
use OGame\Models\Planet\Coordinate;
use OGame\Models\Resources;
use OGame\Services\ObjectService;
use OGame\Services\PlanetService;

class ColonisationMission extends GameMission
Expand Down Expand Up @@ -103,7 +104,7 @@ protected function processArrival(FleetMission $mission): void
// Assembly new unit collection.
$units = $this->fleetMissionService->getFleetUnits($mission);
// Remove one colony ship from the fleet as it was used to colonize the planet.
$colony_ship = $target_planet->objects->getUnitObjectByMachineName('colony_ship');
$colony_ship = ObjectService::getUnitObjectByMachineName('colony_ship');
$units->removeUnit($colony_ship, 1);

// Create and start the return mission (if the colonisation mission had ships other than the colony ship itself).
Expand Down
4 changes: 2 additions & 2 deletions app/GameMissions/RecycleMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use OGame\Models\Enums\PlanetType;
use OGame\Models\FleetMission;
use OGame\Models\Planet\Coordinate;
use OGame\Models\Resources;
use OGame\Services\DebrisFieldService;
use OGame\Services\ObjectService;
use OGame\Services\PlanetService;

class RecycleMission extends GameMission
Expand Down Expand Up @@ -57,7 +57,7 @@ protected function processArrival(FleetMission $mission): void
$debrisField->loadOrCreateForCoordinates($targetCoordinate);

// Get recycler unit count
$recycler = $originPlanet->objects->getShipObjectByMachineName('recycler');
$recycler = ObjectService::getShipObjectByMachineName('recycler');
$recyclerCount = $this->fleetMissionService->getFleetUnits($mission)->getAmountByMachineName($recycler->machine_name);

// Calculate total recycler capacity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ public function __construct(BuildingQueueService $queue)
*
* @param Request $request
* @param PlayerService $player
* @param ObjectService $objects
* @return View
* @throws Exception
*/
public function index(Request $request, PlayerService $player, ObjectService $objects): View
public function index(Request $request, PlayerService $player): View
{
$this->planet = $player->planets->current();

Expand All @@ -90,16 +89,16 @@ public function index(Request $request, PlayerService $player, ObjectService $ob
$count++;

// Get object
$object = $objects->getObjectByMachineName($object_machine_name);
$object = ObjectService::getObjectByMachineName($object_machine_name);

// Get current level of building
$current_level = $this->planet->getObjectLevel($object_machine_name);

// Check requirements of this building
$requirements_met = $objects->objectRequirementsMet($object_machine_name, $this->planet, $player);
$requirements_met = ObjectService::objectRequirementsMet($object_machine_name, $this->planet, $player);

// Check if the current planet has enough resources to build this building.
$enough_resources = $this->planet->hasResources($objects->getObjectPrice($object_machine_name, $this->planet));
$enough_resources = $this->planet->hasResources(ObjectService::getObjectPrice($object_machine_name, $this->planet));

// If building level is 1 or higher, add to header filename parts to
// render the header of this planet.
Expand Down
15 changes: 7 additions & 8 deletions app/Http/Controllers/Abstracts/AbstractUnitsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ public function __construct(UnitQueueService $queue)
*
* @param Request $request
* @param PlayerService $player
* @param ObjectService $objects
* @return View
* @throws Exception
*/
public function index(Request $request, PlayerService $player, ObjectService $objects): View
public function index(Request $request, PlayerService $player): View
{
$planet = $player->planets->current();

Expand All @@ -88,19 +87,19 @@ public function index(Request $request, PlayerService $player, ObjectService $ob
foreach ($objects_row as $object_machine_name) {
$count++;

$object = $objects->getUnitObjectByMachineName($object_machine_name);
$object = ObjectService::getUnitObjectByMachineName($object_machine_name);

// Get current level of building
// Get current amount of this unit.
$amount = $planet->getObjectAmount($object->machine_name);

// Check requirements of this building
$requirements_met = $objects->objectRequirementsMet($object->machine_name, $planet, $player, 0, false);
$requirements_met = ObjectService::objectRequirementsMet($object->machine_name, $planet, $player, 0, false);

// Check if the current planet has enough resources to build this building.
$enough_resources = $planet->hasResources($objects->getObjectPrice($object->machine_name, $planet));
$enough_resources = $planet->hasResources(ObjectService::getObjectPrice($object->machine_name, $planet));

// Get maximum build amount of this building
$max_build_amount = $objects->getObjectMaxBuildAmount($object->machine_name, $planet, $requirements_met);
$max_build_amount = ObjectService::getObjectMaxBuildAmount($object->machine_name, $planet, $requirements_met);

$view_model = new UnitViewModel();
$view_model->object = $object;
Expand All @@ -127,7 +126,7 @@ public function index(Request $request, PlayerService $player, ObjectService $ob
}

/**
* Handles an incoming add buildrequest.
* Handles an incoming add build request.
*
* @param Request $request
* @param PlayerService $player
Expand Down
Loading
Loading