MaintenancePolicy.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Support;
  4. use DateTimeImmutable;
  5. use DateTimeZone;
  6. /** Framework independent; keep the member API copy identical except for its namespace. */
  7. final class MaintenancePolicy
  8. {
  9. public const CATEGORIES = ['third:1' => '真人', 'third:2' => '电子', 'third:3' => '彩票',
  10. 'third:4' => '体育', 'third:5' => '电竞', 'third:6' => '捕鱼', 'third:7' => '棋牌',
  11. 'self:sport' => 'F1体育', 'self:lottery' => 'F1彩票'];
  12. public const LANGUAGES = ['zh' => '中文', 'en' => '英文', 'vi' => '越南语'];
  13. public const STATES = ['disabled' => '已关闭', 'scheduled' => '待开始', 'active' => '维护中', 'expired' => '已结束'];
  14. public const SITE_CODE = 11001;
  15. public const GAME_CODE = 11002;
  16. public static function scopes(): array { return ['site' => '整站'] + self::CATEGORIES; }
  17. public static function platform($value): ?string
  18. {
  19. return is_string($value) && in_array(strtoupper(trim($value)), ['PC', 'H5', 'APP'], true)
  20. ? strtoupper(trim($value)) : null;
  21. }
  22. public static function language($value): ?string
  23. {
  24. if (!is_string($value)) return null;
  25. $value = strtolower(str_replace('_', '-', trim($value)));
  26. $value = explode('-', $value)[0];
  27. return isset(self::LANGUAGES[$value]) ? $value : null;
  28. }
  29. public static function state(array $row, int $now): string
  30. {
  31. if (!(int) $row['enabled']) return 'disabled';
  32. if (self::timestamp($row['starts_at']) > $now) return 'scheduled';
  33. if ($row['ends_at'] !== null && self::timestamp($row['ends_at']) <= $now) return 'expired';
  34. return 'active';
  35. }
  36. public static function platforms(array $row): array
  37. {
  38. return is_array($row['platforms']) ? $row['platforms'] : json_decode($row['platforms'], true, 512, JSON_THROW_ON_ERROR);
  39. }
  40. public static function matches(array $row, ?string $platform, int $now): bool
  41. {
  42. // Unknown clients conservatively match every platform; omission cannot bypass a rule.
  43. return self::state($row, $now) === 'active' && ($platform === null || in_array($platform, self::platforms($row), true));
  44. }
  45. public static function select(array $rows, ?string $platform, ?string $category, int $now): ?array
  46. {
  47. $game = null;
  48. foreach ($rows as $row) {
  49. if (!self::matches($row, $platform, $now)) continue;
  50. if ($row['scope_code'] === 'site') return $row;
  51. if ($row['scope_code'] === $category) $game = $row;
  52. }
  53. return $game;
  54. }
  55. public static function publicRule(array $row, string $language): array
  56. {
  57. $zone = new DateTimeZone($row['timezone']);
  58. $start = new DateTimeImmutable($row['starts_at'], new DateTimeZone('UTC'));
  59. $end = $row['ends_at'] === null ? null : new DateTimeImmutable($row['ends_at'], new DateTimeZone('UTC'));
  60. $fallback = ['zh' => '系统维护中,请稍后再试', 'en' => 'Maintenance in progress. Please try again later.',
  61. 'vi' => 'Hệ thống đang bảo trì. Vui lòng thử lại sau.'];
  62. $content = (string) ($row['content'] ?? '');
  63. return ['scope' => $row['scope_code'] === 'site' ? 'site' : 'game',
  64. 'category' => $row['scope_code'] === 'site' ? null : $row['scope_code'],
  65. 'platforms' => self::platforms($row), 'timezone' => $row['timezone'],
  66. 'starts_at' => $start->setTimezone($zone)->format('Y-m-d H:i:s'),
  67. 'ends_at' => $end ? $end->setTimezone($zone)->format('Y-m-d H:i:s') : null,
  68. 'starts_at_utc' => $start->format('Y-m-d\TH:i:s\Z'), 'ends_at_utc' => $end ? $end->format('Y-m-d\TH:i:s\Z') : null,
  69. 'language' => $language, 'content' => trim($content) === '' ? $fallback[$language] : $content];
  70. }
  71. public static function failure(array $row, ?string $platform, string $language, int $now): array
  72. {
  73. $data = self::publicRule($row, $language) + ['platform' => $platform, 'platform_unknown' => $platform === null,
  74. 'server_time' => gmdate('Y-m-d\TH:i:s\Z', $now)];
  75. return ['code' => $row['scope_code'] === 'site' ? self::SITE_CODE : self::GAME_CODE, 'msg' => $data['content'], 'data' => $data];
  76. }
  77. private static function timestamp(string $value): int { return (new DateTimeImmutable($value, new DateTimeZone('UTC')))->getTimestamp(); }
  78. }