Skip to content

Commit

Permalink
created a way to handle updating event map based faction loyalties wh…
Browse files Browse the repository at this point in the history
…en purgatory is unlocked, while the event is running
  • Loading branch information
AdamKyle committed Dec 17, 2024
1 parent 60f048f commit af86bf2
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 54 deletions.
47 changes: 47 additions & 0 deletions app/Console/AfterDeployment/UpdateCharacterFactionBounties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Console\AfterDeployment;

use App\Flare\Models\Character;
use App\Game\Factions\FactionLoyalty\Services\UpdateFactionLoyaltyService;
use Illuminate\Console\Command;

class UpdateCharacterFactionBounties extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update:character-faction-bounties {characterId}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update a specific characters faction bounties';

/**
* Execute the console command.
*/
public function handle(UpdateFactionLoyaltyService $updateFactionLoyaltyService) {
$characterId = $this->argument('characterId');

if (is_null($characterId)) {
$this->error('Missing character id.');

return;
}

$character = Character::find($characterId);

if (is_null($character)) {
$this->error('Character not found.');

return;
}

$updateFactionLoyaltyService->updateFactionLoyaltyBountyTasks($character);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Game\Factions\FactionLoyalty\Providers;

use App\Game\Factions\FactionLoyalty\Services\FactionLoyaltyService;
use App\Game\Factions\FactionLoyalty\Services\UpdateFactionLoyaltyService;
use Illuminate\Support\ServiceProvider as ApplicationServiceProvider;

class ServiceProvider extends ApplicationServiceProvider
Expand All @@ -14,10 +15,13 @@ class ServiceProvider extends ApplicationServiceProvider
*/
public function register()
{

$this->app->bind(FactionLoyaltyService::class, function ($app) {
return new FactionLoyaltyService;
});

$this->app->bind(UpdateFactionLoyaltyService::class, function ($app) {
return new UpdateFactionLoyaltyService;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace App\Game\Factions\FactionLoyalty\Services;

use Illuminate\Support\Collection;
use App\Flare\Models\Character;
use App\Flare\Models\FactionLoyaltyNpc;
use App\Flare\Models\FactionLoyaltyNpcTask;
use App\Flare\Models\GameMap;
use App\Flare\Models\Monster;
use App\Flare\Values\MapNameValue;

class UpdateFactionLoyaltyService {

/**
* Update faction loyalty bounty tasks for a character.
*
* @param Character $character
* @return void
*/
public function updateFactionLoyaltyBountyTasks(Character $character): void {
$gameMaps = GameMap::whereIn('name', [
MapNameValue::DELUSIONAL_MEMORIES,
MapNameValue::ICE_PLANE,
])->get();

$characterFactions = $character->factions()->whereIn('game_map_id', $gameMaps->pluck('id')->toArray())->get();

$characterFactionLoyalties = $character->factionLoyalties()->whereIn('faction_id', $characterFactions->pluck('id')->toArray())->get();

foreach ($characterFactionLoyalties as $characterFactionLoyalty) {
$factionLoyaltyNpcs = $characterFactionLoyalty->factionLoyaltyNpcs;

$this->handleFactionLoyaltyNpcs($factionLoyaltyNpcs);
}
}

/**
* Handle the faction loyalty for npcs.
*
* @param Collection $factionLoyaltyNpcs
* @return void
*/
private function handleFactionLoyaltyNpcs(Collection $factionLoyaltyNpcs): void {
foreach ($factionLoyaltyNpcs as $factionLoyaltyNpc) {
$task = $factionLoyaltyNpc->factionLoyaltyNpcTasks;

$this->handleTasksForNpc($factionLoyaltyNpc, $task);
}
}

/**
* Handle tasks for npc.
*
* @param FactionLoyaltyNpc $factionLoyaltyNpc
* @param FactionLoyaltyNpcTask $factionLoyaltyNpcTask
* @return void
*/
private function handleTasksForNpc(FactionLoyaltyNpc $factionLoyaltyNpc, FactionLoyaltyNpcTask $factionLoyaltyNpcTask): void {
$tasks = $factionLoyaltyNpcTask->fame_tasks;

$gameMapId = $factionLoyaltyNpc->factionLoyalty->faction->game_map_id;

foreach ($tasks as $index => $task) {
if ($task['type'] === 'bounty') {
$monster = Monster::find($task['monster_id']);

if ($monster->game_map_id !== $gameMapId) {
$newMonster = $this->fetchNewMonster($tasks, $gameMapId);


$task['monster_id'] = $newMonster->id;
$task['monster_name'] = $newMonster->name;

$tasks[$index] = $task;

continue;
}
}
}

$factionLoyaltyNpcTask->update([
'fame_tasks' => $tasks,
]);
}

/**
* Handle finding a new monster for the task.
*
* @param array $tasks
* @param int $gameMapId
* @return Monster
*/
private function fetchNewMonster(array $tasks, int $gameMapId): Monster {
$monster = Monster::where('game_map_id', $gameMapId)
->where('is_raid_monster', false)
->where('is_raid_boss', false)
->where('is_celestial_entity', false)
->whereNull('only_for_location_type')
->inRandomOrder()
->first();

if ($this->hasTaskAlready($tasks, 'monster_id', $monster->id)) {
return $this->fetchNewMonster($tasks, $gameMapId);
}

return $monster;
}

/**
* Check if the monster already has a task.
*
* @param array $tasks
* @param string $key
* @param int $id
* @return bool
*/
private function hasTaskAlready(array $tasks, string $key, int $id): bool
{
foreach ($tasks as $task) {
if ($task['type'] === 'bounty') {
if ($task[$key] === $id) {
return true;
}
}

}

return false;
}
}
15 changes: 9 additions & 6 deletions app/Game/Quests/Handlers/NpcQuestRewardHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Game\Character\Builders\AttackBuilders\Jobs\CharacterAttackTypesCacheBuilder;
use App\Game\Core\Events\UpdateTopBarEvent;
use App\Game\Core\Traits\HandleCharacterLevelUp;
use App\Game\Factions\FactionLoyalty\Services\UpdateFactionLoyaltyService;
use App\Game\Messages\Builders\NpcServerMessageBuilder;
use App\Game\Messages\Events\GlobalMessageEvent;
use App\Game\Messages\Events\ServerMessageEvent;
Expand All @@ -23,12 +24,10 @@ class NpcQuestRewardHandler
{
use HandleCharacterLevelUp;

private NpcServerMessageBuilder $npcServerMessageBuilder;

public function __construct(NpcServerMessageBuilder $npcServerMessageBuilder)
{
$this->npcServerMessageBuilder = $npcServerMessageBuilder;
}
public function __construct(
private readonly NpcServerMessageBuilder $npcServerMessageBuilder,
private readonly UpdateFactionLoyaltyService $updateFactionLoyaltyService)
{}

public function processReward(Quest $quest, Npc $npc, Character $character): void
{
Expand Down Expand Up @@ -163,6 +162,10 @@ public function giveItem(Character $character, Quest $quest, Npc $npc): void

broadcast(new ServerMessageEvent($character->user, 'Careful, child. You seem to have angered The Creator. Are you prepared?'));
}

if ($effectType->purgatory()) {
$this->updateFactionLoyaltyService->updateFactionLoyaltyBountyTasks($character);
}
}

$foundQuestitem = $character->inventory->slots->filter(function ($slot) use ($quest) {
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Console\AfterDeployment\UpdateCharacterFactionBounties;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -72,6 +73,7 @@ public function register(): void
RebalanceTrinkets::class,
ClearPlayersJobs::class,
ClearInvalidCapitalCityQueues::class,
UpdateCharacterFactionBounties::class,

// Development Commands:
CreateCharacter::class,
Expand Down
Loading

0 comments on commit af86bf2

Please sign in to comment.