Add contact form
Stores data in contact.msg, each message having its own sub-table. This table is prooven to have been written and read properly accross restarts of the game.master
parent
849d213303
commit
08792469e3
|
@ -11,7 +11,7 @@ An admin/moderator can be assigned to each case, as all with the `contact` privi
|
|||
Not all of the features mentioned above are done, so here's what is and what isn't.
|
||||
|
||||
- [ ] Admin Dashboard
|
||||
- [ ] Contact
|
||||
- [x] Contact
|
||||
- [ ] Conversations
|
||||
- [ ] Report
|
||||
- [ ] Lock/Hide
|
15
api.md
15
api.md
|
@ -18,4 +18,17 @@ Loads everything stored in the contact world-files to the global `contact` table
|
|||
#### `save`
|
||||
__Usage:__ `contact.save()`
|
||||
|
||||
Gathers specific data from the global `contact` table and saves it in `<worldname>/mod_contact.txt`. __Note:__ this is automatically called at shutdown and whenever specific actions take place.
|
||||
Gathers specific data from the global `contact` table and saves it in `<worldname>/mod_contact.txt`. __Note:__ this is automatically called at shutdown and whenever specific actions take place.
|
||||
|
||||
#### `toggle_main`
|
||||
__Usage:__ `contact.toggle_main(<player name (string)>, <show/hide (boolean>, <error message (string)>, <form fields (table)>`
|
||||
|
||||
This allows an external mod to show the contact form to any specific player. By default, the formspec is shown when I player uses the `/contact` command. Only the `name` string is required, and should be the name of a player. The second parameter allows toggling between showing/updating the formspec or hiding it with `true`/`false` (default: `true`). The third parameter allows one to specify a message to show at the bottom, typically used for errors. The final parameter is used to pre-fill the form fields (see below for structure).
|
||||
|
||||
__Example:__
|
||||
```lua
|
||||
contact.toggle_main("singleplayer", true, "Warning!", {
|
||||
subject = "Test",
|
||||
msg = "Hello World!",
|
||||
})
|
||||
```
|
|
@ -0,0 +1,77 @@
|
|||
-- contact/contact.lua
|
||||
|
||||
-- Formspec
|
||||
local function formspec(error, fields)
|
||||
local error = error or ""
|
||||
local fields = fields or {}
|
||||
local e = minetest.formspec_escape
|
||||
|
||||
fields.subject = fields.subject or ""
|
||||
fields.msg = fields.msg or ""
|
||||
|
||||
return [[
|
||||
size[9,8.9]
|
||||
]]..contact.gui_bg..[[
|
||||
field[0.5,0.7;7.5,1;subject;Message Subject;]]..e(fields.subject)..[[]
|
||||
button_exit[7.7,0.38;1,1;exit;X]
|
||||
tooltip[exit;Discard Message and Close]
|
||||
textarea[0.5,1.6;8.5,7.1;msg;Message;]]..e(fields.msg)..[[]
|
||||
button[0.21,8;8.5,1;send;Send Message]
|
||||
label[0.25,8.8;]]..e(error)..[[]
|
||||
]]
|
||||
end
|
||||
|
||||
-- [function] Toggle Contact Formspec
|
||||
function contact.toggle_main(name, show, error, fields)
|
||||
if not minetest.get_player_by_name(name) then
|
||||
return
|
||||
end
|
||||
|
||||
if show ~= false then
|
||||
minetest.show_formspec(name, "contact:main", formspec(error, fields))
|
||||
else
|
||||
minetest.close_formspec(name, "contact:main")
|
||||
end
|
||||
end
|
||||
|
||||
-- [event] Handle Form Input
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= "contact:main" then return end -- Check formname
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
if fields.send then
|
||||
-- Check for missing fields
|
||||
if not fields.subject or fields.subject == "" then
|
||||
contact.toggle_main(name, true, "Subject Required", fields)
|
||||
elseif not fields.msg or fields.msg == "" then
|
||||
contact.toggle_main(name, true, "Message Required", fields)
|
||||
else -- else, Send message
|
||||
local msgdata = {
|
||||
from = name,
|
||||
subject = fields.subject,
|
||||
msg = fields.msg,
|
||||
}
|
||||
|
||||
-- Insert Data
|
||||
table.insert(contact.msg, msgdata)
|
||||
-- Close form
|
||||
contact.toggle_main(name, false)
|
||||
-- Print to chat
|
||||
minetest.chat_send_player(name, "Message sent!")
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- [register] Main Chatcommand
|
||||
minetest.register_chatcommand("contact", {
|
||||
description = "Contact an admin",
|
||||
func = function(name)
|
||||
-- Show formspec
|
||||
contact.toggle_main(name, true)
|
||||
|
||||
return true, "Opening Contact Form"
|
||||
end,
|
||||
})
|
||||
|
||||
contact.log("Loaded Contact Functionality", "info")
|
7
init.lua
7
init.lua
|
@ -4,6 +4,9 @@ contact = {}
|
|||
local modpath = minetest.get_modpath("contact")
|
||||
local worldpath = minetest.get_worldpath()
|
||||
|
||||
-- Formspec GUI related stuff
|
||||
contact.gui_bg = "bgcolor[#080808BB;true]background[5,5;1,1;gui_formbg.png;true]"
|
||||
|
||||
-- [function] Logger
|
||||
function contact.log(content, log_type)
|
||||
assert(content, "contact.log: content nil")
|
||||
|
@ -46,3 +49,7 @@ minetest.register_on_shutdown(contact.save)
|
|||
|
||||
-- Load on start
|
||||
contact.load()
|
||||
|
||||
-- Load Resources
|
||||
|
||||
dofile(modpath.."/contact.lua") -- Contact Functionality
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 971 B |
Loading…
Reference in New Issue