diff Lists.lua @ 8:b05fcb225c4a

player -> person fix trim bug when trimming all shortened IDs for persons list keys
author John@Yosemite-PC
date Wed, 07 Mar 2012 00:18:05 -0500
parents 241986f7066c
children daed0597deba
line wrap: on
line diff
--- a/Lists.lua	Tue Mar 06 09:33:04 2012 -0500
+++ b/Lists.lua	Wed Mar 07 00:18:05 2012 -0500
@@ -43,12 +43,14 @@
 -- Handling conflicts:
 --
 
+-- todo: list-of-lists must not use int indices. those will lead to peril.
 bsk.lists = {}
-bsk.players = {}
+bsk.persons = {}
 
-local RaidList = {}
-local ReserveList = {}
-local activeList = 0 -- temporary
+local raidList = {}
+local reserveList = {}
+local activeListKey = 1 -- temporary
+local personsReverse = {} 
 
 local tinsert = table.insert
 local sformat = string.format
@@ -77,8 +79,8 @@
 function bsk:PrintChanges()
     bsk:PrintTable(bsk.db.profile.changes)
 end
-function bsk:PrintPlayers()
-    bsk:PrintTable(bsk.players)
+function bsk:PrintPersons()
+    bsk:PrintTable(bsk.persons)
 end
 function bsk:PrintTable(table, depth)
     depth = depth or ""
@@ -102,14 +104,16 @@
 --}}}
 
 function bsk:CreateWorkingStateFromChanges(changes)
-    local playerBase = self.db.profile.players
+    local personsBase = self.db.profile.persons
     local listBase = self.db.profile.listBase
 
     -- copy the base to the working state
     wipe(bsk.lists)
-    wipe(bsk.players)
+    wipe(bsk.persons)
+    wipe(personsReverse)
+
     bsk:tcopy(bsk.lists,listBase)
-    bsk:tcopy(bsk.players,playerBase)
+    bsk:tcopy(bsk.persons,personsBase)
 
     -- now just go through the changes list applying each
     for i,v in ipairs(changes) do
@@ -175,14 +179,14 @@
 --  setting on all operating systems ...
 
 function bsk:ProcessChange(change)
-    if change.action == "AddPlayer" then
-        bsk:DoAddPlayer(change)
+    if change.action == "AddPerson" then
+        bsk:DoAddPerson(change)
     elseif change.action == "CreateList" then
         bsk:DoCreateList(change)
-    elseif change.action == "AddPlayerToList" then
-        bsk:DoAddPlayerToList(change)
-    elseif change.action == "SuicidePlayer" then
-        bsk:DoSuicidePlayer(change)
+    elseif change.action == "AddPersonToList" then
+        bsk:DoAddPersonToList(change)
+    elseif change.action == "SuicidePerson" then
+        bsk:DoSuicidePerson(change)
     else
         bsk:Print("Unknown message encountered")
         bsk:PrintTable(change)
@@ -208,39 +212,43 @@
 -- Note that "undo" has no special voodoo to it. It's basically a change that
 -- reverses the prior change on the stack.
 
--- Players list
-function bsk:DoAddPlayer(change)
+-- persons list
+function bsk:DoAddPerson(change)
     assert(change)
-    assert(change.arg.guid)
+    assert(change.arg.id)
     local arg = change.arg
     -- require admin
-    local players = bsk.players
+    local persons = bsk.persons
     local name = arg.name
-    local guid = arg.guid
-    assert(players[guid]==nil)
-    players[guid] = name
-    players.time=change.time
+    local id = arg.id
+    assert(persons[id]==nil)
+    persons[id] = {main=name}
+    persons.time=change.time
+    personsReverse[name] = id
     return true
 end
 
-function bsk:AddPlayer(name)
-    local players = bsk.players
+function bsk:AddPerson(name)
+    local persons = bsk.persons
     local guid = UnitGUID(name)
     -- TODO: check guid to be sure it's a player
     if not guid then
         self:Print(sformat("Could not add player %s - they must be in range or group",name))
         return
     end
-    if players[guid] and players[guid] ~= name then
-        self:Print(sformat("Namechange detected for %s - new is %s, please rename the existing entry", players[guid], name))
+    local id = string.sub(guid,6) -- skip at least 0x0580 ...
+    id = id:gsub("^0*(.*)","%1") -- nom all leading zeroes remaining
+    
+    if persons[id] and persons[id] ~= name then
+        self:Print(sformat("Namechange detected for %s - new is %s, please rename the existing entry", persons[id], name))
         return
     end
-    if players[guid] ~= nil then
-        self:Print(sformat("%s is already in the players list; disregarding", name))
+    if persons[id] ~= nil then
+        self:Print(sformat("%s is already in the persons list; disregarding", name))
         return
     end
-    local change = {action="AddPlayer",arg={name=name,guid=guid}}
-    if bsk:DoAddPlayer(change) then
+    local change = {action="AddPerson",arg={name=name,id=id}}
+    if bsk:DoAddPerson(change) then
         bsk:CreateChange(change)
     end
 end
@@ -268,7 +276,7 @@
     end
 end
 
-function bsk:DoAddPlayerToList(change)
+function bsk:DoAddPersonToList(change)
     local listIndex = change.arg.listIndex
     local slist = change.arg.slist
     local list = bsk.lists[listIndex]
@@ -283,27 +291,28 @@
     return true
 end
 
