mirror of
https://github.com/mt-mods/mail.git
synced 2025-04-30 08:21:44 -04:00
Fix many issues related to maillists
Notably : edit, delete, selection, creation, registration of players
This commit is contained in:
parent
7247e6bb5c
commit
41aa75d8fe
2 changed files with 101 additions and 51 deletions
57
gui.lua
57
gui.lua
|
@ -87,7 +87,7 @@ mail.maillists_formspec = "size[8,9;]" .. theme .. [[
|
|||
button[6,1.60;2,0.5;delete;Delete]
|
||||
button[6,8.25;2,0.5;back;Back]
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0;5.75,9;contacts;#999,Name,Description]]
|
||||
table[0,0;5.75,9;maillists;#999,Name,Description]]
|
||||
|
||||
|
||||
function mail.show_about(name)
|
||||
|
@ -932,18 +932,18 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
|
||||
if fields.maillists then
|
||||
local evt = minetest.explode_table_event(fields.maillists)
|
||||
for k, _, i in mail.pairsByKeys(maillists) do
|
||||
if i == evt.row - 1 then
|
||||
selected_idxs.maillists[name] = k
|
||||
break
|
||||
end
|
||||
end
|
||||
selected_idxs.maillists[name] = evt.row - 1
|
||||
if evt.type == "DCL" and maillists[selected_idxs.maillists[name]] then
|
||||
local players_ml = mail.getPlayersDataInMaillist(maillists[selected_idxs.maillists[name]].id)
|
||||
local players_string = ""
|
||||
for _, p in ipairs(players_ml) do
|
||||
players_string = players_string .. p.player .. " " .. p.status .. "\n"
|
||||
end
|
||||
mail.show_edit_maillist(
|
||||
name,
|
||||
maillists[selected_idxs.maillists[name]].name,
|
||||
maillists[selected_idxs.maillists[name]].desc,
|
||||
""
|
||||
players_string
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -951,12 +951,17 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
selected_idxs.maillists[name] = "#NEW#"
|
||||
mail.show_edit_maillist(name, "", "", "Player1 to\nPlayer2 cc\nPlayer3 bcc")
|
||||
|
||||
elseif fields.edit and selected_idxs.maillists[name] and maillists[selected_idxs.maillists[name]] then
|
||||
elseif fields.edit and maillists[selected_idxs.maillists[name]] then
|
||||
local players_ml = mail.getPlayersDataInMaillist(maillists[selected_idxs.maillists[name]].id)
|
||||
local players_string = ""
|
||||
for _, p in ipairs(players_ml) do
|
||||
players_string = players_string .. p.player .. " " .. p.status .. "\n"
|
||||
end
|
||||
mail.show_edit_maillist(
|
||||
name,
|
||||
maillists[selected_idxs.maillists[name]].name,
|
||||
maillists[selected_idxs.maillists[name]].desc,
|
||||
""
|
||||
players_string
|
||||
)
|
||||
|
||||
elseif fields.delete then
|
||||
|
@ -970,7 +975,7 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
selected_idxs.maillists[name] = k
|
||||
break
|
||||
elseif k == selected_idxs.maillists[name] then
|
||||
mail.deleteMaillist(name, maillists[selected_idxs.maillists[name]].id)
|
||||
mail.deleteMaillist(maillists[selected_idxs.maillists[name]].id)
|
||||
selected_idxs.maillists[name] = nil
|
||||
found = true
|
||||
else
|
||||
|
@ -1001,32 +1006,14 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
local maillists = mail.getPlayerMaillists(name)
|
||||
|
||||
if fields.save then
|
||||
local maillist = {
|
||||
owner = name,
|
||||
name = fields.name,
|
||||
desc = fields.desc,
|
||||
}
|
||||
if selected_idxs.maillists[name] and selected_idxs.maillists[name] ~= "#NEW#" then
|
||||
local maillist = maillists[selected_idxs.maillists[name]]
|
||||
if selected_idxs.maillists[name] ~= string.lower(fields.name) then
|
||||
-- name changed!
|
||||
if #fields.name == 0 then
|
||||
mail.show_edit_maillist(name, maillist.name, fields.desc, "empty")
|
||||
return true
|
||||
|
||||
elseif maillists[string.lower(fields.name)] ~= nil then
|
||||
mail.show_edit_maillist(name, maillist.name, fields.desc, "collision")
|
||||
return true
|
||||
|
||||
else
|
||||
mail.setMaillist(name, maillist)
|
||||
maillists[selected_idxs.maillists[name]] = nil
|
||||
end
|
||||
end
|
||||
maillist.name = fields.name
|
||||
maillist.desc = fields.desc
|
||||
|
||||
mail.setMaillist(maillists[selected_idxs.maillists[name]].id, maillist, fields.players)
|
||||
else
|
||||
local maillist = {
|
||||
owner = name,
|
||||
name = fields.name,
|
||||
desc = fields.desc,
|
||||
}
|
||||
mail.addMaillist(maillist, fields.players)
|
||||
end
|
||||
mail.show_maillists(name)
|
||||
|
|
93
storage.lua
93
storage.lua
|
@ -227,7 +227,7 @@ function mail.getPlayerMaillists(playername)
|
|||
local playerMaillists = {}
|
||||
for _, maillist in ipairs(maillists) do
|
||||
if maillist.owner == playername then
|
||||
table.insert(playerMaillists, {name = maillist.name, desc = maillist.desc})
|
||||
table.insert(playerMaillists, {id = maillist.id, name = maillist.name, desc = maillist.desc})
|
||||
end
|
||||
end
|
||||
return playerMaillists
|
||||
|
@ -258,6 +258,33 @@ function mail.addMaillist(maillist, players_string)
|
|||
end
|
||||
end
|
||||
|
||||
function mail.setMaillist(ml_id, updated_maillist, players_string)
|
||||
local maillists = mail.getMaillists()
|
||||
local maillist_id = 0
|
||||
for _, maillist in ipairs(maillists) do
|
||||
if maillist.id == ml_id then
|
||||
maillist_id = maillist.id
|
||||
maillists[_] = {id = maillist_id, owner = updated_maillist.owner, name = updated_maillist.name, desc = updated_maillist.desc}
|
||||
end
|
||||
end
|
||||
if mail.write_json_file(mail.maildir .. "/mail.maillists.json", maillists) then
|
||||
-- remove all players
|
||||
mail.removePlayersFromMaillist(maillist_id)
|
||||
-- to add those registered in the updated maillist
|
||||
local players = mail.split(players_string,"\n")
|
||||
for _, player in ipairs(players) do
|
||||
local playerInfo = mail.split(player, " ")
|
||||
if minetest.player_exists(playerInfo[1]) then -- avoid blank names
|
||||
mail.addPlayerToMaillist(playerInfo[1], maillist_id, playerInfo[2])
|
||||
end
|
||||
end
|
||||
return true
|
||||
else
|
||||
minetest.log("error","[mail] Save failed - maillist may be lost!")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function mail.getMaillistIdFromName(ml_name)
|
||||
local maillists = mail.getMaillists()
|
||||
local ml_id = 0
|
||||
|
@ -275,17 +302,28 @@ function mail.getPlayersInMaillists()
|
|||
return players_mls
|
||||
end
|
||||
|
||||
function mail.getPlayersInMaillist(ml_id)
|
||||
function mail.getPlayersDataInMaillist(ml_id)
|
||||
local players_mls = mail.getPlayersInMaillists() -- players from all maillists
|
||||
local player_ml = {} -- players from this maillist
|
||||
local players_ml = {} -- players from this maillist
|
||||
if players_mls[1] then
|
||||
for _, playerInfo in ipairs(players_mls) do
|
||||
if playerInfo.id == ml_id then
|
||||
table.insert(player_ml, 1, playerInfo.player)
|
||||
table.insert(players_ml, 1, playerInfo)
|
||||
end
|
||||
end
|
||||
end
|
||||
return player_ml
|
||||
return players_ml
|
||||
end
|
||||
|
||||
function mail.getPlayersInMaillist(ml_id)
|
||||
local players_ml = mail.getPlayersDataInMaillist(ml_id) -- players from this maillist
|
||||
local players_names_ml = {}
|
||||
if players_ml[1] then
|
||||
for _, playerInfo in ipairs(players_ml) do
|
||||
table.insert(players_names_ml, 1, playerInfo.player)
|
||||
end
|
||||
end
|
||||
return players_names_ml
|
||||
end
|
||||
|
||||
function mail.addPlayerToMaillist(player, ml_id, status)
|
||||
|
@ -300,29 +338,54 @@ function mail.addPlayerToMaillist(player, ml_id, status)
|
|||
end
|
||||
end
|
||||
|
||||
function mail.deleteMaillist(ml_id)
|
||||
-- remove players attached to the maillist
|
||||
local maillists_players = mail.getPlayersInMaillists()
|
||||
for _, player in ipairs(maillist_players) do
|
||||
if player.id then
|
||||
table.remove(maillist_players, _)
|
||||
function mail.setPlayerInMaillist(playername, ml_id, status)
|
||||
local playersMls = mail.getPlayersInMaillists()
|
||||
local updated_player = {id = ml_id, player = player, status = status}
|
||||
table.insert(playersMls, 1, new_player)
|
||||
for _, player in ipairs(playersMls) do
|
||||
if player.player == playername and player.id == ml_id then
|
||||
playersMls[_] = {id = ml_id, player = player, status = status}
|
||||
end
|
||||
end
|
||||
if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", maillist_players) then
|
||||
if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", playersMls) then
|
||||
return true
|
||||
else
|
||||
minetest.log("error","[mail] Save failed - messages may be lost!")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function mail.removePlayersFromMaillist(ml_id)
|
||||
local maillists_players = mail.getPlayersInMaillists()
|
||||
for _, player in ipairs(maillists_players) do
|
||||
if player.id == ml_id then
|
||||
table.remove(maillists_players, _)
|
||||
end
|
||||
end
|
||||
if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", maillists_players) then
|
||||
return true
|
||||
else
|
||||
minetest.log("error","[mail] Save failed!")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
--then remove the maillist itself
|
||||
function mail.deleteMaillist(ml_id)
|
||||
local maillists = mail.getMaillists()
|
||||
local maillists_players = mail.getPlayersInMaillists()
|
||||
-- remove players attached to the maillist
|
||||
for _, player in ipairs(maillists_players) do
|
||||
if player.id == ml_id then
|
||||
table.remove(maillists_players, _)
|
||||
end
|
||||
end
|
||||
-- then remove the maillist itself
|
||||
for _, maillist in ipairs(maillists) do
|
||||
if maillist.id then
|
||||
if maillist.id == ml_id then
|
||||
table.remove(maillists, _)
|
||||
end
|
||||
end
|
||||
if mail.write_json_file(mail.maildir .. "/mail.maillists.json", maillists) then
|
||||
if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", maillists_players) and mail.write_json_file(mail.maildir .. "/mail.maillists.json", maillists) then
|
||||
return true
|
||||
else
|
||||
minetest.log("error","[mail] Save failed!")
|
||||
|
|
Loading…
Add table
Reference in a new issue