changeset 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 dcbe1f04bb31
children a227f3b61cda
files Comm.lua Core.lua Lists.lua
diffstat 3 files changed, 48 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Comm.lua	Mon Apr 16 07:06:27 2012 -0400
+++ b/Comm.lua	Mon Apr 16 07:06:50 2012 -0400
@@ -64,20 +64,54 @@
     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})
+        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 = unpack(packet)
+        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)
--- a/Core.lua	Mon Apr 16 07:06:27 2012 -0400
+++ b/Core.lua	Mon Apr 16 07:06:50 2012 -0400
@@ -18,6 +18,7 @@
 -- ( ) crypto / protection against tampering
 -- ( ) alt tracking
 -- (*) reserves
+-- ( ) comparison vs currently equipped item
 
 local _G=_G
 local strsplit=strsplit
@@ -209,6 +210,7 @@
 
 defaults = {
     profile = {
+        time = 0,
         persons = {},
         changes = {},
         lists = {}
--- a/Lists.lua	Mon Apr 16 07:06:27 2012 -0400
+++ b/Lists.lua	Mon Apr 16 07:06:50 2012 -0400
@@ -85,6 +85,7 @@
 setfenv(1,bsk)
 
 changeListener = nil -- todo: really should not be scoped like this
+timestamp = 0
 
 ListEntry = 
 {
@@ -441,7 +442,7 @@
     end
 end
 function PersonList:SaveToDB(db)
-    db.profile.persons = { toons=self.toons, time=self.time }
+    db.profile.persons = { toons=self.toons, time=self.time } -- if this changes, also check the comm functions that send persons
 end
 function PersonList:Reset()
     self.toons = {}
@@ -637,9 +638,11 @@
     local action = change.action
     if PersonList[action] then
         PersonList[action](PersonList,change.arg,change.time)
+        timestamp = change.time
         return
     elseif LootLists[action] then
         LootLists[action](LootLists,change.arg,change.time)
+        timestamp = change.time
         return
     else
         -- pray that the change has a listIndex in it ...
@@ -647,6 +650,7 @@
             local l = LootLists:Select(change.arg.listIndex)
             if l and l[action] then
                 l[action](l,change.arg,change.time)
+                timestamp = change.time
                 return
             end
         end
@@ -657,6 +661,7 @@
 function SelfDestruct()
     LootLists:Reset()
     PersonList:Reset()
+    db.profile.time = 0
     db.profile.persons = {}
     db.profile.changes = {}
     db.profile.lists = {}
@@ -706,6 +711,7 @@
     -- copy the base to the working state
     LootLists:ConstructFromDB(db)
     PersonList:ConstructFromDB(db)
+    timestamp = db.profile.time
 
     -- now just go through the changes list applying each
     for i,v in ipairs(changes) do
@@ -834,9 +840,11 @@
 
     -- bisect the changes list by "time"
     local before = {}
+    local lastTime = 0
     for i,v in ipairs(db.profile.changes) do
         if v.time <= time then
             tinsert(before,v)
+            lastTime = v.time
         else
             break
         end
@@ -851,6 +859,7 @@
     while db.profile.changes ~= nil and db.profile.changes[1] ~= nil and db.profile.changes[1].time <= time do
         table.remove(db.profile.changes,1)
     end
+    db.profile.time = lastTime
 
     -- using the trimmed list and the new bases, recreate the working state
     CreateWorkingStateFromChanges(db.profile.changes)