Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed May 17, 2024
1 parent 5eb9ccd commit 340cc16
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 105 deletions.
3 changes: 0 additions & 3 deletions app/GameMissions/Abstracts/GameMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ public function startMissionSanityChecks(PlanetService $planet, Coordinate $targ

/**
* Deduct mission resources from the planet (when starting mission).
*
* @throws Exception
*/
public function deductMissionResources(PlanetService $planet, Resources $resources, UnitCollection $units): void
{
Expand All @@ -140,7 +138,6 @@ public function deductMissionResources(PlanetService $planet, Resources $resourc
* @param Resources $resources
* @param int $parent_id
* @return void
* @throws Exception
*/
public function start(PlanetService $planet, Coordinate $targetCoordinate, UnitCollection $units, Resources $resources, int $parent_id = 0): void
{
Expand Down
1 change: 0 additions & 1 deletion app/GameMissions/ColonisationMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use OGame\Factories\PlanetServiceFactory;
use OGame\Factories\PlayerServiceFactory;
use OGame\GameMessages\ColonyEstablished;
use OGame\GameMissions\Abstracts\GameMission;
use OGame\GameMissions\Models\MissionPossibleStatus;
Expand Down
1 change: 0 additions & 1 deletion app/Http/Controllers/FleetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace OGame\Http\Controllers;

use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
Expand Down
29 changes: 15 additions & 14 deletions app/Http/Controllers/FleetEventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace OGame\Http\Controllers;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Carbon;
use Illuminate\View\View;
use OGame\Factories\PlanetServiceFactory;
use OGame\Models\FleetMission;
use OGame\Models\Planet\Coordinate;
use OGame\Models\Resources;
use OGame\Services\FleetMissionService;
Expand All @@ -17,16 +17,12 @@ class FleetEventsController extends OGameController
/**
* Returns fleet mission eventbox JSON.
*
* @param FleetMissionService $fleetMissionService
* @return JsonResponse
* @throws BindingResolutionException
*/
public function fetchEventBox(): JsonResponse
public function fetchEventBox(FleetMissionService $fleetMissionService): JsonResponse
{
/*
* {"components":[],"hostile":0,"neutral":0,"friendly":1,"eventType":"friendly","eventTime":3470,"eventText":"Transport","newAjaxToken":"6f0e9c23c750fcfc85de4833c79fec39"}
*/
// Get all the fleet movements for the current user.
$fleetMissionService = app()->make(FleetMissionService::class);
$friendlyMissionRows = $fleetMissionService->getActiveFleetMissionsForCurrentPlayer();

if ($friendlyMissionRows->isEmpty()) {
Expand All @@ -36,12 +32,19 @@ public function fetchEventBox(): JsonResponse
'time_next_mission' => 0,
];
} else {
$firstMission = $friendlyMissionRows->first();

// Make sure $firstMission is an instance of FleetMission
if (!$firstMission instanceof FleetMission) {
throw new \UnexpectedValueException('Expected instance of FleetMission.');
}

// TODO: make it a (view)model return type
// TODO: refactor data retrieval and processing... duplicate with fetchEventList
$friendlyMissions = [
'mission_count' => $friendlyMissionRows->count(),
'type_next_mission' => $fleetMissionService->missionTypeToLabel($friendlyMissionRows->first()->mission_type) . ($friendlyMissionRows->first()->parent_id ? ' (R)' : ''),
'time_next_mission' => $friendlyMissionRows->first()->time_arrival - (int)Carbon::now()->timestamp,
'type_next_mission' => $fleetMissionService->missionTypeToLabel($firstMission->mission_type) . ($firstMission->parent_id ? ' (R)' : ''),
'time_next_mission' => $firstMission->time_arrival - (int)Carbon::now()->timestamp,
];
}

Expand All @@ -60,20 +63,18 @@ public function fetchEventBox(): JsonResponse
/**
* Fetch the fleet event list HTML which contains all the fleet mission details.
*
* @param FleetMissionService $fleetMissionService
* @param PlanetServiceFactory $planetServiceFactory
* @return View
* @throws BindingResolutionException
*/
public function fetchEventList(): View
public function fetchEventList(FleetMissionService $fleetMissionService, PlanetServiceFactory $planetServiceFactory): View
{
// Get all the fleet movements for the current user.
$fleetMissionService = app()->make(FleetMissionService::class);
$friendlyMissionRows = $fleetMissionService->getActiveFleetMissionsForCurrentPlayer();

$fleet_events = [];
foreach ($friendlyMissionRows as $row) {
// Planet from service
$planetServiceFactory = app()->make(PlanetServiceFactory::class);

$eventRowViewModel = new FleetEventRowViewModel();
$eventRowViewModel->id = $row->id;
$eventRowViewModel->mission_type = $row->mission_type;
Expand Down
12 changes: 7 additions & 5 deletions app/Http/Controllers/HighscoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ class HighscoreController extends OGameController
*
* @param Request $request
* @param PlayerService $player
* @param HighscoreService $highscoreService
* @return View
*/
public function index(Request $request, PlayerService $player): View
public function index(Request $request, PlayerService $player, HighscoreService $highscoreService): View
{
$this->setBodyId('highscore');

return view('ingame.highscore.index')->with([
'initialContent' => $this->ajax($request, $player),
'initialContent' => $this->ajax($request, $player, $highscoreService),
]);
}

Expand All @@ -30,9 +31,10 @@ public function index(Request $request, PlayerService $player): View
*
* @param Request $request
* @param PlayerService $player
* @param HighscoreService $highscoreService
* @return View
*/
public function ajax(Request $request, PlayerService $player): View
public function ajax(Request $request, PlayerService $player, HighscoreService $highscoreService): View
{
// Check if we received category parameter, if so, use it to determine which highscore category to show.
// 1 = players
Expand All @@ -45,9 +47,9 @@ public function ajax(Request $request, PlayerService $player): View
}

if ($category == 1) {
return $this->ajaxPlayer($request, $player);
return $this->ajaxPlayer($request, $player, $highscoreService);
} else {
return $this->ajaxAlliance($request, $player);
return $this->ajaxAlliance($request, $player, $highscoreService);
}
}

Expand Down
1 change: 0 additions & 1 deletion app/Http/Controllers/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace OGame\Http\Controllers;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
Expand Down
23 changes: 10 additions & 13 deletions app/Services/FleetMissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace OGame\Services;

use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Carbon;
use OGame\Factories\GameMissionFactory;
Expand Down Expand Up @@ -69,6 +67,11 @@ class FleetMissionService
*/
protected MessageService $messageService;

/**
* @var GameMissionFactory $gameMissionFactory
*/
protected GameMissionFactory $gameMissionFactory;

/**
* The queue model where this class should get its data from.
*
Expand All @@ -79,11 +82,12 @@ class FleetMissionService
/**
* FleetMissionService constructor.
*/
public function __construct(PlayerService $player, ObjectService $objects, MessageService $messageService)
public function __construct(PlayerService $player, ObjectService $objects, MessageService $messageService, GameMissionFactory $gameMissionFactory)
{
$this->player = $player;
$this->objects = $objects;
$this->messageService = $messageService;
$this->gameMissionFactory = $gameMissionFactory;

$model_name = 'OGame\Models\FleetMission';
$this->model = new $model_name();
Expand Down Expand Up @@ -256,12 +260,10 @@ public function getFleetMissionById(int $id, bool $only_active = true): FleetMis
* @param Resources $resources
* @param int $parent_id
* @return void
* @throws Exception
*/
public function createNewFromPlanet(PlanetService $planet, Coordinate $targetCoordinate, int $missionType, UnitCollection $units, Resources $resources, int $parent_id = 0): void
{
$missionFactory = app()->make(GameMissionFactory::class);
$missionObject = $missionFactory->getMissionById($missionType, [
$missionObject = $this->gameMissionFactory->getMissionById($missionType, [
'fleetMissionService' => $this,
'messageService' => $this->messageService,
]);
Expand All @@ -273,8 +275,6 @@ public function createNewFromPlanet(PlanetService $planet, Coordinate $targetCoo
*
* @param FleetMission $mission
* @return void
* @throws BindingResolutionException
* @throws Exception
*/
public function updateMission(FleetMission $mission): void
{
Expand All @@ -283,8 +283,7 @@ public function updateMission(FleetMission $mission): void
return;
}

$missionFactory = app()->make(GameMissionFactory::class);
$missionObject = $missionFactory->getMissionById($mission->mission_type, [
$missionObject = $this->gameMissionFactory->getMissionById($mission->mission_type, [
'fleetMissionService' => $this,
'messageService' => $this->messageService,
]);
Expand All @@ -296,7 +295,6 @@ public function updateMission(FleetMission $mission): void
*
* @param FleetMission $mission
* @return void
* @throws BindingResolutionException
*/
public function cancelMission(FleetMission $mission): void
{
Expand All @@ -305,8 +303,7 @@ public function cancelMission(FleetMission $mission): void
return;
}

$missionFactory = app()->make(GameMissionFactory::class);
$missionObject = $missionFactory->getMissionById($mission->mission_type, [
$missionObject = $this->gameMissionFactory->getMissionById($mission->mission_type, [
'fleetMissionService' => $this,
'messageService' => $this->messageService,
]);
Expand Down
11 changes: 6 additions & 5 deletions app/Services/HighscoreService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace OGame\Services;

use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use OGame\Facades\AppUtil;
use OGame\Factories\PlayerServiceFactory;
use OGame\Models\User;

/**
Expand All @@ -22,11 +22,14 @@ class HighscoreService
*/
protected int $highscoreType;

protected PlayerServiceFactory $playerServiceFactory;

/**
* Highscore constructor.
*/
public function __construct()
public function __construct(PlayerServiceFactory $playerServiceFactory)
{
$this->playerServiceFactory = $playerServiceFactory;
}

/**
Expand Down Expand Up @@ -122,7 +125,6 @@ public function getPlayerScoreEconomy(PlayerService $player): int
* @param int $offset_start
* @param int $return_amount
* @return array<int, array<string,mixed>>
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function getHighscorePlayers(int $offset_start = 0, int $return_amount = 100): array
{
Expand All @@ -138,7 +140,7 @@ public function getHighscorePlayers(int $offset_start = 0, int $return_amount =
// TODO: we get the player score per player now, but we should get it from a cached highscore table
// to improve performance. Currently it works but is slow for large amounts of players.
// Load player object with all planets.
$playerService = app()->make(PlayerService::class, ['player_id' => $player->id]);
$playerService = $this->playerServiceFactory->make($player->id);
$score = 0;
switch ($this->highscoreType) {
case 1:
Expand Down Expand Up @@ -193,7 +195,6 @@ public function getHighscorePlayers(int $offset_start = 0, int $return_amount =
*
* @param PlayerService $player
* @return int
* @throws BindingResolutionException
*/
public function getHighscorePlayerRank(PlayerService $player): int
{
Expand Down
7 changes: 0 additions & 7 deletions app/Services/MessageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace OGame\Services;

use Illuminate\Contracts\Container\BindingResolutionException;
use OGame\Factories\GameMessageFactory;
use OGame\GameMessages\Abstracts\GameMessage;
use OGame\GameMessages\WelcomeMessage;
Expand Down Expand Up @@ -170,9 +169,6 @@ public function getUnreadMessagesCount(): int
->count();
}

/**
* @throws BindingResolutionException
*/
public function getUnreadMessagesCountForTab(string $tab): int
{
// Get all keys for the tab.
Expand All @@ -184,9 +180,6 @@ public function getUnreadMessagesCountForTab(string $tab): int
->count();
}

/**
* @throws BindingResolutionException
*/
public function getUnreadMessagesCountForSubTab(string $tab, string $subtab): int
{
// Get all keys for the subtab.
Expand Down
Loading

0 comments on commit 340cc16

Please sign in to comment.