Add sorters for inbox/outbox

This commit is contained in:
Athozus 2023-04-05 17:40:40 +02:00
parent def93bdd0f
commit 3e2d022c87
No known key found for this signature in database
GPG key ID: B50895022E8484BF
4 changed files with 105 additions and 36 deletions

View file

@ -43,6 +43,46 @@ function mail.get_message(playername, msg_id)
end
end
function mail.sort_messages(unsorted_messages, sortfield, sortdirection)
local messages = {}
if not sortfield then
local sortfield = 3
end
if not sortdirection then
local sortdirection = 1
end
if unsorted_messages[1] then
-- add first message
table.insert(messages, unsorted_messages[1])
table.remove(unsorted_messages, 1)
-- sort messages
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
table.insert(messages, j+1, unsorted_msg)
is_message_sorted = true
break
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
end
end
if not is_message_sorted then
table.insert(messages, 1, unsorted_msg)
end
end
end
return messages
end
-- marks a mail read by its id
function mail.mark_read(playername, msg_id)
local entry = mail.get_storage_entry(playername)

View file

@ -8,8 +8,17 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- split inbox and sent msgs for different tests
local entry = mail.get_storage_entry(name)
local messagesInbox = entry.inbox
local messagesSent = entry.outbox
local sortfield = fields.sortfield
local sortdirection = fields.sortdirection
if not sortfield or sortfield == "" or sortfield == "0" then
sortfield = 3
end
if not sortdirection or sortdirection == "" or sortdirection == "0" then
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 messagesDrafts = entry.drafts
if fields.inbox then -- inbox table
@ -145,6 +154,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
elseif fields.about then
mail.show_about(name)
elseif fields.sortfield or fields.sortdirection then
mail.show_inbox(name, fields.sortfield, fields.sortdirection)
end
return true

View file

@ -1,30 +1,39 @@
-- translation
local S = minetest.get_translator("mail")
local inbox_formspec = "size[8.5,10;]" .. mail.theme .. [[
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;1;false;false]
button[6,0.10;2.5,0.5;new;]] .. S("New") .. [[]
button[6,0.95;2.5,0.5;read;]] .. S("Read") .. [[]
button[6,1.70;2.5,0.5;reply;]] .. S("Reply") .. [[]
button[6,2.45;2.5,0.5;replyall;]] .. S("Reply all") .. [[]
button[6,3.20;2.5,0.5;forward;]] .. S("Forward") .. [[]
button[6,3.95;2.5,0.5;delete;]] .. S("Delete") .. [[]
button[6,4.82;2.5,0.5;markread;]] .. S("Mark Read") .. [[]
button[6,5.55;2.5,0.5;markunread;]] .. S("Mark Unread") .. [[]
button[6,6.8;2.5,0.5;contacts;]] .. S("Contacts") .. [[]
button[6,7.6;2.5,0.5;maillists;]] .. S("Mail lists") .. [[]
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
function mail.show_inbox(name, sortfield, sortdirection)
if not sortfield or sortfield == "" or sortfield == "0" then
sortfield = 3
end
if not sortdirection or sortdirection == "" or sortdirection == "0" then
sortdirection = 1
end
tablecolumns[color;text;text]
table[0,0.7;5.75,9.35;inbox;#999,]] .. S("From") .. "," .. S("Subject")
local inbox_formspec = "size[8.5,10;]" .. mail.theme .. [[
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;1;false;false]
button[6,0.10;2.5,0.5;new;]] .. S("New") .. [[]
button[6,0.95;2.5,0.5;read;]] .. S("Read") .. [[]
button[6,1.70;2.5,0.5;reply;]] .. S("Reply") .. [[]
button[6,2.45;2.5,0.5;replyall;]] .. S("Reply all") .. [[]
button[6,3.20;2.5,0.5;forward;]] .. S("Forward") .. [[]
button[6,3.95;2.5,0.5;delete;]] .. S("Delete") .. [[]
button[6,4.82;2.5,0.5;markread;]] .. S("Mark Read") .. [[]
button[6,5.55;2.5,0.5;markunread;]] .. S("Mark Unread") .. [[]
button[6,6.8;2.5,0.5;contacts;]] .. S("Contacts") .. [[]
button[6,7.6;2.5,0.5;maillists;]] .. S("Mail lists") .. [[]
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
function mail.show_inbox(name)
dropdown[0,9.4;2,0.5;sortfield;]] .. S("From") .. "," .. 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;inbox;#999,]] .. S("From") .. "," .. S("Subject")
local formspec = { inbox_formspec }
local entry = mail.get_storage_entry(name)
local messages = entry.inbox
local messages = mail.sort_messages(entry.inbox, sortfield, sortdirection)
mail.message_drafts[name] = nil

View file

@ -1,28 +1,37 @@
-- translation
local S = minetest.get_translator("mail")
local sent_formspec = "size[8.5,10;]" .. mail.theme .. [[
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;2;false;false]
button[6,0.10;2.5,0.5;new;]] .. S("New") .. [[]
button[6,0.95;2.5,0.5;read;]] .. S("Read") .. [[]
button[6,1.70;2.5,0.5;reply;]] .. S("Reply") .. [[]
button[6,2.45;2.5,0.5;replyall;]] .. S("Reply all") .. [[]
button[6,3.20;2.5,0.5;forward;]] .. S("Forward") .. [[]
button[6,3.95;2.5,0.5;delete;]] .. S("Delete") .. [[]
button[6,6.8;2.5,0.5;contacts;]] .. S("Contacts") .. [[]
button[6,7.6;2.5,0.5;maillists;]] .. S("Mail lists") .. [[]
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
function mail.show_sent(name, sortfield, sortdirection)
if not sortfield or sortfield == "" or sortfield == "0" then
sortfield = 3
end
if not sortdirection or sortdirection == "" or sortdirection == "0" then
sortdirection = 1
end
tablecolumns[color;text;text]
table[0,0.7;5.75,9.35;sent;#999,]] .. S("To") .. "," .. S("Subject")
local sent_formspec = "size[8.5,10;]" .. mail.theme .. [[
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;2;false;false]
button[6,0.10;2.5,0.5;new;]] .. S("New") .. [[]
button[6,0.95;2.5,0.5;read;]] .. S("Read") .. [[]
button[6,1.70;2.5,0.5;reply;]] .. S("Reply") .. [[]
button[6,2.45;2.5,0.5;replyall;]] .. S("Reply all") .. [[]
button[6,3.20;2.5,0.5;forward;]] .. S("Forward") .. [[]
button[6,3.95;2.5,0.5;delete;]] .. S("Delete") .. [[]
button[6,6.8;2.5,0.5;contacts;]] .. S("Contacts") .. [[]
button[6,7.6;2.5,0.5;maillists;]] .. S("Mail lists") .. [[]
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
function mail.show_sent(name)
dropdown[0,9.4;2,0.5;sortfield;]] .. S("From") .. "," .. 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 = entry.outbox
local messages = mail.sort_messages(entry.outbox, sortfield, sortdirection)
mail.message_drafts[name] = nil