annotate Lists.lua @ 30:7b01c9b2b411

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