Working with new data layout

His Dad [02-23-16 - 05:06]
Working with new data layout
Filename
AIResearchGrid.lua
diff --git a/AIResearchGrid.lua b/AIResearchGrid.lua
index d4bd36f..7479c53 100644
--- a/AIResearchGrid.lua
+++ b/AIResearchGrid.lua
@@ -25,7 +25,7 @@ AIRG.L = {}   -- declare language strings

 -- Set-up the defaults options for saved variables.
 AIRG.defaults = {
-    data        = {},
+    traits        = {},
     styles      = {},
     showMotifs = true,
 }
@@ -125,7 +125,7 @@ function AIRG.initUI()
     local function OnItemSelect(_, choiceText, choice)  --this is the callback function for when an item gets selected in the dropdown
         AIRG.OnCharacterSelect(choiceText)
     end
-    for charName, _ in pairs(AIRG.vars.data) do
+    for charName, _ in pairs(AIRG.vars.char) do
         local entry = AIRG.UI.charDropdown.dropdown:CreateItemEntry(charName, OnItemSelect)
         AIRG.UI.charDropdown.dropdown:AddItem(entry)
     end
@@ -405,9 +405,9 @@ function AIRG.OnCraftSelected(_,thisCraft)

             traitCount = 0
             --Check for research expiry
-            for rowNum,tKnown in pairs(AIRG.vars.data[AIRG.curCharacter][thisCraft][i]) do
+            for rowNum,tKnown in pairs(AIRG.vars.char[AIRG.curCharacter].traits[thisCraft][i]) do
               if (tKnown > 0 and tKnown < GetTimeStamp()) then
-                AIRG.vars.data[AIRG.curCharacter][thisCraft][i][rowNum] = -1  -- Change to known
+                AIRG.vars.char[AIRG.curCharacter].traits[thisCraft][i][rowNum] = -1  -- Change to known
               end
             end

@@ -417,7 +417,7 @@ function AIRG.OnCraftSelected(_,thisCraft)
               -- Great! relative mode! we need to address a matter of practical usage.
               --  We need to see that we actually can research that now, because we can't research more than one item type at a time.
               --  So we need to pre-check for item research and set the colours (of the texture) appropriately
-              for rowNum,tKnown in pairs(AIRG.vars.data[AIRG.curCharacter][thisCraft][i]) do
+              for rowNum,tKnown in pairs(AIRG.vars.char[AIRG.curCharacter].traits[thisCraft][i]) do
                   if (tKnown > 0) then  -- researching
                       busy = true
                   end
@@ -441,7 +441,7 @@ function AIRG.OnCraftSelected(_,thisCraft)


             else -- not relative
-              for rowNum,tKnown in pairs(AIRG.vars.data[AIRG.curCharacter][thisCraft][i]) do
+              for rowNum,tKnown in pairs(AIRG.vars.char[AIRG.curCharacter].traits[thisCraft][i]) do
 				AIRG.UI.gridButtons[i][rowNum].Known = tKnown

                 if (tKnown == -1) then       -- Trait is known
@@ -496,19 +496,11 @@ function AIRG.DeleteCharacter(charName)
     if (charName == AIRG.curCharacter) then
         d(AIRG.L["DeleteFalse"])
     else
-        if (AIRG.vars.data[charName] ~= nil) then
-            AIRG.vars.data[charName] = nil
+        if (AIRG.vars.char[charName] ~= nil) then
+            AIRG.vars.char[charName] = nil
             d(AIRG.L["DeleteTrue"] .. charName)
         end

-        if (AIRG.vars.StyleSingle[charName] ~= nil) then
-            AIRG.vars.StyleSingle[charName] = nil
-        end
-
-        if (AIRG.vars.StyleChapter[charName] ~= nil) then
-            AIRG.vars.StyleChapter[charName] = nil
-        end
-
         AIRG.PopulateAll()
 		--this part is new, Thank you Garkin
         for i, item in ipairs(AIRG.UI.charDropdown.dropdown.m_sortedItems) do
