1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package socialcache
import (
"errors"
"sync"
. "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
}
}
func (list *GroupsList) SetAvatar(id SteamId, hash []byte) {
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
Avatar []byte
Relationship EClanRelationship
MemberTotalCount uint32
MemberOnlineCount uint32
MemberChattingCount uint32
MemberInGameCount uint32
}
|