John@98: local bsk=bsk John@98: local _G=_G John@98: local table=table John@98: local pairs=pairs John@98: local setmetatable=setmetatable John@98: local ipairs=ipairs John@98: local string=string John@98: local sformat=string.format John@98: local strsplit=strsplit John@98: local tostring=tostring John@98: local type=type John@98: local unpack=unpack John@98: local getn=getn John@98: setfenv(1,bsk) John@98: John@98: adminList = {} John@98: John@98: local function GuildRosterUpdate() John@98: local guildInfoText = _G.GetGuildInfoText() John@98: local newAdminList = {} John@98: for line in guildInfoText:gmatch("[^\r\n]+") do John@98: local l,r = line:match("(.*):(.*)") -- could use wow strsplit had I known about it before writing this John@98: l = string.trim(l or "") John@98: r = string.trim(r or "") John@99: if l == "BSKADMIN" then -- found a juicy line. may contain multiple, comma or space delimited John@98: local admins = {strsplit(", ",r)} John@98: for _,a in pairs(admins) do John@98: a = string.trim(a or "") John@98: if a ~= "" then John@98: newAdminList[a] = true John@98: end John@98: end John@98: end John@98: end John@98: John@103: if me == "Breuemama" then -- todo: strictly debugging ... John@103: newAdminList[me] = true John@103: newAdminList["Breue"] = true John@103: end John@103: John@98: if _G.next(adminList) ~= nil then -- had old admins. don't want to spam on initial load John@98: -- diff new vs old John@98: for a in pairs(adminList) do John@98: if not newAdminList[a] then John@98: print("Admin removed:", a) John@98: end John@98: end John@98: for a in pairs(newAdminList) do John@98: if not adminList[a] then John@98: print("Admin added:",a) John@98: end John@98: end John@98: end John@98: adminList = newAdminList John@99: John@103: if adminList[me] then -- I'm an admin! John@99: admin = true John@99: bsk.db.profile.admin = true John@99: RegisterAdminChannels() John@99: else John@99: admin = false John@99: bsk.db.profile.admin = false John@99: UnregisterAdminChannels() John@99: end John@99: end John@99: John@99: function RemoteAdminUpdateReceived(sender,remoteAdminStatusTable) John@99: if not admin then return end John@99: John@99: local rs = remoteAdminStatusTable John@103: local events = {} -- record all timestamps seen in this update John@99: for i,_ in pairs(adminList) do -- update each admin's entry in your own DB John@99: John@99: -- grab the db copy and the incoming copy for that admin John@103: if not db.profile.adminStatus then db.profile.adminStatus = {} end John@103: local dbs = db.profile.adminStatus[i] or {base=0, changes={}} John@103: local ics = rs[i] or {base=0, changes={}} John@99: John@99: -- figure out which is better and keep that one John@99: -- winning criteria: John@99: -- * broadcast was actually from that person (ie best John@99: -- verification possible) John@99: -- * newer base John@99: -- * same base, more entries John@99: -- * todo: see if date last observed is a better option John@99: John@99: if i==sender then John@99: db.profile.adminStatus[i] = ics John@99: elseif ics.base > dbs.base or (ics.base==dbs.base and getn(ics.changes) > getn(dbs.changes)) then John@99: db.profile.adminStatus[i] = ics John@99: end John@99: end John@103: John@103: local rss = rs[sender] John@103: John@103: -- now figure out what I'm missing - and ask for it! John@103: John@103: -- construct a hash table of all entries that the sender has / should have John@103: local entries = {} John@103: for i,v in pairs(rs) do John@103: if v.changes then John@103: for j,k in pairs(v.changes) do John@103: entries[k.time] = true John@103: end John@103: end John@103: end John@103: -- now go back and scrub my own keys from that list John@103: for i,v in ipairs(db.profile.changes) do John@103: entries[v.time] = nil John@103: end John@103: -- what's left is what I need to ask for John@103: local request = {} John@103: for i,v in pairs(entries) do John@103: if v then table.insert(request,i) end John@103: end John@103: table.sort(request) John@103: Comm:RequestSpecificChanges(request,sender) John@103: John@103: for John@103: -- specifically leaving this broken. note to self. John@103: -- this still isn't good enough. it doesn't communicate an admin's John@103: -- present working state. like if they had put in new changes since John@103: -- loading up. or learned of some changes to fill in an old gap John@98: end John@98: John@98: function InitializeAdmin() John@98: if not event then John@98: _G.error("BSK: Improper order of initialization") John@98: end John@103: me = _G.UnitName("player") John@99: John@99: if admin then -- if at last login I was an admin ... John@99: John@99: -- note that we're not transmitting anything here. we'll do that once we John@99: -- know for certain we're an admin John@99: John@99: -- cache the onload status in case it changes in memory later John@99: onloadAdminStatus = {} John@99: tcopy(onloadAdminStatus,db.profile.adminStatus) John@99: John@99: -- update our own entry - safe because comms shouldn't have happened yet John@99: if not onloadAdminStatus then onloadAdminStatus = {} end John@99: if not onloadAdminStatus[me] then onloadAdminStatus[me] = {} end John@99: onloadAdminStatus[me].base = db.profile.time or 0 John@99: onloadAdminStatus[me].changes= {} John@99: for _,v in ipairs(db.profile.changes) do John@99: table.insert(onloadAdminStatus[me].changes,v.time) -- only timestamps John@99: end John@99: John@99: else -- otherwise store a blank slate John@99: onloadAdminStatus = {} John@99: db.profile.adminStatus = nil John@99: end John@99: John@98: event:RegisterEvent("GUILD_ROSTER_UPDATE",GuildRosterUpdate) John@98: _G.GuildRoster() -- will eventually force the event to fire John@103: John@103: if me == "Breuemama" then -- debugging only John@103: GuildRosterUpdate() John@103: end John@98: end John@98: John@98: