ldc-legaulois/app/Enums/Games/GamePointLevel.php

117 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2024-07-04 16:34:10 +02:00
<?php
namespace App\Enums\Games;
use App\Traits\Enums\BaseEnum;
use Illuminate\Support\Facades\Vite;
enum GamePointLevel: int
{
use BaseEnum;
2024-07-30 16:05:27 +02:00
case egg = 1;
case hatchedEgg = 2;
case chick = 3;
case hen = 4;
case chicken = 5;
2024-07-04 16:34:10 +02:00
/**
* Optionnal labels definition.
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
2024-07-30 16:05:27 +02:00
self::egg->name => 'oeuf',
2024-07-04 16:34:10 +02:00
self::hatchedEgg->name => 'oeuf éclos',
2024-07-30 16:05:27 +02:00
self::chick->name => 'poussin',
self::hen->name => 'poule',
self::chicken->name => 'poulet',
2024-07-04 16:34:10 +02:00
];
/**
* Point levels definition.
*/
private const LEVELS = [
2024-07-30 16:05:27 +02:00
self::egg->name => 0,
2024-07-04 16:34:10 +02:00
self::hatchedEgg->name => 50,
2024-07-30 16:05:27 +02:00
self::chick->name => 130,
self::hen->name => 230,
self::chicken->name => 1000,
2024-07-04 16:34:10 +02:00
];
/**
* Get Level
*/
public function level(): int
{
return self::LEVELS[$this->name];
}
/**
* Get picture
*/
public function picture(): string
{
return Vite::asset("resources/assets/images/club/game-points/{$this->name()}.png");
}
/**
* Get enum Levels
*
2026-09-25 16:25:31 +02:00
* @return array<int>
2024-07-04 16:34:10 +02:00
*/
public static function toLevels(): array
{
return self::LEVELS;
}
/**
* Get Level from point value
*/
public static function fromPoints(int $points): self
{
$levels = array_reverse(self::all());
foreach ($levels as $level) {
if ($points >= $level->level()) {
return $level;
}
}
2026-09-25 16:25:31 +02:00
2024-07-04 16:34:10 +02:00
// * This should not happen while egg level is 0
return self::egg;
}
/**
* Next Level
*/
2026-09-25 16:25:31 +02:00
public static function fromPointsNextLevel(int $points): ?self
2024-07-04 16:34:10 +02:00
{
$currentLevel = self::fromPoints($points);
2026-09-25 16:25:31 +02:00
2024-07-04 16:34:10 +02:00
return collect(self::all())
->filter(fn ($level) => $level->level() > $currentLevel->level())
->first();
}
/**
* Percent from points
*/
public static function percentFromPoints(int $points): float
{
$levels = collect(self::toValues());
$level = self::fromPoints($points);
$nextLevel = self::fromPointsNextLevel($points);
2026-09-25 16:25:31 +02:00
if (! $nextLevel) {
2024-07-04 16:34:10 +02:00
return 100;
}
// * Bars between 2 levels
$ratio = 4;
2026-09-25 16:25:31 +02:00
$percentBetween2Levels = 100 / ($levels->count() - 1);
$adjustment = $percentBetween2Levels * ($nextLevel->level() - $points) / ($nextLevel->level() - $level->level());
2024-07-04 16:34:10 +02:00
return round((($level->value() * 100) / $levels->last()) + $percentBetween2Levels - $adjustment);
}
}