5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-27 02:01:35 +00:00
matterbridge/vendor/github.com/Philipp15b/go-steam/socialcache/groups.go

147 lines
3.3 KiB
Go
Raw Normal View History

2017-06-21 23:00:27 +00:00
package socialcache
import (
"errors"
2019-09-07 20:46:58 +00:00
"sync"
2017-06-21 23:00:27 +00:00
. "github.com/Philipp15b/go-steam/protocol/steamlang"
. "github.com/Philipp15b/go-steam/steamid"
)
// Groups list is a thread safe map
// They can be iterated over like so:
// for id, group := range client.Social.Groups.GetCopy() {
// log.Println(id, group.Name)
// }
type GroupsList struct {
mutex sync.RWMutex
byId map[SteamId]*Group
}
// Returns a new groups list
func NewGroupsList() *GroupsList {
return &GroupsList{byId: make(map[SteamId]*Group)}
}
// Adds a group to the group list
func (list *GroupsList) Add(group Group) {
list.mutex.Lock()
defer list.mutex.Unlock()
_, exists := list.byId[group.SteamId]
if !exists { //make sure this doesnt already exist
list.byId[group.SteamId] = &group
}
}
// Removes a group from the group list
func (list *GroupsList) Remove(id SteamId) {
list.mutex.Lock()
defer list.mutex.Unlock()
delete(list.byId, id)
}
// Returns a copy of the groups map
func (list *GroupsList) GetCopy() map[SteamId]Group {
list.mutex.RLock()
defer list.mutex.RUnlock()
glist := make(map[SteamId]Group)
for key, group := range list.byId {
glist[key] = *group
}
return glist
}
// Returns a copy of the group of a given SteamId
func (list *GroupsList) ById(id SteamId) (Group, error) {
list.mutex.RLock()
defer list.mutex.RUnlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
return *val, nil
}
return Group{}, errors.New("Group not found")
}
// Returns the number of groups
func (list *GroupsList) Count() int {
list.mutex.RLock()
defer list.mutex.RUnlock()
return len(list.byId)
}
//Setter methods
func (list *GroupsList) SetName(id SteamId, name string) {
list.mutex.Lock()
defer list.mutex.Unlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
val.Name = name
}
}
2019-09-07 20:46:58 +00:00
func (list *GroupsList) SetAvatar(id SteamId, hash []byte) {
2017-06-21 23:00:27 +00:00
list.mutex.Lock()
defer list.mutex.Unlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
val.Avatar = hash
}
}
func (list *GroupsList) SetRelationship(id SteamId, relationship EClanRelationship) {
list.mutex.Lock()
defer list.mutex.Unlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
val.Relationship = relationship
}
}
func (list *GroupsList) SetMemberTotalCount(id SteamId, count uint32) {
list.mutex.Lock()
defer list.mutex.Unlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
val.MemberTotalCount = count
}
}
func (list *GroupsList) SetMemberOnlineCount(id SteamId, count uint32) {
list.mutex.Lock()
defer list.mutex.Unlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
val.MemberOnlineCount = count
}
}
func (list *GroupsList) SetMemberChattingCount(id SteamId, count uint32) {
list.mutex.Lock()
defer list.mutex.Unlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
val.MemberChattingCount = count
}
}
func (list *GroupsList) SetMemberInGameCount(id SteamId, count uint32) {
list.mutex.Lock()
defer list.mutex.Unlock()
id = id.ChatToClan()
if val, ok := list.byId[id]; ok {
val.MemberInGameCount = count
}
}
// A Group
type Group struct {
SteamId SteamId `json:",string"`
Name string
2019-09-07 20:46:58 +00:00
Avatar []byte
2017-06-21 23:00:27 +00:00
Relationship EClanRelationship
MemberTotalCount uint32
MemberOnlineCount uint32
MemberChattingCount uint32
MemberInGameCount uint32
}