changeset 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 dbfb8f2052b6
children 71fc79846a5d
files Lists.lua
diffstat 1 files changed, 73 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/Lists.lua	Thu Mar 08 12:48:29 2012 -0500
+++ b/Lists.lua	Thu Mar 08 12:49:17 2012 -0500
@@ -61,10 +61,11 @@
 bsk.lists = {}
 bsk.persons = {}
 
-local raidList = {}
-local reserveList = {}
+local raidNameP = {} -- "name" is present in raid
+local raidIdP = {} -- "id" is present in raid
+local reserveIdP = {} -- "reserve id present"
 local activeListKey = 1 -- temporary
-local personsReverse = {} 
+local personName2id = {} -- given "name" get that person's id
 
 local tinsert = table.insert
 local sformat = string.format
@@ -132,11 +133,12 @@
 function bsk:UpdatePersonsReverse()
     for i,v in pairs(bsk.persons) do
         if i ~= "time" then
-            personsReverse[v.main] = i
+            personName2id[v.main] = i
         end
     end
 end
 
+-- Change processing {{{
 function bsk:CreateWorkingStateFromChanges(changes)
     local personsBase = self.db.profile.persons
     local listBase = self.db.profile.listBase
@@ -144,7 +146,7 @@
     -- copy the base to the working state
     wipe(bsk.lists)
     wipe(bsk.persons)
-    wipe(personsReverse)
+    wipe(personName2id)
 
     bsk:tcopy(bsk.lists,listBase)
     bsk:tcopy(bsk.persons,personsBase)
@@ -185,6 +187,25 @@
     -- TODO: broadcast change
 end
 
+function bsk:ProcessChange(change)
+    if change.action == "AddPerson" then
+        bsk:DoAddPerson(change)
+    elseif change.action == "CreateList" then
+        bsk:DoCreateList(change)
+    elseif change.action == "AddToListEnd" then
+        bsk:DoAddPersonToListEnd(change)
+    elseif change.action == "AddToListRand" then
+        bsk:DoAddPersonToListRandom(change)
+    elseif change.action == "SuicidePerson" then
+        bsk:DoSuicidePerson(change)
+    else
+        bsk:Print("Unknown message encountered")
+        bsk:PrintTable(change)
+        assert(false)
+    end 
+end
+
+--}}}
 
 -- timestamp logic:
 -- use time() for comparisons - local clients use date() to make it pretty. only
@@ -215,24 +236,6 @@
 --  X seconds out of sync with the others. Seriously, why isn't NTP a standard
 --  setting on all operating systems ...
 
