Files
2026-10-23 03:02:55 +07:00

24 lines
1.1 KiB
TypeScript

import type { TeamsSnapshot } from '../src/types/screen.js'
import { object, text } from './remote-state.js'
export function parseTeams(value: unknown, packageKey: string): TeamsSnapshot {
const data = object(value)
if (
!/^[a-f0-9]{64}$/.test(packageKey) ||
data.packageKey !== packageKey ||
!Array.isArray(data.teams)
)
throw new Error('Некорректный список команд игры')
const captured = Date.parse(text(data.capturedAt))
if (!Number.isFinite(captured)) throw new Error('Некорректная дата списка команд')
const teams = data.teams
.map((value) => {
const team = object(value)
return { id: text(team.id), name: text(team.name) }
})
.sort((a, b) => a.name.localeCompare(b.name, 'ru') || a.id.localeCompare(b.id))
if (teams.some((team) => !team.id) || new Set(teams.map((team) => team.id)).size !== teams.length)
throw new Error('Повтор или отсутствие команды')
return { packageKey, capturedAt: new Date(captured).toISOString(), teams }
}