blanchon's picture
Nerfies-style home, paper-style match header, prettier setup, dataset README
95e3d2a
raw
history blame
3.51 kB
import type { Match, Round } from '$lib/types';
const TICK_RATE = 64;
export type MapRow = Match & {
duration_s: number;
};
export type RoundRow = {
match_id: number;
map_name: string;
round: number;
demo_round: number;
duration_s: number;
event: string;
team1: string;
team2: string;
score1: number;
score2: number;
winner: Match['winner'];
format: string;
match_date: string;
rounds_played: number;
};
export type MatchRow = {
match_id: number;
event: string;
team1: string;
team2: string;
score1: number;
score2: number;
winner: Match['winner'] | '';
format: string;
match_date: string;
maps: string[];
maps_played: number;
first_map: string;
rounds_played: number;
duration_s: number;
};
export function buildMapRows(matches: Match[], rounds: Round[]): MapRow[] {
const durationByMap = new Map<string, number>();
for (const r of rounds) {
const k = `${r.match_id}|${r.map_name}`;
durationByMap.set(k, (durationByMap.get(k) ?? 0) + (r.round_duration_ticks || 0) / TICK_RATE);
}
return matches.map((m) => ({
...m,
duration_s: durationByMap.get(`${m.match_id}|${m.map_name}`) ?? 0
}));
}
export function buildRoundRows(matches: Match[], rounds: Round[]): RoundRow[] {
const matchByMap = new Map<string, Match>();
for (const m of matches) matchByMap.set(`${m.match_id}|${m.map_name}`, m);
const out: RoundRow[] = [];
for (const r of rounds) {
const m = matchByMap.get(`${r.match_id}|${r.map_name}`);
if (!m) continue;
out.push({
match_id: r.match_id,
map_name: r.map_name,
round: r.round,
demo_round: r.demo_round,
duration_s: (r.round_duration_ticks || 0) / TICK_RATE,
event: m.event,
team1: m.team1,
team2: m.team2,
score1: m.score1,
score2: m.score2,
winner: m.winner,
format: m.format,
match_date: m.match_date,
rounds_played: m.rounds_played
});
}
return out;
}
export function buildMatchRows(matches: Match[], rounds: Round[]): MatchRow[] {
const durationByMap = new Map<string, number>();
for (const r of rounds) {
const k = `${r.match_id}|${r.map_name}`;
durationByMap.set(k, (durationByMap.get(k) ?? 0) + (r.round_duration_ticks || 0) / TICK_RATE);
}
const grouped = new Map<number, Match[]>();
for (const m of matches) {
const arr = grouped.get(m.match_id);
if (arr) arr.push(m);
else grouped.set(m.match_id, [m]);
}
const rows: MatchRow[] = [];
for (const [matchId, mapsForMatch] of grouped) {
const sorted = [...mapsForMatch].sort((a, b) => (a.map_index ?? 0) - (b.map_index ?? 0));
const head = sorted[0];
const team1MapWins = sorted.filter((m) => m.winner === 'team1').length;
const team2MapWins = sorted.filter((m) => m.winner === 'team2').length;
const totalRounds = sorted.reduce((acc, m) => acc + m.rounds_played, 0);
const totalDuration = sorted.reduce(
(acc, m) => acc + (durationByMap.get(`${m.match_id}|${m.map_name}`) ?? 0),
0
);
rows.push({
match_id: matchId,
event: head.event,
team1: head.team1,
team2: head.team2,
score1: team1MapWins,
score2: team2MapWins,
winner: team1MapWins > team2MapWins ? 'team1' : team2MapWins > team1MapWins ? 'team2' : '',
format: head.format,
match_date: head.match_date,
maps: sorted.map((m) => m.map_name),
maps_played: sorted.length,
first_map: head.map_name,
rounds_played: totalRounds,
duration_s: totalDuration
});
}
rows.sort(
(a, b) =>
new Date(b.match_date).getTime() - new Date(a.match_date).getTime() || b.match_id - a.match_id
);
return rows;
}