-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFriendsWithBenefits.lua
200 lines (152 loc) · 4.9 KB
/
FriendsWithBenefits.lua
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
local myname, ns = ...
local db, currop, currfriend
local friendlist = {}
function ns.OnLoad()
ns.Debug("Loading DB")
local factionrealm = UnitFactionGroup("player").. " - "..GetRealmName()
FriendsWithBenefitsDB = FriendsWithBenefitsDB or {}
FriendsWithBenefitsDB[factionrealm] = FriendsWithBenefitsDB[factionrealm] or {}
db = FriendsWithBenefitsDB[factionrealm]
db.friends, db.removed, db.notes = db.friends or {}, db.removed or {}, db.notes or {}
if not db.removed[string.lower(UnitName("player"))] then db.friends[string.lower(UnitName("player"))] = true end
end
function ns.OnLogin()
ns.RegisterEvent("CHAT_MSG_SYSTEM")
ns.RegisterEvent("FRIENDLIST_UPDATE")
ShowFriends()
end
local origAddFriend = AddFriend
AddFriend = function(name, ...)
ns.Debug("Function AddFriend", name, ...)
currop, currfriend = "ADD", string.lower(name)
return origAddFriend(name, ...)
end
local origRemoveFriend = RemoveFriend
RemoveFriend = function(i, ...)
local name = type(i) == "number" and GetFriendInfo(i) or i
ns.Debug("Function RemoveFriend", name, i, ...)
currop, currfriend = "REM", string.lower(name)
return origRemoveFriend(i, ...)
end
local origSetFriendNotes = SetFriendNotes
SetFriendNotes = function(i, note, ...)
ns.Debug("Function SetFriendNotes", i, note, ...)
local name = type(i) == "number" and GetFriendInfo(i) or i
db.notes[string.lower(name)] = note
return origSetFriendNotes(i, note, ...)
end
local function FinalizeAdd()
if not GetFriendInfo(currfriend) then return end
ns.Debug("Friend added", currfriend)
db.friends[currfriend] = true
db.removed[currfriend] = nil
friendlist[currfriend] = true
currop, currfriend = nil
if ns.LoginSync then ns.LoginSync() end
end
local function FinalizeRemove()
if GetFriendInfo(currfriend) then return end
ns.Debug("Friend removed", currfriend)
db.removed[currfriend] = true
db.friends[currfriend] = nil
friendlist[currfriend] = nil
db.notes[currfriend] = nil
currop, currfriend = nil
if ns.LoginSync then ns.LoginSync() end
end
local chat_errors = {
[ERR_FRIEND_NOT_FOUND] = true, -- "Player not found."
[ERR_FRIEND_WRONG_FACTION] = true, -- "Friends must be part of your alliance."
}
function ns.CHAT_MSG_SYSTEM(event, text)
if not chat_errors[text] then return end
ns.Debug("Processing chat error", text, currop, currfriend)
if text == ERR_FRIEND_ERROR then return ns.Abort("An error has occured.") end
if currop == 'REM' then return ns.Abort("Unexpected server response.") end
if text == ERR_FRIEND_NOT_FOUND then
db.removed[currfriend] = true
db.friends[currfriend] = nil
ns.Printf("Cannot find player %q on this realm.", currfriend)
currfriend, currop = nil
elseif text == ERR_FRIEND_WRONG_FACTION then
db.removed[currfriend] = true
db.friends[currfriend] = nil
ns.Printf("Player %q is the wrong faction.", currfriend)
currfriend, currop = nil
end
end
function ns.FRIENDLIST_UPDATE(event)
if currop == "ADD" then return FinalizeAdd()
elseif currop == "REM" then return FinalizeRemove() end
if ns.LoginSync then ns.LoginSync() end
end
local hasannounced
local function AnnounceOnce()
ns.Print("Updating friend list. Please do not add or remove friends until complete.")
hasannounced = true
AnnounceOnce = function() end
end
function ns.LoginSync()
if not hasannounced then ns.Debug("First sync FRIENDLIST_UPDATE") end
if ns.LoginSyncRemote then
local name = ns.LoginSyncRemote()
if name then
AnnounceOnce()
ns.Debug("Removing friend due to sync", name)
return RemoveFriend(name)
end
end
if ns.LoginSyncLocal then
local name = ns.LoginSyncLocal()
if name then
AnnounceOnce()
ns.Debug("Adding friend due to sync", name)
return AddFriend(name)
end
end
if hasannounced then ns.Print("Update completed.") end
ns.Debug("Login sync complete")
ns.LoginSync = nil
end
function ns.LoginSyncRemote()
for i=1,GetNumFriends() do
local name, _, _, _, _, _, note = GetFriendInfo(i)
if not name then
return ns.Abort("Server returned invalid friend data")
else
name = string.lower(name)
friendlist[name] = note or ""
if db.removed[name] then
return name
else db.friends[name] = true end
end
end
ns.LoginSyncRemote = nil
end
function ns.LoginSyncLocal()
for name in pairs(db.friends) do
if not friendlist[name] and string.lower(UnitName("player")) ~= name then
return name
end
end
for i=1,GetNumFriends() do
local name, _, _, _, _, _, note = GetFriendInfo(i)
if not name then
return ns.Abort("Server returned invalid friend data")
else
name = string.lower(name)
if db.notes[name] and db.notes[name] ~= note then
SetFriendNotes(name, db.notes[name])
elseif note ~= "" then
db.notes[name] = note
end
end
end
ns.LoginSyncLocal = nil
end
function ns.Abort(msg)
ns.UnregisterAllEvents()
ns.LoginSync, ns.LoginSyncRemote, ns.LoginSyncLocal = nil
ns.FRIENDLIST_UPDATE, ns.CHAT_MSG_SYSTEM, ns.Abort = nil
ns.Print(msg, "Disabling for the rest of this session.")
end