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
147
|
package socialcache
import (
"errors"
"sync"
. "github.com/Philipp15b/go-steam/protocol/steamlang"
. "github.com/Philipp15b/go-steam/steamid"
)
// Friends list is a thread safe map
// They can be iterated over like so:
// for id, friend := range client.Social.Friends.GetCopy() {
// log.Println(id, friend.Name)
// }
type FriendsList struct {
mutex sync.RWMutex
byId map[SteamId]*Friend
}
// Returns a new friends list
func NewFriendsList() *FriendsList {
return &FriendsList{byId: make(map[SteamId]*Friend)}
}
// Adds a friend to the friend list
func (list *FriendsList) Add(friend Friend) {
list.mutex.Lock()
defer list.mutex.Unlock()
_, exists := list.byId[friend.SteamId]
if !exists { //make sure this doesnt already exist
list.byId[friend.SteamId] = &friend
}
}
// Removes a friend from the friend list
func (list *FriendsList) Remove(id SteamId) {
list.mutex.Lock()
defer list.mutex.Unlock()
delete(list.byId, id)
}
// Returns a copy of the friends map
func (list *FriendsList) GetCopy() map[SteamId]Friend {
list.mutex.RLock()
defer list.mutex.RUnlock()
flist := make(map[SteamId]Friend)
for key, friend := range list.byId {
flist[key] = *friend
}
return flist
}
// Returns a copy of the friend of a given SteamId
func (list *FriendsList) ById(id SteamId) (Friend, error) {
list.mutex.RLock()
defer list.mutex.RUnlock()
if val, ok := list.byId[id]; ok {
return *val, nil
}
return Friend{}, errors.New("Friend not found")
}
// Returns the number of friends
func (list *FriendsList) Count() int {
list.mutex.RLock()
defer list.mutex.RUnlock()
return len(list.byId)
}
//Setter methods
func (list *FriendsList) SetName(id SteamId, name string) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.Name = name
}
}
func (list *FriendsList) SetAvatar(id SteamId, hash []byte) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.Avatar = hash
}
}
func (list *FriendsList) SetRelationship(id SteamId, relationship EFriendRelationship) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.Relationship = relationship
}
}
func (list *FriendsList) SetPersonaState(id SteamId, state EPersonaState) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.PersonaState = state
}
}
func (list *FriendsList) SetPersonaStateFlags(id SteamId, flags EPersonaStateFlag) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.PersonaStateFlags = flags
}
}
func (list *FriendsList) SetGameAppId(id SteamId, gameappid uint32) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.GameAppId = gameappid
}
}
func (list *FriendsList) SetGameId(id SteamId, gameid uint64) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.GameId = gameid
}
}
func (list *FriendsList) SetGameName(id SteamId, name string) {
list.mutex.Lock()
defer list.mutex.Unlock()
if val, ok := list.byId[id]; ok {
val.GameName = name
}
}
// A Friend
type Friend struct {
SteamId SteamId `json:",string"`
Name string
Avatar []byte
Relationship EFriendRelationship
PersonaState EPersonaState
PersonaStateFlags EPersonaStateFlag
GameAppId uint32
GameId uint64 `json:",string"`
GameName string
}
|