Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add admin panel #168

Merged
merged 6 commits into from
May 18, 2024
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ OGameX is an open-source OGame clone aiming to recreate the official OGame exper
- [7. Installation](#installation)
- [a) Development: Install OGameX using Docker](#development)
- [b) Production: Install OGameX using Docker](#production)
- [c) Tips for initial setup](#tips-for-initial-setup)
- [8. Support](#support)
- [9. License](#license)

Expand Down Expand Up @@ -158,6 +159,15 @@ You should review all settings before deploying this project to a publicly acces

After completing the setup, visit http://localhost to access OGameX. You first need to create an account (no email validation), afterwards you can login using that account.

### <a name="tips-for-initial-setup"></a> c) Tips for initial setup
- **Admin account**: By default, the first registered user is assigned the admin role which can see the admin bar and is able to change server settings. You can also assign the admin role manually via the command line:
```
$ php artisan ogamex:assign-admin-role {username}
```
To remove the admin role from a user, use the following command:
```
$ php artisan ogamex:remove-admin-role {username}
```

## <a name="support"></a> 📞 Support

Expand Down
11 changes: 10 additions & 1 deletion app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,20 @@ public function create(array $input): User
'password' => $this->passwordRules(),
])->validate();

return User::create([
$user = User::create([
'lang' => 'en',
'username' => $this->generateUniqueName(),
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);

// Check if the user is the first registered user
if (User::count() === 1) {
$user->assignRole('admin');
$user->username = 'Admin';
$user->save();
}

return $user;
}
}
49 changes: 49 additions & 0 deletions app/Console/Commands/AssignAdminRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace OGame\Console\Commands;

use Illuminate\Console\Command;
use OGame\Models\User;

class AssignAdminRole extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ogamex:assign-admin-role {username}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Assign the admin role to a specified username';

/**
* Execute the console command.
* @return int
*/
public function handle(): int
{
$username = $this->argument('username');

$user = User::where('username', $username)->first();

if (!$user) {
$this->error('User not found.');
return 1;
}

// Assuming you are using the Spatie Permission package
if (!$user->hasRole('admin')) {
$user->assignRole('admin');
$this->info('Admin role assigned to user ' . $username);
} else {
$this->info('User ' . $username . ' already has the admin role.');
}

return 0;
}
}
49 changes: 49 additions & 0 deletions app/Console/Commands/RemoveAdminRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace OGame\Console\Commands;

use Illuminate\Console\Command;
use OGame\Models\User;

class RemoveAdminRole extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ogamex:remove-admin-role {username}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove the admin role from a specified username';

/**
* Execute the console command.
* @return int
*/
public function handle(): int
{
$username = $this->argument('username');

$user = User::where('username', $username)->first();

if (!$user) {
$this->error('User not found.');
return 1;
}

// Assuming you are using the Spatie Permission package
if ($user->hasRole('admin')) {
$user->removeRole('admin');
$this->info('Admin role removed from user ' . $username);
} else {
$this->info('User ' . $username . ' does not have the admin role.');
}

return 0;
}
}
151 changes: 151 additions & 0 deletions app/Facades/AppUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,155 @@ protected static function getFacadeAccessor(): string
{
return 'appUtil';
}

/**
* Format a number without rounding it down. Show the full number.
*
* @param int|float $number
* @return string
*/
public static function formatNumber(int|float $number): string
{
// If number is less than 1,000, just return it with no formatting
return number_format($number, 0, ',', ',');
}

/**
* Format a number with K for thousands and Mn for millions, with one decimal place for thousands
* when necessary, and no decimal places for millions.
*
* @param int|float $number
* @return string
*/
public static function formatNumberShort(int|float $number): string
{
if ($number >= 1000000) {
// If number is 1,000,000 or higher, format as millions (Mn)
return number_format($number / 1000000, 0) . 'Mn';
} elseif ($number > 10000) {
// If number is greater than 10,000 but less than 1,000,000, round down and format as thousands (K) with no decimal places
return number_format(floor($number / 1000), 0) . 'K';
} elseif ($number > 1000) {
// If number is greater than 1,000 but less than or equal to 10,000, format as thousands (K) with up to 1 decimal place
// Avoid rounding up by using floor function after multiplying by 10 and then dividing by 10
$thousands = floor(($number / 1000) * 10) / 10;
$decimalPlaces = ($number % 1000) == 0 ? 0 : 1; // Use 1 decimal place unless it's an exact thousand
return number_format($thousands, $decimalPlaces, ',', '') . 'K';
} else {
// If number is less than 1,000, just return it with no formatting
return number_format($number, 0, ',', ',');
}
}

