changeset 99:5914125bb4ea

Admin comms (partially implemented)
author John@Yosemite-PC
date Sun, 29 Apr 2012 15:54:32 -0400
parents 0cd1d46e7b66
children 790266dbcaff
files Admin.lua Comm.lua
diffstat 2 files changed, 128 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Admin.lua	Fri Apr 27 09:41:26 2012 -0400
+++ b/Admin.lua	Sun Apr 29 15:54:32 2012 -0400
@@ -22,7 +22,7 @@
         local l,r = line:match("(.*):(.*)") -- could use wow strsplit had I known about it before writing this
         l = string.trim(l or "")
         r = string.trim(r or "")
-        if l == "BSKADMIN" then -- found a juicy line. may contain multiple, comma delimited
+        if l == "BSKADMIN" then -- found a juicy line. may contain multiple, comma or space delimited
             local admins = {strsplit(", ",r)}
             for _,a in pairs(admins) do
                 a = string.trim(a or "")
@@ -47,12 +47,74 @@
         end
     end
     adminList = newAdminList
+
+    if adminList[_G.UnitName("player")] then -- I'm an admin!
+        admin = true
+        bsk.db.profile.admin = true
+        RegisterAdminChannels()
+    else
+        admin = false
+        bsk.db.profile.admin = false
+        UnregisterAdminChannels()
+    end
+end
+
+function RemoteAdminUpdateReceived(sender,remoteAdminStatusTable)
+    if not admin then return end
+
+    local rs = remoteAdminStatusTable
+    for i,_ in pairs(adminList) do -- update each admin's entry in your own DB
+
+        -- grab the db copy and the incoming copy for that admin
+        local dbs = db.profile.adminStatus[i] or {}
+        local ics = packet[i] or {}
+
+        -- figure out which is better and keep that one
+        -- winning criteria:
+        --  * broadcast was actually from that person (ie best
+        --  verification possible)
+        --  * newer base
+        --  * same base, more entries
+        --  * todo: see if date last observed is a better option
+
+        if i==sender then
+            db.profile.adminStatus[i] = ics
+        elseif ics.base > dbs.base or (ics.base==dbs.base and getn(ics.changes) > getn(dbs.changes)) then
+            db.profile.adminStatus[i] = ics
+        end
+    end
 end
 
 function InitializeAdmin()
     if not event then
         _G.error("BSK: Improper order of initialization")
     end
+
+    if admin then -- if at last login I was an admin ...
+
+        -- note that we're not transmitting anything here. we'll do that once we
+        -- know for certain we're an admin
+
+        -- cache the onload status in case it changes in memory later
+        onloadAdminStatus = {}
+        tcopy(onloadAdminStatus,db.profile.adminStatus)
+
+        -- update our own entry - safe because comms shouldn't have happened yet
+        me = _G.UnitName("player")
+        if not onloadAdminStatus then onloadAdminStatus = {} end
+        if not onloadAdminStatus[me] then onloadAdminStatus[me] = {} end
+        onloadAdminStatus[me].base = db.profile.time or 0
+        onloadAdminStatus[me].changes= {}
+        for _,v in ipairs(db.profile.changes) do
+            table.insert(onloadAdminStatus[me].changes,v.time) -- only timestamps
+        end
+
+    else -- otherwise store a blank slate
+        onloadAdminStatus = {}
+        db.profile.adminStatus = nil
+    end
+
+    
     event:RegisterEvent("GUILD_ROSTER_UPDATE",GuildRosterUpdate)
     _G.GuildRoster() -- will eventually force the event to fire
 end
--- a/Comm.lua	Fri Apr 27 09:41:26 2012 -0400
+++ b/Comm.lua	Sun Apr 29 15:54:32 2012 -0400
@@ -27,12 +27,21 @@
     --commlib:SendCommMessage("BSKADDON"..commversion,str,"GUILD")
     commlib:SendCommMessage("BSKADDON"..commversion,str,"RAID")
 end
+local function SendAdminMessage(str)
+    --commlib:SendCommMessage("BSKADMIN"..commversion,str,"GUILD")
+    commlib:SendCommMessage("BSKADMIN"..commversion,str,"RAID")
+end
 
 local function Send(handler,message)
     local p,str = BuildPacket(handler,message)
     SendMessage(str)
 end
 
+local function SendAdmin(handler,message)
+    local p,str = BuildPacket(handler,message)
+    SendAdminMessage(str)
+end
+
 -- todo: ActivateList and AddReserve -> state
 Comm =
 {
@@ -119,11 +128,11 @@
     ["RequestCatchup"] = function(self)
         if not admin then
             --local string = _g.tostring(timestamp)
-            --for i,v in pairs(db.profile.changes) do -- append all change timestamps
+            --for i,v in ipairs(db.profile.changes) do -- append all change timestamps
             --    string = string .. "|" .. _g.tostring(v.time)
             --end
             local t = {db.profile.time}
-            for i,v in pairs(db.profile.changes) do -- append all change timestamps
+            for i,v in ipairs(db.profile.changes) do -- append all change timestamps
                 table.insert(t,v.time)
             end
             Send("TS", t) -- todo: send privately to loot master
@@ -132,6 +141,28 @@
         end
 
     end,
+
+    ["SR"] = function(self,packet,sender,isloop)
+        if isloop then return end
+        if admin then
+            RemoteAdminUpdateReceived(sender,packet)
+        end
+    end,
+
+    ["SA"] = function(self,packet,sender,isloop)
+        print("SA")
+        if isloop then return end
+        if admin then
+            SendAdmin("SR",onloadAdminStatus) -- SR ... prevent infinite loop please
+            RemoteAdminUpdateReceived(sender,packet)
+        end
+    end,
+
+    ["SendAdminStatusTable"] = function(self)
+        if admin and onloadAdminStatus and _G.next(onloadAdminStatus) then
+            SendAdmin("SA",onloadAdminStatus) -- only send onload status, since that's the only data you're guaranteed to have safely safed
+        end
+    end,
 }
 
 local function OnCommReceived(prefix, message, distribution, sender)
@@ -144,6 +175,16 @@
     Comm[packet[1]](Comm,packet[2],sender,isloop)
 end
 
+local function OnAdminCommReceived(prefix, message, distribution, sender)
+    print("Received (admin) on", distribution)
+    local success,packet = s:Deserialize(message)
+    local isloop = _G.UnitName("player") == sender
+
+    print("received",message)
+
+    Comm[packet[1]](Comm,packet[2],sender,isloop)
+end
+
 alertlist = {}
 local function OnOlderCommReceived(prefix, message, distribution, sender)
     if not alertlist[sender] then
@@ -170,5 +211,27 @@
 end
 
 function DeinitializeComm()
+    -- todo
+end
 
+local adminregistered = false
+function RegisterAdminChannels()
+    if not adminregistered then
+        for i = 0,commversion-1 do
+            commlib:RegisterComm("BSKADMIN"..i,OnOlderCommReceived)
+        end
+        commlib:RegisterComm("BSKADMIN"..commversion,OnAdminCommReceived)
+        for i = commversion+1,commversion+5 do -- some sensible number
+            commlib:RegisterComm("BSKADMIN"..i,OnNewerCommReceived)
+        end
+        adminregistered = true
+
+        -- now that we're set up, tell the other admins our status
+        Comm:SendAdminStatusTable()
+    end
 end
+
+function UnregisterAdminChannels()
+    -- todo
+end
+