diff Comm.lua @ 99:5914125bb4ea

Admin comms (partially implemented)
author John@Yosemite-PC
date Sun, 29 Apr 2012 15:54:32 -0400
parents 19fd02bff870
children 21567b74fcc7
line wrap: on
line diff
--- 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
+