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