import { LitElement, html } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import { PlayerStatsLeaf, PlayerStatsTree } from "../../../../core/ApiSchemas"; import { Difficulty, GameMode, GameType } from "../../../../core/game/Game"; import { PlayerStats } from "../../../../core/StatsSchemas"; import { renderNumber, translateText } from "../../../Utils"; import "./PlayerStatsGrid"; import "./PlayerStatsTable"; @customElement("player-stats-tree-view") export class PlayerStatsTreeView extends LitElement { @property({ type: Object }) statsTree?: PlayerStatsTree; @state() selectedType: GameType = GameType.Public; @state() selectedMode: GameMode = GameMode.FFA; @state() selectedDifficulty: Difficulty = Difficulty.Medium; private get availableTypes(): GameType[] { if (!this.statsTree) return []; return Object.keys(this.statsTree) as GameType[]; } private get availableModes(): GameMode[] { const typeNode = this.statsTree?.[this.selectedType]; if (!typeNode) return []; return Object.keys(typeNode) as GameMode[]; } private get availableDifficulties(): Difficulty[] { const typeNode = this.statsTree?.[this.selectedType]; const modeNode = typeNode?.[this.selectedMode]; if (!modeNode) return []; return Object.keys(modeNode) as Difficulty[]; } private labelForMode(m: GameMode) { return m === GameMode.FFA ? translateText("player_modal.mode_ffa") : translateText("player_modal.mode_team"); } createRenderRoot() { return this; } private getSelectedLeaf(): PlayerStatsLeaf | null { const typeNode = this.statsTree?.[this.selectedType]; if (!typeNode) return null; const modeNode = typeNode[this.selectedMode]; if (!modeNode) return null; const diffNode = modeNode[this.selectedDifficulty]; if (!diffNode) return null; return diffNode; } private getDisplayedStats(): PlayerStats | null { const leaf = this.getSelectedLeaf(); if (!leaf || !leaf.stats) return null; return leaf.stats; } private setGameType(t: GameType) { if (this.selectedType === t) return; this.selectedType = t; const modes = this.availableModes; if (!modes.includes(this.selectedMode)) { this.selectedMode = modes[0] ?? this.selectedMode; } const diffs = this.availableDifficulties; if (!diffs.includes(this.selectedDifficulty)) { this.selectedDifficulty = diffs[0] ?? this.selectedDifficulty; } this.requestUpdate(); } private setMode(m: GameMode) { if (this.selectedMode === m) return; this.selectedMode = m; const diffs = this.availableDifficulties; if (!diffs.includes(this.selectedDifficulty)) { this.selectedDifficulty = diffs[0] ?? this.selectedDifficulty; } this.requestUpdate(); } private setDifficulty(d: Difficulty) { if (this.selectedDifficulty === d) return; this.selectedDifficulty = d; this.requestUpdate(); } render() { const types = this.availableTypes; if (types.length && !types.includes(this.selectedType)) { this.selectedType = types[0]; } const modes = this.availableModes; if (modes.length && !modes.includes(this.selectedMode)) { this.selectedMode = modes[0]; } const diffs = this.availableDifficulties; if (diffs.length && !diffs.includes(this.selectedDifficulty)) { this.selectedDifficulty = diffs[0]; } const leaf = this.getSelectedLeaf(); const wlr = leaf ? leaf.losses === 0n ? Number(leaf.wins) : Number(leaf.wins) / Number(leaf.losses) : 0; return html`
${types.map( (t) => html` `, )}
${modes.length ? html`
${modes.map( (m) => html` `, )}
` : html``} ${diffs.length ? html`
${diffs.map( (d) => html` `, )}
` : html``} ${leaf ? html`

` : html``} `; } }