|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, ref, Ref, watchEffect } from "vue"; |
| 3 | +import { Player, allCivs } from "../types"; |
| 4 | +import { fetchData, normalizeCivs } from "../utils"; |
| 5 | +import PlayerCard, { PlayerSummary } from "./PlayerCard.vue"; |
| 6 | +
|
| 7 | +const props = defineProps({ |
| 8 | + code: { type: String, required: true }, |
| 9 | + presetMapNames: { type: Object, required: true }, |
| 10 | +}); |
| 11 | +const players: Ref<Player[]> = ref([]); |
| 12 | +
|
| 13 | +watchEffect(async () => { |
| 14 | + players.value = await fetchData(props.code, "players"); |
| 15 | +}); |
| 16 | +
|
| 17 | +const pageUrl = new URL(globalThis.location.toString()); |
| 18 | +const bracket = pageUrl.searchParams.get("bracket"); |
| 19 | +
|
| 20 | +const playersCompared = pageUrl.searchParams.get("players") || ","; |
| 21 | +const [player1, player2] = playersCompared.split(","); |
| 22 | +
|
| 23 | +function summarizePlayer(accumulator: PlayerSummary, game: Player) { |
| 24 | + const wins = accumulator.wins + (game.winner ? 1 : 0); |
| 25 | + const games = accumulator.games + 1; |
| 26 | + const eapms = [...accumulator.eapms, game.eapm]; |
| 27 | + const civPicks = (game.civ_picks as Array<(typeof allCivs)[number]>) |
| 28 | + .map(normalizeCivs) |
| 29 | + .reduce( |
| 30 | + (civs, civ) => ({ |
| 31 | + ...civs, |
| 32 | + [civ]: { ...civs[civ], picks: civs[civ].picks + 1 }, |
| 33 | + }), |
| 34 | + accumulator.civStats, |
| 35 | + ); |
| 36 | + const civBans = (game.civ_bans as Array<(typeof allCivs)[number]>) |
| 37 | + .map(normalizeCivs) |
| 38 | + .reduce( |
| 39 | + (civs, civ) => ({ |
| 40 | + ...civs, |
| 41 | + [civ]: { ...civs[civ], bans: civs[civ].bans + 1 }, |
| 42 | + }), |
| 43 | + civPicks, |
| 44 | + ); |
| 45 | + const gameCiv = normalizeCivs(game.civ) as (typeof allCivs)[number]; |
| 46 | + const civsPlayed = { |
| 47 | + ...civBans, |
| 48 | + [gameCiv]: { |
| 49 | + ...civBans[gameCiv], |
| 50 | + played: civBans[gameCiv].played + 1, |
| 51 | + wins: civBans[gameCiv].wins + (game.winner ? 1 : 0), |
| 52 | + playtime: civBans[gameCiv].playtime + game.duration, |
| 53 | + }, |
| 54 | + }; |
| 55 | + return { |
| 56 | + ...accumulator, |
| 57 | + games, |
| 58 | + wins, |
| 59 | + losses: accumulator.losses + (game.winner ? 0 : 1), |
| 60 | + winrate: Math.round((100 * wins) / games), |
| 61 | + eapms, |
| 62 | + civStats: civsPlayed, |
| 63 | + durations: [...accumulator.durations, game.duration], |
| 64 | + vils: [...accumulator.vils, game.vil_count], |
| 65 | + }; |
| 66 | +} |
| 67 | +const bracketPlayers = computed(() => |
| 68 | + players.value.filter((player) => |
| 69 | + bracket ? player.bracket == bracket : true, |
| 70 | + ), |
| 71 | +); |
| 72 | +
|
| 73 | +function computeStats(playerName: string) { |
| 74 | + return bracketPlayers.value |
| 75 | + .filter((player) => player.player == playerName) |
| 76 | + .reduce(summarizePlayer, { |
| 77 | + name: playerName, |
| 78 | + games: 0, |
| 79 | + wins: 0, |
| 80 | + losses: 0, |
| 81 | + colorPicks: [], |
| 82 | + favouriteColor: 0, |
| 83 | + eapms: [], |
| 84 | + civStats: Object.fromEntries( |
| 85 | + allCivs.map((civ) => [ |
| 86 | + civ, |
| 87 | + { picks: 0, bans: 0, played: 0, wins: 0, playtime: 0 }, |
| 88 | + ]), |
| 89 | + ) as PlayerSummary["civStats"], |
| 90 | + durations: [], |
| 91 | + vils: [], |
| 92 | + } as PlayerSummary); |
| 93 | +} |
| 94 | +const player1Stats = computed(() => computeStats(player1)); |
| 95 | +const player2Stats = computed(() => computeStats(player2)); |
| 96 | +</script> |
| 97 | + |
| 98 | +<template> |
| 99 | + <div :class="$style.comparison"> |
| 100 | + <div class="error" v-if="!player1 || !player2"> |
| 101 | + Missing correct parameter for players. Use the format |
| 102 | + <code>players=player1,player2</code> |
| 103 | + </div> |
| 104 | + <PlayerCard :stats="player1Stats" /> |
| 105 | + <PlayerCard :stats="player2Stats" /> |
| 106 | + </div> |
| 107 | +</template> |
| 108 | + |
| 109 | +<style lang="css" module> |
| 110 | +.comparison { |
| 111 | + display: grid; |
| 112 | + grid-template-columns: 1fr 1fr; |
| 113 | +} |
| 114 | +</style> |
0 commit comments