Forward compatibility with LeoAltholic 1.4.0 and preparing for LeoTrainer 1.1.0

Leandro Silva [10-27-18 - 10:26]
Forward compatibility with LeoAltholic 1.4.0 and preparing for LeoTrainer 1.1.0
Filename
LeoTrainer.lua
LeoTrainer.txt
LeoTrainer.xml
LeoTrainerConst.lua
LeoTrainerUI.lua
diff --git a/LeoTrainer.lua b/LeoTrainer.lua
index 1aec81c..34b195f 100644
--- a/LeoTrainer.lua
+++ b/LeoTrainer.lua
@@ -25,28 +25,49 @@ function LeoTrainer.maxStyle(piece)
     return maxStyleId
 end

-function LeoTrainer.GetCharKnowsTrait(craft, line, trait)
+function LeoTrainer.GetCharKnowsTrait(craft, line, trait, charList)
     local known = {}
     local unknown = {}
     local myself = false
     local all = {}
-    for i, char in ipairs(LeoAltholic.GetCharacters()) do
-        all[char.bio.name] = char.research[craft][line][trait] ~= false
-        if char.research[craft][line][trait] ~= false then
-            if char.bio.name == GetUnitName("player") then
-                myself = true
+    if LeoAltholic.TodayReset == nil then
+        if charList == nil then charList = LeoAltholic.GetCharacters() end
+        for i, char in ipairs(charList) do
+            all[char.bio.name] = char.research[craft][line][trait] ~= false
+            if char.research[craft][line][trait] ~= false then
+                if char.bio.name == GetUnitName("player") then
+                    myself = true
+                end
+                table.insert(known, char.bio.name)
+            else
+                table.insert(unknown, char.bio.name)
+            end
+        end
+    else
+        if charList == nil then charList = LeoAltholic.ExportCharacters() end
+        for i, char in ipairs(charList) do
+            local knows = LeoAltholic.CharKnowsTrait(craft, line, trait, char.bio.name)
+            all[char.bio.name] = knows
+            if knows then
+                if char.bio.name == GetUnitName("player") then
+                    myself = true
+                end
+                table.insert(known, char.bio.name)
+            else
+                table.insert(unknown, char.bio.name)
             end
-            table.insert(known, char.bio.name)
-        else
-            table.insert(unknown, char.bio.name)
         end
     end
     return myself, known, unknown, all
 end

 function LeoTrainer.IKnowTrait(craft, line, trait)
-    local myself = LeoAltholic.GetMyself()
-    return myself.research[craft][line][trait] == true
+    if LeoAltholic.TodayReset ~= nil then
+        return LeoAltholic.CharKnowsTrait(craft, line, trait)
+    else
+        local myself = LeoAltholic.GetMyself()
+        return myself.research[craft][line][trait] == true
+    end
 end

 function LeoTrainer.GetPatternIndexes(craft)
diff --git a/LeoTrainer.txt b/LeoTrainer.txt
index 57456fa..59a7c10 100644
--- a/LeoTrainer.txt
+++ b/LeoTrainer.txt
@@ -1,6 +1,6 @@
 ## Title: Leo's Trainer
 ## APIVersion: 100024 100025
-## Version: 1.0.2
+## Version: 1.0.3
 ## Author: |c39B027@LeandroSilva|r
 ## SavedVariables: LeoTrainerSavedVariables
 ## DependsOn: LeoAltholic
diff --git a/LeoTrainer.xml b/LeoTrainer.xml
index c425a54..f68a3f4 100644
--- a/LeoTrainer.xml
+++ b/LeoTrainer.xml
@@ -14,8 +14,7 @@
                     <Edge edgeSize="1"/>
                 </Backdrop>

-                <Label name="$(parent)Title" color="39B027" font="ZoFontWinH3" wrapMode="ELLIPSIS"
-                       verticalAlignment="CENTER" mouseEnabled="true">
+                <Label name="$(parent)Title" color="39B027" font="ZoFontWinH3" wrapMode="ELLIPSIS" verticalAlignment="CENTER">
                     <Anchor point="TOPLEFT" relativeTo="$(parent)" relativePoint="TOPLEFT" offsetX="16" offsetY="10"/>
                 </Label>

diff --git a/LeoTrainerConst.lua b/LeoTrainerConst.lua
index 2ca79d7..7368e95 100644
--- a/LeoTrainerConst.lua
+++ b/LeoTrainerConst.lua
@@ -2,7 +2,7 @@
 LeoTrainer = {}
 LeoTrainer.name = "LeoTrainer"
 LeoTrainer.displayName = "Leo's Trainer"
