import type { RoundsAPIResponse } from '$lib/types/api/RoundsAPIResponse'; import type { MatchRoundsResponse, RoundDetail, RoundStats, Match } from '$lib/types'; /** * Transform raw rounds API response into structured format * @param rawData - Raw API response * @param matchId - Match ID * @param match - Match data with player information * @returns Structured rounds data */ export function transformRoundsResponse( rawData: RoundsAPIResponse, matchId: string, match?: Match ): MatchRoundsResponse { const rounds: RoundDetail[] = []; // Create player ID to team mapping const playerTeamMap = new Map(); if (match?.players) { for (const player of match.players) { playerTeamMap.set(player.id, player.team_id); } } // Convert object keys to sorted round numbers const roundNumbers = Object.keys(rawData) .map(Number) .sort((a, b) => a - b); for (const roundNum of roundNumbers) { const roundData = rawData[String(roundNum)]; if (!roundData) continue; const players: RoundStats[] = []; // Convert player data for (const [playerId, [bank, equipment, spent]] of Object.entries(roundData)) { players.push({ round: roundNum + 1, // API uses 0-indexed, we use 1-indexed bank, equipment, spent, player_id: Number(playerId) }); } rounds.push({ round: roundNum + 1, winner: 0, // TODO: Determine winner from data if available win_reason: '', // TODO: Determine win reason if available players }); } return { match_id: matchId, rounds }; }