ldc-legaulois/app/Lib/Helpers/GamePeriodHelper.php

171 lines
4.8 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Lib\Helpers;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
2026-05-13 15:02:14 +02:00
/**
* Helper class for managing game periods and states.
*
* This class provides utility methods to determine the current state
* of the game based on configured date periods. It handles the following periods:
* - Start game : between game start and vote start
* - Vote : between vote start and vote end
* - End participation : between end participation start and end
* - End game : between end game start and end
* - Closed : outside all periods or invalid config
*
* Dates are retrieved from the 'goldencrush' config file.
*/
class GamePeriodHelper
{
2026-05-13 15:02:14 +02:00
/**
* Returns the game start date.
*/
public static function startGameAt(): Carbon
{
return Carbon::parse(config('goldencrush.start_game_at'))->startOfDay();
}
2026-05-13 15:02:14 +02:00
/**
* Returns the vote start date.
*/
2026-05-05 18:16:24 +02:00
public static function voteStartAt(): Carbon
{
return Carbon::parse(config('goldencrush.vote_start_at'))->startOfDay();
}
2026-05-13 15:02:14 +02:00
/**
* Returns the vote end date.
*/
2026-05-05 18:16:24 +02:00
public static function voteEndAt(): Carbon
{
return Carbon::parse(config('goldencrush.vote_end_at'))->endOfDay();
}
2026-05-13 15:02:14 +02:00
/**
* Returns the start date of the end participation period.
* (day after the vote end date)
*/
2026-05-05 18:16:24 +02:00
public static function endParticipationStartAt(): Carbon
{
return self::voteEndAt()->copy()->addDay()->startOfDay();
}
2026-05-13 15:02:14 +02:00
/**
* Returns the end date of the end participation period.
*/
2026-05-05 18:16:24 +02:00
public static function endParticipationEndAt(): Carbon
{
return Carbon::parse(config('goldencrush.end_participation_end_at'))->endOfDay();
}
2026-05-13 15:02:14 +02:00
/**
* Returns the start date of the end game period.
* (day after the end participation end date)
*/
2026-05-05 18:16:24 +02:00
public static function endGameStartAt(): Carbon
{
return self::endParticipationEndAt()->copy()->addDay()->startOfDay();
}
2026-05-13 15:02:14 +02:00
/**
* Returns the end date of the game.
*/
2026-05-05 18:16:24 +02:00
public static function endGameEndAt(): Carbon
{
return Carbon::parse(config('goldencrush.end_game_end_at'))->endOfDay();
}
2026-05-13 15:02:14 +02:00
/**
* Checks if the configured periods are valid.
* Validates that the vote start date is before or equal to the vote end date
* and the end game end date.
*/
2026-05-05 18:16:24 +02:00
public static function hasValidPeriods(): bool
{
return self::voteStartAt()->lte(self::voteEndAt())
&& self::voteStartAt()->lte(self::endGameEndAt());
}
2026-05-13 15:02:14 +02:00
/**
* Returns the current game state based on today's date.
*
* Possible states:
* - 'start_game' : between game start and vote start
* - 'vote' : between vote start and vote end
* - 'end_participation' : between end participation start and end
* - 'end_game' : between end game start and end
* - 'closed' : outside all periods or invalid config
*/
2026-05-05 18:16:24 +02:00
public static function state(): string
{
if (! self::hasValidPeriods()) {
Log::error('Config des dates invalide ! ');
2026-09-25 16:25:31 +02:00
2026-05-05 18:16:24 +02:00
return 'closed';
}
$today = now();
if ($today->betweenIncluded(self::startGameAt(), self::voteStartAt())) {
return 'start_game';
}
2026-05-05 18:16:24 +02:00
if ($today->betweenIncluded(self::voteStartAt(), self::voteEndAt())) {
return 'vote';
}
if ($today->betweenIncluded(self::endParticipationStartAt(), self::endParticipationEndAt())) {
return 'end_participation';
}
2026-05-05 18:16:24 +02:00
if ($today->betweenIncluded(self::endGameStartAt(), self::endGameEndAt())) {
return 'end_game';
}
return 'closed';
}
2026-05-13 15:02:14 +02:00
/**
* Returns the route name corresponding to the current game state.
*/
2026-05-05 18:16:24 +02:00
public static function routeName(): string
{
return match (self::state()) {
2026-09-25 16:25:31 +02:00
'vote' => 'club.golden-crush',
2026-05-05 18:16:24 +02:00
'end_participation' => 'club.golden-crush.end-of-participation',
2026-09-25 16:25:31 +02:00
'end_game' => 'club.golden-crush.end-of-game',
default => 'club.home.show',
2026-05-05 18:16:24 +02:00
};
}
2026-05-13 15:02:14 +02:00
/**
* Checks if today is within the active game period.
* (between vote start and end game end)
*/
2026-05-05 18:16:24 +02:00
public static function isGamePeriod(): bool
{
return now()->between(
2026-05-05 18:16:24 +02:00
self::voteStartAt(),
self::endGameEndAt()
);
}
2026-05-13 15:02:14 +02:00
/**
* Checks if the game is currently visible to users.
* Returns true if the state is 'start_game', 'vote', 'end_participation' or 'end_game'.
*/
2026-05-05 18:16:24 +02:00
public static function isGameVisible(): bool
2026-04-29 17:59:19 +02:00
{
2026-05-05 18:16:24 +02:00
return in_array(self::state(), [
'start_game',
2026-05-05 18:16:24 +02:00
'vote',
'end_participation',
'end_game',
]);
2026-04-29 17:59:19 +02:00
}
}