diff Comm.lua @ 72:9e5b0a2368ad

Big progress towards GUI usefulness and communication between clients
author John@Yosemite-PC
date Sat, 07 Apr 2012 13:35:57 -0400
parents
children 22b37c800bc4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Comm.lua	Sat Apr 07 13:35:57 2012 -0400
@@ -0,0 +1,99 @@
+local bsk=bsk
+local _G=_G
+local table=table 
+local pairs=pairs
+local setmetatable=setmetatable
+local ipairs=ipairs
+local unpack=unpack
+local string=string
+local sformat=string.format
+local tostring=tostring
+local type=type
+local getn=getn
+
+local commlib = LibStub("AceComm-3.0") or _G.error("Couldn't load up AceComm")
+local s = LibStub("AceSerializer-3.0") or _G.error("Couldn't load up AceSerializer")
+
+setfenv(1,bsk)
+
+local function BuildPacket(handler,message)
+    local p = {handler,message}
+    local str = s:Serialize({handler,message})
+    print("sending",str)
+    return p,str
+end
+
+local function SendMessage(str)
+    --commlib:SendCommMessage("BSKADDON",str,"GUILD")
+    commlib:SendCommMessage("BSKADDON",str,"RAID")
+end
+
+local function Send(handler,message)
+    local p,str = BuildPacket(handler,message)
+    SendMessage(str)
+end
+
+-- todo: ActivateList and AddReserve -> state
+Comm =
+{
+    ["SS"] = function(self,packet,sender,isloop)
+        print("isloop",isloop)
+        if not isloop then DispatchState(packet) end
+    end,
+    ["SendStateChange"] = function(self,...)
+        local p,str = BuildPacket("SS",{...})
+        DispatchState(p[2])
+        SendMessage(str)
+    end,
+    ["AR"] = function(self,packet,sender,isloop)
+        if isloop then return end
+        PersonList:AddReserve(packet)
+        changeListener:DataEvent()
+    end,
+    ["AddReserve"] = function(self,packet)
+        if changeListener then
+            changeListener:DataEvent(change)
+        end
+        Send("AR",packet)
+    end,
+    ["SendChange"] = function(self,change)
+        if changeListener then
+            changeListener:DataEvent(change)
+        end
+        Send("CC",change)
+    end,
+    ["CC"] = function(self,change,sender,isloop)
+        if isloop then return end
+        ProcessChange(change)
+        changeListener:DataEvent()
+    end,
+    ["Push"] = function(self)
+        Send("PU",{db.profile.lists,db.profile.persons,db.profile.changes})
+    end,
+    ["PU"] = function(self,packet,sender,isloop)
+        if isloop then return end
+        db.profile.lists,db.profile.persons,db.profile.changes = unpack(packet)
+        CreateWorkingStateFromChanges(db.profile.changes)
+        if changeListener then
+            changeListener:DataEvent()
+        end
+    end,
+}
+
+local function OnCommReceived(prefix, message, distribution, sender)
+    print("Received 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
+
+function InitializeComm()
+   commlib:RegisterComm("BSKADDON",OnCommReceived)
+end
+
+function DeinitializeComm()
+
+end