466 lines
11 KiB
Go

package babble
import (
"dk/internal/store"
"fmt"
"sort"
"strings"
"sync"
"time"
)
// Babble represents a global chat message in the game
type Babble struct {
ID int `json:"id"`
Posted int64 `json:"posted"`
Author string `json:"author"`
Babble string `json:"babble"`
}
func (b *Babble) Save() error {
babbleStore := GetStore()
babbleStore.UpdateBabble(b)
return nil
}
func (b *Babble) Delete() error {
babbleStore := GetStore()
babbleStore.RemoveBabble(b.ID)
return nil
}
// Creates a new Babble with sensible defaults
func New() *Babble {
return &Babble{
Posted: time.Now().Unix(),
Author: "",
Babble: "",
}
}
// Validate checks if babble has valid values
func (b *Babble) Validate() error {
if b.Posted <= 0 {
return fmt.Errorf("babble Posted timestamp must be positive")
}
if strings.TrimSpace(b.Author) == "" {
return fmt.Errorf("babble Author cannot be empty")
}
if strings.TrimSpace(b.Babble) == "" {
return fmt.Errorf("babble message cannot be empty")
}
return nil
}
// BabbleStore provides in-memory storage with O(1) lookups and babble-specific indices
type BabbleStore struct {
*store.BaseStore[Babble] // Embedded generic store
byAuthor map[string][]int // Author (lowercase) -> []ID
allByPosted []int // All IDs sorted by posted DESC, id DESC
mu sync.RWMutex // Protects indices
}
// Global in-memory store
var babbleStore *BabbleStore
var storeOnce sync.Once
// Initialize the in-memory store
func initStore() {
babbleStore = &BabbleStore{
BaseStore: store.NewBaseStore[Babble](),
byAuthor: make(map[string][]int),
allByPosted: make([]int, 0),
}
}
// GetStore returns the global babble store
func GetStore() *BabbleStore {
storeOnce.Do(initStore)
return babbleStore
}
// AddBabble adds a babble message to the in-memory store and updates all indices
func (bs *BabbleStore) AddBabble(babble *Babble) {
bs.mu.Lock()
defer bs.mu.Unlock()
// Validate babble
if err := babble.Validate(); err != nil {
return
}
// Add to base store
bs.Add(babble.ID, babble)
// Rebuild indices
bs.rebuildIndicesUnsafe()
}
// RemoveBabble removes a babble message from the store and updates indices
func (bs *BabbleStore) RemoveBabble(id int) {
bs.mu.Lock()
defer bs.mu.Unlock()
// Remove from base store
bs.Remove(id)
// Rebuild indices
bs.rebuildIndicesUnsafe()
}
// UpdateBabble updates a babble message efficiently
func (bs *BabbleStore) UpdateBabble(babble *Babble) {
bs.mu.Lock()
defer bs.mu.Unlock()
// Validate babble
if err := babble.Validate(); err != nil {
return
}
// Update base store
bs.Add(babble.ID, babble)
// Rebuild indices
bs.rebuildIndicesUnsafe()
}
// LoadData loads babble data from JSON file, or starts with empty store
func LoadData(dataPath string) error {
bs := GetStore()
// Load from base store, which handles JSON loading
if err := bs.BaseStore.LoadData(dataPath); err != nil {
return err
}
// Rebuild indices from loaded data
bs.rebuildIndices()
return nil
}
// SaveData saves babble data to JSON file
func SaveData(dataPath string) error {
bs := GetStore()
return bs.BaseStore.SaveData(dataPath)
}
// rebuildIndicesUnsafe rebuilds all indices from base store data (caller must hold lock)
func (bs *BabbleStore) rebuildIndicesUnsafe() {
// Clear indices
bs.byAuthor = make(map[string][]int)
bs.allByPosted = make([]int, 0)
// Collect all babbles and build indices
allBabbles := bs.GetAll()
for id, babble := range allBabbles {
// Author index (case-insensitive)
authorKey := strings.ToLower(babble.Author)
bs.byAuthor[authorKey] = append(bs.byAuthor[authorKey], id)
// All IDs
bs.allByPosted = append(bs.allByPosted, id)
}
// Sort allByPosted by posted DESC, then ID DESC
sort.Slice(bs.allByPosted, func(i, j int) bool {
babbleI, _ := bs.GetByID(bs.allByPosted[i])
babbleJ, _ := bs.GetByID(bs.allByPosted[j])
if babbleI.Posted != babbleJ.Posted {
return babbleI.Posted > babbleJ.Posted // DESC
}
return bs.allByPosted[i] > bs.allByPosted[j] // DESC
})
// Sort author indices by posted DESC, then ID DESC
for author := range bs.byAuthor {
sort.Slice(bs.byAuthor[author], func(i, j int) bool {
babbleI, _ := bs.GetByID(bs.byAuthor[author][i])
babbleJ, _ := bs.GetByID(bs.byAuthor[author][j])
if babbleI.Posted != babbleJ.Posted {
return babbleI.Posted > babbleJ.Posted // DESC
}
return bs.byAuthor[author][i] > bs.byAuthor[author][j] // DESC
})
}
}
// rebuildIndices rebuilds all babble-specific indices from base store data
func (bs *BabbleStore) rebuildIndices() {
bs.mu.Lock()
defer bs.mu.Unlock()
bs.rebuildIndicesUnsafe()
}
// Retrieves a babble message by ID
func Find(id int) (*Babble, error) {
bs := GetStore()
babble, exists := bs.GetByID(id)
if !exists {
return nil, fmt.Errorf("babble with ID %d not found", id)
}
return babble, nil
}
// Retrieves all babble messages ordered by posted time (newest first)
func All() ([]*Babble, error) {
bs := GetStore()
bs.mu.RLock()
defer bs.mu.RUnlock()
result := make([]*Babble, 0, len(bs.allByPosted))
for _, id := range bs.allByPosted {
if babble, exists := bs.GetByID(id); exists {
result = append(result, babble)
}
}
return result, nil
}
// Retrieves babble messages by a specific author
func ByAuthor(author string) ([]*Babble, error) {
bs := GetStore()
bs.mu.RLock()
defer bs.mu.RUnlock()
ids, exists := bs.byAuthor[strings.ToLower(author)]
if !exists {
return []*Babble{}, nil
}
result := make([]*Babble, 0, len(ids))
for _, id := range ids {
if babble, exists := bs.GetByID(id); exists {
result = append(result, babble)
}
}
return result, nil
}
// Retrieves the most recent babble messages (limited by count)
func Recent(limit int) ([]*Babble, error) {
bs := GetStore()
bs.mu.RLock()
defer bs.mu.RUnlock()
if limit > len(bs.allByPosted) {
limit = len(bs.allByPosted)
}
result := make([]*Babble, 0, limit)
for i := 0; i < limit; i++ {
if babble, exists := bs.GetByID(bs.allByPosted[i]); exists {
result = append(result, babble)
}
}
return result, nil
}
// Retrieves babble messages since a specific timestamp
func Since(since int64) ([]*Babble, error) {
bs := GetStore()
bs.mu.RLock()
defer bs.mu.RUnlock()
var result []*Babble
for _, id := range bs.allByPosted {
if babble, exists := bs.GetByID(id); exists && babble.Posted >= since {
result = append(result, babble)
}
}
return result, nil
}
// Retrieves babble messages between two timestamps (inclusive)
func Between(start, end int64) ([]*Babble, error) {
bs := GetStore()
bs.mu.RLock()
defer bs.mu.RUnlock()
var result []*Babble
for _, id := range bs.allByPosted {
if babble, exists := bs.GetByID(id); exists && babble.Posted >= start && babble.Posted <= end {
result = append(result, babble)
}
}
return result, nil
}
// Retrieves babble messages containing the search term (case-insensitive)
func Search(term string) ([]*Babble, error) {
bs := GetStore()
bs.mu.RLock()
defer bs.mu.RUnlock()
var result []*Babble
lowerTerm := strings.ToLower(term)
for _, id := range bs.allByPosted {
if babble, exists := bs.GetByID(id); exists {
if strings.Contains(strings.ToLower(babble.Babble), lowerTerm) {
result = append(result, babble)
}
}
}
return result, nil
}
// Retrieves recent messages from a specific author
func RecentByAuthor(author string, limit int) ([]*Babble, error) {
bs := GetStore()
bs.mu.RLock()
defer bs.mu.RUnlock()
ids, exists := bs.byAuthor[strings.ToLower(author)]
if !exists {
return []*Babble{}, nil
}
if limit > len(ids) {
limit = len(ids)
}
result := make([]*Babble, 0, limit)
for i := 0; i < limit; i++ {
if babble, exists := bs.GetByID(ids[i]); exists {
result = append(result, babble)
}
}
return result, nil
}
// Saves a new babble to the in-memory store and sets the ID
func (b *Babble) Insert() error {
bs := GetStore()
// Validate before insertion
if err := b.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
// Assign new ID if not set
if b.ID == 0 {
b.ID = bs.GetNextID()
}
// Add to store
bs.AddBabble(b)
return nil
}
// Returns the posted timestamp as a time.Time
func (b *Babble) PostedTime() time.Time {
return time.Unix(b.Posted, 0)
}
// Sets the posted timestamp from a time.Time
func (b *Babble) SetPostedTime(t time.Time) {
b.Posted = t.Unix()
}
// Returns true if the babble message was posted within the last hour
func (b *Babble) IsRecent() bool {
return time.Since(b.PostedTime()) < time.Hour
}
// Returns how long ago the babble message was posted
func (b *Babble) Age() time.Duration {
return time.Since(b.PostedTime())
}
// Returns true if the given username is the author of this babble message
func (b *Babble) IsAuthor(username string) bool {
return strings.EqualFold(b.Author, username)
}
// Returns a truncated version of the babble for previews
func (b *Babble) Preview(maxLength int) string {
if len(b.Babble) <= maxLength {
return b.Babble
}
if maxLength < 3 {
return b.Babble[:maxLength]
}
return b.Babble[:maxLength-3] + "..."
}
// Returns the number of words in the babble message
func (b *Babble) WordCount() int {
if b.Babble == "" {
return 0
}
// Simple word count by splitting on whitespace
words := 0
inWord := false
for _, char := range b.Babble {
if char == ' ' || char == '\t' || char == '\n' || char == '\r' {
if inWord {
words++
inWord = false
}
} else {
inWord = true
}
}
if inWord {
words++
}
return words
}
// Returns the character length of the babble message
func (b *Babble) Length() int {
return len(b.Babble)
}
// Returns true if the babble message contains the given term (case-insensitive)
func (b *Babble) Contains(term string) bool {
return strings.Contains(strings.ToLower(b.Babble), strings.ToLower(term))
}
// Returns true if the babble message is empty or whitespace-only
func (b *Babble) IsEmpty() bool {
return strings.TrimSpace(b.Babble) == ""
}
// Returns true if the message exceeds the typical chat length
func (b *Babble) IsLongMessage(threshold int) bool {
return b.Length() > threshold
}
// Returns a slice of usernames mentioned in the message (starting with @)
func (b *Babble) GetMentions() []string {
words := strings.Fields(b.Babble)
var mentions []string
for _, word := range words {
if strings.HasPrefix(word, "@") && len(word) > 1 {
// Clean up punctuation from the end
mention := strings.TrimRight(word[1:], ".,!?;:")
if mention != "" {
mentions = append(mentions, mention)
}
}
}
return mentions
}
// Returns true if the message mentions the given username
func (b *Babble) HasMention(username string) bool {
mentions := b.GetMentions()
for _, mention := range mentions {
if strings.EqualFold(mention, username) {
return true
}
}
return false
}