66 lines
2.3 KiB
PHP
66 lines
2.3 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
namespace App\Actions;
|
|||
|
|
|
|||
|
|
use App\Game\GameRuleViolation;
|
|||
|
|
use App\Game\Map\Continent;
|
|||
|
|
use App\Models\Player;
|
|||
|
|
use App\Models\User;
|
|||
|
|
use App\Models\Village;
|
|||
|
|
use App\Models\World;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* The villages in a w × h window of a world's map, centred on (x, y) the way the
|
|||
|
|
* Die Stämme map is. Without a centre it opens on the player's first village there.
|
|||
|
|
*/
|
|||
|
|
final class ShowMap
|
|||
|
|
{
|
|||
|
|
public function __invoke(User $user, int $worldId, ?int $x, ?int $y, int $w, int $h): array
|
|||
|
|
{
|
|||
|
|
$world = World::find($worldId) ?? throw new GameRuleViolation('not_found', 'World not found.');
|
|||
|
|
$config = $world->gameConfig();
|
|||
|
|
$size = $config->get('world.map.size', 1000);
|
|||
|
|
$max = $config->get('world.map.max_view', 41);
|
|||
|
|
$w = max(1, min($max, $w));
|
|||
|
|
$h = max(1, min($max, $h));
|
|||
|
|
|
|||
|
|
if ($x === null || $y === null) {
|
|||
|
|
$home = Village::whereIn('player_id', Player::where('user_id', $user->id)->where('world_id', $world->id)->select('id'))
|
|||
|
|
->orderBy('id')->first();
|
|||
|
|
$x = $home->x ?? intdiv($size, 2);
|
|||
|
|
$y = $home->y ?? intdiv($size, 2);
|
|||
|
|
}
|
|||
|
|
$x = max(0, min($size - 1, $x));
|
|||
|
|
$y = max(0, min($size - 1, $y));
|
|||
|
|
$left = max(0, min($size - $w, $x - intdiv($w, 2)));
|
|||
|
|
$top = max(0, min($size - $h, $y - intdiv($h, 2)));
|
|||
|
|
|
|||
|
|
$villages = Village::where('world_id', $world->id)
|
|||
|
|
->whereBetween('x', [$left, $left + $w - 1])
|
|||
|
|
->whereBetween('y', [$top, $top + $h - 1])
|
|||
|
|
->with('player:id,user_id,name')
|
|||
|
|
->get(['id', 'player_id', 'name', 'x', 'y', 'points'])
|
|||
|
|
->map(fn (Village $v) => [
|
|||
|
|
'id' => $v->id,
|
|||
|
|
'x' => $v->x,
|
|||
|
|
'y' => $v->y,
|
|||
|
|
'name' => $v->name,
|
|||
|
|
'points' => $v->points,
|
|||
|
|
'continent' => Continent::of($v->x, $v->y),
|
|||
|
|
'player' => $v->player ? ['id' => $v->player->id, 'name' => $v->player->name] : null,
|
|||
|
|
'own' => $v->player?->user_id === $user->id,
|
|||
|
|
])->values()->all();
|
|||
|
|
|
|||
|
|
return [
|
|||
|
|
'world_id' => $world->id,
|
|||
|
|
'size' => $size,
|
|||
|
|
'center' => ['x' => $x, 'y' => $y],
|
|||
|
|
'x' => $left,
|
|||
|
|
'y' => $top,
|
|||
|
|
'w' => $w,
|
|||
|
|
'h' => $h,
|
|||
|
|
'villages' => $villages,
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
}
|