annotate Lists.lua @ 17:71fc79846a5d

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