From 618b3382952e1e7385ae1b67d4e7b5572b2c5469 Mon Sep 17 00:00:00 2001 From: Athozus Date: Wed, 5 Apr 2023 18:21:01 +0200 Subject: [PATCH] Add ascending/descending, fix selection issue --- storage.lua | 31 +++++++++++++++++++++++-------- ui/events.lua | 6 +++--- ui/inbox.lua | 2 +- ui/mail.lua | 6 +++--- ui/outbox.lua | 4 ++-- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/storage.lua b/storage.lua index 98c4287..2a0e103 100644 --- a/storage.lua +++ b/storage.lua @@ -45,11 +45,11 @@ end function mail.sort_messages(unsorted_messages, sortfield, sortdirection) local messages = {} - if not sortfield then - local sortfield = 3 + if not sortfield or sortfield == "" then + sortfield = "3" end - if not sortdirection then - local sortdirection = 1 + if not sortdirection or sortdirection == "" then + sortdirection = "1" end if unsorted_messages[1] then @@ -60,15 +60,19 @@ function mail.sort_messages(unsorted_messages, sortfield, sortdirection) for i, unsorted_msg in ipairs(unsorted_messages) do local is_message_sorted = false for j, sorted_msg in ipairs(messages) do - if sortfield == 1 and unsorted_msg.from >= sorted_msg.from then + if sortfield == "1" and unsorted_msg.from >= sorted_msg.from then -- for inbox table.insert(messages, j+1, unsorted_msg) is_message_sorted = true break - elseif sortfield == 2 and unsorted_msg.subject >= sorted_msg.subject then + elseif sortfield == "1" and unsorted_msg.to >= sorted_msg.to then -- for outbox table.insert(messages, j+1, unsorted_msg) is_message_sorted = true break - elseif sortfield == 3 and unsorted_msg.time >= sorted_msg.time then + elseif sortfield == "2" and unsorted_msg.subject >= sorted_msg.subject then + table.insert(messages, j+1, unsorted_msg) + is_message_sorted = true + break + elseif sortfield == "3" and unsorted_msg.time >= sorted_msg.time then table.insert(messages, j+1, unsorted_msg) is_message_sorted = true break @@ -80,7 +84,18 @@ function mail.sort_messages(unsorted_messages, sortfield, sortdirection) end end - return messages + -- reverse for descending + + local sorted_messages = messages + + if sortdirection == "2" then + sorted_messages = {} + for i=#messages, 1, -1 do + sorted_messages[#sorted_messages+1] = messages[i] + end + end + + return sorted_messages end -- marks a mail read by its id diff --git a/ui/events.lua b/ui/events.lua index e78aa09..8bdd966 100644 --- a/ui/events.lua +++ b/ui/events.lua @@ -17,8 +17,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) sortdirection = 1 end - local messagesInbox = mail.sort_messages(entry.inbox, fields.sortfield, fields.sortdirection) - local messagesSent = mail.sort_messages(entry.outbox, fields.sortfield, fields.sortdirection) + local messagesInbox = mail.sort_messages(entry.inbox, tostring(sortfield), tostring(sortdirection)) + local messagesSent = mail.sort_messages(entry.outbox, tostring(sortfield), tostring(sortdirection)) local messagesDrafts = entry.drafts if fields.inbox then -- inbox table @@ -155,7 +155,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) mail.show_about(name) elseif fields.sortfield or fields.sortdirection then - mail.show_inbox(name, fields.sortfield, fields.sortdirection) + mail.show_mail_menu(name, fields.sortfield, fields.sortdirection) end return true diff --git a/ui/inbox.lua b/ui/inbox.lua index 83a0ae5..d9b1f08 100644 --- a/ui/inbox.lua +++ b/ui/inbox.lua @@ -33,7 +33,7 @@ function mail.show_inbox(name, sortfield, sortdirection) table[0,0.7;5.75,8.35;inbox;#999,]] .. S("From") .. "," .. S("Subject") local formspec = { inbox_formspec } local entry = mail.get_storage_entry(name) - local messages = mail.sort_messages(entry.inbox, sortfield, sortdirection) + local messages = mail.sort_messages(entry.inbox, tostring(sortfield), tostring(sortdirection)) mail.message_drafts[name] = nil diff --git a/ui/mail.lua b/ui/mail.lua index 6f7cbe0..6548550 100644 --- a/ui/mail.lua +++ b/ui/mail.lua @@ -1,11 +1,11 @@ -- helper function for tabbed overview -function mail.show_mail_menu(playername) +function mail.show_mail_menu(playername, sortfield, sortdirection) local index = mail.selected_idxs.boxtab[playername] or 1 if index == 1 then - mail.show_inbox(playername) + mail.show_inbox(playername, sortfield, sortdirection) elseif index == 2 then - mail.show_sent(playername) + mail.show_sent(playername, sortfield, sortdirection) elseif index == 3 then mail.show_drafts(playername) end diff --git a/ui/outbox.lua b/ui/outbox.lua index b9dc635..93e504c 100644 --- a/ui/outbox.lua +++ b/ui/outbox.lua @@ -24,14 +24,14 @@ function mail.show_sent(name, sortfield, sortdirection) button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[] button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[] - dropdown[0,9.4;2,0.5;sortfield;]] .. S("From") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. tostring(sortfield) .. [[;1] + dropdown[0,9.4;2,0.5;sortfield;]] .. S("To") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. tostring(sortfield) .. [[;1] dropdown[2.2,9.4;2,0.5;sortdirection;]] .. S("Ascending") .. "," .. S("Descending") .. [[;]] .. tostring(sortdirection) .. [[;1] tablecolumns[color;text;text] table[0,0.7;5.75,8.35;sent;#999,]] .. S("To") .. "," .. S("Subject") local formspec = { sent_formspec } local entry = mail.get_storage_entry(name) - local messages = mail.sort_messages(entry.outbox, sortfield, sortdirection) + local messages = mail.sort_messages(entry.outbox, tostring(sortfield), tostring(sortdirection)) mail.message_drafts[name] = nil