100 lines
2.8 KiB
PHP
100 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace app\common\service\match;
|
|
|
|
use app\common\model\match\MatchEvent;
|
|
use app\common\model\match\MatchLive;
|
|
|
|
class MatchLiveService
|
|
{
|
|
public static function syncLegacyLiveUrl(int $matchId): void
|
|
{
|
|
$row = MatchLive::where('match_id', $matchId)
|
|
->whereNull('delete_time')
|
|
->order('create_time', 'asc')
|
|
->order('id', 'asc')
|
|
->find();
|
|
|
|
MatchEvent::where('id', $matchId)->update([
|
|
'live_url' => $row ? (string) $row->source_url : '',
|
|
'update_time' => time(),
|
|
]);
|
|
}
|
|
|
|
public static function getLiveListForApi(int $matchId): array
|
|
{
|
|
$liveMap = self::getLiveMapForApi([$matchId]);
|
|
|
|
return $liveMap[$matchId] ?? [];
|
|
}
|
|
|
|
public static function getLiveMapForApi(array $matchIds): array
|
|
{
|
|
$matchIds = array_values(array_unique(array_filter(array_map('intval', $matchIds))));
|
|
if (empty($matchIds)) {
|
|
return [];
|
|
}
|
|
|
|
$rows = MatchLive::whereIn('match_id', $matchIds)
|
|
->whereNull('delete_time')
|
|
->order('create_time', 'asc')
|
|
->order('id', 'asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
$result = [];
|
|
foreach ($rows as &$row) {
|
|
$row['stream_options'] = self::buildStreamOptions($row);
|
|
$row['default_play_url'] = self::resolveDefaultPlayUrl($row);
|
|
$matchId = (int) ($row['match_id'] ?? 0);
|
|
if ($matchId <= 0) {
|
|
continue;
|
|
}
|
|
$result[$matchId][] = $row;
|
|
}
|
|
unset($row);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function buildStreamOptions(array $row): array
|
|
{
|
|
$definitions = [
|
|
['label' => 'M3U8', 'field' => 'play_url_m3u8'],
|
|
['label' => 'HD M3U8', 'field' => 'play_url_hd_m3u8'],
|
|
['label' => 'FLV', 'field' => 'play_url_flv'],
|
|
['label' => 'HD FLV', 'field' => 'play_url_hd_flv'],
|
|
];
|
|
|
|
$options = [];
|
|
foreach ($definitions as $definition) {
|
|
$url = trim((string) ($row[$definition['field']] ?? ''));
|
|
if ($url === '') {
|
|
continue;
|
|
}
|
|
$options[] = [
|
|
'label' => $definition['label'],
|
|
'url' => $url,
|
|
];
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
protected static function resolveDefaultPlayUrl(array $row): string
|
|
{
|
|
if (!empty($row['stream_options'][0]['url'])) {
|
|
return trim((string) $row['stream_options'][0]['url']);
|
|
}
|
|
|
|
foreach (['play_url_m3u8', 'play_url_hd_m3u8', 'play_url_flv', 'play_url_hd_flv'] as $field) {
|
|
$url = trim((string) ($row[$field] ?? ''));
|
|
if ($url !== '') {
|
|
return $url;
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
}
|