comparison Lists.lua @ 16:16b7e6390f42

Renamed a bunch of lists so I can tell easily what the primary key is and what is stored in that list. Trying to stop using the word "list" because it applies to basically everything
author John@Doomsday
date Thu, 08 Mar 2012 12:49:17 -0500
parents e62cbf3a0c07
children 71fc79846a5d
comparison
equal deleted inserted replaced
15:dbfb8f2052b6 16:16b7e6390f42
59 59
60 -- todo: list-of-lists must not use int indices. those will lead to peril. 60 -- todo: list-of-lists must not use int indices. those will lead to peril.
61 bsk.lists = {} 61 bsk.lists = {}
62 bsk.persons = {} 62 bsk.persons = {}
63 63
64 local raidList = {} 64 local raidNameP = {} -- "name" is present in raid
65 local reserveList = {} 65 local raidIdP = {} -- "id" is present in raid
66 local reserveIdP = {} -- "reserve id present"
66 local activeListKey = 1 -- temporary 67 local activeListKey = 1 -- temporary
67 local personsReverse = {} 68 local personName2id = {} -- given "name" get that person's id
68 69
69 local tinsert = table.insert 70 local tinsert = table.insert
70 local sformat = string.format 71 local sformat = string.format
71 local getn = table.getn 72 local getn = table.getn
72 73
130 --}}} 131 --}}}
131 132
132 function bsk:UpdatePersonsReverse() 133 function bsk:UpdatePersonsReverse()
133 for i,v in pairs(bsk.persons) do 134 for i,v in pairs(bsk.persons) do
134 if i ~= "time" then 135 if i ~= "time" then
135 personsReverse[v.main] = i 136 personName2id[v.main] = i
136 end 137 end
137 end 138 end
138 end 139 end
139 140
141 -- Change processing {{{
140 function bsk:CreateWorkingStateFromChanges(changes) 142 function bsk:CreateWorkingStateFromChanges(changes)
141 local personsBase = self.db.profile.persons 143 local personsBase = self.db.profile.persons
142 local listBase = self.db.profile.listBase 144 local listBase = self.db.profile.listBase
143 145
144 -- copy the base to the working state 146 -- copy the base to the working state
145 wipe(bsk.lists) 147 wipe(bsk.lists)
146 wipe(bsk.persons) 148 wipe(bsk.persons)
147 wipe(personsReverse) 149 wipe(personName2id)
148 150
149 bsk:tcopy(bsk.lists,listBase) 151 bsk:tcopy(bsk.lists,listBase)
150 bsk:tcopy(bsk.persons,personsBase) 152 bsk:tcopy(bsk.persons,personsBase)
151 153
152 -- now just go through the changes list applying each 154 -- now just go through the changes list applying each
183 local changes = self.db.profile.changes 185 local changes = self.db.profile.changes
184 tinsert(changes,change) 186 tinsert(changes,change)
185 -- TODO: broadcast change 187 -- TODO: broadcast change
186 end 188 end
187 189
190 function bsk:ProcessChange(change)
191 if change.action == "AddPerson" then
192 bsk:DoAddPerson(change)
193 elseif change.action == "CreateList" then
194 bsk:DoCreateList(change)
195 elseif change.action == "AddToListEnd" then
196 bsk:DoAddPersonToListEnd(change)
197 elseif change.action == "AddToListRand" then
198 bsk:DoAddPersonToListRandom(change)
199 elseif change.action == "SuicidePerson" then
200 bsk:DoSuicidePerson(change)
201 else
202 bsk:Print("Unknown message encountered")
203 bsk:PrintTable(change)
204 assert(false)
205 end
206 end
207
208 --}}}
188 209
189 -- timestamp logic: 210 -- timestamp logic:
190 -- use time() for comparisons - local clients use date() to make it pretty. only 211 -- use time() for comparisons - local clients use date() to make it pretty. only
191 -- dowisde - we can't have a server timestamp. Which kind of sucks, but it turns 212 -- dowisde - we can't have a server timestamp. Which kind of sucks, but it turns
192 -- out you can change timezones when you enter an instance server, so you really 213 -- out you can change timezones when you enter an instance server, so you really
213 -- Whenever an admin signon event happens, have the admins each perform a 234 -- Whenever an admin signon event happens, have the admins each perform a
214 -- timestamp check. Issue warnings for anyone with a clock that's more than 235 -- timestamp check. Issue warnings for anyone with a clock that's more than
215 -- X seconds out of sync with the others. Seriously, why isn't NTP a standard 236 -- X seconds out of sync with the others. Seriously, why isn't NTP a standard
216 -- setting on all operating systems ... 237 -- setting on all operating systems ...
217 238
218 function bsk:ProcessChange(change)
219 if change.action == "AddPerson" then
220 bsk:DoAddPerson(change)
221 elseif change.action == "CreateList" then
222 bsk:DoCreateList(change)
223 elseif change.action == "AddToListEnd" then
224 bsk:DoAddPersonToListEnd(change)
225 elseif change.action == "AddToListRand" then
226 bsk:DoAddPersonToListRandom(change)
227 elseif change.action == "SuicidePerson" then
228 bsk:DoSuicidePerson(change)
229 else
230 bsk:Print("Unknown message encountered")
231 bsk:PrintTable(change)
232 assert(false)
233 end
234 end
235
236 -- Action and DoAction defs {{{ 239 -- Action and DoAction defs {{{
237 -- 240 --
238 -- The actual actions for changes start here 241 -- The actual actions for changes start here
239 -- 242 --
240 -- Each action occurs as a pair of functions. The bsk:Action() function is from 243 -- Each action occurs as a pair of functions. The bsk:Action() function is from
261 local name = arg.name 264 local name = arg.name
262 local id = arg.id 265 local id = arg.id
263 assert(persons[id]==nil) 266 assert(persons[id]==nil)
264 persons[id] = {main=name} 267 persons[id] = {main=name}
265 persons.time=change.time 268 persons.time=change.time
266 personsReverse[name] = id 269 personName2id[name] = id
267 return true 270 return true
268 end 271 end
269 272
270 function bsk:AddPerson(name) 273 function bsk:AddPerson(name)
271 local persons = bsk.persons 274 local persons = bsk.persons
325 list.closedRandom = true 328 list.closedRandom = true
326 329
327 return true 330 return true
328 end 331 end
329 332
330 function bsk:AddPersonToListEnd(name,list) 333 function bsk:AddPersonToListEnd(name,listName)
331 -- require admin 334 -- require admin
332 local listIndex = bsk:GetListIndex(list) 335 local listIndex = bsk:GetListIndex(listName)
333 local id = personsReverse[name] 336 local id = personName2id[name]
334 if bsk:IdIsInList(id,bsk.lists[listIndex]) then 337 if bsk:IdIsInList(id,bsk.lists[listIndex]) then
335 bsk:Print(sformat("Person %s is already on the reqeuested list",name)) 338 bsk:Print(sformat("Person %s is already on the reqeuested list",name))
336 return 339 return
337 end 340 end
338 bsk:Print(sformat("Adding %s (%s) to list %s (%s)", name, id, list, listIndex)) 341 bsk:Print(sformat("Adding %s (%s) to list %s (%s)", name, id, listName, listIndex))
339 local change = {action="AddToListEnd",arg={id=id,listIndex=listIndex}} 342 local change = {action="AddToListEnd",arg={id=id,listIndex=listIndex}}
340 bsk:StartChange(change) 343 bsk:StartChange(change)
341 if bsk:DoAddPersonToListEnd(change) then 344 if bsk:DoAddPersonToListEnd(change) then
342 bsk:CommitChange(change) 345 bsk:CommitChange(change)
343 end 346 end
352 list.time = change.time 355 list.time = change.time
353 356
354 return true 357 return true
355 end 358 end
356 359
357 function bsk:AddPersonToListRandom(name,list) 360 function bsk:AddPersonToListRandom(name,listName)
358 -- require admin 361 -- require admin
359 local listIndex = bsk:GetListIndex(list) 362 local listIndex = bsk:GetListIndex(listName)
360 if bsk.lists[listIndex].closedRandom then 363 if bsk.lists[listIndex].closedRandom then
361 self:Print("Cannot add person to list by random roll because an add-to-end operation has already occurred") 364 self:Print("Cannot add person to list by random roll because an add-to-end operation has already occurred")
362 return false 365 return false
363 end 366 end
364 if bsk:IdIsInList(id,bsk.lists[listIndex]) then 367 if bsk:IdIsInList(id,bsk.lists[listIndex]) then
365 bsk:Print(sformat("Person %s is already on the reqeuested list",name)) 368 bsk:Print(sformat("Person %s is already on the reqeuested list",name))
366 return 369 return
367 end 370 end
368 local id = personsReverse[name] 371 local id = personName2id[name]
369 local roll = math.random() 372 local roll = math.random()
370 bsk:Print(sformat("Adding %s (%s) to list %s (%s) with roll (%f)", name, id, list, listIndex, roll)) 373 bsk:Print(sformat("Adding %s (%s) to list %s (%s) with roll (%f)", name, id, listName, listIndex, roll))
371 local change = {action="AddToListRand",arg={id=id,listIndex=listIndex,roll=roll}} 374 local change = {action="AddToListRand",arg={id=id,listIndex=listIndex,roll=roll}}
372 bsk:StartChange(change) 375 bsk:StartChange(change)
373 if bsk:DoAddPersonToListRandom(change) then 376 if bsk:DoAddPersonToListRandom(change) then
374 bsk:CommitChange(change) 377 bsk:CommitChange(change)
375 end 378 end
383 function bsk:RemovePerson(name) 386 function bsk:RemovePerson(name)
384 -- from both persons and lists 387 -- from both persons and lists
385 end 388 end
386 389
387 function bsk:DoSuicidePerson(change) 390 function bsk:DoSuicidePerson(change)
388 local listIndex = change.arg.listIndex 391 local list = bsk.lists[change.arg.listIndex]
389 local list = bsk.lists[listIndex] 392 local affected = shallowCopy(change.arg.affect)
390 local slist = shallowCopy(change.arg.list)
391 -- the goal here is to rotate the suicide list by 1 393 -- the goal here is to rotate the suicide list by 1
392 -- then we can just mash it on top of the intersection between the original 394 -- then we can just mash it on top of the intersection between the original
393 -- list and the working copy 395 -- list and the working copy
394 local stemp = shallowCopy(change.arg.list) 396 local replacement = shallowCopy(change.arg.affect)
395 local temp = table.remove(stemp,1) -- pop 397 local temp = table.remove(replacement,1) -- pop
396 tinsert(stemp,temp) -- push_back 398 tinsert(replacement,temp) -- push_back
397 --bsk:Print(sformat("Before suicide of %s on list %s",slist[1],list.name)) 399 --bsk:Print(sformat("Before suicide of %s on list %s",slist[1],list.name))
398 --bsk:PrintTable(list) 400 --bsk:PrintTable(list)
399 for i = 1, #list do 401 for i = 1, #list do
400 if list[i].id == slist[1] then 402 if list[i].id == affect[1] then
401 table.remove(slist,1) 403 table.remove(affect,1)
402 list[i].id = stemp[1] 404 list[i].id = replacement[1]
403 table.remove(stemp,1) 405 table.remove(replacement,1)
404 end 406 end
405 end 407 end
406 list.time=change.time 408 list.time=change.time
407 return true 409 return true
408 end 410 end
409 411
410 function bsk:SuicidePerson(name,list) 412 function bsk:SuicidePerson(name,list)
411 -- require admin 413 -- require admin
412 bsk:PopulateRaidList() 414 bsk:PopulateRaidList()
413 local listIndex = bsk:GetListIndex(list) 415 local listIndex = bsk:GetListIndex(listName)
414 local id = personsReverse[name] 416 local id = personName2id[name]
415 local slist=bsk:GetSuicideList(id,bsk.lists[listIndex]) 417 local affect=bsk:GetSuicideList(id,bsk.lists[listIndex])
416 local change = {action="SuicidePerson",arg={list=slist,listIndex=listIndex}} 418 local change = {action="SuicidePerson",arg={affect=affect,listIndex=listIndex}}
417 bsk:PrintTable(change) 419 bsk:PrintTable(change)
418 bsk:StartChange(change) 420 bsk:StartChange(change)
419 if bsk:DoSuicidePerson(change) then 421 if bsk:DoSuicidePerson(change) then
420 bsk:CommitChange(change) 422 bsk:CommitChange(change)
421 end 423 end
459 --}}} 461 --}}}
460 -- Higher order actions (ie calls other Doers){{{ 462 -- Higher order actions (ie calls other Doers){{{
461 function bsk:AddMissingPersons() 463 function bsk:AddMissingPersons()
462 bsk:PopulateRaidList() 464 bsk:PopulateRaidList()
463 local t = {} 465 local t = {}
464 for _,v in pairs(bsk.persons) do 466 for i,_ in pairs(bsk.persons) do
465 t[v.main] = true 467 t[i] = true
466 -- TODO: also add alts here 468 end
467 end 469 for i,_ in pairs(raidNameP) do
468 for i,_ in pairs(raidList) do
469 if t[i] == nil then 470 if t[i] == nil then
470 bsk:Print(sformat("Person %s is missing from the persons list - adding",i)) 471 bsk:Print(sformat("Person %s is missing from the persons list - adding",i))
471 bsk:AddPerson(i) 472 bsk:AddPerson(i)
472 end 473 end
473 end 474 end
478 bsk:PopulateRaidList() 479 bsk:PopulateRaidList()
479 local list = bsk.lists[index] 480 local list = bsk.lists[index]
480 481
481 local t = {} 482 local t = {}
482 --for i = 1,#list do 483 --for i = 1,#list do
483 -- if not (raidList(list[i]) or reserveList(list[i])) then 484 -- if not (raidNameP(list[i]) or reserveIdP(list[i])) then
484 -- tinsert(t,) 485 -- tinsert(t,)
485 -- end 486 -- end
486 --end 487 --end
487 end 488 end
488 --}}} 489 --}}}
489
490 -- "Soft" actions- ie things that cause nonpermanent state {{{ 490 -- "Soft" actions- ie things that cause nonpermanent state {{{
491 491
492 -- reserves 492 -- reserves
493 function bsk:AddReserve(name) 493 function bsk:AddReserve(name)
494 reserveList[personsReverse[name]]=true 494 reserveIdP[personName2id[name]]=true
495 -- TODO: communicate to others. don't store this in any way. 495 -- TODO: communicate to others. don't store this in any way.
496 end 496 end
497 497
498 function bsk:RemoveReserve(name) 498 function bsk:RemoveReserve(name)
499 reserveList[personsReverse[name]]=false 499 reserveIdP[personName2id[name]]=false
500 -- TODO: communicate to others. don't store this in any way. 500 -- TODO: communicate to others. don't store this in any way.
501 end 501 end
502 502
503 503
504 --function bsk:GetActiveList() 504 --function bsk:GetActiveList()
518 rID[i] = format("raid%d", i) 518 rID[i] = format("raid%d", i)
519 end 519 end
520 function bsk:PopulateRaidList() 520 function bsk:PopulateRaidList()
521 local inParty = GetNumPartyMembers() 521 local inParty = GetNumPartyMembers()
522 local inRaid = GetNumRaidMembers() 522 local inRaid = GetNumRaidMembers()
523 523 local add = function(name) raidNameP[name]=true; if personName2id[id] == nil then end end
524 wipe(raidList) 524
525 wipe(raidNameP)
526 wipe(raidIdP)
525 if inRaid > 0 then 527 if inRaid > 0 then
526 for i = 1, inRaid do 528 for i = 1, inRaid do
527 raidList[personsReverse[UnitName(rID[i])]]=true 529 local name = UnitName(rID[i])
530 raidNameP[name]=true
531 raidIdP[personName2id[name]]=true
528 end 532 end
529 elseif inParty > 0 then 533 elseif inParty > 0 then
530 for i = 1, inParty do 534 for i = 1, inParty do
531 raidList[personsReverse[UnitName(pID[i])]]=true 535 local name = UnitName(pID[i])
536 raidNameP[name]=true
537 raidIdP[personName2id[name]]=true
532 end 538 end
533 -- Now add yourself as the last party member 539 -- Now add yourself as the last party member
534 raidList[personsReverse[UnitName("player")]]=true 540 local name = UnitName("player")
541 raidNameP[name]=true
542 raidIdP[personName2id[name]]=true
535 else 543 else
536 -- You're alone 544 -- You're alone
537 raidList[personsReverse[UnitName("player")]]=true 545 local name = UnitName("player")
546 raidNameP[name]=true
547 raidIdP[personName2id[name]]=true
538 end 548 end
539 end 549 end
540 550
541 -- undo rules! 551 -- undo rules!
542 -- only the most recent event can be undone 552 -- only the most recent event can be undone
555 local pushing = false 565 local pushing = false
556 for i = 1, #list do 566 for i = 1, #list do
557 if list[i].id == id then 567 if list[i].id == id then
558 pushing = true 568 pushing = true
559 end 569 end
560 if pushing and (raidList[list[i].id] or reserveList[list[i].id]) then 570 if pushing and (raidIdP[list[i].id] or reserveIdP[list[i].id]) then
561 tinsert(ret,list[i].id) 571 tinsert(ret,list[i].id)
562 end 572 end
563 end 573 end
564 bsk:Print("GSL") 574 --bsk:Print("GSL")
565 bsk:PrintTable(ret) 575 --bsk:PrintTable(ret)
566 bsk:Print("GSL") 576 --bsk:Print("GSL")
567 return ret 577 return ret
568 end 578 end
569 579
570 function bsk:IdIsInList(id,listRef) 580 function bsk:IdIsInList(id,listRef)
571 for i = 1,#listRef do 581 for i = 1,#listRef do