From 15111dccd6441ec64e9c187283e9b44770ac67f6 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 | 107 ++++++++++++------ Libs/DF/packtable.tests.lua | 56 +++++++++ .../window_mythicplus/window_end_of_run.lua | 3 + 5 files changed, 138 insertions(+), 38 deletions(-) create mode 100644 Libs/DF/packtable.tests.lua diff --git a/Libs/DF/fw.lua b/Libs/DF/fw.lua index 1f831fad2..5e07cc5a9 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 d6bfbee10..5b7779bf9 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 419e1a93f..8c41be656 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,8 +22,53 @@ 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 +---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 + newString = newString .. detailsFramework.table.pack(table[i]) .. "," + end + + newString = newString:gsub(",$", "") + return newString +end + +---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) + local totalSize = 0 + local subTablesAmount = #table + + for i = 1, subTablesAmount do + totalSize = totalSize + #table[i] + end + + --set the first index to be the total size of the subtables + local newString = "" .. totalSize .. "," + + for i = 1, subTablesAmount do + local subTable = table[i] + for subIndex = 1, #subTable do + newString = newString .. subTable[subIndex] .. "," + end + end + + 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 @@ -37,15 +82,13 @@ function detailsFramework.table.unpack(data, startIndex) 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 endIndex = currentIndex + currentTableSize local result = {} for i = startIndex, endIndex do @@ -58,47 +101,37 @@ function detailsFramework.table.unpack(data, startIndex) 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 ----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" -function detailsFramework.table.packsub(table) - local newString = "" - for i = 1, #table do - newString = newString .. detailsFramework.table.pack(table[i]) .. "," - end - - newString = newString:gsub(",$", "") - return newString -end +function detailsFramework.table.unpacksub(data, startIndex) + startIndex = startIndex or 1 ----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 ----@param table table ----@return string -function detailsFramework.table.packsubmerge(table) - local totalSize = 0 - local subTablesAmount = #table + local splittedTable = {} + local bIsRunning = true + local result = {} - for i = 1, subTablesAmount do - totalSize = totalSize + #table[i] + for value in data:gmatch("[^,]+") do + splittedTable[#splittedTable+1] = value end - --set the first index to be the total size of the subtables - local newString = "" .. totalSize .. "," + while (bIsRunning) do + local unpackTable, nextIndex = detailsFramework.table.unpack(splittedTable, startIndex) + table.insert(result, unpackTable) - for i = 1, subTablesAmount do - local subTable = table[i] - for subIndex = 1, #subTable do - newString = newString .. subTable[subIndex] .. "," + if (nextIndex == 0) then + bIsRunning = false + break + else + startIndex = nextIndex end end - newString = newString:gsub(",$", "") - return newString + 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 000000000..26d735ed3 --- /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 df8e8688f..cc0b10051 100644 --- a/frames/window_mythicplus/window_end_of_run.lua +++ b/frames/window_mythicplus/window_end_of_run.lua @@ -190,6 +190,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 = {}