Lines 100.00% 18 / 18
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
UsableEnhancements 100.00% 18 / 18 100.00% 2 / 2 10 100.00% 1 / 1
 among 100.00% 6 / 6 100.00% 1 / 1 3
 usable 100.00% 12 / 12 100.00% 1 / 1 7
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Bot\Abilities\Gear;
6
7use Lorekeeper\Core\Item\Equipment\Equipment;
8use Lorekeeper\Core\Item\Item;
9use Lorekeeper\Core\Item\ItemPrice;
10use Lorekeeper\Core\Item\ItemTag;
11use Lorekeeper\Core\Item\Misc\Enhancement\EnhancementItem;
12use Lorekeeper\Core\Player\Hero\Hero;
13use Lorekeeper\Core\Player\PlayerTag;
14use Lorekeeper\Core\World\Currency\Gold;
15use Lorekeeper\Core\World\Shop\CurrencyShop;
16
17final readonly class UsableEnhancements
18{
19    /** @return list<EnhancementItem> */
20    public static function among(CurrencyShop $shop, Equipment $held, Hero $hero): array
21    {
22        $usable = [];
23        foreach ($shop->inventory as $sold) {
24            if (!self::usable($sold, $shop, $held, $hero)) {
25                continue;
26            }
27
28            $usable[] = $sold;
29        }
30
31        return $usable;
32    }
33
34    /** @phpstan-assert-if-true EnhancementItem $sold */
35    private static function usable(Item $sold, CurrencyShop $shop, Equipment $held, Hero $hero): bool
36    {
37        return (
38            $sold instanceof EnhancementItem
39            && $sold->fits($held)
40            && $sold->level->value <= $hero->level->value
41            && ($hero->tags->has(PlayerTag::Member) || !$sold->tags->has(ItemTag::MemberOnly))
42            && $shop->price($sold->id)->isSomeAnd(
43                static fn(ItemPrice $price): bool => (
44                    $price->amount instanceof Gold
45                    && $hero->inventory->wallet->affords($price)
46                ),
47            )
48        );
49    }
50}