ensureOpen(); if (! in_array($direction, Frontier::DIRECTIONS, true)) { throw new GameRuleViolation('direction', "Unknown direction [$direction]."); } return DB::transaction(function () use ($user, $world, $direction) { $player = Player::firstOrCreate(['user_id' => $user->id, 'world_id' => $world->id], ['name' => $user->name]); if ($village = $player->villages()->orderBy('id')->first()) { return $village; } $taken = VillageFactory::takenSpots($world); [$x, $y] = VillageFactory::spawnSpot($world, $taken, $direction); // A first start and a restart after losing every village both get beginner protection. $config = $world->gameConfig(); $player->update(['protected_since' => now(), 'last_active_at' => now()]); $levels = array_fill_keys($config->get('world.village.start_buildings'), 1); $village = $this->villages->create($world, $player, "{$user->name}'s village", $x, $y, $levels); $this->barbarians->around($world, $village, $taken); Raiders::of($village)->ensurePlanned(now()->toImmutable()); return $village; }); } }