Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Supplimental file save and load functions for backpackloot.lua https://pastebin.com/eDNsazPj to persist manifest between reboots
- -- filefunctions.lua to be used in backpackloot.lua as local filefunctions = dofile("filefunctions.lua")
- -- filefunctions.saveManifest("manifest.dat")
- -- filefunctions.loadManifest("manifest.dat")
- local filefunctions = {}
- -- Get current day abbreviation: Mon, Tue, ..., Sun
- local function getDayOfWeek()
- local days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
- local time = os.time()
- local day = os.day("%a") % 7 + 1 -- Minecraft os.day is 0-based
- return days[day]
- end
- -- Save global stats and make a rotating daily backup
- function filefunctions.saveStats(filename)
- -- Backup first
- local backup_name = "stats_backup_" .. getDayOfWeek() .. ".dat"
- if fs.exists(filename) then
- if fs.exists(backup_name) then
- fs.delete(backup_name) -- we only need the most recent daily backup.
- end
- fs.copy(filename, backup_name)
- end
- -- Save main file
- local file = fs.open(filename, "w")
- if file then
- local data = {
- item_quantities = _G.item_quantities,
- item_frequencies = _G.item_frequencies
- }
- file.write(textutils.serialize(data))
- file.close()
- end
- end
- -- Load saved global stats
- function filefunctions.loadStats(filename)
- if fs.exists(filename) then
- local file = fs.open(filename, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- if data then
- _G.item_quantities = data.item_quantities or {}
- _G.item_frequencies = data.item_frequencies or {}
- print("pre-return stats...")
- for k,v in pairs(item_quantities) do
- print("Qty", k, v)
- end
- for k,v in pairs(item_frequencies) do
- print("Freq", k, v)
- end
- -- using _G. return item_quantities, item_frequencies
- end
- else
- print("No data?",data==nil)
- end
- end
- return filefunctions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement