From 16fd6de6f9ec4948e46f5f4aac3febb742683ee0 Mon Sep 17 00:00:00 2001 From: Tercio Jose Date: Sat, 10 Aug 2024 23:52:27 -0300 Subject: [PATCH] Fixed an issue with the end of m+ panel --- Libs/DF/fw.lua | 2 +- Libs/DF/math.lua | 8 ++ Libs/DF/packtable.lua | 131 +++++++++++------- Libs/DF/packtable.tests.lua | 56 ++++++++ .../window_mythicplus/window_end_of_run.lua | 3 + 5 files changed, 150 insertions(+), 50 deletions(-) create mode 100644 Libs/DF/packtable.tests.lua diff --git a/Libs/DF/fw.lua b/Libs/DF/fw.lua index 2dcbcdd2..5e5dad5f 100644 --- a/Libs/DF/fw.lua +++ b/Libs/DF/fw.lua @@ -1,6 +1,6 @@ -local dversion = 560 +local dversion = 561 local major, minor = "DetailsFramework-1.0", dversion local DF, oldminor = LibStub:NewLibrary(major, minor) diff --git a/Libs/DF/math.lua b/Libs/DF/math.lua index 5b279803..f9b52969 100644 --- a/Libs/DF/math.lua +++ b/Libs/DF/math.lua @@ -42,6 +42,7 @@ DF.Math = {} ---@field RandomFraction fun(minValue: number, maxValue: number) : number ---@field GetNinePoints fun(object: uiobject) : df_ninepoints ---@field GetClosestPoint fun(ninePoints: df_ninepoints, coordinate: df_coordinate) : anchorid +---@field GetVectorLength fun(vectorX: number, vectorY: number, vectorZ: number?) : number return the magnitude of a vector ---@class df_coordinate : table ---@field x number @@ -122,6 +123,13 @@ function DF.Math.GetNinePoints(object) return ninePoints end +function DF.Math.GetVectorLength(vectorX, vectorY, vectorZ) + if (not vectorZ) then + return (vectorX * vectorX + vectorY * vectorY) ^ 0.5 + end + return (vectorX * vectorX + vectorY * vectorY + vectorZ * vectorZ) ^ 0.5 +end + ---return a random fraction between two values, example: RandomFraction(.2, .3) returns a number between .2 and .3, 0.25, 0.28, 0.21, etc function DF.Math.RandomFraction(minValue, maxValue) minValue = minValue or 0 diff --git a/Libs/DF/packtable.lua b/Libs/DF/packtable.lua index 419e1a93..8c41be65 100644 --- a/Libs/DF/packtable.lua +++ b/Libs/DF/packtable.lua @@ -7,8 +7,8 @@ end ---@cast detailsFramework detailsframework ---pack a table into a string separating values with commas ----the first index tells the table length, expected table: {1, 2, 3, 4, 5, 6, 7, 8, 9, ...} ----can pack strings and numbers, returned string: "9,1,2,3,4,5,6,7,8,9", where the first number is the total size of table +---example: table: {1, 2, 3, 4, 5, 6, 7, 8, 9} +---returned string: "9,1,2,3,4,5,6,7,8,9", where the first number is the total size of table ---@param table table ---@return string function detailsFramework.table.pack(table) @@ -22,50 +22,10 @@ function detailsFramework.table.pack(table) return newString end ----unpack a string and an array of data into a indexed table, sarting from the startIndex also returns the next index to start reading ----expected data: "3,1,2,3,4,5,6,7,8" or {3,1,2,3,4,5,6,7,8}, with the example the returned table is: {1, 2, 3} and the next index to read is 5 -function detailsFramework.table.unpack(data, startIndex) - local splittedTable - - if (type(data) == "table") then - splittedTable = data - else - splittedTable = {} - for value in data:gmatch("[^,]+") do - splittedTable[#splittedTable+1] = value - end - end - - local currentIndex = startIndex or 1 - - local currentTableSize = tonumber(splittedTable[currentIndex]) - if (not currentTableSize) then - error("Details! Framework: table.unpack: invalid table size.") - end - - startIndex = (startIndex and startIndex + 1) or 2 - local endIndex = currentTableSize + 1 - - local result = {} - - for i = startIndex, endIndex do - local value = splittedTable[i] - local asNumber = tonumber(value) - if (asNumber) then - table.insert(result, asNumber) - else - table.insert(result, value) - end - end - - return result, endIndex + 1 --return the position of the last index plus 1 to account for the table size index -end - - ----pack subtables into a string separating values with commas ----the first index tells the table length of the first packed table, the index t[currentIndex+length+1] tells the length of the next table. ----expected table: { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, ... } ----can pack strings and numbers, returned string: "3,1,2,3,3,4,5,6,3,7,8,9" +---pack subtables into a string separating values with commas, the first index tells the table length of the first packed table, the index t[currentIndex+length+1] tells the length of the next table. +---can pack strings and numbers, example: +---passed table: { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, ... } +---returned string: "3,1,2,3,3,4,5,6,3,7,8,9" > 3 indicating the total size of the first subtable followed by the sub table data, then 3 indicating the total size of the second subtable and so on function detailsFramework.table.packsub(table) local newString = "" for i = 1, #table do @@ -76,9 +36,10 @@ function detailsFramework.table.packsub(table) return newString end ----merge multiple tables into a single one and pack it into a string separating values with commas ----the first index tells the table length, expected table: { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, ... } ----can pack strings and numbers, returned string: "9,1,2,3,4,5,6,7,8,9", where the first number is the total size of table +---merge multiple tables into a single one and pack it into a string separating values with commas where the first index tells the table length +---can pack strings and numbers, example: +---passed table: { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}} +---result string: "9,1,2,3,4,5,6,7,8,9" > 9 indicating the total size of the subtables following by the indexes of the subtables ---@param table table ---@return string function detailsFramework.table.packsubmerge(table) @@ -101,4 +62,76 @@ function detailsFramework.table.packsubmerge(table) newString = newString:gsub(",$", "") return newString +end + +---unpack a string and an array of data into a indexed table, starting from the startIndex also returns the next index to start reading +---expected data: "3,1,2,3,4,5,6,7,8" or {3,1,2,3,4,5,6,7,8}, with the example the returned table is: {1, 2, 3} and the next index to read is 5 +---@param data string|table +---@param startIndex number? +---@return table, number +function detailsFramework.table.unpack(data, startIndex) + local splittedTable + + if (type(data) == "table") then + splittedTable = data + else + splittedTable = {} + for value in data:gmatch("[^,]+") do + splittedTable[#splittedTable+1] = value + end + end + + local currentIndex = startIndex or 1 + local currentTableSize = tonumber(splittedTable[currentIndex]) + if (not currentTableSize) then + error("Details! Framework: table.unpack: invalid table size.") + end + + startIndex = (startIndex and startIndex + 1) or 2 + local endIndex = currentIndex + currentTableSize + local result = {} + + for i = startIndex, endIndex do + local value = splittedTable[i] + local asNumber = tonumber(value) + if (asNumber) then + table.insert(result, asNumber) + else + table.insert(result, value) + end + end + + local nextIndex = endIndex + 1 + if (not splittedTable[nextIndex]) then + return result, 0 + end + + return result, endIndex + 1 --return the position of the last index plus 1 to account for the table size index +end + + +function detailsFramework.table.unpacksub(data, startIndex) + startIndex = startIndex or 1 + + local splittedTable = {} + local bIsRunning = true + local result = {} + + for value in data:gmatch("[^,]+") do + splittedTable[#splittedTable+1] = value + end + + while (bIsRunning) do + local unpackTable, nextIndex = detailsFramework.table.unpack(splittedTable, startIndex) + table.insert(result, unpackTable) + + if (nextIndex == 0) then + bIsRunning = false + break + else + startIndex = nextIndex + end + end + + return result end \ No newline at end of file diff --git a/Libs/DF/packtable.tests.lua b/Libs/DF/packtable.tests.lua new file mode 100644 index 00000000..26d735ed --- /dev/null +++ b/Libs/DF/packtable.tests.lua @@ -0,0 +1,56 @@ + +--this file isn't loaded, the tests need to be copied, loaded and run. + +function PackTest() + local table = {1, 2, 3, 4, 5} + local packed = DetailsFramework.table.pack(table) + print("Testing table.pack, table: {1, 2, 3, 4, 5}") + local expected = "\"5,1,2,3,4,5\"" + print("Expected: string ", expected) + print("Result: " .. type(packed) .. " \"" .. packed .. "\"") +end + +function PackSubTest() + local table = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } + local packed = DetailsFramework.table.packsub(table) + print("Testing table.packsub, table: { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }") + local expected = "\"3,1,2,3,3,4,5,6,3,7,8,9\"" + print("Expected: string ", expected) + print("Result: " .. type(packed) .. " \"" .. packed .. "\"") +end + +function PackSubMergeTest() + local table = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } + local packed = DetailsFramework.table.packsubmerge(table) + print("Testing table.packsubmerge, table: { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }") + local expected = "\"9,1,2,3,4,5,6,7,8,9\"" + print("Expected: string ", expected) + print("Result: " .. type(packed) .. " \"" .. packed .. "\"") +end + +function UnpackTest() + local packed = "5,1,2,3,4,5" + local table, nextIndex = DetailsFramework.table.unpack(packed) + print("Testing table.unpack, data: \"5,1,2,3,4,5\"") + local expected = "table {1, 2, 3, 4, 5}, 0" + print("Expected:", expected) + print("Result: " .. type(table) .. " {" .. table[1] .. ", " .. table[2] .. ", " .. table[3] .. ", " .. table[4] .. ", " .. table[5] .. "},", nextIndex) +end + +function UnpackSecondTest() + local packed = "5,1,2,3,4,5,2,5,4,3,1,2,3" + local table, nextIndex = DetailsFramework.table.unpack(packed, 7) + print("Testing table.unpack with Idx 7, data: \"5,1,2,3,4,5,2,5,4,3,1,2,3\"") + local expected = "table {5, 4}, 10" + print("Expected:", expected) + print("Result: " .. type(table) .. " {" .. table[1] .. ", " .. table[2] .. "},", nextIndex) +end + +function UnpackSubTest() + local packed = "3,1,2,3,3,4,5,6,3,7,8,9" + local tables = DetailsFramework.table.unpacksub(packed) + print("Testing table.unpacksub, data: \"3,1,2,3,3,4,5,6,3,7,8,9\"") + local expected = "table {table {1, 2, 3}, table {4, 5, 6}, table {7, 8, 9}}" + print("Expected:", expected) + print("Result: " .. type(tables) .. " {" .. type(tables[1]) .. " {" .. tables[1][1] .. ", " .. tables[1][2] .. ", " .. tables[1][3] .. "}, " .. type(tables[2]) .. " {" .. tables[2][1] .. ", " .. tables[2][2] .. ", " .. tables[2][3] .. "}, " .. type(tables[3]) .. " {" .. tables[3][1] .. ", " .. tables[3][2] .. ", " .. tables[3][3] .. "}}") +end \ No newline at end of file diff --git a/frames/window_mythicplus/window_end_of_run.lua b/frames/window_mythicplus/window_end_of_run.lua index ef0553b4..7b797640 100644 --- a/frames/window_mythicplus/window_end_of_run.lua +++ b/frames/window_mythicplus/window_end_of_run.lua @@ -186,6 +186,9 @@ function lootFrame.UpdateUnitLoot(playerBanner) local timeNow = GetTime() local lootCache = lootFrame.LootCache[unitName] + if (not lootCache) then + return + end ---@type details_loot_cache[] local lootCandidates = {}