-function bsk:ProcessChange(change)
-    if change.action == "AddPerson" then
-        bsk:DoAddPerson(change)
-    elseif change.action == "CreateList" then
-        bsk:DoCreateList(change)
-    elseif change.action == "AddToListEnd" then
-        bsk:DoAddPersonToListEnd(change)
-    elseif change.action == "AddToListRand" then
-        bsk:DoAddPersonToListRandom(change)
-    elseif change.action == "SuicidePerson" then
-        bsk:DoSuicidePerson(change)
-    else
-        bsk:Print("Unknown message encountered")
-        bsk:PrintTable(change)
-        assert(false)
-    end 
-end
-
 -- Action and DoAction defs {{{
 --
 -- The actual actions for changes start here
@@ -263,7 +266,7 @@
     assert(persons[id]==nil)
     persons[id] = {main=name}
     persons.time=change.time
-    personsReverse[name] = id
+    personName2id[name] = id
     return true
 end
 
@@ -327,15 +330,15 @@
     return true
 end
 
-function bsk:AddPersonToListEnd(name,list)
+function bsk:AddPersonToListEnd(name,listName)
     -- require admin
-    local listIndex = bsk:GetListIndex(list)
-    local id = personsReverse[name]
+    local listIndex = bsk:GetListIndex(listName)
+    local id = personName2id[name]
     if bsk:IdIsInList(id,bsk.lists[listIndex]) then
         bsk:Print(sformat("Person %s is already on the reqeuested list",name))
         return
     end
-    bsk:Print(sformat("Adding %s (%s) to list %s (%s)", name, id, list, listIndex))
+    bsk:Print(sformat("Adding %s (%s) to list %s (%s)", name, id, listName, listIndex))
     local change = {action="AddToListEnd",arg={id=id,listIndex=listIndex}}
     bsk:StartChange(change)
     if bsk:DoAddPersonToListEnd(change) then
@@ -354,9 +357,9 @@
     return true
 end
 
-function bsk:AddPersonToListRandom(name,list)
+function bsk:AddPersonToListRandom(name,listName)
     -- require admin
-    local listIndex = bsk:GetListIndex(list)
+    local listIndex = bsk:GetListIndex(listName)
     if bsk.lists[listIndex].closedRandom then
         self:Print("Cannot add person to list by random roll because an add-to-end operation has already occurred")
         return false
@@ -365,9 +368,9 @@
         bsk:Print(sformat("Person %s is already on the reqeuested list",name))
         return
     end
-    local id = personsReverse[name]
+    local id = personName2id[name]
     local roll = math.random()
-    bsk:Print(sformat("Adding %s (%s) to list %s (%s) with roll (%f)", name, id, list, listIndex, roll))
+    bsk:Print(sformat("Adding %s (%s) to list %s (%s) with roll (%f)", name, id, listName, listIndex, roll))
     local change = {action="AddToListRand",arg={id=id,listIndex=listIndex,roll=roll}}
     bsk:StartChange(change)
     if bsk:DoAddPersonToListRandom(change) then
@@ -385,22 +388,21 @@
 end
 
 function bsk:DoSuicidePerson(change)
-    local listIndex = change.arg.listIndex
-    local list = bsk.lists[listIndex]
-    local slist = shallowCopy(change.arg.list)
+    local list = bsk.lists[change.arg.listIndex]
+    local affected = shallowCopy(change.arg.affect)
     -- the goal here is to rotate the suicide list by 1
     -- then we can just mash it on top of the intersection between the original
     -- list and the working copy
-    local stemp = shallowCopy(change.arg.list)
-    local temp = table.remove(stemp,1) -- pop
-    tinsert(stemp,temp) -- push_back
+    local replacement = shallowCopy(change.arg.affect)
+    local temp = table.remove(replacement,1) -- pop
+    tinsert(replacement,temp) -- push_back
     --bsk:Print(sformat("Before suicide of %s on list %s",slist[1],list.name))
     --bsk:PrintTable(list)
     for i = 1, #list do
-        if list[i].id == slist[1] then
-            table.remove(slist,1)
-            list[i].id = stemp[1]
-            table.remove(stemp,1)
+        if list[i].id == affect[1] then
+            table.remove(affect,1)
+            list[i].id = replacement[1]
+            table.remove(replacement,1)
         end
     end
     list.time=change.time
@@ -410,10 +412,10 @@
 function bsk:SuicidePerson(name,list)
     -- require admin
     bsk:PopulateRaidList()
-    local listIndex = bsk:GetListIndex(list)
-    local id = personsReverse[name]
-    local slist=bsk:GetSuicideList(id,bsk.lists[listIndex])
-    local change = {action="SuicidePerson",arg={list=slist,listIndex=listIndex}}
+    local listIndex = bsk:GetListIndex(listName)
+    local id = personName2id[name]
+    local affect=bsk:GetSuicideList(id,bsk.lists[listIndex])
+    local change = {action="SuicidePerson",arg={affect=affect,listIndex=listIndex}}
     bsk:PrintTable(change)
     bsk:StartChange(change)
     if bsk:DoSuicidePerson(change) then
@@ -461,11 +463,10 @@
 function bsk:AddMissingPersons()
     bsk:PopulateRaidList() 
     local t = {}
-    for _,v in pairs(bsk.persons) do
-        t[v.main] = true
-        -- TODO: also add alts here
+    for i,_ in pairs(bsk.persons) do
+        t[i] = true
     end
-    for i,_ in pairs(raidList) do
+    for i,_ in pairs(raidNameP) do
         if t[i] == nil then
             bsk:Print(sformat("Person %s is missing from the persons list - adding",i))
             bsk:AddPerson(i)
@@ -480,23 +481,22 @@
 
     local t = {}
     --for i = 1,#list do
-    --    if not (raidList(list[i]) or reserveList(list[i])) then
+    --    if not (raidNameP(list[i]) or reserveIdP(list[i])) then
     --        tinsert(t,)
     --    end
     --end
 end
 --}}}
-
 -- "Soft" actions- ie things that cause nonpermanent state {{{
 
 -- reserves
 function bsk:AddReserve(name)
-    reserveList[personsReverse[name]]=true
+    reserveIdP[personName2id[name]]=true
     -- TODO: communicate to others. don't store this in any way.
 end
 
 function bsk:RemoveReserve(name)
-    reserveList[personsReverse[name]]=false
+    reserveIdP[personName2id[name]]=false
     -- TODO: communicate to others. don't store this in any way.
 end
 
@@ -520,21 +520,31 @@
 function bsk:PopulateRaidList()
     local inParty = GetNumPartyMembers()
     local inRaid = GetNumRaidMembers()
+    local add = function(name) raidNameP[name]=true; if personName2id[id] == nil then  end end
 
-    wipe(raidList)
+    wipe(raidNameP)
+    wipe(raidIdP)
     if inRaid > 0 then
         for i = 1, inRaid do
-            raidList[personsReverse[UnitName(rID[i])]]=true
+            local name = UnitName(rID[i])
+            raidNameP[name]=true
+            raidIdP[personName2id[name]]=true
         end
     elseif inParty > 0 then
         for i = 1, inParty do
-            raidList[personsReverse[UnitName(pID[i])]]=true
+            local name = UnitName(pID[i])
+            raidNameP[name]=true
+            raidIdP[personName2id[name]]=true
         end
         -- Now add yourself as the last party member
-        raidList[personsReverse[UnitName("player")]]=true
+        local name = UnitName("player")
+        raidNameP[name]=true
+        raidIdP[personName2id[name]]=true
     else
         -- You're alone
-        raidList[personsReverse[UnitName("player")]]=true
+        local name = UnitName("player")
+        raidNameP[name]=true
+        raidIdP[personName2id[name]]=true
     end
 end
 
@@ -557,13 +567,13 @@
         if list[i].id == id then
             pushing = true
         end
-        if pushing and (raidList[list[i].id] or reserveList[list[i].id]) then
+        if pushing and (raidIdP[list[i].id] or reserveIdP[list[i].id]) then
             tinsert(ret,list[i].id)
         end
     end
-    bsk:Print("GSL")
-    bsk:PrintTable(ret)
-    bsk:Print("GSL")
+    --bsk:Print("GSL")
+    --bsk:PrintTable(ret)
+    --bsk:Print("GSL")
     return ret
 end