John@98
|
1 local bsk=bsk
|
John@98
|
2 local _G=_G
|
John@98
|
3 local table=table
|
John@98
|
4 local pairs=pairs
|
John@98
|
5 local setmetatable=setmetatable
|
John@98
|
6 local ipairs=ipairs
|
John@98
|
7 local string=string
|
John@98
|
8 local sformat=string.format
|
John@98
|
9 local strsplit=strsplit
|
John@98
|
10 local tostring=tostring
|
John@98
|
11 local type=type
|
John@98
|
12 local unpack=unpack
|
John@98
|
13 local getn=getn
|
John@98
|
14 setfenv(1,bsk)
|
John@98
|
15
|
John@98
|
16 adminList = {}
|
John@98
|
17
|
John@98
|
18 local function GuildRosterUpdate()
|
John@98
|
19 local guildInfoText = _G.GetGuildInfoText()
|
John@98
|
20 local newAdminList = {}
|
John@98
|
21 for line in guildInfoText:gmatch("[^\r\n]+") do
|
John@98
|
22 local l,r = line:match("(.*):(.*)") -- could use wow strsplit had I known about it before writing this
|
John@98
|
23 l = string.trim(l or "")
|
John@98
|
24 r = string.trim(r or "")
|
John@99
|
25 if l == "BSKADMIN" then -- found a juicy line. may contain multiple, comma or space delimited
|
John@98
|
26 local admins = {strsplit(", ",r)}
|
John@98
|
27 for _,a in pairs(admins) do
|
John@98
|
28 a = string.trim(a or "")
|
John@98
|
29 if a ~= "" then
|
John@98
|
30 newAdminList[a] = true
|
John@98
|
31 end
|
John@98
|
32 end
|
John@98
|
33 end
|
John@98
|
34 end
|
John@98
|
35
|
John@103
|
36 if me == "Breuemama" then -- todo: strictly debugging ...
|
John@103
|
37 newAdminList[me] = true
|
John@103
|
38 newAdminList["Breue"] = true
|
John@103
|
39 end
|
John@103
|
40
|
John@98
|
41 if _G.next(adminList) ~= nil then -- had old admins. don't want to spam on initial load
|
John@98
|
42 -- diff new vs old
|
John@98
|
43 for a in pairs(adminList) do
|
John@98
|
44 if not newAdminList[a] then
|
John@98
|
45 print("Admin removed:", a)
|
John@98
|
46 end
|
John@98
|
47 end
|
John@98
|
48 for a in pairs(newAdminList) do
|
John@98
|
49 if not adminList[a] then
|
John@98
|
50 print("Admin added:",a)
|
John@98
|
51 end
|
John@98
|
52 end
|
John@98
|
53 end
|
John@98
|
54 adminList = newAdminList
|
John@99
|
55
|
John@103
|
56 if adminList[me] then -- I'm an admin!
|
John@99
|
57 admin = true
|
John@99
|
58 bsk.db.profile.admin = true
|
John@99
|
59 RegisterAdminChannels()
|
John@99
|
60 else
|
John@99
|
61 admin = false
|
John@99
|
62 bsk.db.profile.admin = false
|
John@99
|
63 UnregisterAdminChannels()
|
John@99
|
64 end
|
John@99
|
65 end
|
John@99
|
66
|
John@99
|
67 function RemoteAdminUpdateReceived(sender,remoteAdminStatusTable)
|
John@99
|
68 if not admin then return end
|
John@99
|
69
|
John@99
|
70 local rs = remoteAdminStatusTable
|
John@103
|
71 local events = {} -- record all timestamps seen in this update
|
John@99
|
72 for i,_ in pairs(adminList) do -- update each admin's entry in your own DB
|
John@99
|
73
|
John@99
|
74 -- grab the db copy and the incoming copy for that admin
|
John@103
|
75 if not db.profile.adminStatus then db.profile.adminStatus = {} end
|
John@103
|
76 local dbs = db.profile.adminStatus[i] or {base=0, changes={}}
|
John@103
|
77 local ics = rs[i] or {base=0, changes={}}
|
John@99
|
78
|
John@99
|
79 -- figure out which is better and keep that one
|
John@99
|
80 -- winning criteria:
|
John@99
|
81 -- * broadcast was actually from that person (ie best
|
John@99
|
82 -- verification possible)
|
John@99
|
83 -- * newer base
|
John@99
|
84 -- * same base, more entries
|
John@99
|
85 -- * todo: see if date last observed is a better option
|
John@99
|
86
|
John@99
|
87 if i==sender then
|
John@99
|
88 db.profile.adminStatus[i] = ics
|
John@99
|
89 elseif ics.base > dbs.base or (ics.base==dbs.base and getn(ics.changes) > getn(dbs.changes)) then
|
John@99
|
90 db.profile.adminStatus[i] = ics
|
John@99
|
91 end
|
John@99
|
92 end
|
John@103
|
93
|
John@103
|
94 local rss = rs[sender]
|
John@103
|
95
|
John@103
|
96 -- now figure out what I'm missing - and ask for it!
|
John@103
|
97
|
John@103
|
98 -- construct a hash table of all entries that the sender has / should have
|
John@103
|
99 local entries = {}
|
John@103
|
100 for i,v in pairs(rs) do
|
John@103
|
101 if v.changes then
|
John@103
|
102 for j,k in pairs(v.changes) do
|
John@103
|
103 entries[k.time] = true
|
John@103
|
104 end
|
John@103
|
105 end
|
John@103
|
106 end
|
John@103
|
107 -- now go back and scrub my own keys from that list
|
John@103
|
108 for i,v in ipairs(db.profile.changes) do
|
John@103
|
109 entries[v.time] = nil
|
John@103
|
110 end
|
John@103
|
111 -- what's left is what I need to ask for
|
John@103
|
112 local request = {}
|
John@103
|
113 for i,v in pairs(entries) do
|
John@103
|
114 if v then table.insert(request,i) end
|
John@103
|
115 end
|
John@103
|
116 table.sort(request)
|
John@103
|
117 Comm:RequestSpecificChanges(request,sender)
|
John@103
|
118
|
John@103
|
119 for
|
John@103
|
120 -- specifically leaving this broken. note to self.
|
John@103
|
121 -- this still isn't good enough. it doesn't communicate an admin's
|
John@103
|
122 -- present working state. like if they had put in new changes since
|
John@103
|
123 -- loading up. or learned of some changes to fill in an old gap
|
John@98
|
124 end
|
John@98
|
125
|
John@98
|
126 function InitializeAdmin()
|
John@98
|
127 if not event then
|
John@98
|
128 _G.error("BSK: Improper order of initialization")
|
John@98
|
129 end
|
John@103
|
130 me = _G.UnitName("player")
|
John@99
|
131
|
John@99
|
132 if admin then -- if at last login I was an admin ...
|
John@99
|
133
|
John@99
|
134 -- note that we're not transmitting anything here. we'll do that once we
|
John@99
|
135 -- know for certain we're an admin
|
John@99
|
136
|
John@99
|
137 -- cache the onload status in case it changes in memory later
|
John@99
|
138 onloadAdminStatus = {}
|
John@99
|
139 tcopy(onloadAdminStatus,db.profile.adminStatus)
|
John@99
|
140
|
John@99
|
141 -- update our own entry - safe because comms shouldn't have happened yet
|
John@99
|
142 if not onloadAdminStatus then onloadAdminStatus = {} end
|
John@99
|
143 if not onloadAdminStatus[me] then onloadAdminStatus[me] = {} end
|
John@99
|
144 onloadAdminStatus[me].base = db.profile.time or 0
|
John@99
|
145 onloadAdminStatus[me].changes= {}
|
John@99
|
146 for _,v in ipairs(db.profile.changes) do
|
John@99
|
147 table.insert(onloadAdminStatus[me].changes,v.time) -- only timestamps
|
John@99
|
148 end
|
John@99
|
149
|
John@99
|
150 else -- otherwise store a blank slate
|
John@99
|
151 onloadAdminStatus = {}
|
John@99
|
152 db.profile.adminStatus = nil
|
John@99
|
153 end
|
John@99
|
154
|
John@98
|
155 event:RegisterEvent("GUILD_ROSTER_UPDATE",GuildRosterUpdate)
|
John@98
|
156 _G.GuildRoster() -- will eventually force the event to fire
|
John@103
|
157
|
John@103
|
158 if me == "Breuemama" then -- debugging only
|
John@103
|
159 GuildRosterUpdate()
|
John@103
|
160 end
|
John@98
|
161 end
|
John@98
|
162
|
John@98
|
163
|