-LeoTrainer.version = "1.0.2"
+LeoTrainer.version = "1.0.3"
 LeoTrainer.chatPrefix = "|c39B027" .. LeoTrainer.name .. "|r: "
 LeoTrainer.setting = nil
 LeoTrainer.hidden = true
diff --git a/LeoTrainerUI.lua b/LeoTrainerUI.lua
index 23e34a7..e1c5328 100644
--- a/LeoTrainerUI.lua
+++ b/LeoTrainerUI.lua
@@ -294,10 +294,44 @@ function LeoTrainer.RemoveFromQueue(pos)
     LeoTrainer.queueScroll:RefreshData()
 end

+local function getMaxCraft(char, craft)
+    if char.research.done ~= nil then
+        return char.research.done[craft].max
+    end
+    return char.research[craft].max
+end
+
+local function getTraitData(research, craft, line, trait)
+    local traitData
+    if research.doing ~= nil then
+        traitData = research.done[craft][line][trait] or false
+        if traitData == false then
+            for _, researching in pairs(research.doing[craft]) do
+                if researching.line == line and researching.trait == trait then
+                    return researching.time, research.done[craft].max
+                end
+            end
+        end
+        return traitData
+    else
+        traitData = research[craft][line][trait] or false
+        if type(traitData) == 'number' then
+            traitData = traitData - GetTimeStamp()
+        end
+        return traitData
+    end
+end
+
 function LeoTrainer.FillSlots()
     local knownCount = {}
     local traitsKnown = {}
-    for _, char in pairs(LeoAltholic.GetCharacters()) do
+    local charList
+    if LeoAltholic.TodayReset ~= nil then
+        charList = LeoAltholic.ExportCharacters()
+    else
+        charList = LeoAltholic.GetCharacters()
+    end
+    for _, char in pairs(charList) 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
@@ -306,32 +340,32 @@ function LeoTrainer.FillSlots()
                 if traitsKnown[craft] == nil then traitsKnown[craft] = {} end
                 local lower = {}
                 for line = 1, GetNumSmithingResearchLines(craft) do
-                    local lineName, lineIcon = GetSmithingResearchLineInfo(craft, line)
+                    local lineName, _, numTraits = GetSmithingResearchLineInfo(craft, line)
                     knownCount[char.bio.name][craft][line] = 0
                     local firstUnknownTrait = {
                         id = 0,
                         name = ''
                     }
                     if traitsKnown[craft][line] == nil then traitsKnown[craft][line] = {} end
-                    for trait = 1, LeoAltholic.maxTraits do
+                    for trait = 1, numTraits do
                         local traitType = GetSmithingResearchLineTraitInfo(craft, line, trait)
-                        local traitData = char.research[craft][line][trait] or false
+                        local traitData = getTraitData(char.research, craft, line, trait)
                         local traitName = GetString('SI_ITEMTRAITTYPE', traitType)

                         if traitsKnown[craft][line][trait] == nil then
-                            local _, known, unknown, all = LeoTrainer.GetCharKnowsTrait(craft, line, trait)
+                            local _, known, unknown, all = LeoTrainer.GetCharKnowsTrait(craft, line, trait, charList)
                             traitsKnown[craft][line][trait] = known
                         end
-                        if type(traitData) == 'number' and GetDiffBetweenTimeStamps(traitData, GetTimeStamp()) > 0 then
+                        if type(traitData) == 'number' and traitData > 0 then
                             researching = researching + 1
-                        elseif (type(traitData) == 'number' and GetDiffBetweenTimeStamps(traitData, GetTimeStamp()) <= 0) or traitData == true then
+                        elseif (type(traitData) == 'number' and traitData <= 0) or traitData == true then
                             knownCount[char.bio.name][craft][line] = knownCount[char.bio.name][craft][line] + 1
                         elseif traitData == false and firstUnknownTrait.id == 0 then
                             firstUnknownTrait.id = trait
                             firstUnknownTrait.name = traitName
                         end
                     end
-                    if firstUnknownTrait.id > 0 and #lower < char.research[craft].max then
+                    if firstUnknownTrait.id > 0 and #lower < getMaxCraft(char, craft) then
                         table.insert(lower, {
                             line = line,
                             lineName = lineName,
@@ -353,12 +387,12 @@ function LeoTrainer.FillSlots()
                         end
                     end
                 end
-                if #lower > 0 and researching < char.research[craft].max then
+                if #lower > 0 and researching < getMaxCraft(char, craft) then
                     table.sort(lower, function(a, b)
                         return a.count < b.count
                     end)
                     local styleId = LeoTrainer.maxStyle(lower.line)
-                    local max = math.min(#lower, char.research[craft].max-researching)
+                    local max = math.min(#lower, getMaxCraft(char, craft) - researching)
                     for i = 1, max do
                         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.")