| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- declare(strict_types=1);
- namespace App\Support;
- use DateTimeImmutable;
- use DateTimeZone;
- /** Framework independent; keep the member API copy identical except for its namespace. */
- final class MaintenancePolicy
- {
- public const CATEGORIES = ['third:1' => '真人', 'third:2' => '电子', 'third:3' => '彩票',
- 'third:4' => '体育', 'third:5' => '电竞', 'third:6' => '捕鱼', 'third:7' => '棋牌',
- 'self:sport' => 'F1体育', 'self:lottery' => 'F1彩票'];
- /** Seed for the sport_list config; the admin list is the source of truth afterwards. */
- public const SPORTS = ['football' => '足球', 'basketball' => '篮球', 'tabletennis' => '乒乓球',
- 'volleyball' => '排球', 'cricket' => '板球', 'tennis' => '网球'];
- public const LANGUAGES = ['zh' => '中文', 'en' => '英文', 'vi' => '越南语'];
- public const STATES = ['disabled' => '已关闭', 'scheduled' => '待开始', 'active' => '维护中', 'expired' => '已结束'];
- public const SITE_CODE = 11001;
- public const GAME_CODE = 11002;
- public static function scopes(): array { return ['site' => '整站'] + self::CATEGORIES; }
- /**
- * Subcategory of one game_list entry. List `type` is only a display id; the code follows the game
- * and its params.type, which are what the bet and lobby endpoints receive.
- */
- public static function lotterySubcategory(array $item): ?string
- {
- $game = strtolower(trim((string) ($item['game'] ?? '')));
- $type = $item['params']['type'] ?? null;
- $type = is_numeric($type) && (int) $type == $type && (int) $type > 0 && (int) $type < 1000 ? (int) $type : null;
- if ($game === 'canada28') return 'self:lottery:pc28';
- if ($game === 'jisu28') return 'self:lottery:jisu28';
- if (in_array($game, ['lhc', 'jisu'], true) && $type !== null) return 'self:lottery:' . $game . ':' . $type;
- return null;
- }
- /** Decodes a game_list / sport_list config value; malformed values read as an empty list. */
- public static function configList($json): array
- {
- $list = is_string($json) ? json_decode($json, true) : null;
- return is_array($list) ? array_values(array_filter($list, 'is_array')) : [];
- }
- public static function sportSubcategory(string $code): ?string
- {
- $code = strtolower(trim($code));
- return preg_match('/^[a-z0-9]{1,32}$/', $code) ? 'self:sport:' . $code : null;
- }
- public static function thirdSubcategory(int $gameType, string $platType): ?string
- {
- $category = 'third:' . $gameType;
- if (!isset(self::CATEGORIES[$category])) return null;
- $plat = strtolower(trim($platType));
- if ($plat === '' || strlen($plat) > 32 || !preg_match('/^[a-z0-9]+$/', $plat)) return null;
- return $category . ':' . $plat;
- }
- /** Parent category of a platform, lottery game or sport; null for site and category scopes. */
- public static function parentScope(string $code): ?string
- {
- if (preg_match('/^(third:[1-7]):([a-z0-9]{1,32})$/', $code, $match) && isset(self::CATEGORIES[$match[1]])) return $match[1];
- if (preg_match('/^self:lottery:(pc28|jisu28|(lhc|jisu):[1-9][0-9]{0,2})$/', $code)) return 'self:lottery';
- if (preg_match('/^self:sport:[a-z0-9]{1,32}$/', $code)) return 'self:sport';
- return null;
- }
- public static function platform($value): ?string
- {
- return is_string($value) && in_array(strtoupper(trim($value)), ['PC', 'H5', 'APP'], true)
- ? strtoupper(trim($value)) : null;
- }
- public static function language($value): ?string
- {
- if (!is_string($value)) return null;
- $value = strtolower(str_replace('_', '-', trim($value)));
- $value = explode('-', $value)[0];
- return isset(self::LANGUAGES[$value]) ? $value : null;
- }
- public static function state(array $row, int $now): string
- {
- if (!(int) $row['enabled']) return 'disabled';
- if (self::timestamp($row['starts_at']) > $now) return 'scheduled';
- if ($row['ends_at'] !== null && self::timestamp($row['ends_at']) <= $now) return 'expired';
- return 'active';
- }
- public static function platforms(array $row): array
- {
- return is_array($row['platforms']) ? $row['platforms'] : json_decode($row['platforms'], true, 512, JSON_THROW_ON_ERROR);
- }
- public static function matches(array $row, ?string $platform, int $now): bool
- {
- // Unknown clients conservatively match every platform; omission cannot bypass a rule.
- return self::state($row, $now) === 'active' && ($platform === null || in_array($platform, self::platforms($row), true));
- }
- public static function select(array $rows, ?string $platform, ?string $category, int $now, ?string $subcategory = null): ?array
- {
- $game = null; $child = null;
- foreach ($rows as $row) {
- if (!self::matches($row, $platform, $now)) continue;
- if ($row['scope_code'] === 'site') return $row;
- if ($row['scope_code'] === $category) $game = $row;
- if ($subcategory !== null && $row['scope_code'] === $subcategory && self::parentScope($subcategory) === $category) $child = $row;
- }
- return $child ?? $game;
- }
- public static function publicRule(array $row, string $language): array
- {
- $zone = new DateTimeZone($row['timezone']);
- $start = new DateTimeImmutable($row['starts_at'], new DateTimeZone('UTC'));
- $end = $row['ends_at'] === null ? null : new DateTimeImmutable($row['ends_at'], new DateTimeZone('UTC'));
- $fallback = ['zh' => '系统维护中,请稍后再试', 'en' => 'Maintenance in progress. Please try again later.',
- 'vi' => 'Hệ thống đang bảo trì. Vui lòng thử lại sau.'];
- $content = (string) ($row['content'] ?? '');
- $parent = self::parentScope($row['scope_code']);
- return ['scope' => $row['scope_code'] === 'site' ? 'site' : 'game',
- 'category' => $row['scope_code'] === 'site' ? null : ($parent ?? $row['scope_code']),
- 'subcategory' => $parent === null ? null : $row['scope_code'],
- 'platforms' => self::platforms($row), 'timezone' => $row['timezone'],
- 'starts_at' => $start->setTimezone($zone)->format('Y-m-d H:i:s'),
- 'ends_at' => $end ? $end->setTimezone($zone)->format('Y-m-d H:i:s') : null,
- '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,
- 'language' => $language, 'content' => trim($content) === '' ? $fallback[$language] : $content];
- }
- public static function failure(array $row, ?string $platform, string $language, int $now): array
- {
- $data = self::publicRule($row, $language) + ['platform' => $platform, 'platform_unknown' => $platform === null,
- 'server_time' => gmdate('Y-m-d\TH:i:s\Z', $now)];
- return ['code' => $row['scope_code'] === 'site' ? self::SITE_CODE : self::GAME_CODE, 'msg' => $data['content'], 'data' => $data];
- }
- private static function timestamp(string $value): int { return (new DateTimeImmutable($value, new DateTimeZone('UTC')))->getTimestamp(); }
- }
|