John@0
|
1 -- lists consist of three things
|
John@0
|
2 -- 1) a base state - agreed on by one or more list holders
|
John@0
|
3 -- 2) change sets - incremental list changes (can be rolled forwards or
|
John@0
|
4 -- backwards)
|
John@0
|
5 -- 3) working state - not saved because it can be so easily calculated
|
John@0
|
6 --
|
John@0
|
7 -- A separate user list is held - lists index into this
|
John@0
|
8
|
John@0
|
9
|
John@0
|
10 -- TODO: rename player
|
John@5
|
11 -- TODO: list trimming
|
John@12
|
12 -- TODO: lists allow double-add
|
John@12
|
13 -- TODO: collapse slists into delimited strings for space
|
John@0
|
14
|
John@4
|
15 -- notes on list storage:
|
John@7
|
16 -- Using names as keys as I do now is atrocious.
|
John@4
|
17 -- It prevents insertions (twss) to the middle of the list because then it acts
|
John@4
|
18 -- as a side effect onto all the others. ie ABCD -> AXBCD would be phrased as
|
John@4
|
19 -- "insert X and shift down B,C,D" which sucks. BCD haven't really been affected
|
John@4
|
20 -- (yet) because their relative positions to the others are still intact - ie
|
John@4
|
21 -- they are still below A right where they belong. But really X hasn't done
|
John@4
|
22 -- anything to affect their relative standing.
|
John@4
|
23 --
|
John@4
|
24 -- Ok so we can't use names.
|
John@4
|
25 --
|
John@4
|
26 -- We can't use monotonic integers either because it suffers the same problem.
|
John@4
|
27 -- Also consider, randoming in someone to a list of ABCD. Say they roll spot 2.
|
John@4
|
28 -- What if someone else runs a separate raid and also randoms someone into slot
|
John@4
|
29 -- 2? How do you handle that conflict? Difficult. Also, consider this:
|
John@4
|
30 -- List of ABCD on night 1.
|
John@4
|
31 -- Admin 1 on night 2 rolls in 30 new people. ABCD's indexes are shuffled to be
|
John@4
|
32 -- between 1-35.
|
John@4
|
33 -- Admin 2 on night 3 rolls in 5 new ones and people ABCD and PQRST now all have
|
John@4
|
34 -- indexes between 1-9.
|
John@4
|
35 -- When these two are resolved against one another, do the 1-9 peopole end up on
|
John@4
|
36 -- top of the list compared to those other 30?
|
John@4
|
37 --
|
John@4
|
38 -- Solution:
|
John@4
|
39 -- Need a huge random space with purposely left gaps to leave plenty of room for
|
John@4
|
40 -- conflicts.
|
John@4
|
41 -- So if ABCD had randomed on a space of say, 10,000 and then were sorted into
|
John@4
|
42 -- order, then the next 30 could roll into that same space and have a proper
|
John@4
|
43 -- ordering. Then the next 5, etc.
|
John@4
|
44 --
|
John@4
|
45 -- Handling conflicts:
|
John@4
|
46 --
|
John@9
|
47 -- Executive decision: random on a range of [0,1], ie math.random
|
John@9
|
48 -- then on an add-to-end event just do last + .1
|
John@9
|
49 -- disallow random after any add-to-end event occurs
|
John@9
|
50 -- because the list either elongates beyond 1 OR becomes
|
John@9
|
51 -- ridiculously bottom heavy, thus meaning that randoms
|
John@9
|
52 -- don't get an even distibution from then on (in fact
|
John@9
|
53 -- they'll end up getting top favor)
|
John@9
|
54 -- * if a stream contains a random-add after an add-to-end
|
John@9
|
55 -- it is declared invalid. tough tits. it's just not a fair
|
John@9
|
56 -- distribution at that point.
|
John@10
|
57 -- * actually, fuck it. I'll give them an unlock command and
|
John@10
|
58 -- let them screw over their lists :)
|
John@0
|
59
|
John@8
|
60 -- todo: list-of-lists must not use int indices. those will lead to peril.
|
John@0
|
61 bsk.lists = {}
|
John@8
|
62 bsk.persons = {}
|
John@0
|
63
|
John@16
|
64 local raidNameP = {} -- "name" is present in raid
|
John@16
|
65 local raidIdP = {} -- "id" is present in raid
|
John@16
|
66 local reserveIdP = {} -- "reserve id present"
|
John@8
|
67 local activeListKey = 1 -- temporary
|
John@16
|
68 local personName2id = {} -- given "name" get that person's id
|
John@0
|
69
|
John@0
|
70 local tinsert = table.insert
|
John@0
|
71 local sformat = string.format
|
John@0
|
72 local getn = table.getn
|
John@0
|
73
|
John@0
|
74 function bsk:tcopy(to, from)
|
John@0
|
75 for k,v in pairs(from) do
|
John@0
|
76 if(type(v)=="table") then
|
John@0
|
77 to[k] = {}
|
John@0
|
78 bsk:tcopy(to[k], v);
|
John@0
|
79 else
|
John@0
|
80 to[k] = v;
|
John@0
|
81 end
|
John@0
|
82 end
|
John@0
|
83 end
|
John@0
|
84 local shallowCopy = function(t)
|
John@0
|
85 local u = { }
|
John@0
|
86 for k, v in pairs(t) do u[k] = v end
|
John@0
|
87 return setmetatable(u, getmetatable(t))
|
John@0
|
88 end
|
John@0
|
89
|
John@1
|
90 -- Debugging {{{
|
John@9
|
91 function bsk:PrettyPrintList(listIndex)
|
John@9
|
92 local list = bsk.lists[listIndex]
|
John@9
|
93 bsk:Print("List: " .. list.name .. " (" .. list.time .. ")")
|
John@9
|
94 for i = 1,#list do
|
John@12
|
95 bsk:Print(" " .. i .. " - " .. bsk.persons[list[i].id].main)
|
John@9
|
96 end
|
John@9
|
97 end
|
John@9
|
98 function bsk:PrettyPrintLists()
|
John@9
|
99 for i,_ in pairs(bsk.lists) do
|
John@9
|
100 bsk:PrettyPrintList(i)
|
John@9
|
101 end
|
John@9
|
102 end
|
John@1
|
103 function bsk:PrintLists()
|
John@1
|
104 bsk:PrintTable(bsk.lists)
|
John@1
|
105 end
|
John@1
|
106 function bsk:PrintChanges()
|
John@1
|
107 bsk:PrintTable(bsk.db.profile.changes)
|
John@1
|
108 end
|
John@8
|
109 function bsk:PrintPersons()
|
John@8
|
110 bsk:PrintTable(bsk.persons)
|
John@1
|
111 end
|
John@0
|
112 function bsk:PrintTable(table, depth)
|
John@0
|
113 depth = depth or ""
|
John@0
|
114 if not table then return end
|
John@0
|
115 for i,v in pairs(table) do
|
John@0
|
116 if( type(v) == "string" ) then
|
John@0
|
117 self:Print(depth .. i .. " - " .. v)
|
John@0
|
118 elseif( type(v) == "number" ) then
|
John@0
|
119 self:Print(depth .. i .. " - " .. tostring(v))
|
John@0
|
120 elseif( type(v) == "table" ) then
|
John@0
|
121 self:Print(depth .. i .." - ")
|
John@0
|
122 self:PrintTable(v,depth.." ")
|
John@0
|
123 elseif( type(v) == "boolean" ) then
|
John@0
|
124 self:Print(depth .. i .. " - " .. tostring(v))
|
John@0
|
125 else
|
John@0
|
126 self:Print(depth .. i .. " - not sure how to print type: " .. type(v) )
|
John@0
|
127 end
|
John@0
|
128 end
|
John@0
|
129 end
|
John@0
|
130
|
John@0
|
131 --}}}
|
John@0
|
132
|
John@9
|
133 function bsk:UpdatePersonsReverse()
|
John@9
|
134 for i,v in pairs(bsk.persons) do
|
John@9
|
135 if i ~= "time" then
|
John@16
|
136 personName2id[v.main] = i
|
John@9
|
137 end
|
John@9
|
138 end
|
John@9
|
139 end
|
John@9
|
140
|
John@16
|
141 -- Change processing {{{
|
John@5
|
142 function bsk:CreateWorkingStateFromChanges(changes)
|
John@8
|
143 local personsBase = self.db.profile.persons
|
John@0
|
144 local listBase = self.db.profile.listBase
|
John@0
|
145
|
John@0
|
146 -- copy the base to the working state
|
John@0
|
147 wipe(bsk.lists)
|
John@8
|
148 wipe(bsk.persons)
|
John@16
|
149 wipe(personName2id)
|
John@8
|
150
|
John@0
|
151 bsk:tcopy(bsk.lists,listBase)
|
John@8
|
152 bsk:tcopy(bsk.persons,personsBase)
|
John@0
|
153
|
John@0
|
154 -- now just go through the changes list applying each
|
John@5
|
155 for i,v in ipairs(changes) do
|
John@0
|
156 bsk:ProcessChange(v)
|
John@0
|
157 end
|
John@9
|
158
|
John@9
|
159 -- update the persons reverse list
|
John@9
|
160 bsk:UpdatePersonsReverse()
|
John@0
|
161 end
|
John@0
|
162
|
John@0
|
163 function bsk:CreateChange(change)
|
John@0
|
164 -- sanity
|
John@0
|
165 assert(change)
|
John@0
|
166 assert(change.action)
|
John@0
|
167 assert(change.arg)
|
John@0
|
168
|
John@0
|
169 bsk:StartChange(change)
|
John@0
|
170 bsk:CommitChange(change)
|
John@0
|
171 end
|
John@0
|
172
|
John@0
|
173 function bsk:StartChange(change)
|
John@0
|
174 local changes = self.db.profile.changes
|
John@0
|
175 change.time = time()
|
John@0
|
176 local n = getn(changes)
|
John@0
|
177 if n > 0 then
|
John@0
|
178 if changes[n].time >= change.time then
|
John@0
|
179 change.time = changes[n].time + 1
|
John@0
|
180 end
|
John@0
|
181 end
|
John@0
|
182 end
|
John@0
|
183
|
John@0
|
184 function bsk:CommitChange(change)
|
John@0
|
185 local changes = self.db.profile.changes
|
John@0
|
186 tinsert(changes,change)
|
John@0
|
187 -- TODO: broadcast change
|
John@0
|
188 end
|
John@0
|
189
|
John@16
|
190 function bsk:ProcessChange(change)
|
John@16
|
191 if change.action == "AddPerson" then
|
John@16
|
192 bsk:DoAddPerson(change)
|
John@16
|
193 elseif change.action == "CreateList" then
|
John@16
|
194 bsk:DoCreateList(change)
|
John@16
|
195 elseif change.action == "AddToListEnd" then
|
John@16
|
196 bsk:DoAddPersonToListEnd(change)
|
John@16
|
197 elseif change.action == "AddToListRand" then
|
John@16
|
198 bsk:DoAddPersonToListRandom(change)
|
John@16
|
199 elseif change.action == "SuicidePerson" then
|
John@16
|
200 bsk:DoSuicidePerson(change)
|
John@16
|
201 else
|
John@16
|
202 bsk:Print("Unknown message encountered")
|
John@16
|
203 bsk:PrintTable(change)
|
John@16
|
204 assert(false)
|
John@16
|
205 end
|
John@16
|
206 end
|
John@16
|
207
|
John@16
|
208 --}}}
|
John@0
|
209
|
John@0
|
210 -- timestamp logic:
|
John@0
|
211 -- use time() for comparisons - local clients use date() to make it pretty. only
|
John@0
|
212 -- dowisde - we can't have a server timestamp. Which kind of sucks, but it turns
|
John@0
|
213 -- out you can change timezones when you enter an instance server, so you really
|
John@0
|
214 -- never know what time it is.
|
John@0
|
215 -- There's unfortunately no hard-and-proven method for determining the true time
|
John@0
|
216 -- difference between local time and server time. You can't just query the two
|
John@0
|
217 -- and compare them because your server timezone can change (!) if you go into
|
John@0
|
218 -- an instance server with a different timezone. This is apparently a big
|
John@0
|
219 -- problem on Oceanic realms.
|
John@0
|
220 --
|
John@0
|
221 -- Timestamp handling (brainstorming how to deal with drift):
|
John@0
|
222 -- (not an issue) if someone sends you time in the future, update your offset so you won't
|
John@0
|
223 -- send out events in the "past" to that person
|
John@0
|
224 -- (not an issue - using local UTC now) on change-zone-event: check if you've changed timezones - might need update
|
John@0
|
225 -- each time you add a change, check the tail of the change list; if this is
|
John@0
|
226 -- less than that, you have a problem. Print a message. if this is equal, then
|
John@0
|
227 -- that's ok, just bump it by 1 second. This could happen in the case of, say,
|
John@0
|
228 -- spam-clicking the undo button or adding names to the list. The recipients
|
John@0
|
229 -- should be ok with this since they'll follow the same algorithm. The only
|
John@0
|
230 -- real chance for a problem is if two people click within the 1 second window?
|
John@0
|
231 -- if someone sends you a past event,
|
John@0
|
232 -- it's ok if it's newer than anything in the changes list
|
John@0
|
233 -- otherwise ... causality has been violated.
|
John@0
|
234 -- Whenever an admin signon event happens, have the admins each perform a
|
John@0
|
235 -- timestamp check. Issue warnings for anyone with a clock that's more than
|
John@0
|
236 -- X seconds out of sync with the others. Seriously, why isn't NTP a standard
|
John@0
|
237 -- setting on all operating systems ...
|
John@0
|
238
|
John@1
|
239 -- Action and DoAction defs {{{
|
John@0
|
240 --
|
John@0
|
241 -- The actual actions for changes start here
|
John@0
|
242 --
|
John@0
|
243 -- Each action occurs as a pair of functions. The bsk:Action() function is from
|
John@0
|
244 -- a list admin's point of view. Each will check for admin status, then create a
|
John@0
|
245 -- change bundle, call the handler for that change (ie the DoAction func), and
|
John@0
|
246 -- then record/transmist the bundle. These are simple and repetitive functions.
|
John@0
|
247 --
|
John@0
|
248 -- The bsk:DoAction() function is tasked with executing the bundle and is what
|
John@0
|
249 -- non-admins and admins alike will call to transform their working state via a
|
John@0
|
250 -- change packet. Each Do() function will accept *only* a change packet, and
|
John@0
|
251 -- it's assumed that the change has been vetted elsewhere. These are very blunt
|
John@0
|
252 -- routines.
|
John@0
|
253 --
|
John@0
|
254 -- Note that "undo" has no special voodoo to it. It's basically a change that
|
John@0
|
255 -- reverses the prior change on the stack.
|
John@0
|
256
|
John@8
|
257 -- persons list
|
John@8
|
258 function bsk:DoAddPerson(change)
|
John@0
|
259 assert(change)
|
John@8
|
260 assert(change.arg.id)
|
John@0
|
261 local arg = change.arg
|
John@0
|
262 -- require admin
|
John@8
|
263 local persons = bsk.persons
|
John@0
|
264 local name = arg.name
|
John@8
|
265 local id = arg.id
|
John@8
|
266 assert(persons[id]==nil)
|
John@8
|
267 persons[id] = {main=name}
|
John@8
|
268 persons.time=change.time
|
John@16
|
269 personName2id[name] = id
|
John@0
|
270 return true
|
John@0
|
271 end
|
John@0
|
272
|
John@8
|
273 function bsk:AddPerson(name)
|
John@8
|
274 local persons = bsk.persons
|
John@0
|
275 local guid = UnitGUID(name)
|
John@0
|
276 -- TODO: check guid to be sure it's a player
|
John@0
|
277 if not guid then
|
John@0
|
278 self:Print(sformat("Could not add player %s - they must be in range or group",name))
|
John@0
|
279 return
|
John@0
|
280 end
|
John@8
|
281 local id = string.sub(guid,6) -- skip at least 0x0580 ...
|
John@8
|
282 id = id:gsub("^0*(.*)","%1") -- nom all leading zeroes remaining
|
John@8
|
283
|
John@8
|
284 if persons[id] and persons[id] ~= name then
|
John@8
|
285 self:Print(sformat("Namechange detected for %s - new is %s, please rename the existing entry", persons[id], name))
|
John@0
|
286 return
|
John@0
|
287 end
|
John@8
|
288 if persons[id] ~= nil then
|
John@8
|
289 self:Print(sformat("%s is already in the persons list; disregarding", name))
|
John@0
|
290 return
|
John@0
|
291 end
|
John@8
|
292 local change = {action="AddPerson",arg={name=name,id=id}}
|
John@8
|
293 if bsk:DoAddPerson(change) then
|
John@0
|
294 bsk:CreateChange(change)
|
John@0
|
295 end
|
John@0
|
296 end
|
John@0
|
297
|
John@0
|
298 function bsk:DoCreateList(change)
|
John@0
|
299 -- TODO: this segment will probably be useful as bsk:SearchForListByName
|
John@0
|
300 local lists = bsk.lists
|
John@0
|
301 for i,v in pairs(lists) do
|
John@0
|
302 if v.name == change.arg.name then
|
John@0
|
303 self:Print(sformat("List %s already exists",v.name))
|
John@0
|
304 return false
|
John@0
|
305 end
|
John@0
|
306 end
|
John@0
|
307 tinsert(lists,{name=change.arg.name,time=change.time})
|
John@0
|
308 return true
|
John@0
|
309 end
|
John@0
|
310
|
John@0
|
311 function bsk:CreateList(name)
|
John@0
|
312 -- require admin
|
John@0
|
313 local change={action="CreateList",arg={name=name}}
|
John@0
|
314 bsk:StartChange(change)
|
John@0
|
315 self:Print("Creating ... " .. name)
|
John@0
|
316 if bsk:DoCreateList(change) then
|
John@0
|
317 bsk:CommitChange(change)
|
John@0
|
318 end
|
John@0
|
319 end
|
John@0
|
320
|
John@10
|
321 function bsk:DoAddPersonToListEnd(change)
|
John@10
|
322 local list = bsk.lists[change.arg.listIndex]
|
John@10
|
323 local index = list[#list].index + 0.1;
|
John@10
|
324 local entry = {index=index, id=change.arg.id}
|
John@0
|
325
|
John@10
|
326 tinsert(list,entry)
|
John@10
|
327 list.time = change.time
|
John@10
|
328 list.closedRandom = true
|
John@10
|
329
|
John@0
|
330 return true
|
John@0
|
331 end
|
John@0
|
332
|
John@16
|
333 function bsk:AddPersonToListEnd(name,listName)
|
John@0
|
334 -- require admin
|
John@16
|
335 local listIndex = bsk:GetListIndex(listName)
|
John@16
|
336 local id = personName2id[name]
|
John@13
|
337 if bsk:IdIsInList(id,bsk.lists[listIndex]) then
|
John@13
|
338 bsk:Print(sformat("Person %s is already on the reqeuested list",name))
|
John@13
|
339 return
|
John@13
|
340 end
|
John@16
|
341 bsk:Print(sformat("Adding %s (%s) to list %s (%s)", name, id, listName, listIndex))
|
John@10
|
342 local change = {action="AddToListEnd",arg={id=id,listIndex=listIndex}}
|
John@0
|
343 bsk:StartChange(change)
|
John@10
|
344 if bsk:DoAddPersonToListEnd(change) then
|
John@10
|
345 bsk:CommitChange(change)
|
John@10
|
346 end
|
John@10
|
347 end
|
John@10
|
348
|
John@10
|
349 function bsk:DoAddPersonToListRandom(change)
|
John@10
|
350 local list = bsk.lists[change.arg.listIndex]
|
John@10
|
351 local entry = {index=change.arg.roll, id=change.arg.id}
|
John@10
|
352
|
John@10
|
353 tinsert(list,entry)
|
John@12
|
354 table.sort(list,function(a,b) return a.index < b.index end)
|
John@10
|
355 list.time = change.time
|
John@10
|
356
|
John@10
|
357 return true
|
John@10
|
358 end
|
John@10
|
359
|
John@16
|
360 function bsk:AddPersonToListRandom(name,listName)
|
John@10
|
361 -- require admin
|
John@16
|
362 local listIndex = bsk:GetListIndex(listName)
|
John@10
|
363 if bsk.lists[listIndex].closedRandom then
|
John@10
|
364 self:Print("Cannot add person to list by random roll because an add-to-end operation has already occurred")
|
John@12
|
365 return false
|
John@10
|
366 end
|
John@13
|
367 if bsk:IdIsInList(id,bsk.lists[listIndex]) then
|
John@13
|
368 bsk:Print(sformat("Person %s is already on the reqeuested list",name))
|
John@13
|
369 return
|
John@13
|
370 end
|
John@16
|
371 local id = personName2id[name]
|
John@10
|
372 local roll = math.random()
|
John@16
|
373 bsk:Print(sformat("Adding %s (%s) to list %s (%s) with roll (%f)", name, id, listName, listIndex, roll))
|
John@10
|
374 local change = {action="AddToListRand",arg={id=id,listIndex=listIndex,roll=roll}}
|
John@10
|
375 bsk:StartChange(change)
|
John@10
|
376 if bsk:DoAddPersonToListRandom(change) then
|
John@0
|
377 bsk:CommitChange(change)
|
John@0
|
378 end
|
John@0
|
379 end
|
John@0
|
380
|
John@8
|
381 function bsk:DoRemovePerson(change)
|
John@0
|
382
|
John@0
|
383 -- return true
|
John@0
|
384 end
|
John@0
|
385
|
John@8
|
386 function bsk:RemovePerson(name)
|
John@8
|
387 -- from both persons and lists
|
John@0
|
388 end
|
John@0
|
389
|
John@8
|
390 function bsk:DoSuicidePerson(change)
|
John@16
|
391 local list = bsk.lists[change.arg.listIndex]
|
John@16
|
392 local affected = shallowCopy(change.arg.affect)
|
John@0
|
393 -- the goal here is to rotate the suicide list by 1
|
John@0
|
394 -- then we can just mash it on top of the intersection between the original
|
John@0
|
395 -- list and the working copy
|
John@16
|
396 local replacement = shallowCopy(change.arg.affect)
|
John@16
|
397 local temp = table.remove(replacement,1) -- pop
|
John@16
|
398 tinsert(replacement,temp) -- push_back
|
John@0
|
399 --bsk:Print(sformat("Before suicide of %s on list %s",slist[1],list.name))
|
John@0
|
400 --bsk:PrintTable(list)
|
John@0
|
401 for i = 1, #list do
|
John@16
|
402 if list[i].id == affect[1] then
|
John@16
|
403 table.remove(affect,1)
|
John@16
|
404 list[i].id = replacement[1]
|
John@16
|
405 table.remove(replacement,1)
|
John@0
|
406 end
|
John@0
|
407 end
|
John@0
|
408 list.time=change.time
|
John@0
|
409 return true
|
John@0
|
410 end
|
John@0
|
411
|
John@8
|
412 function bsk:SuicidePerson(name,list)
|
John@0
|
413 -- require admin
|
John@0
|
414 bsk:PopulateRaidList()
|
John@16
|
415 local listIndex = bsk:GetListIndex(listName)
|
John@16
|
416 local id = personName2id[name]
|
John@16
|
417 local affect=bsk:GetSuicideList(id,bsk.lists[listIndex])
|
John@16
|
418 local change = {action="SuicidePerson",arg={affect=affect,listIndex=listIndex}}
|
John@10
|
419 bsk:PrintTable(change)
|
John@0
|
420 bsk:StartChange(change)
|
John@8
|
421 if bsk:DoSuicidePerson(change) then
|
John@0
|
422 bsk:CommitChange(change)
|
John@0
|
423 end
|
John@0
|
424 end
|
John@5
|
425
|
John@5
|
426 function bsk:TrimLists(time)
|
John@5
|
427 if not bsk:CheckListCausality() then
|
John@5
|
428 self:Print("Unable to trim lists due to violated causality")
|
John@5
|
429
|
John@5
|
430 return false
|
John@5
|
431 end
|
John@5
|
432
|
John@5
|
433 if type(time) ~= "number" then
|
John@5
|
434 time = tonumber(time)
|
John@5
|
435 end
|
John@5
|
436
|
John@5
|
437 -- bisect the changes list by "time"
|
John@5
|
438 local before = {}
|
John@5
|
439 for i,v in ipairs(self.db.profile.changes) do
|
John@5
|
440 if v.time <= time then
|
John@5
|
441 tinsert(before,v)
|
John@5
|
442 else
|
John@5
|
443 break
|
John@5
|
444 end
|
John@5
|
445 end
|
John@5
|
446
|
John@5
|
447 -- apply first half
|
John@5
|
448 bsk:CreateWorkingStateFromChanges(before)
|
John@5
|
449
|
John@5
|
450 -- save this state permanently; trim the changes permanently
|
John@8
|
451 bsk:tcopy(bsk.db.profile.persons,bsk.persons)
|
John@5
|
452 bsk:tcopy(bsk.db.profile.listBase,bsk.lists)
|
John@8
|
453 while bsk.db.profile.changes ~= nil and bsk.db.profile.changes[1] ~= nil and bsk.db.profile.changes[1].time <= time do
|
John@5
|
454 table.remove(bsk.db.profile.changes,1)
|
John@5
|
455 end
|
John@5
|
456
|
John@5
|
457 -- using the trimmed list and the new bases, recreate the working state
|
John@5
|
458 bsk:CreateWorkingStateFromChanges(bsk.db.profile.changes)
|
John@5
|
459 end
|
John@5
|
460
|
John@1
|
461 --}}}
|
John@1
|
462 -- Higher order actions (ie calls other Doers){{{
|
John@8
|
463 function bsk:AddMissingPersons()
|
John@1
|
464 bsk:PopulateRaidList()
|
John@1
|
465 local t = {}
|
John@16
|
466 for i,_ in pairs(bsk.persons) do
|
John@16
|
467 t[i] = true
|
John@1
|
468 end
|
John@16
|
469 for i,_ in pairs(raidNameP) do
|
John@2
|
470 if t[i] == nil then
|
John@8
|
471 bsk:Print(sformat("Person %s is missing from the persons list - adding",i))
|
John@8
|
472 bsk:AddPerson(i)
|
John@1
|
473 end
|
John@1
|
474 end
|
John@1
|
475 -- TODO: batch into a single op - no need to spam 25 messages in a row
|
John@1
|
476 end
|
John@3
|
477 function bsk:PopulateListRandom(index)
|
John@3
|
478 -- difference (raid+reserve)^list, then random shuffle that, then add
|
John@3
|
479 bsk:PopulateRaidList()
|
John@3
|
480 local list = bsk.lists[index]
|
John@3
|
481
|
John@3
|
482 local t = {}
|
John@3
|
483 --for i = 1,#list do
|
John@16
|
484 -- if not (raidNameP(list[i]) or reserveIdP(list[i])) then
|
John@3
|
485 -- tinsert(t,)
|
John@3
|
486 -- end
|
John@3
|
487 --end
|
John@3
|
488 end
|
John@1
|
489 --}}}
|
John@1
|
490 -- "Soft" actions- ie things that cause nonpermanent state {{{
|
John@1
|
491
|
John@1
|
492 -- reserves
|
John@1
|
493 function bsk:AddReserve(name)
|
John@16
|
494 reserveIdP[personName2id[name]]=true
|
John@1
|
495 -- TODO: communicate to others. don't store this in any way.
|
John@1
|
496 end
|
John@1
|
497
|
John@1
|
498 function bsk:RemoveReserve(name)
|
John@16
|
499 reserveIdP[personName2id[name]]=false
|
John@1
|
500 -- TODO: communicate to others. don't store this in any way.
|
John@1
|
501 end
|
John@1
|
502
|
John@1
|
503
|
John@1
|
504 --function bsk:GetActiveList()
|
John@1
|
505 -- return bsk.lists[1] -- todo!
|
John@1
|
506 --end
|
John@1
|
507
|
John@1
|
508 --}}}
|
John@0
|
509
|
John@0
|
510 -- The following code is from Xinhuan (wowace forum member)
|
John@0
|
511 -- Pre-create the unitID strings we will use
|
John@0
|
512 local pID = {}
|
John@0
|
513 local rID = {}
|
John@0
|
514 for i = 1, 4 do
|
John@0
|
515 pID[i] = format("party%d", i)
|
John@0
|
516 end
|
John@0
|
517 for i = 1, 40 do
|
John@0
|
518 rID[i] = format("raid%d", i)
|
John@0
|
519 end
|
John@0
|
520 function bsk:PopulateRaidList()
|
John@0
|
521 local inParty = GetNumPartyMembers()
|
John@0
|
522 local inRaid = GetNumRaidMembers()
|
John@16
|
523 local add = function(name) raidNameP[name]=true; if personName2id[id] == nil then end end
|
John@0
|
524
|
John@16
|
525 wipe(raidNameP)
|
John@16
|
526 wipe(raidIdP)
|
John@0
|
527 if inRaid > 0 then
|
John@0
|
528 for i = 1, inRaid do
|
John@16
|
529 local name = UnitName(rID[i])
|
John@16
|
530 raidNameP[name]=true
|
John@16
|
531 raidIdP[personName2id[name]]=true
|
John@0
|
532 end
|
John@0
|
533 elseif inParty > 0 then
|
John@0
|
534 for i = 1, inParty do
|
John@16
|
535 local name = UnitName(pID[i])
|
John@16
|
536 raidNameP[name]=true
|
John@16
|
537 raidIdP[personName2id[name]]=true
|
John@0
|
538 end
|
John@0
|
539 -- Now add yourself as the last party member
|
John@16
|
540 local name = UnitName("player")
|
John@16
|
541 raidNameP[name]=true
|
John@16
|
542 raidIdP[personName2id[name]]=true
|
John@0
|
543 else
|
John@0
|
544 -- You're alone
|
John@16
|
545 local name = UnitName("player")
|
John@16
|
546 raidNameP[name]=true
|
John@16
|
547 raidIdP[personName2id[name]]=true
|
John@0
|
548 end
|
John@0
|
549 end
|
John@0
|
550
|
John@0
|
551 -- undo rules!
|
John@0
|
552 -- only the most recent event can be undone
|
John@0
|
553 -- ^^^ on a given list?
|
John@0
|
554 -- algorithm is easy, given "Suicide A B C"
|
John@0
|
555 -- just find A,B,C in the list and replace in order from the s message
|
John@0
|
556 -- while undo is allowed *per-list*, certain events in the stream will
|
John@0
|
557 -- prevent proper undo, such as add/delete player or add/delete list
|
John@0
|
558
|
John@0
|
559
|
John@12
|
560 function bsk:GetSuicideList(id,list)
|
John@1
|
561 --self:Print("Calculating changeset for "..name.." from list -")
|
John@1
|
562 --self:PrintTable(list)
|
John@1
|
563 local t = {}
|
John@1
|
564 local ret = {}
|
John@1
|
565 local pushing = false
|
John@1
|
566 for i = 1, #list do
|
John@12
|
567 if list[i].id == id then
|
John@1
|
568 pushing = true
|
John@1
|
569 end
|
John@16
|
570 if pushing and (raidIdP[list[i].id] or reserveIdP[list[i].id]) then
|
John@10
|
571 tinsert(ret,list[i].id)
|
John@1
|
572 end
|
John@1
|
573 end
|
John@16
|
574 --bsk:Print("GSL")
|
John@16
|
575 --bsk:PrintTable(ret)
|
John@16
|
576 --bsk:Print("GSL")
|
John@1
|
577 return ret
|
John@0
|
578 end
|
John@0
|
579
|
John@13
|
580 function bsk:IdIsInList(id,listRef)
|
John@13
|
581 for i = 1,#listRef do
|
John@13
|
582 if id == listRef[i].id then
|
John@13
|
583 return true
|
John@13
|
584 end
|
John@13
|
585 end
|
John@13
|
586 return false
|
John@13
|
587 end
|
John@13
|
588
|
John@5
|
589 -- returns true if the events in the list are in time order
|
John@5
|
590 function bsk:CheckListCausality()
|
John@5
|
591 local t = nil
|
John@5
|
592 for i,v in ipairs(bsk.db.profile.changes) do
|
John@5
|
593 if t ~= nil then
|
John@5
|
594 if v.time <= t then
|
John@5
|
595 return false
|
John@5
|
596 end
|
John@5
|
597 end
|
John@5
|
598 t = v.time
|
John@5
|
599 end
|
John@5
|
600 return true
|
John@5
|
601 end
|
John@0
|
602
|
John@0
|
603 -- Support functions
|
John@0
|
604
|
John@0
|
605 function bsk:GetListIndex(name)
|
John@0
|
606 for i,v in pairs(bsk.lists) do
|
John@0
|
607 if v.name == name then
|
John@0
|
608 return i
|
John@0
|
609 end
|
John@0
|
610 end
|
John@0
|
611 assert(false)
|
John@0
|
612 end
|
John@1
|
613
|
John@3
|
614 local shuffleArray = function(array)
|
John@3
|
615 local arrayCount = #array
|
John@3
|
616 for i = arrayCount, 2, -1 do
|
John@3
|
617 local j = math.random(1, i)
|
John@3
|
618 array[i], array[j] = array[j], array[i]
|
John@3
|
619 end
|
John@3
|
620 return array
|
John@3
|
621 end
|
John@3
|
622
|