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@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@99: if adminList[_G.UnitName("player")] 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@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@99: local dbs = db.profile.adminStatus[i] or {} John@99: local ics = packet[i] or {} 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@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@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: me = _G.UnitName("player") 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@99: John@98: event:RegisterEvent("GUILD_ROSTER_UPDATE",GuildRosterUpdate) John@98: _G.GuildRoster() -- will eventually force the event to fire John@98: end John@98: John@98: