diff Lists.lua @ 5:2ce0f4db1a3e

Changelist trimming
author John@Yosemite-PC
date Mon, 05 Mar 2012 23:11:45 -0500
parents 6c8f9473b22e
children 6d460ff2135c
line wrap: on
line diff
--- a/Lists.lua	Sat Mar 03 10:25:01 2012 -0500
+++ b/Lists.lua	Mon Mar 05 23:11:45 2012 -0500
@@ -8,6 +8,7 @@
 
 
 -- TODO: rename player
+-- TODO: list trimming
 
 -- notes on list storage:
 -- Storing names as I do now is atrocious.
@@ -100,10 +101,9 @@
 
 --}}}
 
-function bsk:CreateWorkingStateFromChanges()
+function bsk:CreateWorkingStateFromChanges(changes)
     local playerBase = self.db.profile.players
     local listBase = self.db.profile.listBase
-    local changes = self.db.profile.changes
 
     -- copy the base to the working state
     wipe(bsk.lists)
@@ -112,7 +112,7 @@
     bsk:tcopy(bsk.players,playerBase)
 
     -- now just go through the changes list applying each
-    for i,v in pairs(changes) do
+    for i,v in ipairs(changes) do
         bsk:ProcessChange(v)
     end
 end
@@ -339,6 +339,42 @@
        bsk:CommitChange(change)
     end
 end
+
+function bsk:TrimLists(time)
+    if not bsk:CheckListCausality() then
+        self:Print("Unable to trim lists due to violated causality")
+
+        return false
+    end
+
+    if type(time) ~= "number" then
+        time = tonumber(time)
+    end
+
+    -- bisect the changes list by "time"
+    local before = {}
+    for i,v in ipairs(self.db.profile.changes) do
+        if v.time <= time then
+            tinsert(before,v)
+        else
+            break
+        end
+    end
+
+    -- apply first half
+    bsk:CreateWorkingStateFromChanges(before)
+
+    -- save this state permanently; trim the changes permanently
+    bsk:tcopy(bsk.db.profile.players,bsk.players)
+    bsk:tcopy(bsk.db.profile.listBase,bsk.lists)
+    while bsk.db.profile.changes[1].time <= time do
+        table.remove(bsk.db.profile.changes,1)
+    end
+
+    -- using the trimmed list and the new bases, recreate the working state
+    bsk:CreateWorkingStateFromChanges(bsk.db.profile.changes)
+end
+
 --}}}
 -- Higher order actions (ie calls other Doers){{{
 function bsk:AddMissingPlayers()
@@ -446,7 +482,19 @@
     return ret
 end
 
-
+-- returns true if the events in the list are in time order
+function bsk:CheckListCausality()
+    local t = nil
+    for i,v in ipairs(bsk.db.profile.changes) do
+        if t ~= nil then
+            if v.time <= t then
+                return false
+            end
+        end
+        t = v.time
+    end
+    return true
+end
 
 -- Support functions