mirror of
https://github.com/mt-mods/mail.git
synced 2025-04-30 08:21:44 -04:00
Receive player messages from global storage
This commit is contained in:
parent
0d69f0242b
commit
128b209067
2 changed files with 24 additions and 10 deletions
16
api.lua
16
api.lua
|
@ -85,7 +85,6 @@ function mail.send(...)
|
||||||
|
|
||||||
-- form the actual mail
|
-- form the actual mail
|
||||||
local msg = {
|
local msg = {
|
||||||
unread = true,
|
|
||||||
sender = m.from,
|
sender = m.from,
|
||||||
to = m.to,
|
to = m.to,
|
||||||
cc = m.cc,
|
cc = m.cc,
|
||||||
|
@ -112,3 +111,18 @@ function mail.send(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mail.split(str, delimiter) -- flux split function
|
||||||
|
local rv = {}
|
||||||
|
local i, j = str:find(delimiter, nil, true)
|
||||||
|
|
||||||
|
while i do
|
||||||
|
table.insert(rv, str:sub(1, i - 1))
|
||||||
|
str = str:sub(j + 1)
|
||||||
|
i, j = str:find(delimiter, nil, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(rv, str)
|
||||||
|
|
||||||
|
return rv
|
||||||
|
end
|
||||||
|
|
18
storage.lua
18
storage.lua
|
@ -26,22 +26,22 @@ function mail.getMessages()
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.getPlayerMessages(playername)
|
function mail.getPlayerMessages(playername)
|
||||||
local messages = mail.read_json_file(mail.getMailFile(playername))
|
local messages = mail.getMessages()
|
||||||
|
local playerMessages = {}
|
||||||
if messages then
|
if messages then
|
||||||
for _, msg in ipairs(messages) do
|
for _, msg in ipairs(messages) do
|
||||||
if not msg.time then
|
local receivers = mail.split((msg.to .. ", " .. (msg.cc or "") .. ", " .. (msg.bcc or "")),",")
|
||||||
-- add missing time field if not available (happens with old data)
|
for _, receiver in ipairs(receivers) do
|
||||||
msg.time = 0
|
if receiver == playername then
|
||||||
|
table.insert(playerMessages, msg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- sort by received date descending
|
|
||||||
table.sort(messages, function(a,b) return a.time > b.time end)
|
|
||||||
-- show hud notification
|
-- show hud notification
|
||||||
mail.hud_update(playername, messages)
|
mail.hud_update(playername, playerMessages)
|
||||||
end
|
end
|
||||||
|
|
||||||
return messages
|
return playerMessages
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.setMessages(playername, messages)
|
function mail.setMessages(playername, messages)
|
||||||
|
|
Loading…
Add table
Reference in a new issue