Add ascending/descending, fix selection issue

This commit is contained in:
Athozus 2023-04-05 18:21:01 +02:00
parent 3e2d022c87
commit 618b338295
No known key found for this signature in database
GPG key ID: B50895022E8484BF
5 changed files with 32 additions and 17 deletions

View file

@ -45,11 +45,11 @@ end
function mail.sort_messages(unsorted_messages, sortfield, sortdirection) function mail.sort_messages(unsorted_messages, sortfield, sortdirection)
local messages = {} local messages = {}
if not sortfield then if not sortfield or sortfield == "" then
local sortfield = 3 sortfield = "3"
end end
if not sortdirection then if not sortdirection or sortdirection == "" then
local sortdirection = 1 sortdirection = "1"
end end
if unsorted_messages[1] then 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 for i, unsorted_msg in ipairs(unsorted_messages) do
local is_message_sorted = false local is_message_sorted = false
for j, sorted_msg in ipairs(messages) do 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) table.insert(messages, j+1, unsorted_msg)
is_message_sorted = true is_message_sorted = true
break 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) table.insert(messages, j+1, unsorted_msg)
is_message_sorted = true is_message_sorted = true
break 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) table.insert(messages, j+1, unsorted_msg)
is_message_sorted = true is_message_sorted = true
break break
@ -80,7 +84,18 @@ function mail.sort_messages(unsorted_messages, sortfield, sortdirection)
end end
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 end
-- marks a mail read by its id -- marks a mail read by its id

View file

@ -17,8 +17,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
sortdirection = 1 sortdirection = 1
end end
local messagesInbox = mail.sort_messages(entry.inbox, fields.sortfield, fields.sortdirection) local messagesInbox = mail.sort_messages(entry.inbox, tostring(sortfield), tostring(sortdirection))
local messagesSent = mail.sort_messages(entry.outbox, fields.sortfield, fields.sortdirection) local messagesSent = mail.sort_messages(entry.outbox, tostring(sortfield), tostring(sortdirection))
local messagesDrafts = entry.drafts local messagesDrafts = entry.drafts
if fields.inbox then -- inbox table if fields.inbox then -- inbox table
@ -155,7 +155,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
mail.show_about(name) mail.show_about(name)
elseif fields.sortfield or fields.sortdirection then 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 end
return true return true

View file

@ -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") table[0,0.7;5.75,8.35;inbox;#999,]] .. S("From") .. "," .. S("Subject")
local formspec = { inbox_formspec } local formspec = { inbox_formspec }
local entry = mail.get_storage_entry(name) 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 mail.message_drafts[name] = nil

View file

@ -1,11 +1,11 @@
-- helper function for tabbed overview -- 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 local index = mail.selected_idxs.boxtab[playername] or 1
if index == 1 then if index == 1 then
mail.show_inbox(playername) mail.show_inbox(playername, sortfield, sortdirection)
elseif index == 2 then elseif index == 2 then
mail.show_sent(playername) mail.show_sent(playername, sortfield, sortdirection)
elseif index == 3 then elseif index == 3 then
mail.show_drafts(playername) mail.show_drafts(playername)
end end

View file

@ -24,14 +24,14 @@ function mail.show_sent(name, sortfield, sortdirection)
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[] button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[] 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] dropdown[2.2,9.4;2,0.5;sortdirection;]] .. S("Ascending") .. "," .. S("Descending") .. [[;]] .. tostring(sortdirection) .. [[;1]
tablecolumns[color;text;text] tablecolumns[color;text;text]
table[0,0.7;5.75,8.35;sent;#999,]] .. S("To") .. "," .. S("Subject") table[0,0.7;5.75,8.35;sent;#999,]] .. S("To") .. "," .. S("Subject")
local formspec = { sent_formspec } local formspec = { sent_formspec }
local entry = mail.get_storage_entry(name) 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 mail.message_drafts[name] = nil