annotate Lists.lua @ 44:8913e7d79cad

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