Skip to content

Commit

Permalink
Last thing to do is make sure all the hands are filled
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKyle committed Nov 30, 2023
1 parent a8574f3 commit 0c63863
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class FetchBestItemForPositionFromInventory {

private ?Collection $inventorySlots = null;

private ?Collection $currentlyEquipped = null;

private InventoryItemComparison $inventoryItemComparison;

public function __construct(InventoryItemComparison $inventoryItemComparison) {
Expand All @@ -21,8 +23,35 @@ public function setInventory(?Collection $inventory = null): FetchBestItemForPos
return $this;
}

public function setCurrentlyEquipped(?Collection $currentlyEquipped = null): FetchBestItemForPositionFromInventory {
$this->currentlyEquipped = $currentlyEquipped;

return $this;
}

public function fetchBestItemForPosition(array $typesForPosition, bool $ignoreMythicsAndUniques = false): ?InventorySlot {
$inventorySlots = $this->fetchBestItemsForPositionTypes($typesForPosition, $ignoreMythicsAndUniques);

$hasMythicEquipped = $this->currentlyHasSpecialEquipped('is_mythic');
$hasUniqueEquipped = $this->currentlyHasSpecialEquipped('is_unique');

if (!$ignoreMythicsAndUniques && !$hasMythicEquipped) {

$mythicSlots = $this->fetchSpecialItemFromPossibleItems($typesForPosition, 'is_mythic');

if (!empty($mythicSlots)) {
return $this->findBestItem($mythicSlots);
}
}

if (!$ignoreMythicsAndUniques && !$hasUniqueEquipped && !$hasMythicEquipped) {
$uniqueSlots = $this->fetchSpecialItemFromPossibleItems($typesForPosition, 'is_unique');

if (!empty($uniqueSlots)) {
return $this->findBestItem($uniqueSlots);
}
}

$inventorySlots = $this->fetchBestItemsForPositionTypes($typesForPosition);

if (empty($inventorySlots)) {
return null;
Expand All @@ -31,6 +60,32 @@ public function fetchBestItemForPosition(array $typesForPosition, bool $ignoreMy
return $this->findBestItem($inventorySlots);
}

protected function fetchSpecialItemFromPossibleItems(array $typesForPosition, string $attributeKey): array {

$specialSlotsForType = [];

if (!$this->currentlyHasSpecialEquipped($attributeKey)) {

$specialSlotsForType = $this->inventorySlots
->reject(fn($slot) => $this->currentlyHasSpecialEquipped($attributeKey))
->filter(function ($slot) use ($typesForPosition, $attributeKey) {
return in_array($slot->item->type, $typesForPosition) && $slot->item->{$attributeKey} === true;
})->all();
}

return array_values($specialSlotsForType);
}

protected function currentlyHasSpecialEquipped(string $key): bool {
if (is_null($this->currentlyEquipped)) {
return false;
}

return $this->currentlyEquipped->filter(function($slot) use ($key) {
return $slot->item->{$key} === true;
})->isNotEmpty();
}

protected function findBestItem(array $itemSlotsForTypes): ?InventorySlot {
if (count($itemSlotsForTypes) === 1) {
return $itemSlotsForTypes[0];
Expand All @@ -55,7 +110,7 @@ protected function findBestItem(array $itemSlotsForTypes): ?InventorySlot {
return $bestItemForPosition;
}

protected function fetchBestItemsForPositionTypes(array $typesForPosition, bool $ignoreMythicsAndUniques = false): array {
protected function fetchBestItemsForPositionTypes(array $typesForPosition): array {

$bestItems = [];

Expand All @@ -65,13 +120,8 @@ protected function fetchBestItemsForPositionTypes(array $typesForPosition, bool

foreach ($typesForPosition as $type) {
$bestSlot = $this->inventorySlots
->filter(function ($slot) use ($type, $ignoreMythicsAndUniques) {

if ($ignoreMythicsAndUniques) {
return $slot->item->type === $type && !$slot->item->is_unique && !$slot->item->is_mythic;
}

return $slot->item->type === $type;
->filter(function ($slot) use ($type) {
return $slot->item->type === $type && !$slot->item->is_unique && !$slot->item->is_mythic;
})
->reduce(function ($bestItem, $currentItem) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function handleHands(Character $character, ?InventorySlot $slotToEquip, s
$slotForPosition = $this->getSlotForPosition($oppositePosition);

if (is_null($slotForPosition)) {

$this->equipItem($character, $slotToEquip, $position);

return $character->refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Flare\Builders\Character\Traits\FetchEquipped;
use App\Flare\Models\Character;
use App\Flare\Values\ArmourTypes;
use App\Flare\Values\WeaponTypes;
use App\Game\CharacterInventory\Handlers\EquipBest\FetchBestItemForPositionFromInventory;
use App\Game\CharacterInventory\Handlers\EquipBest\HandleHands;
use App\Game\CharacterInventory\Handlers\EquipBest\HandleRegularComparisonAndReplace;
Expand Down Expand Up @@ -65,35 +67,74 @@ public function hasEquipmentChanged(): bool {

protected function processPositions(Character $character): void {

$this->fetchBestItemForPositionFromInventory = $this->fetchBestItemForPositionFromInventory->setInventory($this->inventorySlots);
$this->fetchBestItemForPositionFromInventory = $this->fetchBestItemForPositionFromInventory
->setCurrentlyEquipped($this->currentlyEquippedSlots)
->setInventory($this->inventorySlots);

$positions = EquippablePositions::equippablePositions();

$character = $this->handleEquippingBest($character, $positions);

$currentHands = collect($this->currentlyEquippedSlots->whereIn('position', [
EquippablePositions::LEFT_HAND, EquippablePositions::RIGHT_HAND
])->all());

if ($currentHands->count() === 1) {

$hasSpecialEquipped = $this->currentlyEquippedSlots->filter(function($slot) {
return $slot->item->is_unique || $slot->item->is_mythic;
})->isNotEmpty();

$currentSlot = $currentHands->first();

$oppositeHand = EquippablePositions::getOppisitePosition($currentSlot->position);

$bestSlot = $this->fetchBestItemForPositionFromInventory
->fetchBestItemForPosition(EquippablePositions::typesForPositions($oppositeHand), $hasSpecialEquipped);

if (!is_null($bestSlot)) {
$this->handleHands->setCurrentlyEquipped($this->currentlyEquippedSlots)
->handleHands($character, $bestSlot, $oppositeHand);
}
}

}

protected function handleEquippingBest(Character $character, array $positions, array $overrideTypesForPosition = []): Character {
foreach ($positions as $position) {

$typesForEquip = EquippablePositions::typesForPositions($position);

if (!empty($overrideTypesForPosition)) {
$typesForEquip = $overrideTypesForPosition;
}

$bestSlot = $this->fetchBestItemForPositionFromInventory
->fetchBestItemForPosition($typesForEquip);

foreach (EquippablePositions::equippablePositions() as $position) {
$bestSlot = $this->fetchBestItemForPositionFromInventory->fetchBestItemForPosition(EquippablePositions::typesForPositions($position));

if (is_null($bestSlot)) {

continue;
}

dump($bestSlot->id . ' ' . $bestSlot->item->affix_name . ' ' . $position);

if ($bestSlot->item->is_mythic || $bestSlot->item->is_unique) {

$character = $this->handleUniquesAndMythics->setCurrentlyEquipped($this->currentlyEquippedSlots)
->handleUniquesOrMythics($character, $position, $bestSlot);
->handleUniquesOrMythics($character, $position, $bestSlot);

$this->fetchInventoryDetails($character);

if (!$this->handleUniquesAndMythics->replacedSpecialItem()) {
dump($this->inventorySlots->pluck('item.affix_name')->toArray());
continue;
}

$slotForReplacedSpecialItem = $this->fetchBestItemForPositionFromInventory->fetchBestItemForPosition(
EquippablePositions::typesForPositions(
$this->handleUniquesAndMythics->getSpecialSlotPosition()
),
true,
);
$position = $this->handleUniquesAndMythics->getSpecialSlotPosition();

$slotForReplacedSpecialItem = $this->fetchBestItemForPositionFromInventory
->fetchBestItemForPosition(
EquippablePositions::typesForPositions($position),
true,
);

if (is_null($slotForReplacedSpecialItem)) {
continue;
Expand All @@ -104,8 +145,7 @@ protected function processPositions(Character $character): void {

if (in_array($position, [EquippablePositions::LEFT_HAND, EquippablePositions::RIGHT_HAND])) {
$character = $this->handleHands->setCurrentlyEquipped($this->currentlyEquippedSlots)
->handleHands($character, $bestSlot, $position);

->handleHands($character, $bestSlot, $position);

$this->fetchInventoryDetails($character);

Expand All @@ -114,7 +154,7 @@ protected function processPositions(Character $character): void {

if (in_array($position, [EquippablePositions::TRINKET, EquippablePositions::ARTIFACT])) {
$character = $this->handleHands->setCurrentlyEquipped($this->currentlyEquippedSlots)
->handleHands($character, $bestSlot, $position);
->handleHands($character, $bestSlot, $position);


$this->fetchInventoryDetails($character);
Expand All @@ -123,10 +163,12 @@ protected function processPositions(Character $character): void {
}

$this->handleRegularComparisonAndReplace->setCurrentlyEquipped($this->currentlyEquippedSlots)
->handleRegularComparison($character, $bestSlot, $position);
->handleRegularComparison($character, $bestSlot, $position);

$this->fetchInventoryDetails($character);
}

return $character->refresh();
}

protected function fetchInventoryDetails(Character $character): void {
Expand All @@ -147,6 +189,8 @@ protected function fetchInventoryDetails(Character $character): void {
$this->equipmentHasChanged = $this->currentlyEquippedSlots->diff($oldCurrentlyEquipped)->count() > 0;
}

$this->fetchBestItemForPositionFromInventory = $this->fetchBestItemForPositionFromInventory->setInventory($this->inventorySlots);
$this->fetchBestItemForPositionFromInventory = $this->fetchBestItemForPositionFromInventory
->setCurrentlyEquipped($this->currentlyEquippedSlots)
->setInventory($this->inventorySlots);
}
}

0 comments on commit 0c63863

Please sign in to comment.