Mercurial > wow > breuesk
view Comm.lua @ 91:5ade79caeece
Starting to add a global timestamp to allow for incremental updates.
author | John@Yosemite-PC |
---|---|
date | Mon, 16 Apr 2012 07:06:50 -0400 |
parents | 22b37c800bc4 |
children | a227f3b61cda |
line wrap: on
line source
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"..commversion,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 table.insert(db.profile.changes,change) ProcessChange(change) changeListener:DataEvent() end, ["Push"] = function(self) Send("PU",{db.profile.lists,db.profile.persons,db.profile.changes,db.profile.time}) end, ["PU"] = function(self,packet,sender,isloop) if isloop then return end db.profile.lists,db.profile.persons,db.profile.changes,db.profile.time = unpack(packet) CreateWorkingStateFromChanges(db.profile.changes) if changeListener then changeListener:DataEvent() end end, ["TS"] = function(self,packet,sender,isloop) if isloop then return end if masterLooterIsMe and admin then -- only non-admins will send this message, send them the present -- working state -- todo: if they send a timestamp that's somewhere along our -- timeline, then just catch them up local dbPersons = {toons=PersonList.toons,time=PersonList.time} Send("CU",{dbPersons,LootLists.l,timestamp}) -- todo: send privately end end, ["CU"] = function(self,packet,sender,isloop) -- blindly trust an admin loot master if isloop then return end print("CU") db.profile.persons,db.profile.lists,db.profile.time = unpack(packet) db.profile.changes = {} CreateWorkingStateFromChanges(db.profile.changes) if changeListener then changeListener:DataEvent() end end, ["RequestCatchup"] = function(self) if not admin then Send("TS", timestamp) -- todo: send privately to loot master else -- todo: admins talking to one another 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 alertlist = {} local function OnOlderCommReceived(prefix, message, distribution, sender) if not alertlist[sender] then printf("Received communication from %s, who is using an older version of the addon; ignoring",sender) alertlist[sender]=true end end local function OnNewerCommReceived(prefix, message, distribution, sender) if not alertlist[sender] then printf("Received communication from %s, who is using a newer version of the addon; ignoring",sender) alertlist[sender]=true end end function InitializeComm() for i = 0,commversion-1 do commlib:RegisterComm("BSKADDON"..i,OnOlderCommReceived) end commlib:RegisterComm("BSKADDON"..commversion,OnCommReceived) for i = commversion+1,commversion+5 do -- some sensible number commlib:RegisterComm("BSKADDON"..i,OnNewerCommReceived) end end function DeinitializeComm() end