mirror of
https://github.com/mt-mods/mail.git
synced 2025-07-07 07:00:31 -04:00
Add sorters for inbox/outbox
This commit is contained in:
parent
def93bdd0f
commit
3e2d022c87
4 changed files with 105 additions and 36 deletions
40
storage.lua
40
storage.lua
|
@ -43,6 +43,46 @@ function mail.get_message(playername, msg_id)
|
||||||
end
|
end
|
||||||
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
|
-- marks a mail read by its id
|
||||||
function mail.mark_read(playername, msg_id)
|
function mail.mark_read(playername, msg_id)
|
||||||
local entry = mail.get_storage_entry(playername)
|
local entry = mail.get_storage_entry(playername)
|
||||||
|
|
|
@ -8,8 +8,17 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
-- split inbox and sent msgs for different tests
|
-- split inbox and sent msgs for different tests
|
||||||
local entry = mail.get_storage_entry(name)
|
local entry = mail.get_storage_entry(name)
|
||||||
|
|
||||||
local messagesInbox = entry.inbox
|
local sortfield = fields.sortfield
|
||||||
local messagesSent = entry.outbox
|
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
|
local messagesDrafts = entry.drafts
|
||||||
|
|
||||||
if fields.inbox then -- inbox table
|
if fields.inbox then -- inbox table
|
||||||
|
@ -145,6 +154,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
elseif fields.about then
|
elseif fields.about then
|
||||||
mail.show_about(name)
|
mail.show_about(name)
|
||||||
|
|
||||||
|
elseif fields.sortfield or fields.sortdirection then
|
||||||
|
mail.show_inbox(name, fields.sortfield, fields.sortdirection)
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
19
ui/inbox.lua
19
ui/inbox.lua
|
@ -1,6 +1,15 @@
|
||||||
-- translation
|
-- translation
|
||||||
local S = minetest.get_translator("mail")
|
local S = minetest.get_translator("mail")
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
local inbox_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
local inbox_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
||||||
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;1;false;false]
|
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;1;false;false]
|
||||||
|
|
||||||
|
@ -17,14 +26,14 @@ local inbox_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
||||||
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[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,9.35;inbox;#999,]] .. S("From") .. "," .. S("Subject")
|
table[0,0.7;5.75,8.35;inbox;#999,]] .. S("From") .. "," .. S("Subject")
|
||||||
|
|
||||||
|
|
||||||
function mail.show_inbox(name)
|
|
||||||
local formspec = { inbox_formspec }
|
local formspec = { inbox_formspec }
|
||||||
local entry = mail.get_storage_entry(name)
|
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
|
mail.message_drafts[name] = nil
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
-- translation
|
-- translation
|
||||||
local S = minetest.get_translator("mail")
|
local S = minetest.get_translator("mail")
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
local sent_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
local sent_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
||||||
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;2;false;false]
|
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;2;false;false]
|
||||||
|
|
||||||
|
@ -15,14 +24,14 @@ local sent_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
||||||
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[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,9.35;sent;#999,]] .. S("To") .. "," .. S("Subject")
|
table[0,0.7;5.75,8.35;sent;#999,]] .. S("To") .. "," .. S("Subject")
|
||||||
|
|
||||||
|
|
||||||
function mail.show_sent(name)
|
|
||||||
local formspec = { sent_formspec }
|
local formspec = { sent_formspec }
|
||||||
local entry = mail.get_storage_entry(name)
|
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
|
mail.message_drafts[name] = nil
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue