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