annotate Lists.lua @ 41:dc9bfacca238

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