Lines
100.00%
105 / 105
Methods
100.00%
17 / 17
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| moveTo | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| attempt | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [Lorekeeper\Bot\Abilities\Moves\Joins] join | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 3 | ||
| [Lorekeeper\Bot\Abilities\Moves\Joins] leaveCombat | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 4 | ||
| [Lorekeeper\Bot\Abilities\Moves\Joins] refusal | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 4 | ||
| [Lorekeeper\Bot\Abilities\Moves\Hunts] hunt | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 7 | ||
| [Lorekeeper\Bot\Abilities\Moves\Hunts] holds | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Bot\Abilities\Quests\Quests] me | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| [Lorekeeper\Bot\Abilities\Quests\Quests] send | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| [Lorekeeper\Bot\Abilities\Quests\Quests] alreadyDone | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 4 | ||
| [Lorekeeper\Bot\Abilities\Quests\Quests] accept | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [Lorekeeper\Bot\Abilities\Quests\Quests] turnIn | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 5 | ||
| [Lorekeeper\Bot\Abilities\Moves\Rests] rest | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 5 | ||
| [Lorekeeper\Bot\Abilities\Paces] paced | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 7 | ||
| [Lorekeeper\Bot\BotScript] __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Bot\BotScript] rules | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| [Lorekeeper\Bot\BotScript] run | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Bot\BotScript] me | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Bot\BotScript] send | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Bot\BotScript] now | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 21 | abstract class StepScript extends BotScript | |
| 22 | { | |
| 23 | use Joins, Hunts, Quests, Rests, Paces { | |
| 24 | Joins::join as public; | |
| 25 | Hunts::hunt as public; | |
| 26 | Hunts::kill as public; | |
| 27 | Hunts::attackIn as public; | |
| 28 | Hunts::collect as public; | |
| 29 | Quests::alreadyDone as public; | |
| 30 | Quests::accept as public; | |
| 31 | Quests::turnIn as public; | |
| 32 | Rests::rest as public; | |
| 33 | Paces::paced as public; | |
| 34 | } | |
| 35 | ||
| 36 | /** @return Generator<int, Instruction, null|Event|TimedOut, void> */ | |
| 37 | public function moveTo(MapSpot $spot): Generator | |
| 38 | { | |
| 39 | yield from $this->jump($spot); | |
| 40 | } | |
| 41 | ||
| 42 | /** @return Generator<int, Instruction, null|Event|TimedOut, null|Failed> */ | |
| 43 | final public function attempt(Action $action): Generator | |
| 44 | { | |
| 45 | try { | |
| 46 | $playing = $action->play($this); | |
| 47 | yield from $playing; | |
| 48 | ||
| 49 | return $playing->getReturn(); | |
| 50 | } catch (RuntimeException $broke) { | |
| 51 | return new Failed($broke->getMessage()); | |
| 52 | } | |
| 53 | } | |
| 54 | } |
From Lorekeeper\Bot\Abilities\Moves\Joins
| 25 | trait Joins | |
| 26 | { | |
| 27 | use Jumps; | |
| 28 | ||
| 29 | private const int SECONDS_TO_LEAVE_COMBAT = 10; | |
| 30 | ||
| 31 | /** | |
| 32 | * @throws RuntimeException | |
| 33 | * | |
| 34 | * @return Generator<int, Instruction, null|Event|TimedOut, void> | |
| 35 | */ | |
| 36 | protected function join(MapName $map): Generator | |
| 37 | { | |
| 38 | if ($this->me()->location()->map->name->value === $map->value) { | |
| 39 | return; | |
| 40 | } | |
| 41 | ||
| 42 | yield from $this->leaveCombat(); | |
| 43 | ||
| 44 | /** @var Event|TimedOut $joined */ | |
| 45 | $joined = yield $this->send(new JoinMapCommand($this->me()->hero()->name, $map, Option\none()))->awaits( | |
| 46 | Duration::seconds(15), | |
| 47 | MapJoins::class, | |
| 48 | MapRefusals::class, | |
| 49 | ); | |
| 50 | ||
| 51 | if (!MapJoins::contains($joined)) { | |
| 52 | throw new RuntimeException("Could not join {$map->value}: " . self::refusal($joined)); | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | /** @return Generator<int, Instruction, null|Event|TimedOut, void> */ | |
| 57 | private function leaveCombat(): Generator | |
| 58 | { | |
| 59 | if ($this->me()->state() !== CombatState::Fighting) { | |
| 60 | return; | |
| 61 | } | |
| 62 | ||
| 63 | yield from $this->jump(new MapSpot(new MapCell('Enter'), MapPad::Spawn)); | |
| 64 | for ($second = 0; $second < self::SECONDS_TO_LEAVE_COMBAT; $second++) { | |
| 65 | if ($this->me()->state() !== CombatState::Fighting) { | |
| 66 | return; | |
| 67 | } | |
| 68 | ||
| 69 | yield new Await([PlayerStateUpdatedEvent::class], Duration::seconds(1)); | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | private static function refusal(mixed $answer): string | |
| 74 | { | |
| 75 | return match (true) { | |
| 76 | $answer instanceof Event => $answer->summary(), | |
| 77 | $answer instanceof TimedOut => 'no answer', | |
| 78 | default => 'the server refused', | |
| 79 | }; | |
| 80 | } | |
| 81 | } |
From Lorekeeper\Bot\Abilities\Moves\Hunts
| 19 | trait Hunts | |
| 20 | { | |
| 21 | use Kills; | |
| 22 | use Loots; | |
| 23 | ||
| 24 | /** | |
| 25 | * @throws RuntimeException | |
| 26 | * | |
| 27 | * @return Generator<int, Instruction, null|Event|TimedOut, bool> | |
| 28 | */ | |
| 29 | protected function hunt(MapSpot $spot, Attack $attack, ItemId $item, Quantity $wanted, Duration $limit): Generator | |
| 30 | { | |
| 31 | $deadline = $this->now()->plus($limit); | |
| 32 | while (!$this->holds($item, $wanted) && !$this->now()->after($deadline)) { | |
| 33 | yield from $this->kill( | |
| 34 | $spot, | |
| 35 | $attack, | |
| 36 | fn(): bool => ( | |
| 37 | $this->holds($item, $wanted) | |
| 38 | || $this->me()->drops()->find($item) !== null | |
| 39 | || $this->now()->after($deadline) | |
| 40 | ), | |
| 41 | ); | |
| 42 | if ($this->me()->drops()->find($item) === null) { | |
| 43 | continue; | |
| 44 | } | |
| 45 | ||
| 46 | $pickingUp = $this->pickUp($item); | |
| 47 | yield from $pickingUp; | |
| 48 | if (!$pickingUp->getReturn()) { | |
| 49 | break; | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | return $this->holds($item, $wanted); | |
| 54 | } | |
| 55 | ||
| 56 | private function holds(ItemId $item, Quantity $wanted): bool | |
| 57 | { | |
| 58 | return $this | |
| 59 | ->me() | |
| 60 | ->hero() | |
| 61 | ->inventory->quantity($item) >= $wanted->value; | |
| 62 | } | |
| 63 | } |
From Lorekeeper\Bot\Abilities\Quests\Quests
| 24 | trait Quests | |
| 25 | { | |
| 26 | abstract protected function me(): Character; | |
| 27 | ||
| 28 | abstract protected function send(Command $command): Send; | |
| 29 | ||
| 30 | /** @return Generator<int, Instruction, null|Event|TimedOut, bool> */ | |
| 31 | protected function alreadyDone(QuestId $quest): Generator | |
| 32 | { | |
| 33 | /** @var Event|TimedOut $answer */ | |
| 34 | $answer = yield $this->send(new LoadQuestCommand($this->me()->location()->area, $quest))->awaits( | |
| 35 | Duration::seconds(10), | |
| 36 | QuestLoadedEvent::class, | |
| 37 | ); | |
| 38 | if (!$answer instanceof QuestLoadedEvent) { | |
| 39 | return false; | |
| 40 | } | |
| 41 | ||
| 42 | foreach ($answer->quests as $loaded) { | |
| 43 | if ($loaded->id->value !== $quest->value) { | |
| 44 | continue; | |
| 45 | } | |
| 46 | ||
| 47 | return $this | |
| 48 | ->me() | |
| 49 | ->hero() | |
| 50 | ->progression->quests->completed($loaded); | |
| 51 | } | |
| 52 | ||
| 53 | return false; | |
| 54 | } | |
| 55 | ||
| 56 | /** @return Generator<int, Instruction, null|Event|TimedOut, bool> */ | |
| 57 | protected function accept(QuestId $quest): Generator | |
| 58 | { | |
| 59 | /** @var Event|TimedOut $answer */ | |
| 60 | $answer = yield $this->send(new AcceptQuestCommand($this->me()->location()->area, $quest))->awaits( | |
| 61 | Duration::seconds(10), | |
| 62 | QuestAcceptedEvent::class, | |
| 63 | ); | |
| 64 | ||
| 65 | return $answer instanceof QuestAcceptedEvent && $answer->quest->value === $quest->value; | |
| 66 | } | |
| 67 | ||
| 68 | /** @return Generator<int, Instruction, null|Event|TimedOut, ?string> */ | |
| 69 | protected function turnIn(QuestId $quest): Generator | |
| 70 | { | |
| 71 | /** @var Event|TimedOut $answer */ | |
| 72 | $answer = yield $this->send(new TurnInQuestCommand($this->me()->location()->area, $quest))->awaits( | |
| 73 | Duration::seconds(10), | |
| 74 | QuestCompletedEvent::class, | |
| 75 | QuestTurnInRefusedEvent::class, | |
| 76 | ); | |
| 77 | ||
| 78 | return match (true) { | |
| 79 | $answer instanceof QuestCompletedEvent => null, | |
| 80 | $answer instanceof QuestTurnInRefusedEvent => $answer->reason === '' ? 'no reason given' : $answer->reason, | |
| 81 | default => 'the game did not answer', | |
| 82 | }; | |
| 83 | } | |
| 84 | } |
From Lorekeeper\Bot\Abilities\Moves\Rests
| 21 | trait Rests | |
| 22 | { | |
| 23 | use Jumps; | |
| 24 | ||
| 25 | private const int SECONDS_TO_STOP_FIGHTING = 15; | |
| 26 | ||
| 27 | private const int SECONDS_TO_COOL_DOWN = 5; | |
| 28 | ||
| 29 | /** @return Generator<int, Instruction, null|Event|TimedOut, void> */ | |
| 30 | protected function rest(): Generator | |
| 31 | { | |
| 32 | $fought = $this->me()->state() === CombatState::Fighting; | |
| 33 | if ($this->me()->location()->spot->cell->value !== 'Enter') { | |
| 34 | yield from $this->jump(new MapSpot(new MapCell('Enter'), MapPad::Spawn)); | |
| 35 | } | |
| 36 | ||
| 37 | for ($second = 0; $second < self::SECONDS_TO_STOP_FIGHTING; $second++) { | |
| 38 | if ($this->me()->state() !== CombatState::Fighting) { | |
| 39 | break; | |
| 40 | } | |
| 41 | ||
| 42 | $fought = true; | |
| 43 | yield new Await([PlayerStateUpdatedEvent::class, PlayersStateChangedEvent::class], Duration::seconds(1)); | |
| 44 | } | |
| 45 | ||
| 46 | if ($fought) { | |
| 47 | yield new Pause(Duration::seconds(self::SECONDS_TO_COOL_DOWN)); | |
| 48 | } | |
| 49 | } | |
| 50 | } |
From Lorekeeper\Bot\Abilities\Paces
| 18 | trait Paces | |
| 19 | { | |
| 20 | private const int TRIES_WHEN_TOO_SOON = 4; | |
| 21 | ||
| 22 | /** @return Generator<int, Instruction, null|Event|TimedOut, null|Event|TimedOut> */ | |
| 23 | protected function paced(Send $send): Generator | |
| 24 | { | |
| 25 | $await = $send->await; | |
| 26 | if ($await === null) { | |
| 27 | return yield $send; | |
| 28 | } | |
| 29 | ||
| 30 | $watching = new Send($send->command, new Await([ | |
| 31 | ...$await->events, | |
| 32 | ActionTooSoonEvent::class, | |
| 33 | TooSoonAfterCombatEvent::class, | |
| 34 | ], $await->timeout)); | |
| 35 | for ($try = 1;; $try++) { | |
| 36 | $answer = yield $watching; | |
| 37 | $tooSoon = $answer instanceof ActionTooSoonEvent || $answer instanceof TooSoonAfterCombatEvent; | |
| 38 | if (!$tooSoon || $try >= self::TRIES_WHEN_TOO_SOON) { | |
| 39 | return $answer; | |
| 40 | } | |
| 41 | ||
| 42 | yield new Pause(Duration::seconds($answer instanceof TooSoonAfterCombatEvent ? 2 : 1)); | |
| 43 | } | |
| 44 | } | |
| 45 | } |
Inherited from Lorekeeper\Bot\BotScript
| 30 | public function __construct(?Closure $clock = null) | |
| 31 | { | |
| 32 | $this->clock = $clock ?? Timestamp::monotonic(...); | |
| 33 | } |
| 36 | abstract protected function rules(): Generator; |
| 40 | final public function run(Character $me): Generator | |
| 41 | { | |
| 42 | $this->me = $me; | |
| 43 | ||
| 44 | return $this->rules(); | |
| 45 | } |
| 47 | public function me(): Character | |
| 48 | { | |
| 49 | return $this->me; | |
| 50 | } |
| 52 | public function send(Command $command): Send | |
| 53 | { | |
| 54 | return new Send($command); | |
| 55 | } |
| 57 | public function now(): Timestamp | |
| 58 | { | |
| 59 | return ($this->clock)(); | |
| 60 | } |