30 lines
986 B
PHP
30 lines
986 B
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Engine\Quests;
|
|
use App\Engine\Simulation;
|
|
use App\Game\GameRuleViolation;
|
|
use App\Models\User;
|
|
use App\Models\Village;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/** Give an own village a new name: 1 to 32 visible characters, spaces squeezed. */
|
|
final class RenameVillage extends VillageAction
|
|
{
|
|
public const MAX_LENGTH = 32;
|
|
|
|
public function __invoke(User $user, int $villageId, string $name): Village
|
|
{
|
|
$name = trim((string) preg_replace('/\s+/u', ' ', (string) preg_replace('/\p{C}/u', '', $name)));
|
|
if ($name === '' || mb_strlen($name) > self::MAX_LENGTH) {
|
|
throw new GameRuleViolation('name', 'A village name has 1 to '.self::MAX_LENGTH.' characters.');
|
|
}
|
|
|
|
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($name) {
|
|
$village->name = $name;
|
|
Quests::bump($village->player_id, 'village.renamed');
|
|
});
|
|
}
|
|
}
|