@@ -552,13 +544,13 @@ end -- AIRG.PopulateMatrix
 function AIRG.PopulateStyleData()

       for StyleIndex, Style in ipairs (AIRG.StyleSingleDat) do
-        AIRG.vars.StyleSingle[AIRG.curCharacter][StyleIndex] = IsSmithingStyleKnown(Style[2], 1) --patternIndex set to 1, temporary workaround for changes introduced in Update 4
+        AIRG.vars.char[AIRG.curCharacter].StyleSingle[StyleIndex] = IsSmithingStyleKnown(Style[2], 1) --patternIndex set to 1, temporary workaround for changes introduced in Update 4
       end

       --Chapter Styles
-      AIRG.vars.StyleChapter[AIRG.curCharacter] = {}		-- Table for each style
+      AIRG.vars.char[AIRG.curCharacter].StyleChapter = {}		-- Table for each style
       for StyleIndex, Style in ipairs (AIRG.StyleChapterDat) do
-			AIRG.vars.StyleChapter[AIRG.curCharacter][StyleIndex] = {}	-- Table for each Chapter (weapon/clothing)
+			AIRG.vars.char[AIRG.curCharacter].StyleChapter[StyleIndex] = {}	-- Table for each Chapter (weapon/clothing)


 		for ChapterIndex, Chapter in ipairs(AIRG.styleChaptersLookup) do
@@ -567,7 +559,7 @@ function AIRG.PopulateStyleData()
 						if numCompleted == 1 then
 					--		d(ChapterIndex .. " " .. Chapter .. ":  Completed: " .. desc)
 						end
-			AIRG.vars.StyleChapter[AIRG.curCharacter][StyleIndex][Chapter]	= numCompleted == 1
+			AIRG.vars.char[AIRG.curCharacter].StyleChapter[StyleIndex][Chapter]	= numCompleted == 1
 	    end
 	  end

@@ -579,40 +571,39 @@ end -- AIRG.PopulateStyleData
 -- The ALL char data was erased at startup
 function AIRG.PopulateAll()
     local craft, item, trait, thisname
-    for thisname, _ in pairs(AIRG.vars.data) do   --iterate over all characters
+    for thisname, _ in pairs(AIRG.vars.char) do   --iterate over all characters
       if thisname ~= AIRG.all then                --skip our virtual
-        for craft, _ in pairs(AIRG.vars.data[thisname]) do   --iterate over all crafts
-          for item, _ in pairs(AIRG.vars.data[thisname][craft]) do   --iterate over all weapons/armour
-            for trait, _ in pairs(AIRG.vars.data[thisname][craft][item]) do   --iterate over all traits
-              if AIRG.vars.data[thisname][craft][item][trait] ~=  0 then -- researching counts as learned.
-                 AIRG.vars.data[AIRG.all][craft][item][trait] = -1
+        for craft, _ in pairs(AIRG.vars.char[thisname].traits) do   --iterate over all crafts
+          for item, _ in pairs(AIRG.vars.char[thisname].traits[craft]) do   --iterate over all weapons/armour
+            for trait, _ in pairs(AIRG.vars.char[thisname].traits[craft][item]) do   --iterate over all traits
+              if AIRG.vars.char[thisname].traits[craft][item][trait] ~=  0 then -- researching counts as learned.
+                 AIRG.vars.char[AIRG.all].traits[craft][item][trait] = -1
               end
             end
           end
         end
         -- Fix crash for empty data.

-        if type (AIRG.vars.StyleSingle[thisname])  ~= "table" then
-          AIRG.vars.StyleSingle[thisname] = {}
+        if type (AIRG.vars.char[thisname].StyleSingle)  ~= "table" then
+          AIRG.vars.char[thisname].StyleSingle = {}
         end

-        if type (AIRG.vars.StyleChapter[thisname])  ~= "table" then
-          AIRG.vars.StyleChapter[thisname] = {}	-- no contents, but ipairs doesnt mind- runs zero times
+        if type (AIRG.vars.char[thisname].StyleChapter)  ~= "table" then
+          AIRG.vars.char[thisname].StyleChapter = {}	-- no contents, but ipairs doesnt mind- runs zero times
         end

         -- Loop through my Singles
