clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

bug/architecture-test-trim-path
puzzletea / app / handle_stats.go
handle_stats.go 2.0K
1package app23import (4 "time"56 "github.com/FelineStateMachine/puzzletea/daily"7 "github.com/FelineStateMachine/puzzletea/registry"8 "github.com/FelineStateMachine/puzzletea/stats"9 "github.com/FelineStateMachine/puzzletea/ui"1011 "charm.land/bubbles/v2/viewport"12 tea "charm.land/bubbletea/v2"13)1415func (m model) handleStatsEnter() (model, tea.Cmd) {16 m = m.clearNotice()1718 catStats, err := m.store.GetCategoryStats()19 if err != nil {20 return m.setErrorf("Could not load category stats: %v", err), nil21 }22 modeStats, err := m.store.GetModeStats()23 if err != nil {24 return m.setErrorf("Could not load mode stats: %v", err), nil25 }26 streakDates, err := m.store.GetDailyStreakDates()27 if err != nil {28 return m.setErrorf("Could not load streak data: %v", err), nil29 }30 weekliesCompleted, err := m.store.GetCompletedWeeklyGauntlets()31 if err != nil {32 return m.setErrorf("Could not load weekly progress: %v", err), nil33 }34 now := time.Now()35 currentYear, currentWeek := now.ISOWeek()36 thisWeekHighestIndex, err := m.store.GetCurrentWeeklyHighestCompletedIndex(currentYear, currentWeek)37 if err != nil {38 return m.setErrorf("Could not load this week’s progress: %v", err), nil39 }40 currentDaily := false41 rec, err := m.store.GetDailyGame(daily.Name(now))42 if err != nil {43 m = m.setErrorf("Could not check today’s daily puzzle: %v", err)44 } else {45 currentDaily = rec != nil46 }4748 weights := stats.WeightsFromDefinitions(registry.Definitions())49 m.stats.cards = stats.BuildCards(weights, catStats, modeStats)50 m.stats.profile = stats.BuildProfileBanner(51 catStats,52 modeStats,53 weights,54 streakDates,55 currentDaily,56 weekliesCompleted,57 thisWeekHighestIndex,58 )5960 statsWidth, statsHeight := statsViewportSize(m.width, m.height, m.stats.cards)61 m.stats.viewport = viewport.New(62 viewport.WithWidth(statsWidth),63 viewport.WithHeight(statsHeight),64 )65 m.stats.viewport.SetContent(ui.RenderStatsCardGrid(m.stats.cards, statsWidth))66 m.state = statsView67 m = m.initScreen(statsView)68 return m, nil69}