/
social_mgr.go
342 lines (276 loc) · 8.27 KB
/
social_mgr.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package realm
import (
"sort"
"github.com/Gophercraft/core/guid"
"github.com/Gophercraft/core/packet"
"github.com/Gophercraft/core/packet/chat"
"github.com/Gophercraft/core/packet/social"
"github.com/Gophercraft/core/realm/wdb/models"
"github.com/Gophercraft/core/vsn"
)
func (ws *Server) GetFriendStatus(id guid.GUID) social.Status {
_, err := ws.GetSessionByGUID(id)
if err != nil {
return social.FriendOffline
}
return social.FriendOnline
}
// ---- Begin Vanilla
func (s *Session) SendFriendList() {
var contacts []models.Contact
if err := s.DB().Where("player = ?", s.GUID().Counter()).Where("friended = 1").Find(&contacts); err != nil {
panic(err)
}
list := &social.PlayerList{
Type: packet.SMSG_FRIEND_LIST,
Players: make([]guid.GUID, len(contacts)),
}
for i, v := range contacts {
list.Players[i] = guid.RealmSpecific(guid.Player, s.Server.RealmID(), v.Friend)
}
s.Send(list)
}
func (s *Session) SendIgnoreList() {
var contacts []models.Contact
if err := s.DB().Where("player = ?", s.GUID().Counter()).Where("ignored = 1").Find(&contacts); err != nil {
panic(err)
}
list := &social.PlayerList{
Type: packet.SMSG_IGNORE_LIST,
Players: make([]guid.GUID, len(contacts)),
}
for i, v := range contacts {
list.Players[i] = guid.RealmSpecific(guid.Player, s.Server.RealmID(), v.Friend)
}
s.Send(list)
}
// ---- End Vanilla
// Starting in protocol 8606, the friends list and the ignore list are merged into a single packet.
func (s *Session) SendSocialList() {
if s.Build().RemovedIn(vsn.V2_4_3) {
s.SendFriendList()
s.SendIgnoreList()
return
}
p := &social.ContactList{
Flags: social.DefaultContactFlags,
}
var contacts []models.Contact
s.DB().Where("player = ?", s.GUID().Counter()).Find(&contacts)
for _, mcontact := range contacts {
var scontact social.Contact
if mcontact.Friended {
scontact.Flags |= social.FlagFriend
}
if mcontact.Muted {
scontact.Flags |= social.FlagMuted
}
if mcontact.Ignored {
scontact.Flags |= social.FlagIgnored
}
p.Contacts = append(p.Contacts, scontact)
}
s.Send(p)
}
func (s *Session) SendDanceMoves() {
s.Send(&chat.LearnedDanceMoves{})
}
func (s *Session) HandleWho(wr *social.WhoRequest) {
w := &social.Who{}
var usernames []string
s.Server.GuardPlayerList.Lock()
for k := range s.Server.PlayerList {
usernames = append(usernames, k)
}
sort.Strings(usernames)
whoMatches := make([]social.WhoMatch, len(usernames))
for _i, user := range usernames {
playerPtr := s.Server.PlayerList[user]
whoMatches[_i] = social.WhoMatch{
PlayerName: user,
GuildName: "",
Level: uint32(playerPtr.GetLevel()),
Class: uint32(playerPtr.GetPlayerClass()),
Race: uint32(playerPtr.GetPlayerRace()),
ZoneID: playerPtr.ZoneID,
}
}
w.WhoMatches = whoMatches
s.Server.GuardPlayerList.Unlock()
s.Send(w)
}
func (s *Session) SendFriendStatus(result social.FriendResult, id guid.GUID, note string, status social.Status, area, level, class uint32) {
// data := packet.NewWorldPacket(packet.SMSG_FRIEND_STATUS)
// data.WriteByte(result)
// id.EncodeUnpacked(s.Build(), data)
// switch result {
// case social.FriendAddedOnline, social.FriendOnline:
// data.WriteByte(uint8(status))
// data.WriteUint32(area)
// data.WriteUint32(level)
// data.WriteUint32(class)
// default:
// }
// s.SendPacket(data)
var fs social.FriendStatus
fs.Result = result
fs.ID = id
fs.Note = note
fs.Status = status
fs.AreaID = area
fs.Level = level
fs.Class = class
s.Send(&fs)
}
func (s *Session) GetContact(friend guid.GUID) *models.Contact {
var contact models.Contact
found, err := s.DB().Where("player = ?", s.GUID().Counter()).Where("friend = ?", friend.Counter()).Get(&contact)
if err != nil {
panic(err)
}
if !found {
contact.Player = s.GUID().Counter()
contact.Friend = friend.Counter()
if _, err := s.DB().Insert(&contact); err != nil {
panic(err)
}
return &contact
}
return &contact
}
func (s *Session) HandleFriendAdd(add *social.Add) {
name := add.Name
id, err := s.Server.GetGUIDByPlayerName(name)
if err != nil {
s.SendFriendStatus(social.FriendNotFound, guid.Nil, "", 0, 0, 0, 0)
return
}
if id == s.GUID() {
s.SendFriendStatus(social.FriendSelf, guid.Nil, "", 0, 0, 0, 0)
return
}
contact := s.GetContact(id)
if contact.Friended {
s.SendFriendStatus(social.FriendAlready, guid.Nil, "", 0, 0, 0, 0)
return
}
contact.Friended = true
s.DB().Where("player = ?", s.GUID().Counter()).Where("friend = ?", id.Counter()).Cols("friended").Update(contact)
status := s.Server.GetFriendStatus(id)
var area, level, class uint32
if status&social.FriendOnline != 0 {
session, err := s.Server.GetSessionByGUID(id)
if err != nil {
s.SendFriendStatus(social.FriendDBError, guid.Nil, "", 0, 0, 0, 0)
return
}
area = session.ZoneID
level = uint32(session.GetLevel())
class = uint32(session.GetPlayerClass())
s.SendFriendStatus(social.FriendAddedOnline, id, contact.Note, status, area, level, class)
} else {
s.SendFriendStatus(social.FriendAddedOffline, id, contact.Note, status, area, level, class)
}
}
func (s *Session) HandleFriendDelete(del *social.Delete) {
id := del.ID
if id == s.GUID() {
s.SendFriendStatus(social.FriendSelf, guid.Nil, "", 0, 0, 0, 0)
return
}
var contact models.Contact
found, err := s.DB().Where("player = ?", s.GUID().Counter()).Where("friend = ?", id.Counter()).Get(&contact)
if err != nil {
panic(err)
}
if !found {
s.SendFriendStatus(social.FriendNotFound, guid.Nil, "", 0, 0, 0, 0)
return
}
if contact.Friended == false {
s.SendFriendStatus(social.FriendAlready, guid.Nil, "", 0, 0, 0, 0)
return
}
contact.Friended = false
s.DB().Where("player = ?", s.GUID().Counter()).Where("friend = ?", id.Counter()).Cols("friended").Update(&contact)
s.SendFriendStatus(social.FriendRemoved, id, "", 0, 0, 0, 0)
}
func (s *Session) HandleFriendListRequest() {
s.SendFriendList()
s.SendIgnoreList()
}
func (s *Session) HandleSocialListRequest() {
s.SendSocialList()
}
func (s *Session) BroadcastStatus(status social.Status) {
player := s.GUID().Counter()
var contacts []models.Contact
s.DB().Where("friend = ?", player).Where("friended = 1").Find(&contacts)
for _, contact := range contacts {
id := guid.RealmSpecific(guid.Player, s.Server.RealmID(), contact.Player)
sess, _ := s.Server.GetSessionByGUID(id)
if sess != nil {
var result social.FriendResult
var area, level, class uint32
if status&social.FriendOnline != 0 {
result = social.FriendOnline
area = s.ZoneID
level = uint32(s.GetLevel())
class = uint32(s.GetPlayerClass())
} else {
result = social.FriendOffline
}
sess.SendFriendStatus(result, s.GUID(), contact.Note, status, area, level, class)
}
}
}
func (s *Session) HandleIgnoreAdd(add *social.Add) {
name := add.Name
var char models.Character
found, err := s.DB().Where("name = ?", name).Get(&char)
if err != nil {
panic(err)
}
if !found {
s.SendFriendStatus(social.FriendIgnoreNotFound, guid.Nil, "", 0, 0, 0, 0)
return
}
id := guid.RealmSpecific(guid.Player, s.Server.RealmID(), char.ID)
if id == s.GUID() {
s.SendFriendStatus(social.FriendIgnoreSelf, guid.Nil, "", 0, 0, 0, 0)
return
}
contact := s.GetContact(id)
if contact.Ignored == true {
s.SendFriendStatus(social.FriendIgnoreAlready, id, "", 0, 0, 0, 0)
return
}
contact.Ignored = true
if _, err := s.DB().Where("player = ?", s.GUID().Counter()).Where("friend = ?", id.Counter()).Cols("ignored").Update(contact); err != nil {
panic(err)
}
s.SendFriendStatus(social.FriendIgnoreAdded, id, "", 0, 0, 0, 0)
}
func (s *Session) HandleIgnoreDelete(del *social.Delete) {
id := del.ID
if id == s.GUID() {
s.SendFriendStatus(social.FriendIgnoreSelf, guid.Nil, "", 0, 0, 0, 0)
return
}
var contact models.Contact
found, err := s.DB().Where("player = ?", s.GUID().Counter()).Where("friend = ?", id.Counter()).Get(&contact)
if err != nil {
panic(err)
}
if !found {
s.SendFriendStatus(social.FriendIgnoreNotFound, id, "", 0, 0, 0, 0)
return
}
if contact.Ignored == false {
s.SendFriendStatus(social.FriendIgnoreAlready, id, "", 0, 0, 0, 0)
return
}
contact.Ignored = false
s.DB().Where("player = ?", s.GUID().Counter()).Where("friend = ?", id.Counter()).Cols("ignored").Update(&contact)
s.SendFriendStatus(social.FriendIgnoreRemoved, id, "", 0, 0, 0, 0)
}