65 lines
1.5 KiB
PHP
65 lines
1.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Form to create your first character.
|
|
*/
|
|
function char_controller_create_first_get(): void
|
|
{
|
|
auth_only();
|
|
|
|
// If the user already has a character, redirect them to the main page.
|
|
if (char_count(user('id')) > 0) redirect('/');
|
|
|
|
echo render('layouts/basic', ['view' => 'pages/chars/first']);
|
|
}
|
|
|
|
/**
|
|
* Create a character for the currently logged in user.
|
|
*/
|
|
function char_controller_create_post(): void
|
|
{
|
|
auth_only();
|
|
csrf_ensure();
|
|
|
|
$errors = [];
|
|
|
|
$name = $_POST['name'] ?? '';
|
|
|
|
// Trim the input.
|
|
$name = trim($name);
|
|
|
|
/*
|
|
A name is required.
|
|
A name must be between 3 and 18 characters.
|
|
A name must contain only alphanumeric characters and spaces.
|
|
*/
|
|
if (empty($name) || strlen($name) < 3 || strlen($name) > 18 || !ctype_alnum(str_replace(' ', '', $name))) {
|
|
$errors['name'][] = 'Name is required and must be between 3 and 18 characters long and contain only alphanumeric characters and spaces.';
|
|
}
|
|
|
|
/*
|
|
A character's name must be unique.
|
|
*/
|
|
if (char_name_exists($name)) $errors['name'][] = 'Name is already taken.';
|
|
|
|
// If there are errors at this point, send them to the page with errors flashed.
|
|
if (!empty($errors)) {
|
|
flash('errors', $errors);
|
|
redirect('/');
|
|
}
|
|
|
|
// Create the character
|
|
$char = char_create(user('id'), $name);
|
|
if ($char === false) router_error(400);
|
|
|
|
// Create the auxiliary tables
|
|
char_location_create($char);
|
|
char_wallet_create($char);
|
|
char_gear_create($char);
|
|
|
|
// Set the character as the user's selected character
|
|
change_user_character($char);
|
|
|
|
redirect('/');
|
|
}
|