67 lines
2.4 KiB
PHP
67 lines
2.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Actions;
|
||
|
|
|
||
|
|
use App\Engine\Quests;
|
||
|
|
use App\Game\Buildings\BuildingRules;
|
||
|
|
use App\Game\GameRuleViolation;
|
||
|
|
use App\Game\Quests\QuestBook;
|
||
|
|
use App\Models\LevelReward;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Village;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The `quest.index` payload: every line with its current quest and goal progress, the
|
||
|
|
* level rewards waiting to be collected, and what the next level of each building in
|
||
|
|
* this village would pay back.
|
||
|
|
*/
|
||
|
|
final class ShowQuests
|
||
|
|
{
|
||
|
|
public function __invoke(User $user, int $villageId): array
|
||
|
|
{
|
||
|
|
$village = Village::with(['world', 'player', 'buildings'])->find($villageId);
|
||
|
|
if (! $village || $village->player?->user_id !== $user->id) {
|
||
|
|
throw new GameRuleViolation('not_found', 'Village not found.');
|
||
|
|
}
|
||
|
|
$config = $village->world->gameConfig();
|
||
|
|
$book = new QuestBook($config);
|
||
|
|
$rules = new BuildingRules($config);
|
||
|
|
$playerId = $village->player_id;
|
||
|
|
$claimed = Quests::claimed($playerId);
|
||
|
|
$facts = Quests::facts($playerId);
|
||
|
|
|
||
|
|
$lines = array_map(fn (array $line) => [
|
||
|
|
...$line,
|
||
|
|
'quests' => array_map(fn (string $key) => [
|
||
|
|
'key' => $key,
|
||
|
|
'claimed' => in_array($key, $claimed, true),
|
||
|
|
], array_keys($book->lines()[$line['line']])),
|
||
|
|
], $book->progress($claimed, $facts));
|
||
|
|
|
||
|
|
$rewards = LevelReward::with('village:id,name')->where('player_id', $playerId)->whereNull('claimed_at')
|
||
|
|
->orderBy('reached_at')->orderBy('id')->get();
|
||
|
|
|
||
|
|
$upcoming = [];
|
||
|
|
foreach ($village->buildings->pluck('type')->unique()->sort()->values() as $type) {
|
||
|
|
$level = ($facts->levels[$type] ?? 0) + 1;
|
||
|
|
if ($level > $rules->maxLevel($type)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$upcoming[] = ['type' => $type, 'level' => $level, 'reward' => $book->levelReward($rules->cost($type, $level, 0))];
|
||
|
|
}
|
||
|
|
|
||
|
|
return [
|
||
|
|
'lines' => $lines,
|
||
|
|
'rewards' => $rewards->map(fn (LevelReward $r) => [
|
||
|
|
'id' => $r->id,
|
||
|
|
'type' => $r->type,
|
||
|
|
'level' => $r->level,
|
||
|
|
'reward' => $r->reward,
|
||
|
|
'village' => ['id' => $r->village_id, 'name' => $r->village?->name],
|
||
|
|
])->values()->all(),
|
||
|
|
'upcoming' => $upcoming,
|
||
|
|
'share' => (float) $config->get('quests.level_reward_share', 0),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|