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@98
|
36 if _G.next(adminList) ~= nil then -- had old admins. don't want to spam on initial load
|
John@98
|
37 -- diff new vs old
|
John@98
|
38 for a in pairs(adminList) do
|
John@98
|
39 if not newAdminList[a] then
|
John@98
|
40 print("Admin removed:", a)
|
John@98
|
41 end
|
John@98
|
42 end
|
John@98
|
43 for a in pairs(newAdminList) do
|
John@98
|
44 if not adminList[a] then
|
John@98
|
45 print("Admin added:",a)
|
John@98
|
46 end
|
John@98
|
47 end
|
John@98
|
48 end
|
John@98
|
49 adminList = newAdminList
|
John@99
|
50
|
John@99
|
51 if adminList[_G.UnitName("player")] then -- I'm an admin!
|
John@99
|
52 admin = true
|
John@99
|
53 bsk.db.profile.admin = true
|
John@99
|
54 RegisterAdminChannels()
|
John@99
|
55 else
|
John@99
|
56 admin = false
|
John@99
|
57 bsk.db.profile.admin = false
|
John@99
|
58 UnregisterAdminChannels()
|
John@99
|
59 end
|
John@99
|
60 end
|
John@99
|
61
|
John@99
|
62 function RemoteAdminUpdateReceived(sender,remoteAdminStatusTable)
|
John@99
|
63 if not admin then return end
|
John@99
|
64
|
John@99
|
65 local rs = remoteAdminStatusTable
|
John@99
|
66 for i,_ in pairs(adminList) do -- update each admin's entry in your own DB
|
John@99
|
67
|
John@99
|
68 -- grab the db copy and the incoming copy for that admin
|
John@99
|
69 local dbs = db.profile.adminStatus[i] or {}
|
John@99
|
70 local ics = packet[i] or {}
|
John@99
|
71
|
John@99
|
72 -- figure out which is better and keep that one
|
John@99
|
73 -- winning criteria:
|
John@99
|
74 -- * broadcast was actually from that person (ie best
|
John@99
|
75 -- verification possible)
|
John@99
|
76 -- * newer base
|
John@99
|
77 -- * same base, more entries
|
John@99
|
78 -- * todo: see if date last observed is a better option
|
John@99
|
79
|
John@99
|
80 if i==sender then
|
John@99
|
81 db.profile.adminStatus[i] = ics
|
John@99
|
82 elseif ics.base > dbs.base or (ics.base==dbs.base and getn(ics.changes) > getn(dbs.changes)) then
|
John@99
|
83 db.profile.adminStatus[i] = ics
|
John@99
|
84 end
|
John@99
|
85 end
|
John@98
|
86 end
|
John@98
|
87
|
John@98
|
88 function InitializeAdmin()
|
John@98
|
89 if not event then
|
John@98
|
90 _G.error("BSK: Improper order of initialization")
|
John@98
|
91 end
|
John@99
|
92
|
John@99
|
93 if admin then -- if at last login I was an admin ...
|
John@99
|
94
|
John@99
|
95 -- note that we're not transmitting anything here. we'll do that once we
|
John@99
|
96 -- know for certain we're an admin
|
John@99
|
97
|
John@99
|
98 -- cache the onload status in case it changes in memory later
|
John@99
|
99 onloadAdminStatus = {}
|
John@99
|
100 tcopy(onloadAdminStatus,db.profile.adminStatus)
|
John@99
|
101
|
John@99
|
102 -- update our own entry - safe because comms shouldn't have happened yet
|
John@99
|
103 me = _G.UnitName("player")
|
John@99
|
104 if not onloadAdminStatus then onloadAdminStatus = {} end
|
John@99
|
105 if not onloadAdminStatus[me] then onloadAdminStatus[me] = {} end
|
John@99
|
106 onloadAdminStatus[me].base = db.profile.time or 0
|
John@99
|
107 onloadAdminStatus[me].changes= {}
|
John@99
|
108 for _,v in ipairs(db.profile.changes) do
|
John@99
|
109 table.insert(onloadAdminStatus[me].changes,v.time) -- only timestamps
|
John@99
|
110 end
|
John@99
|
111
|
John@99
|
112 else -- otherwise store a blank slate
|
John@99
|
113 onloadAdminStatus = {}
|
John@99
|
114 db.profile.adminStatus = nil
|
John@99
|
115 end
|
John@99
|
116
|
John@99
|
117
|
John@98
|
118 event:RegisterEvent("GUILD_ROSTER_UPDATE",GuildRosterUpdate)
|
John@98
|
119 _G.GuildRoster() -- will eventually force the event to fire
|
John@98
|
120 end
|
John@98
|
121
|
John@98
|
122
|