package userstats import ( "sync" "time" ) type UserStats struct { Username string `json:"username"` MessageCount int `json:"message_count"` LastActive time.Time `json:"last_active"` IsMod bool `json:"is_mod"` IsVip bool `json:"is_vip"` IsSubscriber bool `json:"is_subscriber"` IsMarked bool `json:"is_marked"` LastMarkedDate time.Time `json:"-"` } type Store struct { mu sync.RWMutex users map[string]*UserStats // key = username (lowercase) } func NewStore() *Store { return &Store{ users: make(map[string]*UserStats), } } func (s *Store) GetOrCreate(username string) *UserStats { s.mu.Lock() defer s.mu.Unlock() lower := username if u, ok := s.users[lower]; ok { return u } u := &UserStats{Username: username} s.users[lower] = u return u } func (s *Store) Update(username string, fn func(*UserStats)) { s.mu.Lock() defer s.mu.Unlock() lower := username u, ok := s.users[lower] if !ok { u = &UserStats{Username: username} s.users[lower] = u } fn(u) } func (s *Store) GetAll() []*UserStats { s.mu.RLock() defer s.mu.RUnlock() res := make([]*UserStats, 0, len(s.users)) for _, u := range s.users { res = append(res, u) } return res }