Lines
100.00%
18 / 18
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| TurnInQuest | 100.00% | 18 / 18 | 100.00% | 3 / 3 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| describe | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| play | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 5 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Bot\Steps\Quests; | |
| 6 | ||
| 7 | use Generator; | |
| 8 | use Lorekeeper\Bot\Steps\Action; | |
| 9 | use Lorekeeper\Bot\Steps\StepScript; | |
| 10 | use Lorekeeper\Client\Script\Failed; | |
| 11 | use Lorekeeper\Core\Item\ItemId; | |
| 12 | use Lorekeeper\Core\World\Quest\QuestId; | |
| 13 | use Override; | |
| 14 | ||
| 15 | final readonly class TurnInQuest implements Action | |
| 16 | { | |
| 17 | public function __construct( | |
| 18 | public QuestId $quest, | |
| 19 | public ?ItemId $reward, | |
| 20 | ) {} | |
| 21 | ||
| 22 | #[Override] | |
| 23 | public function describe(): string | |
| 24 | { | |
| 25 | return $this->reward === null | |
| 26 | ? "Turn in quest {$this->quest->value}" | |
| 27 | : "Turn in quest {$this->quest->value} for item {$this->reward->value}"; | |
| 28 | } | |
| 29 | ||
| 30 | #[Override] | |
| 31 | public function play(StepScript $bot): Generator | |
| 32 | { | |
| 33 | $heldBefore = $this->reward === null ? 0 : $bot->me()->hero()->inventory->quantity($this->reward); | |
| 34 | $turningIn = $bot->turnIn($this->quest); | |
| 35 | yield from $turningIn; | |
| 36 | $refusal = $turningIn->getReturn(); | |
| 37 | if ($refusal !== null) { | |
| 38 | return new Failed("the game would not complete the quest ({$refusal})"); | |
| 39 | } | |
| 40 | ||
| 41 | $reward = $this->reward; | |
| 42 | if ($reward === null) { | |
| 43 | return null; | |
| 44 | } | |
| 45 | ||
| 46 | $collecting = $bot->collect($reward, $heldBefore); | |
| 47 | yield from $collecting; | |
| 48 | ||
| 49 | return $collecting->getReturn() | |
| 50 | ? null | |
| 51 | : new Failed("the quest was completed, but item {$reward->value} could not be picked up"); | |
| 52 | } | |
| 53 | } |