comparison Lists.lua @ 5:2ce0f4db1a3e

Changelist trimming
author John@Yosemite-PC
date Mon, 05 Mar 2012 23:11:45 -0500
parents 6c8f9473b22e
children 6d460ff2135c
comparison
equal deleted inserted replaced
4:6c8f9473b22e 5:2ce0f4db1a3e
6 -- 6 --
7 -- A separate user list is held - lists index into this 7 -- A separate user list is held - lists index into this
8 8
9 9
10 -- TODO: rename player 10 -- TODO: rename player
11 -- TODO: list trimming
11 12
12 -- notes on list storage: 13 -- notes on list storage:
13 -- Storing names as I do now is atrocious. 14 -- Storing names as I do now is atrocious.
14 -- It prevents insertions (twss) to the middle of the list because then it acts 15 -- It prevents insertions (twss) to the middle of the list because then it acts
15 -- as a side effect onto all the others. ie ABCD -> AXBCD would be phrased as 16 -- as a side effect onto all the others. ie ABCD -> AXBCD would be phrased as
98 end 99 end
99 end 100 end
100 101
101 --}}} 102 --}}}
102 103
103 function bsk:CreateWorkingStateFromChanges() 104 function bsk:CreateWorkingStateFromChanges(changes)
104 local playerBase = self.db.profile.players 105 local playerBase = self.db.profile.players
105 local listBase = self.db.profile.listBase 106 local listBase = self.db.profile.listBase
106 local changes = self.db.profile.changes
107 107
108 -- copy the base to the working state 108 -- copy the base to the working state
109 wipe(bsk.lists) 109 wipe(bsk.lists)
110 wipe(bsk.players) 110 wipe(bsk.players)
111 bsk:tcopy(bsk.lists,listBase) 111 bsk:tcopy(bsk.lists,listBase)
112 bsk:tcopy(bsk.players,playerBase) 112 bsk:tcopy(bsk.players,playerBase)
113 113
114 -- now just go through the changes list applying each 114 -- now just go through the changes list applying each
115 for i,v in pairs(changes) do 115 for i,v in ipairs(changes) do
116 bsk:ProcessChange(v) 116 bsk:ProcessChange(v)
117 end 117 end
118 end 118 end
119 119
120 function bsk:CreateChange(change) 120 function bsk:CreateChange(change)
337 bsk:StartChange(change) 337 bsk:StartChange(change)
338 if bsk:DoSuicidePlayer(change) then 338 if bsk:DoSuicidePlayer(change) then
339 bsk:CommitChange(change) 339 bsk:CommitChange(change)
340 end 340 end
341 end 341 end
342
343 function bsk:TrimLists(time)
344 if not bsk:CheckListCausality() then
345 self:Print("Unable to trim lists due to violated causality")
346
347 return false
348 end
349
350 if type(time) ~= "number" then
351 time = tonumber(time)
352 end
353
354 -- bisect the changes list by "time"
355 local before = {}
356 for i,v in ipairs(self.db.profile.changes) do
357 if v.time <= time then
358 tinsert(before,v)
359 else
360 break
361 end
362 end
363
364 -- apply first half
365 bsk:CreateWorkingStateFromChanges(before)
366
367 -- save this state permanently; trim the changes permanently
368 bsk:tcopy(bsk.db.profile.players,bsk.players)
369 bsk:tcopy(bsk.db.profile.listBase,bsk.lists)
370 while bsk.db.profile.changes[1].time <= time do
371 table.remove(bsk.db.profile.changes,1)
372 end
373
374 -- using the trimmed list and the new bases, recreate the working state
375 bsk:CreateWorkingStateFromChanges(bsk.db.profile.changes)
376 end
377
342 --}}} 378 --}}}
343 -- Higher order actions (ie calls other Doers){{{ 379 -- Higher order actions (ie calls other Doers){{{
344 function bsk:AddMissingPlayers() 380 function bsk:AddMissingPlayers()
345 bsk:PopulateRaidList() 381 bsk:PopulateRaidList()
346 local t = {} 382 local t = {}
444 end 480 end
445 end 481 end
446 return ret 482 return ret
447 end 483 end
448 484
449 485 -- returns true if the events in the list are in time order
486 function bsk:CheckListCausality()
487 local t = nil
488 for i,v in ipairs(bsk.db.profile.changes) do
489 if t ~= nil then
490 if v.time <= t then
491 return false
492 end
493 end
494 t = v.time
495 end
496 return true
497 end
450 498
451 -- Support functions 499 -- Support functions
452 500
453 function bsk:GetListIndex(name) 501 function bsk:GetListIndex(name)
454 for i,v in pairs(bsk.lists) do 502 for i,v in pairs(bsk.lists) do