annotate Lists.lua @ 43:4109683c3172

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