56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/tolelom/catacombs/store"
|
|
)
|
|
|
|
// StatsScreen shows player statistics.
|
|
type StatsScreen struct{}
|
|
|
|
func NewStatsScreen() *StatsScreen {
|
|
return &StatsScreen{}
|
|
}
|
|
|
|
func (s *StatsScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
|
|
if key, ok := msg.(tea.KeyMsg); ok {
|
|
if isKey(key, "s") || isEnter(key) || isQuit(key) {
|
|
return NewTitleScreen(), nil
|
|
}
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (s *StatsScreen) View(ctx *Context) string {
|
|
var stats store.PlayerStats
|
|
if ctx.Store != nil {
|
|
stats, _ = ctx.Store.GetStats(ctx.PlayerName)
|
|
}
|
|
return renderStats(ctx.PlayerName, stats, ctx.Width, ctx.Height)
|
|
}
|
|
|
|
func renderStats(playerName string, stats store.PlayerStats, width, height int) string {
|
|
title := styleHeader.Render("── Player Statistics ──")
|
|
|
|
var content string
|
|
content += stylePlayer.Render(fmt.Sprintf(" %s", playerName)) + "\n\n"
|
|
content += fmt.Sprintf(" Total Runs: %s\n", styleGold.Render(fmt.Sprintf("%d", stats.TotalRuns)))
|
|
content += fmt.Sprintf(" Best Floor: %s\n", styleGold.Render(fmt.Sprintf("B%d", stats.BestFloor)))
|
|
content += fmt.Sprintf(" Total Gold: %s\n", styleGold.Render(fmt.Sprintf("%d", stats.TotalGold)))
|
|
content += fmt.Sprintf(" Victories: %s\n", styleHeal.Render(fmt.Sprintf("%d", stats.Victories)))
|
|
|
|
winRate := 0.0
|
|
if stats.TotalRuns > 0 {
|
|
winRate = float64(stats.Victories) / float64(stats.TotalRuns) * 100
|
|
}
|
|
content += fmt.Sprintf(" Win Rate: %s\n", styleSystem.Render(fmt.Sprintf("%.1f%%", winRate)))
|
|
|
|
footer := styleSystem.Render("[S] Back")
|
|
|
|
return lipgloss.Place(width, height, lipgloss.Center, lipgloss.Center,
|
|
lipgloss.JoinVertical(lipgloss.Center, title, "", content, "", footer))
|
|
}
|