Files
csgowtf/src/routes/player/[id]/+page.ts
vikingowl a861b1c1b6 fix: Fix player profile loading with API transformer and improve UI layout
- Add LegacyPlayerProfile transformer to handle API response format mismatch
- Transform avatar hashes to full Steam CDN URLs
- Map team IDs correctly (API 1/2 -> Schema 2/3)
- Calculate aggregate stats (avg_kills, avg_deaths, win_rate) from matches
- Reduce featured matches on homepage from 6 to 3
- Show 4 recent matches on player profile instead of 10
- Display recent matches in 4-column grid on desktop (side-by-side)

Fixes "Player not found" error for all player profiles.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-05 00:43:50 +01:00

63 lines
2.0 KiB
TypeScript

import { error } from '@sveltejs/kit';
import { api } from '$lib/api';
import type { PageLoad } from './$types';
export const load: PageLoad = async ({ params }) => {
const playerId = params.id; // Keep as string to preserve uint64 precision
if (!playerId || playerId.trim() === '') {
throw error(400, 'Invalid player ID');
}
try {
// Fetch player profile and recent matches in parallel
const [profile, matchesData] = await Promise.all([
api.players.getPlayerMeta(playerId),
api.matches.getMatches({ player_id: playerId, limit: 20 })
]);
// Fetch full match details with player stats for performance charts
// Limit to first 15 matches to avoid too many API calls
const matchDetailsPromises = matchesData.matches
.slice(0, 15)
.map((match) => api.matches.getMatch(match.match_id));
const matchesWithDetails = await Promise.all(matchDetailsPromises);
// Extract player stats from each match
const playerStats = matchesWithDetails
.map((match) => {
const playerData = match.players?.find((p) => p.id === playerId);
if (!playerData) return null;
return {
match_id: match.match_id,
map: match.map,
date: match.date,
...playerData,
// Add match result (did player win?)
won:
(playerData.team_id === 2 && match.score_team_a > match.score_team_b) ||
(playerData.team_id === 3 && match.score_team_b > match.score_team_a)
};
})
.filter((stat): stat is NonNullable<typeof stat> => stat !== null);
return {
profile,
recentMatches: matchesData.matches.slice(0, 4), // Show 4 in recent matches section
playerStats, // Full stats for charts
meta: {
title: `${profile.name} - Player Profile | CS2.WTF`,
description: `View ${profile.name}'s CS2 statistics, match history, and performance metrics.`
}
};
} catch (err) {
console.error(
`Failed to load player ${playerId}:`,
err instanceof Error ? err.message : String(err)
);
throw error(404, `Player ${playerId} not found`);
}
};