Files
csgowtf/src/lib/api/transformers/roundsTransformer.ts
vikingowl 05a6c10458 fix: Fix match detail data loading and add API transformation layer
- Update Zod schemas to match raw API response formats
- Create transformation layer (rounds, weapons, chat) to convert raw API to structured format
- Add player name mapping in transformers for better UX
- Fix Svelte 5 reactivity issues in chat page (replace $effect with $derived)
- Fix Chart.js compatibility with Svelte 5 state proxies using JSON serialization
- Add economy advantage chart with halftime perspective flip (WIP)
- Remove stray comment from details page
- Update layout to load match data first, then pass to API methods

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 00:37:41 +01:00

61 lines
1.5 KiB
TypeScript

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<string, number>();
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
};
}