-        for i, Known in ipairs(AIRG.vars.StyleSingle[thisname]) do
+        for i, Known in ipairs(AIRG.vars.char[thisname].StyleSingle) do
           if  Known then
-            AIRG.vars.StyleSingle[AIRG.all][i] = true
+            AIRG.vars.char[AIRG.all].StyleSingle[i] = true
           end
         end

         -- Loop through my Chapters
-        for i, Style in ipairs(AIRG.vars.StyleChapter[thisname]) do
-          for j, Chapter in ipairs (Style) do
-
+        for i, Style in ipairs(AIRG.vars.char[thisname].StyleChapter) do
+          for j, Known in ipairs (Style) do
             if  Known then
-              AIRG.vars.StyleChapter[AIRG.all][j] = true
+              AIRG.vars.char[AIRG.all].StyleChapter[i][j] = true
             end
           end
         end
@@ -625,7 +616,7 @@ end  -- AIRG.PopulateAll
 function AIRG.DisplayStyles()

     -- Single Styles
-    for StyleIndex, known in ipairs(AIRG.vars.StyleSingle[AIRG.curCharacter]) do
+    for StyleIndex, known in ipairs(AIRG.vars.char[AIRG.curCharacter].StyleSingle) do
         if (known) then
               AIRG.UI.StyleSingleButtons[StyleIndex]:SetTexture(AIRG.StyleSingleDat[StyleIndex][3] .. "down.dds")
               AIRG.UI.StyleSingleButtons[StyleIndex]:SetColor(0.2, 1, 0.2, 1)   -- Green
@@ -637,7 +628,7 @@ function AIRG.DisplayStyles()
     end
 	-- =================
 	-- Chapter Styles
-	for StyleIndex, Style in ipairs(AIRG.vars.StyleChapter[AIRG.curCharacter]) do
+	for StyleIndex, Style in ipairs(AIRG.vars.char[AIRG.curCharacter].StyleChapter) do
 		local tooltipText = ""
 		local itemStyle,tDesc
 		local KnownCount  = 0
@@ -646,7 +637,7 @@ function AIRG.DisplayStyles()
 		-- Run through the complete list in AIRG.styleChaptersLookup, and look up to see which we have.
 		-- check how many chapters are known and build tooltip
 		for ChapterIndex, chapter in ipairs(AIRG.styleChaptersLookup) do
-			if (AIRG.vars.StyleChapter[AIRG.curCharacter][StyleIndex][ChapterIndex]) then
+			if (AIRG.vars.char[AIRG.curCharacter].StyleChapter[StyleIndex][ChapterIndex]) then
 				tooltipText = zo_strjoin(nil, tooltipText, "\n|cFFFFFF", GetString("SI_ITEMSTYLECHAPTER", chapter), "|r")
 				KnownCount = KnownCount +1
 			else
@@ -670,45 +661,6 @@ function AIRG.DisplayStyles()
 		tDesc = GetSmithingStyleItemLink(AIRG.StyleChapterDat[StyleIndex][2])
 		AIRG.UI.StyleChapterButtons[StyleIndex].tooltipText = zo_strjoin(nil, zo_strformat("<<t:1>> (<<2>>/14)\n<<t:3>>\n", GetString("SI_ITEMSTYLE", itemStyle), KnownCount, tDesc), tooltipText)
 	end
