comparison Lists.lua @ 26:4c3140c7a1b4

Lots of folding markers so I can see the API clearly
author John@Yosemite-PC
date Sat, 10 Mar 2012 09:05:37 -0500
parents cd241c70ba6c
children 611808dbc0c0
comparison
equal deleted inserted replaced
25:cd241c70ba6c 26:4c3140c7a1b4
287 -- routines. 287 -- routines.
288 -- 288 --
289 -- Note that "undo" has no special voodoo to it. It's basically a change that 289 -- Note that "undo" has no special voodoo to it. It's basically a change that
290 -- reverses the prior change on the stack. 290 -- reverses the prior change on the stack.
291 291
292 -- persons list 292 function bsk:DoAddPerson(change)--{{{
293 function bsk:DoAddPerson(change)
294 assert(change) 293 assert(change)
295 assert(change.arg.id) 294 assert(change.arg.id)
296 local arg = change.arg 295 local arg = change.arg
297 -- require admin 296 -- require admin
298 local persons = bsk.persons 297 local persons = bsk.persons
301 assert(persons[id]==nil) 300 assert(persons[id]==nil)
302 persons[id] = {main=name} 301 persons[id] = {main=name}
303 persons.time=change.time 302 persons.time=change.time
304 personName2id[name] = id 303 personName2id[name] = id
305 return true 304 return true
306 end 305 end--}}}
307 306 function bsk:AddPerson(name)--{{{
308 function bsk:AddPerson(name)
309 local persons = bsk.persons 307 local persons = bsk.persons
310 local guid = UnitGUID(name) 308 local guid = UnitGUID(name)
311 -- TODO: check guid to be sure it's a player 309 -- TODO: check guid to be sure it's a player
312 if not guid then 310 if not guid then
313 self:Print(sformat("Could not add player %s - they must be in range or group",name)) 311 self:Print(sformat("Could not add player %s - they must be in range or group",name))
326 end 324 end
327 local change = {action="AddPerson",arg={name=name,id=id}} 325 local change = {action="AddPerson",arg={name=name,id=id}}
328 if bsk:DoAddPerson(change) then 326 if bsk:DoAddPerson(change) then
329 bsk:CreateChange(change) 327 bsk:CreateChange(change)
330 end 328 end
331 end 329 end--}}}
332 330 function bsk:DoCreateList(change)--{{{
333 function bsk:DoCreateList(change)
334 --if bsk:GetListIndex(change.arg.name) then 331 --if bsk:GetListIndex(change.arg.name) then
335 -- self:Print(sformat("List %s already exists",v.name)) 332 -- self:Print(sformat("List %s already exists",v.name))
336 -- return false 333 -- return false
337 --end 334 --end
338 bsk.lists[change.arg.id]={name=change.arg.name,time=change.time} 335 bsk.lists[change.arg.id]={name=change.arg.name,time=change.time}
339 return true 336 return true
340 end 337 end--}}}
341 338 function bsk:CreateList(name)--{{{
342 function bsk:CreateList(name)
343 -- require admin 339 -- require admin
344 local change={action="CreateList",arg={name=name}} 340 local change={action="CreateList",arg={name=name}}
345 bsk:StartChange(change) 341 bsk:StartChange(change)
346 change.arg.id=change.time -- use the creation timestamp as the list's index. it's as unique as anything... 342 change.arg.id=change.time -- use the creation timestamp as the list's index. it's as unique as anything...
347 self:Print("Creating ... " .. name) 343 self:Print("Creating ... " .. name)
348 if bsk:DoCreateList(change) then 344 if bsk:DoCreateList(change) then
349 bsk:CommitChange(change) 345 bsk:CommitChange(change)
350 end 346 end
351 end 347 end--}}}
352 348 function bsk:DoAddPersonToListEnd(change)--{{{
353 function bsk:DoAddPersonToListEnd(change)
354 local list = bsk.lists[change.arg.listIndex] 349 local list = bsk.lists[change.arg.listIndex]
355 local index 350 local index
356 if getn(list) > 0 then 351 if getn(list) > 0 then
357 index = list[#list].index + 0.1 352 index = list[#list].index + 0.1
358 else 353 else
363 tinsert(list,entry) 358 tinsert(list,entry)
364 list.time = change.time 359 list.time = change.time
365 list.closedRandom = true 360 list.closedRandom = true
366 361
367 return true 362 return true
368 end 363 end--}}}
369 364 function bsk:AddPersonToListEnd(name,listName)--{{{
370 function bsk:AddPersonToListEnd(name,listName)
371 -- require admin 365 -- require admin
372 local listIndex = bsk:GetListIndex(listName) 366 local listIndex = bsk:GetListIndex(listName)
373 local id = personName2id[name] 367 local id = personName2id[name]
374 if bsk:IdIsInList(id,bsk.lists[listIndex]) then 368 if bsk:IdIsInList(id,bsk.lists[listIndex]) then
375 bsk:Print(sformat("Person %s is already on the reqeuested list",name)) 369 bsk:Print(sformat("Person %s is already on the reqeuested list",name))
379 local change = {action="AddToListEnd",arg={id=id,listIndex=listIndex}} 373 local change = {action="AddToListEnd",arg={id=id,listIndex=listIndex}}
380 bsk:StartChange(change) 374 bsk:StartChange(change)
381 if bsk:DoAddPersonToListEnd(change) then 375 if bsk:DoAddPersonToListEnd(change) then
382 bsk:CommitChange(change) 376 bsk:CommitChange(change)
383 end 377 end
384 end 378 end--}}}
385 379 function bsk:DoAddPersonToListRandom(change)--{{{
386 function bsk:DoAddPersonToListRandom(change)
387 local list = bsk.lists[change.arg.listIndex] 380 local list = bsk.lists[change.arg.listIndex]
388 local entry = {index=change.arg.roll, id=change.arg.id} 381 local entry = {index=change.arg.roll, id=change.arg.id}
389 382
390 tinsert(list,entry) 383 tinsert(list,entry)
391 table.sort(list,function(a,b) return a.index < b.index end) 384 table.sort(list,function(a,b) return a.index < b.index end)
392 list.time = change.time 385 list.time = change.time
393 386
394 return true 387 return true
395 end 388 end--}}}
396 389 function bsk:AddPersonToListRandom(name,listName)--{{{
397 function bsk:AddPersonToListRandom(name,listName)
398 -- require admin 390 -- require admin
399 local listIndex = bsk:GetListIndex(listName) 391 local listIndex = bsk:GetListIndex(listName)
400 if bsk.lists[listIndex].closedRandom then 392 if bsk.lists[listIndex].closedRandom then
401 self:Print("Cannot add person to list by random roll because an add-to-end operation has already occurred") 393 self:Print("Cannot add person to list by random roll because an add-to-end operation has already occurred")
402 return false 394 return false
411 local change = {action="AddToListRand",arg={id=id,listIndex=listIndex,roll=roll}} 403 local change = {action="AddToListRand",arg={id=id,listIndex=listIndex,roll=roll}}
412 bsk:StartChange(change) 404 bsk:StartChange(change)
413 if bsk:DoAddPersonToListRandom(change) then 405 if bsk:DoAddPersonToListRandom(change) then
414 bsk:CommitChange(change) 406 bsk:CommitChange(change)
415 end 407 end
416 end 408 end--}}}
417 409 function bsk:DoRemovePerson(change)--{{{
418 function bsk:DoRemovePerson(change)
419 410
420 -- return true 411 -- return true
421 end 412 end--}}}
422 413 function bsk:RemovePerson(name)--{{{
423 function bsk:RemovePerson(name)
424 -- from both persons and lists 414 -- from both persons and lists
425 end 415 end--}}}
426 416 function bsk:DoSuicidePerson(change)--{{{
427 function bsk:DoSuicidePerson(change)
428 local list = bsk.lists[change.arg.listIndex] 417 local list = bsk.lists[change.arg.listIndex]
429 local affected = shallowCopy(change.arg.affect) 418 local affected = shallowCopy(change.arg.affect)
430 -- the goal here is to rotate the suicide list by 1 419 -- the goal here is to rotate the suicide list by 1
431 -- then we can just mash it on top of the intersection between the original 420 -- then we can just mash it on top of the intersection between the original
432 -- list and the working copy 421 -- list and the working copy
443 table.remove(replacement,1) 432 table.remove(replacement,1)
444 end 433 end
445 end 434 end
446 list.time=change.time 435 list.time=change.time
447 return true 436 return true
448 end 437 end--}}}
449 438 function bsk:SuicidePerson(name,listName)--{{{
450 function bsk:SuicidePerson(name,listName)
451 -- require admin 439 -- require admin
452 bsk:PopulateRaidList() 440 bsk:PopulateRaidList()
453 local listIndex = bsk:GetListIndex(listName) 441 local listIndex = bsk:GetListIndex(listName)
454 local id = personName2id[name] 442 local id = personName2id[name]
455 local affect=bsk:GetSuicideList(id,bsk.lists[listIndex]) 443 local affect=bsk:GetSuicideList(id,bsk.lists[listIndex])
456 local change = {action="SuicidePerson",arg={affect=affect,listIndex=listIndex}} 444 local change = {action="SuicidePerson",arg={affect=affect,listIndex=listIndex}}
457 bsk:StartChange(change) 445 bsk:StartChange(change)
458 if bsk:DoSuicidePerson(change) then 446 if bsk:DoSuicidePerson(change) then
459 bsk:CommitChange(change) 447 bsk:CommitChange(change)
460 end 448 end
461 end 449 end--}}}
462 450 function bsk:DoRenameList(change)--{{{
463 function bsk:DoRenameList(change)
464 bsk.lists[change.arg.listIndex].name = change.arg.name 451 bsk.lists[change.arg.listIndex].name = change.arg.name
465 bsk.lists[change.arg.listIndex].time = change.time 452 bsk.lists[change.arg.listIndex].time = change.time
466 return true 453 return true
467 end 454 end--}}}
468 455 function bsk:RenameList(listName,newListName)--{{{
469 function bsk:RenameList(listName,newListName)
470 -- require admin 456 -- require admin
471 local listIndex = bsk:GetListIndex(listName) 457 local listIndex = bsk:GetListIndex(listName)
472 local change = {action="RenameList",arg={listIndex=listIndex,name=newListName}} 458 local change = {action="RenameList",arg={listIndex=listIndex,name=newListName}}
473 bsk:StartChange(change) 459 bsk:StartChange(change)
474 if bsk:DoRenameList(change) then 460 if bsk:DoRenameList(change) then
475 bsk:CommitChange(change) 461 bsk:CommitChange(change)
476 end 462 end
477 end 463 end--}}}
478 464 function bsk:DoDeleteList(change)--{{{
479 function bsk:DoDeleteList(change)
480 bsk.lists[change.arg.listIndex] = nil 465 bsk.lists[change.arg.listIndex] = nil
481 return true 466 return true
482 end 467 end--}}}
483 468 function bsk:DeleteList(listName)--{{{
484 function bsk:DeleteList(listName)
485 local listIndex = bsk:GetListIndex(listName) 469 local listIndex = bsk:GetListIndex(listName)
486 local change = {action="DeleteList",arg={listIndex=listIndex}} 470 local change = {action="DeleteList",arg={listIndex=listIndex}}
487 bsk:StartChange(change) 471 bsk:StartChange(change)
488 if bsk:DoDeleteList(change) then 472 if bsk:DoDeleteList(change) then
489 bsk:CommitChange(change) 473 bsk:CommitChange(change)
490 end 474 end
491 end 475 end--}}}
492 476 function bsk:DoRemovePersonFromList(change)--{{{
493 function bsk:DoRemovePersonFromList(change)
494 local list = bsk.lists[change.arg.listIndex] 477 local list = bsk.lists[change.arg.listIndex]
495 478
496 for i,v in pairs(list) do 479 for i,v in pairs(list) do
497 if v.id == change.arg.id then 480 if v.id == change.arg.id then
498 table.remove(list,i) 481 table.remove(list,i)
500 end 483 end
501 end 484 end
502 table.sort(list,function(a,b) return a.index < b.index end) 485 table.sort(list,function(a,b) return a.index < b.index end)
503 list.time = change.time 486 list.time = change.time
504 return true 487 return true
505 end 488 end--}}}
506 489 function bsk:RemovePersonFromList(name,listName)--{{{
507 function bsk:RemovePersonFromList(name,listName)
508 local listIndex = bsk:GetListIndex(listName) 490 local listIndex = bsk:GetListIndex(listName)
509 local pid = personName2id[name] 491 local pid = personName2id[name]
510 local change = {action="RemovePersonFromList",arg={id=pid,listIndex=listIndex}} 492 local change = {action="RemovePersonFromList",arg={id=pid,listIndex=listIndex}}
511 bsk:StartChange(change) 493 bsk:StartChange(change)
512 if bsk:DoRemovePersonFromList(change) then 494 if bsk:DoRemovePersonFromList(change) then
513 bsk:CommitChange(change) 495 bsk:CommitChange(change)
514 end 496 end
515 end 497 end
498 --}}}
516 --}}} 499 --}}}
517 -- Higher order actions (ie calls other standard actions){{{ 500 -- Higher order actions (ie calls other standard actions){{{
518 501
519 function bsk:TrimLists(time) 502 function bsk:TrimLists(time)
520 if not bsk:CheckListCausality() then 503 if not bsk:CheckListCausality() then