comparison Lists.lua @ 9:daed0597deba

Pretty printing for lists A decision on how to index them up Bug about reverse list maintenance fixed. Next step noted.
author John@Doomsday
date Wed, 07 Mar 2012 14:56:25 -0500
parents b05fcb225c4a
children 433d31ea992a
comparison
equal deleted inserted replaced
8:b05fcb225c4a 9:daed0597deba
40 -- order, then the next 30 could roll into that same space and have a proper 40 -- order, then the next 30 could roll into that same space and have a proper
41 -- ordering. Then the next 5, etc. 41 -- ordering. Then the next 5, etc.
42 -- 42 --
43 -- Handling conflicts: 43 -- Handling conflicts:
44 -- 44 --
45 -- Executive decision: random on a range of [0,1], ie math.random
46 -- then on an add-to-end event just do last + .1
47 -- disallow random after any add-to-end event occurs
48 -- because the list either elongates beyond 1 OR becomes
49 -- ridiculously bottom heavy, thus meaning that randoms
50 -- don't get an even distibution from then on (in fact
51 -- they'll end up getting top favor)
52 -- * if a stream contains a random-add after an add-to-end
53 -- it is declared invalid. tough tits. it's just not a fair
54 -- distribution at that point.
45 55
46 -- todo: list-of-lists must not use int indices. those will lead to peril. 56 -- todo: list-of-lists must not use int indices. those will lead to peril.
47 bsk.lists = {} 57 bsk.lists = {}
48 bsk.persons = {} 58 bsk.persons = {}
49 59
71 for k, v in pairs(t) do u[k] = v end 81 for k, v in pairs(t) do u[k] = v end
72 return setmetatable(u, getmetatable(t)) 82 return setmetatable(u, getmetatable(t))
73 end 83 end
74 84
75 -- Debugging {{{ 85 -- Debugging {{{
86 function bsk:PrettyPrintList(listIndex)
87 local list = bsk.lists[listIndex]
88 bsk:Print("List: " .. list.name .. " (" .. list.time .. ")")
89 for i = 1,#list do
90 bsk:Print(" " .. i .. " - " .. bsk.persons[list[i]].main)
91 end
92 end
93 function bsk:PrettyPrintLists()
94 for i,_ in pairs(bsk.lists) do
95 bsk:PrettyPrintList(i)
96 end
97 end
76 function bsk:PrintLists() 98 function bsk:PrintLists()
77 bsk:PrintTable(bsk.lists) 99 bsk:PrintTable(bsk.lists)
78 end 100 end
79 function bsk:PrintChanges() 101 function bsk:PrintChanges()
80 bsk:PrintTable(bsk.db.profile.changes) 102 bsk:PrintTable(bsk.db.profile.changes)
101 end 123 end
102 end 124 end
103 125
104 --}}} 126 --}}}
105 127
128 function bsk:UpdatePersonsReverse()
129 for i,v in pairs(bsk.persons) do
130 if i ~= "time" then
131 personsReverse[v.main] = i
132 end
133 end
134 end
135
106 function bsk:CreateWorkingStateFromChanges(changes) 136 function bsk:CreateWorkingStateFromChanges(changes)
107 local personsBase = self.db.profile.persons 137 local personsBase = self.db.profile.persons
108 local listBase = self.db.profile.listBase 138 local listBase = self.db.profile.listBase
109 139
110 -- copy the base to the working state 140 -- copy the base to the working state
117 147
118 -- now just go through the changes list applying each 148 -- now just go through the changes list applying each
119 for i,v in ipairs(changes) do 149 for i,v in ipairs(changes) do
120 bsk:ProcessChange(v) 150 bsk:ProcessChange(v)
121 end 151 end
152
153 -- update the persons reverse list
154 bsk:UpdatePersonsReverse()
122 end 155 end
123 156
124 function bsk:CreateChange(change) 157 function bsk:CreateChange(change)
125 -- sanity 158 -- sanity
126 assert(change) 159 assert(change)
274 if bsk:DoCreateList(change) then 307 if bsk:DoCreateList(change) then
275 bsk:CommitChange(change) 308 bsk:CommitChange(change)
276 end 309 end
277 end 310 end
278 311
312 -- TODO: break into AddPersonToListEnd and AddPersonToListRandom
279 function bsk:DoAddPersonToList(change) 313 function bsk:DoAddPersonToList(change)
280 local listIndex = change.arg.listIndex 314 local listIndex = change.arg.listIndex
281 local slist = change.arg.slist 315 local slist = change.arg.slist
282 local list = bsk.lists[listIndex] 316 local list = bsk.lists[listIndex]
283 317
293 327
294 function bsk:AddPersonToList(name,list) 328 function bsk:AddPersonToList(name,list)
295 -- require admin 329 -- require admin
296 local listIndex = bsk:GetListIndex(list) 330 local listIndex = bsk:GetListIndex(list)
297 local id = personsReverse[name] 331 local id = personsReverse[name]
332 bsk:Print(sformat("Adding %s (%s) to list %s (%s)", name, id, list, listIndex))
298 local slist = {id} -- TODO: support adding to elsewhere besides the end 333 local slist = {id} -- TODO: support adding to elsewhere besides the end
299 local change = {action="AddPersonToList",arg={id=id,listIndex=listIndex,slist=slist}} 334 local change = {action="AddPersonToList",arg={id=id,listIndex=listIndex,slist=slist}}
300 bsk:StartChange(change) 335 bsk:StartChange(change)
301 if bsk:DoAddPersonToList(change) then 336 if bsk:DoAddPersonToList(change) then
302 bsk:CommitChange(change) 337 bsk:CommitChange(change)