Загрузить файлы в «util»
This commit is contained in:
parent
9058805f8d
commit
5c832dfc33
58
util/colors.lua
Normal file
58
util/colors.lua
Normal file
@ -0,0 +1,58 @@
|
||||
local generic_colors = {
|
||||
header = "#999999",
|
||||
selected = "#72FF63",
|
||||
important = "#FFD700",
|
||||
additional = "#CCCCDD",
|
||||
highlighted = "#608631",
|
||||
new = "#00F529",
|
||||
warning = "#FF8800",
|
||||
disabled = "#332222",
|
||||
muted = "#CCCCCC",
|
||||
}
|
||||
|
||||
local function get_base_color(c)
|
||||
return generic_colors[c] or ""
|
||||
end
|
||||
|
||||
local function hex2rgb(hex)
|
||||
hex = hex:gsub("#","")
|
||||
return {
|
||||
r = tonumber("0x" .. hex:sub(1,2)),
|
||||
g = tonumber("0x" .. hex:sub(3,4)),
|
||||
b = tonumber("0x" .. hex:sub(5,6))
|
||||
}
|
||||
end
|
||||
|
||||
local function rgb2hex(rgb)
|
||||
return "#" .. string.format("%x", rgb.r) .. string.format("%x", rgb.g) .. string.format("%x", rgb.b)
|
||||
end
|
||||
|
||||
local function rgb_colors_mix(colors)
|
||||
local R = 0
|
||||
local G = 0
|
||||
local B = 0
|
||||
for _, c in ipairs(colors) do
|
||||
R = R + c.r
|
||||
G = G + c.g
|
||||
B = B + c.b
|
||||
end
|
||||
R = math.floor(R / #colors + 0.5)
|
||||
G = math.floor(G / #colors + 0.5)
|
||||
B = math.floor(B / #colors + 0.5)
|
||||
return {r=R,g=G,b=B}
|
||||
end
|
||||
|
||||
function mail.get_color(mix)
|
||||
if type(mix) == "string" then
|
||||
return get_base_color(mix)
|
||||
elseif #mix == 1 then
|
||||
return get_base_color(mix[1])
|
||||
else
|
||||
local colors2mix = {}
|
||||
for _, c in ipairs(mix) do
|
||||
colors2mix[#colors2mix+1] = hex2rgb(get_base_color(c))
|
||||
end
|
||||
local mixed_color = rgb_colors_mix(colors2mix)
|
||||
return rgb2hex(mixed_color)
|
||||
end
|
||||
end
|
75
util/contact.lua
Normal file
75
util/contact.lua
Normal file
@ -0,0 +1,75 @@
|
||||
-- translation
|
||||
local S = mail.S
|
||||
|
||||
function mail.compile_contact_list(name, selected, playernames)
|
||||
-- TODO: refactor this - not just compiles *a* list, but *the* list for the contacts screen (too inflexible)
|
||||
local formspec = {}
|
||||
local contacts = mail.get_contacts(name)
|
||||
|
||||
if playernames == nil then
|
||||
local length = 0
|
||||
for k, contact, i, l in mail.pairs_by_keys(contacts) do
|
||||
if i == 1 then length = l end
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(contact.name)
|
||||
formspec[#formspec + 1] = ","
|
||||
local note = contact.note
|
||||
-- display an ellipsis if the note spans multiple lines
|
||||
local idx = string.find(note, '\n')
|
||||
if idx ~= nil then
|
||||
note = string.sub(note, 1, idx-1) .. ' ...'
|
||||
end
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(note)
|
||||
if type(selected) == "string" then
|
||||
if string.lower(selected) == k then
|
||||
selected = i
|
||||
end
|
||||
end
|
||||
end
|
||||
if length > 0 then
|
||||
if selected and type(selected) == "number" then
|
||||
formspec[#formspec + 1] = ";"
|
||||
formspec[#formspec + 1] = tostring(selected + 1)
|
||||
end
|
||||
formspec[#formspec + 1] = "]"
|
||||
else
|
||||
formspec[#formspec + 1] = "]label[2,4.5;" .. S("No contacts") .. "]"
|
||||
end
|
||||
else
|
||||
if type(playernames) == "string" then
|
||||
playernames = mail.parse_player_list(playernames)
|
||||
end
|
||||
for i,c in ipairs(playernames) do
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(c)
|
||||
formspec[#formspec + 1] = ","
|
||||
if contacts[string.lower(c)] == nil then
|
||||
formspec[#formspec + 1] = ""
|
||||
else
|
||||
local note = contacts[string.lower(c)].note
|
||||
-- display an ellipsis if the note spans multiple lines
|
||||
local idx = string.find(note, '\n')
|
||||
if idx ~= nil then
|
||||
note = string.sub(note, 1, idx-1) .. ' ...'
|
||||
end
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(note)
|
||||
end
|
||||
if not selected then
|
||||
if type(selected) == "string" then
|
||||
if string.lower(selected) == string.lower(c) then
|
||||
selected = i
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if #playernames > 0 and selected and type(selected) == "number" then
|
||||
formspec[#formspec + 1] = ";"
|
||||
formspec[#formspec + 1] = tostring(selected + 1)
|
||||
end
|
||||
formspec[#formspec + 1] = "]"
|
||||
end
|
||||
return table.concat(formspec, "")
|
||||
|
||||
end
|
9
util/init.lua
Normal file
9
util/init.lua
Normal file
@ -0,0 +1,9 @@
|
||||
-- sub files
|
||||
local MP = minetest.get_modpath(minetest.get_current_modname())
|
||||
dofile(MP .. "/util/normalize.lua")
|
||||
dofile(MP .. "/util/colors.lua")
|
||||
dofile(MP .. "/util/contact.lua")
|
||||
dofile(MP .. "/util/settings.lua")
|
||||
dofile(MP .. "/util/spam.lua")
|
||||
dofile(MP .. "/util/time_ago.lua")
|
||||
dofile(MP .. "/util/uuid.lua")
|
81
util/normalize.lua
Normal file
81
util/normalize.lua
Normal file
@ -0,0 +1,81 @@
|
||||
-- translation
|
||||
local S = mail.S
|
||||
|
||||
local function recursive_expand_recipient_names(sender, list, is_toplevel, recipients, undeliverable)
|
||||
for _, name in ipairs(list) do
|
||||
if not (recipients[name] or undeliverable[name] or (name == sender and not is_toplevel)) then
|
||||
local succ, value
|
||||
for _, handler in ipairs(mail.registered_recipient_handlers) do
|
||||
succ, value = handler(sender, name)
|
||||
if succ ~= nil then
|
||||
break
|
||||
end
|
||||
end
|
||||
local vtp = type(value)
|
||||
if succ then
|
||||
if vtp == "string" then
|
||||
recursive_expand_recipient_names(sender, {value}, is_toplevel, recipients, undeliverable)
|
||||
elseif vtp == "table" then
|
||||
recursive_expand_recipient_names(sender, value, false, recipients, undeliverable)
|
||||
elseif vtp == "function" then
|
||||
recipients[name] = value
|
||||
else
|
||||
undeliverable[name] = S("The method of delivery to @1 is invalid.", name)
|
||||
end
|
||||
elseif succ == nil then
|
||||
undeliverable[name] = S("The recipient @1 could not be identified.", name)
|
||||
else
|
||||
local reason = tostring(value) or S("@1 rejected your mail.", name)
|
||||
undeliverable[name] = reason
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
return the field normalized (comma separated, single space)
|
||||
and add individual player names to recipient list
|
||||
--]]
|
||||
function mail.normalize_players_and_add_recipients(sender, field, recipients, undeliverable)
|
||||
local order = mail.parse_player_list(field)
|
||||
recursive_expand_recipient_names(sender, order, true, recipients, undeliverable)
|
||||
return mail.concat_player_list(order)
|
||||
end
|
||||
|
||||
function mail.parse_player_list(field)
|
||||
if not field then
|
||||
return {}
|
||||
end
|
||||
|
||||
local separator = ",%s"
|
||||
local pattern = "([^" .. separator .. "]+)"
|
||||
|
||||
-- get individual players
|
||||
local order = {}
|
||||
for name in field:gmatch(pattern) do
|
||||
table.insert(order, name)
|
||||
end
|
||||
|
||||
return order
|
||||
end
|
||||
|
||||
function mail.concat_player_list(order)
|
||||
-- turn list of players back into normalized string
|
||||
if order == nil or #order == 0 then
|
||||
return ""
|
||||
end
|
||||
return table.concat(order, ", ")
|
||||
end
|
||||
|
||||
function mail.player_in_list(name, list)
|
||||
list = list or {}
|
||||
if type(list) == "string" then
|
||||
list = mail.parse_player_list(list)
|
||||
end
|
||||
for _, player_name in pairs(list) do
|
||||
if name == player_name then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
12
util/normalize.spec.lua
Normal file
12
util/normalize.spec.lua
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
mtt.register("util/normalize_players_and_add_recipients", function(callback)
|
||||
local recipients = {}
|
||||
local undeliverable = {}
|
||||
local to = mail.normalize_players_and_add_recipients("sender", "player1,player2", recipients, undeliverable)
|
||||
|
||||
assert(to == "player1, player2")
|
||||
assert(not next(undeliverable))
|
||||
assert(recipients["player1"])
|
||||
assert(recipients["player2"])
|
||||
callback()
|
||||
end)
|
Loading…
Reference in New Issue
Block a user