Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
29 changes: 27 additions & 2 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace OGame\Actions\Fortify;

use Exception;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use OGame\Factories\PlanetServiceFactory;
use OGame\Factories\PlayerServiceFactory;
use OGame\Http\Middleware\Locale;
use OGame\Models\User;
use OGame\Models\UserTech;
use OGame\Services\MessageService;
Expand Down Expand Up @@ -156,12 +158,20 @@ public function create(array $input): User
'password' => $this->passwordRules(),
])->validate();

// Resolve the current locale so the new user inherits the language they
// registered in (dropdown / URL / session) instead of a hardcoded 'en'.
// Falls back to 'en' if the detected locale is not one we officially support.
$currentLocale = App::getLocale();
if (!in_array($currentLocale, Locale::SUPPORTED_LOCALES, true)) {
$currentLocale = 'en';
}

// Add try/catch to retry creating user 5 times because exception could be triggered
// if the username is already taken.
for ($attempt = 0; $attempt < 5; $attempt++) {
try {
$user = User::create([
'lang' => 'en',
'lang' => $currentLocale,
'username' => $this->generateUniqueName(),
'email' => $input['email'],
'password' => Hash::make($input['password']),
Expand Down Expand Up @@ -204,7 +214,22 @@ private function createInitialGameDataForUser($user): void

// Create initial planet(s) for the player.
$playerService = $this->playerServiceFactory->make($user->id);
$planetNames = ['Homeworld', 'Colony'];

// Translate the default planet names into the user's registration locale
// so the values saved to planets.name are already localized (and therefore
// also get retranslated later by LanguageController when the user changes lang).
$userLocale = $user->lang ?: 'en';
$homeworldName = (string) trans('t_ingame.overview.homeworld', [], $userLocale);
$colonyName = (string) trans('t_ingame.overview.colony', [], $userLocale);
// Guard against missing translation (trans() returns the key when not found).
if ($homeworldName === 't_ingame.overview.homeworld') {
$homeworldName = 'Homeworld';
}
if ($colonyName === 't_ingame.overview.colony') {
$colonyName = 'Colony';
}

$planetNames = [$homeworldName, $colonyName];
// The amount of planets to create is defined in the settings and defaults to 1.
for ($i = 0; $i < $this->settings->registrationPlanetAmount(); $i++) {
$this->planetServiceFactory->createInitialPlanetForPlayer($playerService, $planetNames[$i === 0 ? 0 : 1]);
Expand Down
Loading
Loading