feat: add match live service helpers
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$root = dirname(__DIR__, 2);
|
||||||
|
$servicePath = $root . '/server/app/common/service/match/MatchLiveService.php';
|
||||||
|
|
||||||
|
if (!is_file($servicePath)) {
|
||||||
|
fwrite(STDERR, "missing service file\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$service = file_get_contents($servicePath);
|
||||||
|
$requiredTokens = [
|
||||||
|
'class MatchLiveService',
|
||||||
|
'public static function syncLegacyLiveUrl',
|
||||||
|
'public static function getLiveListForApi',
|
||||||
|
'public static function buildStreamOptions',
|
||||||
|
'play_url_m3u8',
|
||||||
|
'default_play_url',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($requiredTokens as $needle) {
|
||||||
|
if (strpos($service, $needle) === false) {
|
||||||
|
fwrite(STDERR, "missing service token: {$needle}\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "service static checks passed\n";
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
<?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)
|
||||||
|
->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
|
||||||
|
{
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user