Skip to content

Commit

Permalink
Started abstracting out the way we handle sending resource requests f…
Browse files Browse the repository at this point in the history
…or capital cities
  • Loading branch information
AdamKyle committed Sep 5, 2024
1 parent 061ac4a commit a9876d7
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function handleResourceRequests(
Kingdom $kingdom
): void
{
$kingdomWhoCanAfford = $this->getKingdomWhoCanAffordCosts($character, $summedMissingCosts);
$kingdomWhoCanAfford = $this->getKingdomWhoCanAffordCosts($character, $kingdom, $summedMissingCosts);

if (is_null($kingdomWhoCanAfford)) {
$requestData = $this->markRequestsAsRejected($requestData);
Expand Down Expand Up @@ -376,17 +376,18 @@ private function createResourceRequest(
* Find the first kingdom who can afford the costs.
*
* @param Character $character
* @param Kingdom $kingdom
* @param array $missingCosts
* @return Kingdom|null
*/
private function getKingdomWhoCanAffordCosts(Character $character, array $missingCosts): ?Kingdom {
private function getKingdomWhoCanAffordCosts(Character $character, Kingdom $kingdom, array $missingCosts): ?Kingdom {
return $character->kingdoms()->where(function ($q) use ($missingCosts) {
foreach ($missingCosts as $resource => $amount) {
if ($resource !== 'population') {
$q->where('current_' . $resource, '>=', $amount);
}
}
})->first();
})->where('game_map_id', $kingdom->game_map_id)->first();
}

private function getTimeToKingdom(Character $character, Kingdom $kingdomAskingForResources, Kingdom $requestingFromKingdom):int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Game\Kingdoms\Handlers;

class CapitalCityRequestResourcesHandler {

}
1 change: 1 addition & 0 deletions app/Game/Kingdoms/Jobs/CapitalCityBuildingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(private readonly int $capitalCityQueueId, private re

public function handle(CapitalCityBuildingManagement $capitalCityBuildingManagement): void
{

$queueData = CapitalCityBuildingQueue::find($this->capitalCityQueueId);

if (is_null($queueData)) {
Expand Down
96 changes: 92 additions & 4 deletions app/Game/Kingdoms/Jobs/CapitalCityResourceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace App\Game\Kingdoms\Jobs;

use App\Flare\Models\CapitalCityBuildingQueue;
use App\Flare\Models\CapitalCityResourceRequest as CapitalCityResourceRequestModel;
use App\Flare\Models\CapitalCityUnitQueue;
use App\Game\Kingdoms\Events\UpdateCapitalCityBuildingQueueTable;
use App\Game\Kingdoms\Service\CapitalCityBuildingManagement;
use App\Game\Kingdoms\Events\UpdateCapitalCityUnitQueueTable;
use App\Game\Kingdoms\Values\CapitalCityQueueStatus;
use App\Game\Kingdoms\Values\CapitalCityResourceRequestType;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -20,7 +24,16 @@ public function __construct(protected readonly int $capitalCityQueueId, protecte

public function handle(): void
{
$queueData = CapitalCityBuildingQueue::find($this->capitalCityQueueId);

$queueData = null;

if ($this->type === CapitalCityResourceRequestType::BUILDING_QUEUE) {
$queueData = CapitalCityBuildingQueue::find($this->capitalCityQueueId);
}

if ($this->type === CapitalCityResourceRequestType::UNIT_QUEUE) {
$queueData = CapitalCityUnitQueue::find($this->capitalCityQueueId);
}

if (is_null($queueData)) {
return;
Expand All @@ -40,14 +53,89 @@ public function handle(): void
CapitalCityBuildingRequestMovement::dispatch(
$this->capitalCityQueueId,
$this->characterId,
$this->type
)->delay($time);

return;
// @codeCoverageIgnoreEnd
}
}

dump('CapitalCityResourceRequest');
dump($queueData);
$capitalCityResourceRequestData = CapitalCityResourceRequestModel::where(
'kingdom_requesting_id', $queueData->requested_kingdom,
)->first();

$resourcesForKingdom = $capitalCityResourceRequestData->resources;

$requestingKingdom = $capitalCityResourceRequestData->requestingKingdom;

foreach ($resourcesForKingdom as $resourceName => $resourceAmount) {
$newAmount = $requestingKingdom->{'current_'.$resourceName} + $resourceAmount;

if ($newAmount > $requestedKingdom->{'max_'.$resourceName}) {
$newAmount = $requestedKingdom->{'max_'.$resourceName};
}

$requestedKingdom->{'current_'.$resourceName} = $newAmount;

$requestedKingdom->save();

$requestedKingdom = $requestedKingdom->refresh();
}

if ($this->type === CapitalCityResourceRequestType::UNIT_QUEUE) {
$unitRequestData = $queueData->unit_request_data;

$updatedUnits = collect($unitRequestData)->transform(function ($unit) {
return $unit['secondary_status'] === CapitalCityQueueStatus::REQUESTING
? array_merge($unit, ['secondary_status' => CapitalCityQueueStatus::RECRUITING])
: $unit;
});

$queueData->update([
'unit_request_data' => $updatedUnits,
]);

$queueData = $queueData->refresh();

event(new UpdateCapitalCityUnitQueueTable($queueData->character->refresh()));

dump('Process the recruiting of the units.');

return;
}

if ($this->type === CapitalCityResourceRequestType::BUILDING_QUEUE) {
$buildingRequestData = $queueData->building_request_data;

$updatedBuildingRequestData = collect($buildingRequestData)->transform(function ($buildingData) {
if ($buildingData['secondary_status'] === CapitalCityQueueStatus::REQUESTING) {
$newStatus = ($buildingData['type'] === 'repair')
? CapitalCityQueueStatus::REPAIRING
: CapitalCityQueueStatus::BUILDING;

return array_merge($buildingData, ['secondary_status' => $newStatus]);
}

return $buildingData;
});


$queueData->update([
'building_request_data' => $updatedBuildingRequestData,
]);

$queueData = $queueData->refresh();

event(new UpdateCapitalCityBuildingQueueTable($queueData->character->refresh()));

dump('Process the building or repairing of the buildings.');

return;
}

throw new Exception(
'Could not determine what to do with the resources.'
);
}
}
6 changes: 3 additions & 3 deletions app/Game/Kingdoms/Service/ResourceTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private function hasRequestedResourceAmount(Kingdom $requestFromKingdom, int $am
return $amount <= $currentAmountFromKingdom;
}

private function canAffordPopulationCost(Kingdom $requestFromKingdom): bool
public function canAffordPopulationCost(Kingdom $requestFromKingdom): bool
{
return $requestFromKingdom->current_population >= self::POPULATION_COST;
}
Expand All @@ -178,7 +178,7 @@ private function hasAirShip(Kingdom $requestFromKingdom): bool
return $requestFromKingdom->units->where('gameUnit.name', '=', UnitNames::AIRSHIP)->count() > 0;
}

private function hasRequiredSpearmen(Kingdom $requestFromKingdom): bool
public function hasRequiredSpearmen(Kingdom $requestFromKingdom): bool
{

$spearmen = $requestFromKingdom->units->where('gameUnit.name', '=', UnitNames::SPEARMEN)->first();
Expand All @@ -190,7 +190,7 @@ private function hasRequiredSpearmen(Kingdom $requestFromKingdom): bool
return $spearmen->amount >= self::SPEARMEN_COST;
}

private function bothKingdomsHaveAMarketPlace(Kingdom $requestingKingdom, Kingdom $requestingFromKingdom): bool
public function bothKingdomsHaveAMarketPlace(Kingdom $requestingKingdom, Kingdom $requestingFromKingdom): bool
{

$requestingMarketPlace = $requestingKingdom->buildings->where('gameBuilding.name', '=', BuildingCosts::MARKET_PLACE)->where('level', '>=', 5)->first();
Expand Down

0 comments on commit a9876d7

Please sign in to comment.