From 30c28f1c45acfb971ad80793767fea829e001e82 Mon Sep 17 00:00:00 2001 From: Rose Liverman Date: Fri, 31 Jul 2026 19:15:59 -0600 Subject: [PATCH] filters, sort orders, beginning of data structure --- asymptome.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/asymptome.lua b/asymptome.lua index 69101df..0849cde 100644 --- a/asymptome.lua +++ b/asymptome.lua @@ -2,6 +2,17 @@ local json = require "lib.dkjson" 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 file, error_message = io.open(file_name, "r") assert(file, error_message) @@ -18,7 +29,7 @@ local save_json = function(file_name, data) file:close() end -local random_order = function(array) +local get_random_order = function(array) local temporary, order = {}, {} for i = 1, #array do temporary[i] = i @@ -28,3 +39,67 @@ local random_order = function(array) end return order 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