-  --[[
-
-
-	--Dwemer Motif, special case
-	--update old saved variables, if exists
-
-	--making StyleIndex as variable, so it will be easier to make a loop if there will be any new styles
-	local StyleIndex = 15
-        -- start of loop if its ever written
-	local tooltipText = ""
-  local itemStyle,tDesc
-	local knownCount = 0
-	for i = 1, 14 do
-		--check how many chapters are known and build tooltip
-		if (AIRG.vars.styles[AIRG.curCharacter][StyleIndex][i]) then
-		   tooltipText = zo_strjoin(nil, tooltipText, "\n|cFFFFFF", GetString("SI_ITEMSTYLECHAPTER", i), "|r")
-		   knownCount = knownCount + 1
-		else
-		   tooltipText = zo_strjoin(nil, tooltipText, "\n|c806060", GetString("SI_ITEMSTYLECHAPTER", i), "|r")
-		end
-	end
-	if knownCount == 0 then
-		AIRG.UI.StyleSingleButtons[StyleIndex]:SetTexture(AIRG.styleLookupIcons[StyleIndex] .. "up.dds")
-		AIRG.UI.StyleSingleButtons[StyleIndex]:SetColor(1, 1, 1, 0.7)   -- grey
-	else
-		AIRG.UI.StyleSingleButtons[StyleIndex]:SetTexture(AIRG.styleLookupIcons[StyleIndex] .. "down.dds")
-		if knownCount == 14 then
-			AIRG.UI.StyleSingleButtons[StyleIndex]:SetColor(0.2, 1, 0.2, 1)   -- Green
-		else
-			AIRG.UI.StyleSingleButtons[StyleIndex]:SetColor(1, 1, 0.6, 1)    -- yellow
-		end
-	end
-	itemStyle = AIRG.styleLookupValue[StyleIndex]
-	tDesc = GetSmithingStyleItemLink(AIRG.styleLookupItem[StyleIndex])
-	AIRG.UI.StyleSingleButtons[StyleIndex].tooltipText = zo_strjoin(nil, zo_strformat("<<t:1>> (<<2>>/14)\n<<t:3>>\n", GetString("SI_ITEMSTYLE", itemStyle), knownCount, tDesc), tooltipText)
- --]]
-
-
-
 end -- AIRG.DisplayStyles()

 -- Look up all the data on this profession and commit to saved variables for this character.
@@ -716,12 +668,12 @@ function AIRG.CreateDataMatrix(thisCraft)  -- for current  player, not the selec
     local tType, tKnown, tRemain, rowNum, tTargetStamp
     local rCount = 0
     local maxLines = GetNumSmithingResearchLines(thisCraft)     -- the number of columns for this profession
-    AIRG.vars.data[AIRG.playerName][thisCraft] = {}             -- create a table for this profession for real traits
+    AIRG.vars.char[AIRG.playerName].traits[thisCraft] = {}             -- create a table for this profession for real traits

     -- Not relative updates, show all for Current Character
     -- Cycle through items for this profession
     for i = 1, maxLines do
-        AIRG.vars.data[AIRG.playerName][thisCraft][i] = {}      -- create a table for this item
+        AIRG.vars.char[AIRG.playerName].traits[thisCraft][i] = {}      -- create a table for this item
         -- Cycle through the traits for this item
         for j = 1, 9 do
             tType,_,tKnown = GetSmithingResearchLineTraitInfo(thisCraft,i,j)
@@ -729,15 +681,15 @@ function AIRG.CreateDataMatrix(thisCraft)  -- for current  player, not the selec
             rowNum = AIRG.gridTraits[tType]

             if (tKnown) then
-                AIRG.vars.data[AIRG.playerName][thisCraft][i][rowNum] = -1       -- this trait is known
+                AIRG.vars.char[AIRG.playerName].traits[thisCraft][i][rowNum] = -1       -- this trait is known
                 rCount = rCount + 1
             else
                 if (tRemain) then
                     tTargetStamp = GetTimeStamp() + tRemain
-                    AIRG.vars.data[AIRG.playerName][thisCraft][i][rowNum] = tTargetStamp   -- this trait is being currently researched
+                    AIRG.vars.char[AIRG.playerName].traits[thisCraft][i][rowNum] = tTargetStamp   -- this trait is being currently researched
                     -- The value we are setting this to is the timestamp for when the research will be complete.
                 else
-                    AIRG.vars.data[AIRG.playerName][thisCraft][i][rowNum] = 0   -- this trait is NOT known
+                    AIRG.vars.char[AIRG.playerName].traits[thisCraft][i][rowNum] = 0   -- this trait is NOT known
                 end
             end
         end
