Preventing that a slot is filled with a traitless item if a char known all traits of a skill line or if no one knows it

Leandro Silva [10-08-18 - 15:28]
Preventing that a slot is filled with a traitless item if a char known all traits of a skill line or if no one knows it
Filename
LeoTrainer.txt
LeoTrainerConst.lua
LeoTrainerUI.lua
diff --git a/LeoTrainer.txt b/LeoTrainer.txt
index 6bf37ac..7ecc02e 100644
--- a/LeoTrainer.txt
+++ b/LeoTrainer.txt
@@ -1,6 +1,6 @@
 ## Title: Leo's Trainer
 ## APIVersion: 100024 100025
-## Version: 1.0.0
+## Version: 1.0.1
 ## Author: |c39B027@LeandroSilva|r
 ## SavedVariables: LeoTrainerSavedVariables
 ## DependsOn: LeoAltholic
diff --git a/LeoTrainerConst.lua b/LeoTrainerConst.lua
index 57d8eab..45c193b 100644
--- a/LeoTrainerConst.lua
+++ b/LeoTrainerConst.lua
@@ -2,7 +2,7 @@
 LeoTrainer = {}
 LeoTrainer.name = "LeoTrainer"
 LeoTrainer.displayName = "Leo's Trainer"
-LeoTrainer.version = "1.0.0"
+LeoTrainer.version = "1.0.1"
 LeoTrainer.chatPrefix = "|c39B027" .. LeoTrainer.name .. "|r: "
 LeoTrainer.setting = nil
 LeoTrainer.hidden = true
diff --git a/LeoTrainerUI.lua b/LeoTrainerUI.lua
index 8814312..932cda5 100644
--- a/LeoTrainerUI.lua
+++ b/LeoTrainerUI.lua
@@ -296,12 +296,14 @@ end

 function LeoTrainer.FillSlots()
     local knownCount = {}
+    local traitsKnown = {}
     for _, char in pairs(LeoAltholic.GetCharacters()) do
         knownCount[char.bio.name] = {}
         for _,craft in pairs(LeoAltholic.craftResearch) do
             if LeoTrainer.isTrackingSkill(char.bio.name, craft) and LeoTrainer.canFillSlotWithSkill(char.bio.name, craft) then
                 local researching = 0
                 knownCount[char.bio.name][craft] = {}
+                if traitsKnown[craft] == nil then traitsKnown[craft] = {} end
                 local lower = {}
                 for line = 1, GetNumSmithingResearchLines(craft) do
                     local lineName, lineIcon = GetSmithingResearchLineInfo(craft, line)
@@ -310,12 +312,16 @@ function LeoTrainer.FillSlots()
                         id = 0,
                         name = ''
                     }
+                    if traitsKnown[craft][line] == nil then traitsKnown[craft][line] = {} end
                     for trait = 1, LeoAltholic.maxTraits do
                         local traitType = GetSmithingResearchLineTraitInfo(craft, line, trait)
                         local traitData = char.research[craft][line][trait] or false
                         local traitName = GetString('SI_ITEMTRAITTYPE', traitType)

-                        local myself, known, unknown, all = LeoTrainer.GetCharKnowsTrait(craft, line, trait)
+                        if traitsKnown[craft][line][trait] == nil then
+                            local _, known, unknown, all = LeoTrainer.GetCharKnowsTrait(craft, line, trait)
+                            traitsKnown[craft][line][trait] = known
+                        end
                         if type(traitData) == 'number' then
                             researching = researching + 1
                         elseif traitData == true then
@@ -325,7 +331,7 @@ function LeoTrainer.FillSlots()
                             firstUnknownTrait.name = traitName
                         end
                     end
-                    if #lower < char.research[craft].max then
+                    if firstUnknownTrait.id > 0 and #lower < char.research[craft].max then
                         table.insert(lower, {
                             line = line,
                             lineName = lineName,
@@ -333,7 +339,7 @@ function LeoTrainer.FillSlots()
                             trait = firstUnknownTrait.id,
                             traitName = firstUnknownTrait.name
                         })
-                    else
+                    elseif firstUnknownTrait.id > 0 then
                         local changed = false
                         for i = 1, #lower do
                             if changed == false and lower[i].count > knownCount[char.bio.name][craft][line] then
@@ -347,33 +353,38 @@ function LeoTrainer.FillSlots()
                         end
                     end
                 end
-                if researching < char.research[craft].max then
+                if #lower > 0 and researching < char.research[craft].max then
                     table.sort(lower, function(a, b)
                         return a.count < b.count
                     end)
                     local styleId = LeoTrainer.maxStyle(lower.line)
-                    local max = char.research[craft].max-researching
+                    local max = math.min(#lower, char.research[craft].max-researching)
                     for i = 1, max do
-                        local traitType = GetSmithingResearchLineTraitInfo(craft, lower[i].line, lower[i].trait)
-                        LeoTrainer.AddToQueue({
-                            trainer = "Anyone",
-                            trainee = char.bio.name,
-                            craft = craft,
-                            line = lower[i].line,
-                            trait = lower[i].trait,
-                            patternIndex = -1,
-                            materialIndex = 1,
-                            materialQuantity = -1,
-                            itemStyleId = styleId,
-                            traitIndex = traitType,
-                            useUniversalStyleItem = false,
-                            researchName = lower[i].lineName .. " " .. lower[i].traitName,
-                            itemLink = LeoTrainer.const.craftItems[craft][lower[i].line][lower[i].trait]
-                        })
+                        if #traitsKnown[craft][lower[i].line][lower[i].trait] == 0 then
+                            LeoTrainer.log("No one knows "..lower[i].lineName .. " " .. lower[i].traitName.." for " ..char.bio.name.."'s training.")
+                        else
+                            local traitType = GetSmithingResearchLineTraitInfo(craft, lower[i].line, lower[i].trait)
+                            LeoTrainer.AddToQueue({
+                                trainer = "Anyone",
+                                trainee = char.bio.name,
+                                craft = craft,
+                                line = lower[i].line,
+                                trait = lower[i].trait,
+                                patternIndex = -1,
+                                materialIndex = 1,
+                                materialQuantity = -1,
+                                itemStyleId = styleId,
+                                traitIndex = traitType,
+                                useUniversalStyleItem = false,
+                                researchName = lower[i].lineName .. " " .. lower[i].traitName,
+                                itemLink = LeoTrainer.const.craftItems[craft][lower[i].line][lower[i].trait]
+                            })
+                        end
                     end
                     LeoTrainer.queueScroll:RefreshData()
                 end
             end
         end
     end
+    LeoTrainer.log("Done filling empty research slots.")
 end