diff Lists.lua @ 105:c6c748a5823b tip

Collaborative list trimming 80% functional Updates for the zone-in problem
author John@Yosemite-PC
date Sun, 06 May 2012 08:33:34 -0400
parents 25c127c4c1e8
children
line wrap: on
line diff
--- a/Lists.lua	Sun May 06 08:30:15 2012 -0400
+++ b/Lists.lua	Sun May 06 08:33:34 2012 -0400
@@ -8,7 +8,6 @@
 
 
 -- TODO: switch all action functions to use identifiers rather than names
--- TODO: collaborative list trimming
 -- TODO: collapse slists into delimited strings for space (premature optimization?)
 -- TODO: organize working state data a little more carefully - hard to keep
 -- track of all the arrays that are floating out there
@@ -850,19 +849,31 @@
         end
     end
 
-    -- apply first half
+    print("before -",#before)
+    --return
+
+    --apply first half
     CreateWorkingStateFromChanges(before)
 
     -- save this state permanently; trim the changes permanently
     LootLists:SaveToDB(db)
     PersonList:SaveToDB(db)
     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)
+       table.remove(db.profile.changes,1)
     end
+    print("reapplying ",_G.getn(db.profile.changes))
     db.profile.time = lastTime
 
     -- using the trimmed list and the new bases, recreate the working state
-    CreateWorkingStateFromChanges(db.profile.changes)
+    --CreateWorkingStateFromChanges(db.profile.changes)
+
+    for i,v in ipairs(db.profile.changes) do
+        ProcessChange(v)
+    end
+
+    if changelistener then
+       changelistener:dataevent()
+    end
 end
 
 
@@ -943,7 +954,7 @@
     end
 end
 
-function IntegrateChangeDiff(remoteChanges) -- todo: check against remoteBase before committing to insanity
+function IntegrateChangeDiff(remoteBase,remoteChanges)
     local c = remoteChanges
     local old = db.profile.changes
 
@@ -953,9 +964,20 @@
     local cp = 1
 
     local no = getn(old)
+
+    local worthRefresh = false -- only worth refreshing if we take changes from c
+
+    -- if remotebase is older, don't integrate anything prior to db.profile.time. if they're newer, it's ok to merge in their changes
+    if remoteBase < db.profile.time then
+        while c[1] and c[1].time < db.profile.time do
+            table.remove(c,1)
+        end
+    end
+
     local nc = getn(c)
 
-    if no == 0 then
+    if no == 0 and nc > 0 then
+        worthRefresh = true
         db.profile.changes = c
     else
         while op <= no or cp <= nc do -- lists are pre-sorted. insertion merge them
@@ -963,9 +985,17 @@
                 table.insert(new,old[op])
                 op = op + 1
             elseif op > no then
+                worthRefresh=true
                 table.insert(new,c[cp])
                 cp = cp + 1
+            elseif c[cp].time == old[op].time then
+                -- todo: even though they're the same timestamp, still compare
+                -- contents for sanity
+                table.insert(new,old[cp])
+                cp = cp + 1
+                op = op + 1
             elseif c[cp].time < old[op].time then
+                worthRefresh=true
                 table.insert(new,c[cp])
                 cp = cp + 1
             elseif c[cp].time > old[op].time then
@@ -979,8 +1009,10 @@
         db.profile.changes = new
     end
 
-    CreateWorkingStateFromChanges(db.profile.changes)
-    if changeListener then
-        changeListener:DataEvent()
+    if worthRefresh then
+        CreateWorkingStateFromChanges(db.profile.changes)
+        if changelistener then
+            changelistener:dataevent()
+        end
     end
 end