@@ -747,17 +699,17 @@ function AIRG.CreateDataMatrix(thisCraft)  -- for current  player, not the selec
 end -- AIRG.CreateDataMatrix

 function AIRG.CreateDataMatrixRelative(thisCraft)  -- for the current selected  player, relative to the real player
-    for thisname, _ in pairs(AIRG.vars.data) do   --iterate over all characters
+    for thisname, _ in pairs(AIRG.vars.char) do   --iterate over all characters
       if thisname ~= AIRG.all then                --skip our virtual
         AIRG.relative[thisname] = {}
-        for craft, _ in pairs(AIRG.vars.data[thisname]) do   --iterate over all crafts
+        for craft, _ in pairs(AIRG.vars.char[thisname].traits) do   --iterate over all crafts
           AIRG.relative[thisname][craft] = {}
-          for item, _ in pairs(AIRG.vars.data[thisname][craft]) do   --iterate over all weapons/armour
+          for item, _ in pairs(AIRG.vars.char[thisname].traits[craft]) do   --iterate over all weapons/armour
             AIRG.relative[thisname][craft][item] = {}
-            for trait, _ in pairs(AIRG.vars.data[thisname][craft][item]) do   --iterate over all traits
+            for trait, _ in pairs(AIRG.vars.char[thisname].traits[craft][item]) do   --iterate over all traits
               AIRG.relative[thisname][craft][item][trait] = {}
-              if AIRG.vars.data[thisname][craft][item][trait] ==  0 then -- thischar doesn't have traits
-                if AIRG.vars.data[AIRG.playerName][craft][item][trait] ==  -1  ---PLayer Does
+              if AIRG.vars.char[thisname].traits[craft][item][trait] ==  0 then -- thischar doesn't have traits
+                if AIRG.vars.char[AIRG.playerName].traits[craft][item][trait] ==  -1  ---PLayer Does
                 then AIRG.relative[thisname][craft][item][trait] = -1
                 else AIRG.relative[thisname][craft][item][trait] = nil
                 end
@@ -888,86 +840,83 @@ function AIRG.Initialise(eventCode, addOnName)
 		AIRG.vars.StyleChapter = {}
 	end

-  AIRG.vars.style = nil        -- erase old tables
+    if type(AIRG.vars.char) ~= "table" then
+		AIRG.vars.char = {}
+	end
+

 	-- Remove "ALL" data, we will recreate it.
-	AIRG.vars.StyleChapter[AIRG.all] = {}
-	AIRG.vars.StyleSingle[AIRG.all] = {}
-  AIRG.vars.data[AIRG.all] = {}
-
-    -- Create these here so the UI dropdown will have them first time around.
-  AIRG.vars.data[AIRG.playerName] = {}    -- create a table for this character's matrix
-	AIRG.vars.StyleSingle[AIRG.playerName] = {}
-	AIRG.vars.StyleChapter[AIRG.playerName] = {}
+	AIRG.vars.char[AIRG.all]= {}
+	AIRG.vars.char[AIRG.all].traits = {}
+	AIRG.vars.char[AIRG.all].StyleChapter = {}
+	AIRG.vars.char[AIRG.all].StyleSingle = {}
+	-- Remove player data, we will reload it.
+	AIRG.vars.char[AIRG.playerName] = {}
+	AIRG.vars.char[AIRG.playerName].traits = {}    -- create a table for this character's matrix
+ 	AIRG.vars.char[AIRG.playerName].StyleChapter = {}
+	AIRG.vars.char[AIRG.playerName].StyleSingle = {}
+	AIRG.vars.StyleSingle = nil --TEMP
+	AIRG.vars.StyleChapter= nil


   -- Create Empty data for "ALL" set to 0, (Not known)

-    AIRG.vars.data[AIRG.all][CRAFTING_TYPE_BLACKSMITHING] = {}
+    AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_BLACKSMITHING] = {}
     -- Cycle through items for this profession
     for i = 1, 7 do   -- Weapons
