changeset 96:082ff877c443

Small refactor
author John@Doomsday
date Mon, 23 Apr 2012 09:11:21 -0400
parents 5df2c9cdb8c8
children 19fd02bff870
files Comm.lua Lists.lua
diffstat 2 files changed, 73 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/Comm.lua	Sun Apr 22 23:18:35 2012 -0400
+++ b/Comm.lua	Mon Apr 23 09:11:21 2012 -0400
@@ -83,8 +83,7 @@
     ["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
+            -- only non-admins will send this message
 
             local t = packet
             local remoteBase = table.remove(t,1)
@@ -100,24 +99,12 @@
             -- were synced up to the release point. So do we trust that and use
             -- this as a rebase opportunity? print an error? just reply that we
             -- don't have anything new to share with them? only send them 
-            --
 
-            if remoteBase == db.profile.time then
-                local j = 1 -- index in foreign list
-                local n = getn(t)
-                local o = {}
-                for i,v in pairs(db.profile.changes) do -- for each timestamp in our list
-                    if t and t[j] < v.time then
-                        table.insert(o,v)
-                    end
-                    while j<n and t[j] <= v.time do j = j+1 end -- advance the foreign pointer past our current entry
-                end -- j>=n ? add because the remote hit end of road. lt? add because it's a missing stamp
-                print("Received request at timebase",remoteBase,"and returning:")
-                PrintTable(o)
-                if getn(o) > 0 then
-                    Send("CU",o) -- todo: send privately to the requster
-                end
-            else
+            local success, o = CreateChangeDiff(remoteBase,t)
+            if success and getn(o) > 0 then
+                Send("CU",o)
+            end
+            if not success then -- push
                 print("Received request at differing timebase",remoteBase,db.profile.time," ... pushing")
                 self:Push() -- todo: send privately to requester
             end
@@ -126,46 +113,7 @@
 
     ["CU"] = function(self,packet,sender,isloop) -- blindly trust an admin loot master
         if isloop then return end
-
-        local c = packet
-        local old = db.profile.changes
-
-        local new = {}
-
-        local op = 1
-        local cp = 1
-
-        local no = getn(old)
-        local nc = getn(c)
-
-        if no == 0 then
-            db.profile.changes = c
-        else
-            while op <= no or cp <= nc do -- lists are pre-sorted. insertion merge them
-                if cp > nc then -- inelegant
-                    table.insert(new,old[op])
-                    op = op + 1
-                elseif op > no then
-                    table.insert(new,c[cp])
-                    cp = cp + 1
-                elseif c[cp].time < old[op].time then
-                    table.insert(new,c[cp])
-                    cp = cp + 1
-                elseif c[cp].time > old[op].time then
-                    table.insert(new,old[op])
-                    op = op + 1
-                else
-                    error("Bad update received from ",sender)
-                end
-            end
-            print("Updating changes - ",getn(new), "entries")
-            db.profile.changes = new
-        end
-
-        CreateWorkingStateFromChanges(db.profile.changes)
-        if changeListener then
-            changeListener:DataEvent()
-        end
+        IntegrateChangeDiff(packet)
     end,
 
     ["RequestCatchup"] = function(self)
--- a/Lists.lua	Sun Apr 22 23:18:35 2012 -0400
+++ b/Lists.lua	Mon Apr 23 09:11:21 2012 -0400
@@ -921,3 +921,69 @@
     return true
 end
 
+
+function CreateChangeDiff(remoteBase,remoteChanges)
+    local t = remoteChanges
+
+    if remoteBase == db.profile.time then
+        local j = 1 -- index in foreign list
+        local n = getn(t)
+        local o = {}
+        for i,v in pairs(db.profile.changes) do -- for each timestamp in our list
+            if t and t[j] < v.time then
+                table.insert(o,v)
+            end
+            while j<n and t[j] <= v.time do j = j+1 end -- advance the foreign pointer past our current entry
+        end -- j>=n ? add because the remote hit end of road. lt? add because it's a missing stamp
+        print("Received request at timebase",remoteBase,"and returning:")
+        PrintTable(o)
+        if getn(o) > 0 then
+            Send("CU",o) -- todo: send privately to the requster
+        end
+        return true, o
+    else
+        return false, {}
+    end
+end
+
+function IntegrateChangeDiff(remoteChanges) -- todo: check against remoteBase before committing to insanity
+    local c = remoteChanges
+    local old = db.profile.changes
+
+    local new = {}
+
+    local op = 1
+    local cp = 1
+
+    local no = getn(old)
+    local nc = getn(c)
+
+    if no == 0 then
+        db.profile.changes = c
+    else
+        while op <= no or cp <= nc do -- lists are pre-sorted. insertion merge them
+            if cp > nc then -- inelegant
+                table.insert(new,old[op])
+                op = op + 1
+            elseif op > no then
+                table.insert(new,c[cp])
+                cp = cp + 1
+            elseif c[cp].time < old[op].time then
+                table.insert(new,c[cp])
+                cp = cp + 1
+            elseif c[cp].time > old[op].time then
+                table.insert(new,old[op])
+                op = op + 1
+            else
+                error("Bad update received from ",sender)
+            end
+        end
+        print("Updating changes - ",getn(new), "entries")
+        db.profile.changes = new
+    end
+
+    CreateWorkingStateFromChanges(db.profile.changes)
+    if changeListener then
+        changeListener:DataEvent()
+    end
+end