Working on Provisioning Sorting

Wobin [05-11-14 - 17:09]
Working on Provisioning Sorting
Filename
Provisioning.lua
SousChef.lua
diff --git a/Provisioning.lua b/Provisioning.lua
index 6df5269..312f5c6 100644
--- a/Provisioning.lua
+++ b/Provisioning.lua
@@ -57,7 +57,7 @@ function SousChef:HookRecipeTreeFunction()
             function(...)
                 local node, control, data, open, userRequested, enabled = ...
                 SousChef.hookedProvisioningFunction(...)
-                local link = GetRecipeResultItemLink(data.recipeListIndex, data.recipeIndex, LINK_STYLE_BRACKETS)
+                local link = GetRecipeResultItemLink(data.recipeListIndex, SousChef.recipeLookup[data.recipeListIndex][data.recipeIndex], LINK_STYLE_BRACKETS)
                 if link then
                     SousChef:AddNotifier(control, SousChef.settings.shoppingList[zo_strformat(SI_TOOLTIP_ITEM_NAME, link)])
                 end
@@ -119,4 +119,51 @@ function SousChef:UnhookRecipeTree()
     if KEYBIND_STRIP:HasKeybindButtonGroup(bGroup) then
         KEYBIND_STRIP:RemoveKeybindButtonGroup(bGroup)
     end
+end
+
+function SousChef:HookGetRecipeInfo()
+    SousChef.GetRecipeInfo = GetRecipeInfo
+    GetRecipeInfo =
+        function(recipeListIndex, recipeIndex)
+            local val = SousChef.recipeIndex[recipeListIndex][recipeIndex]
+            return val.known, val.name, val.ingredients, val.levelReq, val.quality, val.specialIngType
+        end
+
+    SousChef.GetSelectedRecipeIndex = PROVISIONER.GetSelectedRecipeIndex
+    PROVISIONER.GetSelectedRecipeIndex =
+        function()
+            return SousChef.recipeLookup[PROVISIONER:GetSelectedRecipeListIndex()][SousChef.GetSelectedRecipeIndex(PROVISIONER)]
+        end
+end
+
+local function SortRecipe(a, b)
+    if a.levelReq == b.levelReq then
+        if a.quality == b.quality then
+            if a.name == b.name then
+                return a.index < b.index
+            end
+            return a.name < b.name
+        end
+        return a.quality < b.quality
+    end
+    return a.levelReq < b.levelReq
+end
+
+function SousChef:ResortRecipes()
+    SousChef.recipeIndex = {}
+    SousChef.recipeLookup = {}
+
+    for recipeListIndex = 1, GetNumRecipeLists() do
+        local recipeListName, numRecipes = GetRecipeListInfo(recipeListIndex)
+        SousChef.recipeIndex[recipeListIndex] = {}
+        SousChef.recipeLookup[recipeListIndex] = {}
+        for recipeIndex = 1, numRecipes do
+            local known, recipeName, numIngredients, provisionerLevelReq, qualityReq, specialIngredientType = SousChef.GetRecipeInfo(recipeListIndex, recipeIndex)
+            table.insert(SousChef.recipeIndex[recipeListIndex], {index = recipeIndex, known = known, name = recipeName, ingredients = numIngredients, levelReq = provisionerLevelReq, quality = qualityReq, specialIngType = specialIngredientType})
+        end
+        table.sort(SousChef.recipeIndex[recipeListIndex], SortRecipe)
+        for i,v in ipairs(SousChef.recipeIndex[recipeListIndex]) do
+            SousChef.recipeLookup[recipeListIndex][i] = v.index
+        end
+    end
 end
\ No newline at end of file
diff --git a/SousChef.lua b/SousChef.lua
index 631a8fd..e658704 100644
--- a/SousChef.lua
+++ b/SousChef.lua
@@ -48,9 +48,16 @@ local typeIconLookup = {

 function SousChef:ParseRecipes()
 	local lists = GetNumRecipeLists()
+
+	if SousChef.GetRecipeInfo then SousChef:ResortRecipes() end
+
 	for listIndex = 1, lists do
 		local name, count = GetRecipeListInfo(listIndex)
 		for recipeIndex = 1, count do
+
+			-- If we've already hooked, then use the new index
+			if SousChef.GetRecipeInfo then recipeIndex = SousChef.recipeLookup[listIndex][recipeIndex] end
+
 			if GetRecipeInfo(listIndex, recipeIndex) then
 				-- Store the recipes known
 				local recipeName = u.CleanString((GetRecipeResultItemInfo(listIndex, recipeIndex)))
@@ -87,8 +94,7 @@ function SousChef:ParseRecipes()
 	end
 	ZO_ScrollList_RefreshVisible(BACKPACK)
 	ZO_ScrollList_RefreshVisible(BANK)
-	ZO_ScrollList_RefreshVisible(GUILD_BANK)
-
+	ZO_ScrollList_RefreshVisible(GUILD_BANK)
 end

 local function SousChefCreateSettings()
@@ -242,7 +248,8 @@ local function SousChef_Loaded(eventCode, addOnName)
 	SLASH_COMMANDS['/scilist'] = SousChef.ListIgnoredRecipes

 	SousChef:UpdateProvisioningTable()
-
+
+	SousChef:HookGetRecipeInfo()
 	-- Now we want to hook into the function that sets the details on the inventory slot
 	zo_callLater(SousChef.HookEvents, 3000)
 end