/**
* Format a number above 1 million as "1.000Mn" with three decimal places, and
* format numbers between 1,000 and 1 million with commas as thousands separator.
*
* @param int|float $number
* @return string
*/
public static function formatNumberLong(int|float $number): string
{
if ($number >= 1000000) {
// If number is 1,000,000 or higher, format as "1.000Mn" with three decimal places

// Divide by 1,000,000 and format with up to 3 decimal places
$formattedNumber = number_format($number / 1000000, 3, '.', '');

// If the number is a whole million, no decimals are needed
if (floor($number / 1000000) == $number / 1000000) {
return floor($number / 1000000) . 'Mn';
}

// Remove trailing zeros and the decimal point if it becomes unnecessary
$formattedNumber = rtrim($formattedNumber, '0'); // Remove trailing zeros
$formattedNumber = rtrim($formattedNumber, '.'); // Remove trailing decimal point if no decimal numbers are left

return $formattedNumber . 'Mn';
} elseif ($number >= 1000) {
// If number is 1,000 or higher but less than 1,000,000, format with commas as thousands separator
return number_format($number, 0, '.', ',');
} else {
// If number is less than 1,000, just return it with no formatting
return number_format($number, 0, '.', ',');
}
}

/**
* Helper method to convert building/research time from seconds to human
* readable format, including weeks and days if applicable.
*
* @param int|float $seconds
* @return string
*/
public static function formatTimeDuration(int|float $seconds): string
{
$weeks = floor($seconds / 604800); // 60*60*24*7
$days = floor(($seconds % 604800) / 86400); // Remaining seconds divided by number of seconds in a day
$hours = floor(($seconds % 86400) / 3600); // Remaining seconds divided by number of seconds in an hour
$minutes = floor(($seconds / 60) % 60);
$seconds = $seconds % 60;

$formatted_string = '';
if ($weeks > 0) {
$formatted_string .= $weeks . 'w ';
}

if ($days > 0) {
$formatted_string .= $days . 'd ';
}

if ($hours > 0) {
$formatted_string .= $hours . 'h ';
}

if ($minutes > 0) {
$formatted_string .= $minutes . 'm ';
}

if (empty($formatted_string)) {
$formatted_string .= $seconds . 's';
}

return trim($formatted_string);
}

/**
* Helper method to convert building/research time from seconds to <time datetime=""> format.
*
* @param int|float $seconds
* @return string
*/
public static function formatDateTimeDuration(int|float $seconds): string
{
// TODO: add unittest for this and check what is the expected output for hours/days/weeks.
$weeks = floor($seconds / 604800); // 60*60*24*7
$days = floor(($seconds % 604800) / 86400); // Remaining seconds divided by number of seconds in a day
$hours = floor(($seconds % 86400) / 3600); // Remaining seconds divided by number of seconds in an hour
$minutes = floor(($seconds / 60) % 60);
$seconds = $seconds % 60;

$formatted_string = '';
if ($weeks > 0) {
$formatted_string .= $weeks . 'W';
}

if ($days > 0) {
$formatted_string .= $days . 'D';
}

if ($hours > 0) {
$formatted_string .= $hours . 'H';
}

if ($minutes > 0) {
$formatted_string .= $minutes . 'M';
}

if ($seconds > 0) {
$formatted_string .= $seconds . 'S';
}

return 'PT' . trim($formatted_string);
}
}
10 changes: 8 additions & 2 deletions app/Utils/GitInfoUtil.php → app/Facades/GitInfoUtil.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace OGame\Utils;
namespace OGame\Facades;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Facade;

/**
* Class GitInfoUtil.
Expand All @@ -11,8 +12,13 @@
*
* @package OGame\Utils
*/
class GitInfoUtil
class GitInfoUtil extends Facade
{
protected static function getFacadeAccessor(): string
{
return 'gitInfoUtil';
}

/**
* Get the current branch.
*
Expand Down
Loading
Loading