-function bsk:AddPlayerToList(name,list)
+function bsk:AddPersonToList(name,list)
     -- require admin
     local listIndex = bsk:GetListIndex(list)
-    local slist = {name} -- TODO: support adding to elsewhere besides the end
-    local change = {action="AddPlayerToList",arg={name=name,listIndex=listIndex,slist=slist}}
+    local id = personsReverse[name]
+    local slist = {id} -- TODO: support adding to elsewhere besides the end
+    local change = {action="AddPersonToList",arg={id=id,listIndex=listIndex,slist=slist}}
     bsk:StartChange(change)
-    if bsk:DoAddPlayerToList(change) then
+    if bsk:DoAddPersonToList(change) then
         bsk:CommitChange(change)
     end
 end
 
-function bsk:DoRemovePlayer(change)
+function bsk:DoRemovePerson(change)
 
     -- return true
 end
 
-function bsk:RemovePlayer(name)
-    -- from both players and lists
+function bsk:RemovePerson(name)
+    -- from both persons and lists
 end
 
-function bsk:DoSuicidePlayer(change)
+function bsk:DoSuicidePerson(change)
     local listIndex = change.arg.listIndex
     local list = bsk.lists[listIndex]
     local slist = shallowCopy(change.arg.list)
@@ -328,14 +337,14 @@
     return true
 end
 
-function bsk:SuicidePlayer(name,list)
+function bsk:SuicidePerson(name,list)
     -- require admin
     bsk:PopulateRaidList()
     local listIndex = bsk:GetListIndex(list)
     local slist=bsk:GetSuicideList(name,bsk.lists[listIndex])
-    local change = {action="SuicidePlayer",arg={names=names,list=slist,listIndex=listIndex}}
+    local change = {action="SuicidePerson",arg={names=names,list=slist,listIndex=listIndex}}
     bsk:StartChange(change)
-    if bsk:DoSuicidePlayer(change) then
+    if bsk:DoSuicidePerson(change) then
        bsk:CommitChange(change)
     end
 end
@@ -365,9 +374,9 @@
     bsk:CreateWorkingStateFromChanges(before)
 
     -- save this state permanently; trim the changes permanently
-    bsk:tcopy(bsk.db.profile.players,bsk.players)
+    bsk:tcopy(bsk.db.profile.persons,bsk.persons)
     bsk:tcopy(bsk.db.profile.listBase,bsk.lists)
-    while bsk.db.profile.changes[1].time <= time do
+    while bsk.db.profile.changes ~= nil and bsk.db.profile.changes[1] ~= nil and bsk.db.profile.changes[1].time <= time do
         table.remove(bsk.db.profile.changes,1)
     end
 
@@ -377,16 +386,17 @@
 
 --}}}
 -- Higher order actions (ie calls other Doers){{{
-function bsk:AddMissingPlayers()
+function bsk:AddMissingPersons()
     bsk:PopulateRaidList() 
     local t = {}
-    for i,v in pairs(bsk.players) do
-        t[v] = true
+    for _,v in pairs(bsk.persons) do
+        t[v.main] = true
+        -- TODO: also add alts here
     end
-    for i,v in pairs(RaidList) do
+    for i,_ in pairs(raidList) do
         if t[i] == nil then
-            bsk:Print(sformat("Player %s is missing from the players list - adding",i))
-            bsk:AddPlayer(i)
+            bsk:Print(sformat("Person %s is missing from the persons list - adding",i))
+            bsk:AddPerson(i)
         end
     end
     -- TODO: batch into a single op - no need to spam 25 messages in a row
@@ -398,7 +408,7 @@
 
     local t = {}
     --for i = 1,#list do
-    --    if not (RaidList(list[i]) or ReserveList(list[i])) then
+    --    if not (raidList(list[i]) or reserveList(list[i])) then
     --        tinsert(t,)
     --    end
     --end
@@ -409,12 +419,12 @@
 
 -- reserves
 function bsk:AddReserve(name)
-    ReserveList[name]=true
+    reserveList[name]=true
     -- TODO: communicate to others. don't store this in any way.
 end
 
 function bsk:RemoveReserve(name)
-    ReserveList[name]=false
+    reserveList[name]=false
     -- TODO: communicate to others. don't store this in any way.
 end
 
@@ -439,20 +449,20 @@
     local inParty = GetNumPartyMembers()
     local inRaid = GetNumRaidMembers()
 
-    wipe(RaidList)
+    wipe(raidList)
     if inRaid > 0 then
         for i = 1, inRaid do
-            RaidList[UnitName(rID[i])]=true
+            raidList[UnitName(rID[i])]=true
         end
     elseif inParty > 0 then
         for i = 1, inParty do
-            RaidList[UnitName(pID[i])]=true
+            raidList[UnitName(pID[i])]=true
         end
         -- Now add yourself as the last party member
-        RaidList[UnitName("player")]=true
+        raidList[UnitName("player")]=true
     else
         -- You're alone
-        RaidList[UnitName("player")]=true
+        raidList[UnitName("player")]=true
     end
 end
 
@@ -475,7 +485,7 @@
         if list[i] == name then
             pushing = true
         end
-        if pushing and (RaidList[list[i]] or ReserveList[list[i]]) then
+        if pushing and (raidList[list[i]] or reserveList[list[i]]) then
             tinsert(ret,list[i])
         end
     end