snow/person/sync.go

214 lines
3.6 KiB
Go
Raw Normal View History

2023-10-31 22:40:14 +00:00
package person
import (
"sync"
)
type ItemMutex struct {
sync.Map
ProfileType string
2023-10-31 22:40:14 +00:00
}
func NewItemMutex(profile string) *ItemMutex {
return &ItemMutex{
ProfileType: profile,
}
2023-10-31 22:40:14 +00:00
}
func (m *ItemMutex) AddItem(item *Item) {
item.ProfileType = m.ProfileType
2023-10-31 22:40:14 +00:00
m.Store(item.ID, item)
// storage.Repo.SaveItem(item)
}
func (m *ItemMutex) DeleteItem(id string) {
item := m.GetItem(id)
if item == nil {
return
}
item.Delete()
2023-10-31 22:40:14 +00:00
m.Delete(id)
// storage.Repo.DeleteItem(id)
}
func (m *ItemMutex) GetItem(id string) *Item {
item, ok := m.Load(id)
if !ok {
return nil
}
return item.(*Item)
}
2023-11-01 01:24:42 +00:00
func (m *ItemMutex) GetItemByTemplateID(templateID string) *Item {
var item *Item
m.Range(func(key, value interface{}) bool {
if value.(*Item).TemplateID == templateID {
item = value.(*Item)
return false
}
return true
})
return item
}
2023-10-31 22:40:14 +00:00
func (m *ItemMutex) RangeItems(f func(key string, value *Item) bool) {
m.Range(func(key, value interface{}) bool {
return f(key.(string), value.(*Item))
})
}
func (m *ItemMutex) Count() int {
count := 0
m.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}
type GiftMutex struct {
sync.Map
ProfileType string
2023-10-31 22:40:14 +00:00
}
func NewGiftMutex() *GiftMutex {
return &GiftMutex{}
}
func (m *GiftMutex) AddGift(gift *Gift) {
m.Store(gift.ID, gift)
// storage.Repo.SaveGift(gift)
}
func (m *GiftMutex) DeleteGift(id string) {
m.Delete(id)
// storage.Repo.DeleteGift(id)
}
func (m *GiftMutex) GetGift(id string) *Gift {
gift, ok := m.Load(id)
if !ok {
return nil
}
return gift.(*Gift)
}
func (m *GiftMutex) RangeGifts(f func(key string, value *Gift) bool) {
m.Range(func(key, value interface{}) bool {
return f(key.(string), value.(*Gift))
})
}
func (m *GiftMutex) Count() int {
count := 0
m.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}
type QuestMutex struct {
sync.Map
}
func NewQuestMutex() *QuestMutex {
return &QuestMutex{}
}
func (m *QuestMutex) AddQuest(quest *Quest) {
m.Store(quest.ID, quest)
// storage.Repo.SaveQuest(quest)
}
func (m *QuestMutex) DeleteQuest(id string) {
m.Delete(id)
// storage.Repo.DeleteQuest(id)
}
func (m *QuestMutex) GetQuest(id string) *Quest {
quest, ok := m.Load(id)
if !ok {
return nil
}
return quest.(*Quest)
}
func (m *QuestMutex) RangeQuests(f func(key string, value *Quest) bool) {
m.Range(func(key, value interface{}) bool {
return f(key.(string), value.(*Quest))
})
}
func (m *QuestMutex) Count() int {
count := 0
m.Range(func(key, value interface{}) bool {
count++
return true
})
return count
2023-11-01 00:05:17 +00:00
}
type AttributeMutex struct {
sync.Map
}
func NewAttributeMutex() *AttributeMutex {
return &AttributeMutex{}
}
func (m *AttributeMutex) AddAttribute(attribute *Attribute) {
2023-11-02 17:50:52 +00:00
m.Store(attribute.ID, attribute)
2023-11-01 00:05:17 +00:00
// storage.Repo.SaveAttribute(key, value)
}
2023-11-02 17:50:52 +00:00
func (m *AttributeMutex) DeleteAttribute(id string) {
m.Delete(id)
2023-11-01 00:05:17 +00:00
// storage.Repo.DeleteAttribute(key)
}
2023-11-02 17:50:52 +00:00
func (m *AttributeMutex) GetAttribute(id string) *Attribute {
value, ok := m.Load(id)
2023-11-01 00:05:17 +00:00
if !ok {
return nil
}
return value.(*Attribute)
}
2023-11-02 17:50:52 +00:00
func (m *AttributeMutex) GetAttributeByKey(key string) *Attribute {
var found *Attribute
m.RangeAttributes(func(id string, attribute *Attribute) bool {
if attribute.Key == key {
found = attribute
return false
}
return true
})
return found
}
func (m *AttributeMutex) RangeAttributes(f func(id string, attribute *Attribute) bool) {
2023-11-01 00:05:17 +00:00
m.Range(func(key, value interface{}) bool {
return f(key.(string), value.(*Attribute))
})
2023-11-02 17:50:52 +00:00
}
func (m *AttributeMutex) Count() int {
count := 0
m.Range(func(key, value interface{}) bool {
count++
return true
})
return count
2023-10-31 22:40:14 +00:00
}