Files
sbnews/server/app/common/service/match/MatchLiveService.php
T

66 lines
1.9 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
{
// MatchLive uses SoftDelete, so default queries only return non-deleted rows.
$row = MatchLive::where('match_id', $matchId)
->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
{
// MatchLive uses SoftDelete, so default queries only return non-deleted rows.
$rows = MatchLive::where('match_id', $matchId)
->order('create_time', 'asc')
->order('id', 'asc')
->select()
->toArray();
foreach ($rows as &$row) {
$row['stream_options'] = self::buildStreamOptions($row);
$row['default_play_url'] = $row['stream_options'][0]['url'] ?? '';
}
unset($row);
return $rows;
}
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;
}
}