-        AIRG.vars.data[AIRG.all][CRAFTING_TYPE_BLACKSMITHING][i] = {}
+        AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_BLACKSMITHING][i] = {}
         -- Cycle through the traits for this item
         for j = 1, 9 do
-                       AIRG.vars.data[AIRG.all][CRAFTING_TYPE_BLACKSMITHING][i][j] = 0   -- set to not known
+                       AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_BLACKSMITHING][i][j] = 0   -- set to not known
         end
     end
     for i = 8, 14 do  -- Armour
-        AIRG.vars.data[AIRG.all][CRAFTING_TYPE_BLACKSMITHING][i] = {}
+        AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_BLACKSMITHING][i] = {}
         -- Cycle through the traits for this item
         for j = 10, 18 do
-                       AIRG.vars.data[AIRG.all][CRAFTING_TYPE_BLACKSMITHING][i][j] = 0   -- set to not known
+                       AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_BLACKSMITHING][i][j] = 0   -- set to not known
         end
     end

-    AIRG.vars.data[AIRG.all][CRAFTING_TYPE_CLOTHIER] = {}
+    AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_CLOTHIER] = {}
     -- Cycle through items for this profession
     for i = 1, 14 do
-        AIRG.vars.data[AIRG.all][CRAFTING_TYPE_CLOTHIER][i] = {}
+        AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_CLOTHIER][i] = {}
         -- Cycle through the traits for this item
         for j = 10, 18  do
-                       AIRG.vars.data[AIRG.all][CRAFTING_TYPE_CLOTHIER][i][j] = 0   -- set to not known
+                       AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_CLOTHIER][i][j] = 0   -- set to not known
         end
     end

-     AIRG.vars.data[AIRG.all][CRAFTING_TYPE_WOODWORKING] = {}
+     AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_WOODWORKING] = {}
     -- Cycle through items for this profession
     for i = 1, 5 do
-        AIRG.vars.data[AIRG.all][CRAFTING_TYPE_WOODWORKING][i] = {}
+        AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_WOODWORKING][i] = {}
         -- Cycle through the traits for this item
         for j = 1, 9 do
-                       AIRG.vars.data[AIRG.all][CRAFTING_TYPE_WOODWORKING][i][j] = 0   -- set to not known
+                       AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_WOODWORKING][i][j] = 0   -- set to not known
         end
     end
     for i = 6, 6 do  --Shields
-        AIRG.vars.data[AIRG.all][CRAFTING_TYPE_WOODWORKING][i] = {}
+        AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_WOODWORKING][i] = {}
         -- Cycle through the traits for this item
         for j = 10, 18 do
-                       AIRG.vars.data[AIRG.all][CRAFTING_TYPE_WOODWORKING][i][j] = 0   -- set to not known
+                       AIRG.vars.char[AIRG.all].traits[CRAFTING_TYPE_WOODWORKING][i][j] = 0   -- set to not known
         end
     end


 	for i,_ in ipairs(AIRG.StyleSingleDat) do
-		AIRG.vars.StyleSingle[AIRG.all][i] = false
+		AIRG.vars.char[AIRG.all].StyleSingle[i] = false
 	end

 	for i,_ in ipairs(AIRG.StyleChapterDat) do
-		AIRG.vars.StyleChapter[AIRG.all][i] =  {}
+		AIRG.vars.char[AIRG.all].StyleChapter[i] =  {}
 		for j,_ in ipairs(AIRG.styleChaptersLookup) do
-			AIRG.vars.StyleChapter[AIRG.all][i][j] = false
+			AIRG.vars.char[AIRG.all].StyleChapter[i][j] = false
 		end
 	end

-	 -- AIRG.CreateDataMatrix
-
-    -- Following replaced with PlayerActivate
-    -- Query data for THIS character and populate the data matrix with fresh data.
-    --AIRG.PopulateMatrix()
-
-    -- Query data for THIS character for STYLES
-    --AIRG.PopulateStyleData()
-
     -- UI set-up. Create frames, position labels & buttons etc.
     AIRG.initUI()
 	-- Create the configuration/settings menu.