282 lines
5.8 KiB
Go

package babble
import (
"dk/internal/store"
"fmt"
"sort"
"strings"
"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 {
return GetStore().UpdateWithRebuild(b.ID, b)
}
func (b *Babble) Delete() error {
GetStore().RemoveWithRebuild(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 with enhanced BaseStore
type BabbleStore struct {
*store.BaseStore[Babble]
}
// Global store with singleton pattern
var GetStore = store.NewSingleton(func() *BabbleStore {
bs := &BabbleStore{BaseStore: store.NewBaseStore[Babble]()}
// Register indices
bs.RegisterIndex("byAuthor", store.BuildStringGroupIndex(func(b *Babble) string {
return strings.ToLower(b.Author)
}))
bs.RegisterIndex("allByPosted", store.BuildSortedListIndex(func(a, b *Babble) bool {
if a.Posted != b.Posted {
return a.Posted > b.Posted // DESC
}
return a.ID > b.ID // DESC
}))
return bs
})
// Enhanced CRUD operations
func (bs *BabbleStore) AddBabble(babble *Babble) error {
return bs.AddWithRebuild(babble.ID, babble)
}
func (bs *BabbleStore) RemoveBabble(id int) {
bs.RemoveWithRebuild(id)
}
func (bs *BabbleStore) UpdateBabble(babble *Babble) error {
return bs.UpdateWithRebuild(babble.ID, babble)
}
// Data persistence
func LoadData(dataPath string) error {
bs := GetStore()
return bs.BaseStore.LoadData(dataPath)
}
func SaveData(dataPath string) error {
bs := GetStore()
return bs.BaseStore.SaveData(dataPath)
}
// Query functions using enhanced store
func Find(id int) (*Babble, error) {
bs := GetStore()
babble, exists := bs.Find(id)
if !exists {
return nil, fmt.Errorf("babble with ID %d not found", id)
}
return babble, nil
}
func All() ([]*Babble, error) {
bs := GetStore()
return bs.AllSorted("allByPosted"), nil
}
func ByAuthor(author string) ([]*Babble, error) {
bs := GetStore()
messages := bs.GroupByIndex("byAuthor", strings.ToLower(author))
// Sort by posted DESC, then ID DESC
sort.Slice(messages, func(i, j int) bool {
if messages[i].Posted != messages[j].Posted {
return messages[i].Posted > messages[j].Posted // DESC
}
return messages[i].ID > messages[j].ID // DESC
})
return messages, nil
}
func Recent(limit int) ([]*Babble, error) {
bs := GetStore()
all := bs.AllSorted("allByPosted")
if limit > len(all) {
limit = len(all)
}
return all[:limit], nil
}
func Since(since int64) ([]*Babble, error) {
bs := GetStore()
return bs.FilterByIndex("allByPosted", func(b *Babble) bool {
return b.Posted >= since
}), nil
}
func Between(start, end int64) ([]*Babble, error) {
bs := GetStore()
return bs.FilterByIndex("allByPosted", func(b *Babble) bool {
return b.Posted >= start && b.Posted <= end
}), nil
}
func Search(term string) ([]*Babble, error) {
bs := GetStore()
lowerTerm := strings.ToLower(term)
return bs.FilterByIndex("allByPosted", func(b *Babble) bool {
return strings.Contains(strings.ToLower(b.Babble), lowerTerm)
}), nil
}
func RecentByAuthor(author string, limit int) ([]*Babble, error) {
messages, err := ByAuthor(author)
if err != nil {
return nil, err
}
if limit > len(messages) {
limit = len(messages)
}
return messages[:limit], nil
}
// Insert with ID assignment
func (b *Babble) Insert() error {
bs := GetStore()
if b.ID == 0 {
b.ID = bs.GetNextID()
}
return bs.AddBabble(b)
}
// Helper methods
func (b *Babble) PostedTime() time.Time {
return time.Unix(b.Posted, 0)
}
func (b *Babble) SetPostedTime(t time.Time) {
b.Posted = t.Unix()
}
func (b *Babble) IsRecent() bool {
return time.Since(b.PostedTime()) < time.Hour
}
func (b *Babble) Age() time.Duration {
return time.Since(b.PostedTime())
}
func (b *Babble) IsAuthor(username string) bool {
return strings.EqualFold(b.Author, username)
}
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] + "..."
}
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
}
func (b *Babble) Length() int {
return len(b.Babble)
}
func (b *Babble) Contains(term string) bool {
return strings.Contains(strings.ToLower(b.Babble), strings.ToLower(term))
}
func (b *Babble) IsEmpty() bool {
return strings.TrimSpace(b.Babble) == ""
}
func (b *Babble) IsLongMessage(threshold int) bool {
return b.Length() > threshold
}
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
}
func (b *Babble) HasMention(username string) bool {
mentions := b.GetMentions()
for _, mention := range mentions {
if strings.EqualFold(mention, username) {
return true
}
}
return false
}