128 lines
4.1 KiB
PHP
128 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Game\Quests;
|
|
|
|
use App\Game\WorldConfig;
|
|
|
|
/**
|
|
* The quest lines and how far a player is in each, from facts about the player:
|
|
* the highest level per building type, the most instances of a type in one village,
|
|
* and the action counters. A line's current quest is its first unclaimed one; it is
|
|
* complete while every goal is met. Nothing is stored but which quests were claimed.
|
|
*/
|
|
final class QuestBook
|
|
{
|
|
public function __construct(private readonly WorldConfig $config) {}
|
|
|
|
/** @return array<string, array<string, array{goals: list<array>, reward: array<string, int>}>> */
|
|
public function lines(): array
|
|
{
|
|
return $this->config->get('quests.lines', []);
|
|
}
|
|
|
|
/** @return array{goals: list<array>, reward: array<string, int>}|null */
|
|
public function quest(string $key): ?array
|
|
{
|
|
foreach ($this->lines() as $quests) {
|
|
if (isset($quests[$key])) {
|
|
return $quests[$key];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Every line with its quests, the current one with goal progress.
|
|
*
|
|
* @param list<string> $claimed
|
|
* @return list<array{line: string, done: int, total: int, quest: array|null}>
|
|
*/
|
|
public function progress(array $claimed, Facts $facts): array
|
|
{
|
|
$out = [];
|
|
foreach ($this->lines() as $line => $quests) {
|
|
$current = null;
|
|
foreach ($quests as $key => $quest) {
|
|
if (! in_array($key, $claimed, true)) {
|
|
$current = $key;
|
|
break;
|
|
}
|
|
}
|
|
$done = count(array_intersect(array_keys($quests), $claimed));
|
|
$out[] = [
|
|
'line' => $line,
|
|
'done' => $done,
|
|
'total' => count($quests),
|
|
'quest' => $current === null ? null : $this->present($current, $quests[$current], $facts),
|
|
];
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
/** Whether the quest is a line's current one and all its goals are met. */
|
|
public function claimable(string $key, array $claimed, Facts $facts): bool
|
|
{
|
|
foreach ($this->progress($claimed, $facts) as $line) {
|
|
if ($line['quest'] && $line['quest']['key'] === $key) {
|
|
return $line['quest']['complete'];
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/** How many lines have a completed quest waiting to be claimed. */
|
|
public function claimableCount(array $claimed, Facts $facts): int
|
|
{
|
|
return count(array_filter($this->progress($claimed, $facts), fn (array $line) => $line['quest']['complete'] ?? false));
|
|
}
|
|
|
|
private function present(string $key, array $quest, Facts $facts): array
|
|
{
|
|
$goals = array_map(fn (array $goal) => $this->goal($goal, $facts), $quest['goals']);
|
|
|
|
return [
|
|
'key' => $key,
|
|
'goals' => $goals,
|
|
'reward' => $quest['reward'],
|
|
'complete' => ! in_array(false, array_column($goals, 'met'), true),
|
|
];
|
|
}
|
|
|
|
/** @return array{kind: string, subject: string, have: int, need: int, met: bool, hint: string} */
|
|
private function goal(array $goal, Facts $facts): array
|
|
{
|
|
[$kind, $subject, $need] = $goal;
|
|
$have = match ($kind) {
|
|
'building' => $facts->levels[$subject] ?? 0,
|
|
'instances' => $facts->instances[$subject] ?? 0,
|
|
'counter' => $facts->counters[$subject] ?? 0,
|
|
};
|
|
|
|
return [
|
|
'kind' => $kind,
|
|
'subject' => $subject,
|
|
'have' => min($have, $need),
|
|
'need' => $need,
|
|
'met' => $have >= $need,
|
|
// Buildings point at their own screen; counters name theirs.
|
|
'hint' => $kind === 'counter' ? $goal[3] : $subject,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* What a level reached for the first time pays back: the share of what its order cost.
|
|
*
|
|
* @param array<string, int> $cost
|
|
* @return array<string, int>
|
|
*/
|
|
public function levelReward(array $cost): array
|
|
{
|
|
$share = (float) $this->config->get('quests.level_reward_share', 0);
|
|
|
|
return array_map(fn (int $n) => (int) round($n * $share), $cost);
|
|
}
|
|
}
|