filters, sort orders, beginning of data structure

This commit is contained in:
2026-07-31 19:15:59 -06:00
parent 3ebeacf2e0
commit 30c28f1c45
+76 -1
View File
@@ -2,6 +2,17 @@
local json = require "lib.dkjson" local json = require "lib.dkjson"
math.randomseed(os.time()) math.randomseed(os.time())
local default_file = "books.json"
local path_exists = function(path_name)
local file = io.open(path_name, "r")
if file then
file:close()
return true
end
return false
end
local load_json = function(file_name) local load_json = function(file_name)
local file, error_message = io.open(file_name, "r") local file, error_message = io.open(file_name, "r")
assert(file, error_message) assert(file, error_message)
@@ -18,7 +29,7 @@ local save_json = function(file_name, data)
file:close() file:close()
end end
local random_order = function(array) local get_random_order = function(array)
local temporary, order = {}, {} local temporary, order = {}, {}
for i = 1, #array do for i = 1, #array do
temporary[i] = i temporary[i] = i
@@ -28,3 +39,67 @@ local random_order = function(array)
end end
return order return order
end end
local filters = {
unread = function(book)
if book.progress == 0 then
return true
end
return false
end,
read = function(book)
if book.progress == 1 then
return true
end
return false
end,
in_progress = function(book)
if book.progress > 0 and book.progress < 1 then
return true
end
return false
end,
}
local sort_orders
sort_orders = {
most_progress = function(A, B)
return A.progress > B.progress
end,
least_progress = function(A, B)
return A.progress < B.progress
end,
highest_priority = function(A, B)
return A.priority > B.priority
end,
lowest_priority = function(A, B)
return A.priority < B.priority
end,
most_pages_remaining = function(A, B)
if not (A.pages and B.pages) then -- if pages is unknown for both, fallback to percentage
return A.progress > B.progress
end
local average_page_count = data.total_pages / #data.books
local a = (A.pages or average_page_count) - (A.pages or average_page_count) * A.progress
local b = (B.pages or average_page_count) - (B.pages or average_page_count) * B.progress
return a > b
end,
fewest_pages_remaining = function(A, B)
return not sort_orders.most_pages_remaining(A, B)
end,
}
local data
if path_exists(default_file) then
data = load_json(default_file)
-- TODO verify data structure
else
data = {
books = {},
elo = {
distance_constant = 5,
maximum_change = 2.5,
},
total_pages = 0,
}
end