Icon representing a recipe

Recipe: Auto Recover (from autobkup) v1.0

created by man_the_stan

Profile


Name
Auto Recover (from autobkup) v1.0
ID
108632
Shared with
Public
Parent
None
Children
None
Created on
April 05, 2024 at 19:34 PM UTC
Updated on
April 05, 2024 at 19:35 PM UTC
Description

(Companion recipe to Auto Backup)
Finds the 5 most recent solutions with names that conform to the Auto Backup naming scheme and presents them (along with their score info and any additional descriptive information) via dialog to the user for selection for loading.
Optionally (default: true) autosaves the current solution before loading the selected one.
As always, you will be notified via popup dialog if something goes wrong.

Best for


Code


-- Auto Recover v1.0 (companion to Auto Backup) -- man_the_stan SAVE_NAME_PREFIX = "AUTOBKUP__" SAVE_STAMP_FORMAT = "[0-9][0-9][0-9][0-9][\\-][0-9][0-9][\\-][0-9][0-9]_[A-Z][a-z][a-z]_[0-9][0-9][\\-][0-9][0-9][\\-][0-9][0-9]_?_?" -- followed by optional descriptive suffix (ignored by this recipe) MAX_MATCHES = 5 SAVE_NAME_PREFIX_LEN = string.len(SAVE_NAME_PREFIX) sols = save.GetSolutions() local matches, nmatches = {}, 0 for _,solv in pairs(sols) do -- print("---- ", solk, " ----") -- for k,v in pairs(solv) do -- print(k, v) -- end -- print(string.find(solv.name, SAVE_NAME_PREFIX, 1, true)) if string.find(solv.name, SAVE_NAME_PREFIX, 1, true) == 1 then local nameRest = string.sub(solv.name, SAVE_NAME_PREFIX_LEN + 1) -- print(nameRest) local i1, i2a = string.find(nameRest, SAVE_STAMP_FORMAT) if i1 == 1 then local i2 = string.find(nameRest, "__", i2a - 2, true) nmatches = nmatches + 1 matches[nmatches] = { stamp = string.sub(nameRest, 1, i2 and i2 - 1 or i2a); data = solv } end end end if nmatches > 0 then table.sort(matches, function(a, b) return a.stamp > b.stamp end) -- for i,x in ipairs(matches) do -- print(i, x.stamp) -- end local numToShow = math.min(MAX_MATCHES, nmatches) local dia = dialog.CreateDialog("Confirm Load Recipe Autosave") dia.label0 = dialog.AddLabel("Found " .. nmatches .. " recipe autosaves for this puzzle.") dia.label00 = dialog.AddLabel("Here are the " .. numToShow .. " most recent (most recent listed first):") for i,match in ipairs(matches) do dia["label" .. i] = dialog.AddLabel( string.format("%i) %s (Score: %+0.9g)", i, match.stamp, match.data.score)) local suffix = string.sub(match.data.name, SAVE_NAME_PREFIX_LEN + string.len(match.stamp) + 1) if string.len(suffix) > 2 then dia["label" .. i .. "b"] = dialog.AddLabel(" (" .. string.sub(suffix, 3) .. ")") end if i >= numToShow then break end end dia.checkbox = dialog.AddCheckbox("Autosave current solution before load", true) dia.slider = dialog.AddSlider("Soln #", 1, 1, numToShow, 0) dia.load = dialog.AddButton("LOAD", 1) dia.cancel = dialog.AddButton("Cancel", 0) local res = dialog.Show(dia) if res == 1 then local tf, msg if dia.checkbox.value then tf, _ = pcall(save.SaveSolution, os.date("AUTO__%Y-%M-%d_%a_%H-%M-%S__AUTORECO") --[[@as string]]) if not tf then print("Error message:", msg) local dia2 = dialog.Create("Saving solution failed") dia2.label = dialog.AddLabel( "Failed to save solution before loading the selected autosave. (See output for details.) Proceed with loading anyway?") dia2.yes = dialog.AddButton("Yes (!!)", 1) dia2.no = dialog.AddButton("No", 0) tf = dialog.Show(dia2) == 1 end else tf = true end end if tf then local match = matches[dia.slider.value] tf, msg = pcall(save.LoadSolution, match.data) if not tf then print("Failed to load solution due to error:", msg) end end else local dia = dialog.CreateDialog("No matches found") dia.btn = dialog.AddButton("OK", 0) dialog.Show(dia) end

Comments