2019-02-16 16:45:32 +01:00
|
|
|
local m = mtimer
|
2019-03-06 18:22:50 +01:00
|
|
|
local d = mtimer.dialog
|
2019-02-16 16:45:32 +01:00
|
|
|
|
2019-02-22 20:33:42 +01:00
|
|
|
|
|
|
|
-- When formspec data is sent to the server check for the formname and run the
|
|
|
|
-- specific action for the given form. See Individual descriptions. The code
|
|
|
|
-- for this is very simple because most of the logic is handled in the
|
|
|
|
-- timer functions and not in the formspec code.
|
2019-02-16 16:45:32 +01:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
local meta = player:get_meta()
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
|
|
|
|
-- Select what formspec to show basing on main menu button
|
|
|
|
if formname == 'mtimer:main_menu' then
|
2019-03-06 18:22:50 +01:00
|
|
|
if fields.set_visibility then d.set_visibility(name) end
|
|
|
|
if fields.set_position then d.set_position(name) end
|
|
|
|
if fields.set_color then d.set_color(name) end
|
|
|
|
if fields.timezone_offset then d.timezone_offset(name) end
|
|
|
|
if fields.ingame_time_format then d.ingame_time_format(name) end
|
|
|
|
if fields.real_world_time_format then d.real_world_time_format(name) end
|
2019-02-21 21:07:35 +01:00
|
|
|
if fields.session_start_time_format then
|
2019-03-06 18:22:50 +01:00
|
|
|
d.session_start_time_format(name)
|
2019-02-21 21:07:35 +01:00
|
|
|
end
|
|
|
|
if fields.session_duration_format then
|
2019-03-06 18:22:50 +01:00
|
|
|
d.session_duration_format(name)
|
2019-02-21 21:07:35 +01:00
|
|
|
end
|
2019-03-06 18:22:50 +01:00
|
|
|
if fields.timer_format then d.timer_format(name) end
|
2019-02-16 16:45:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Set timer visibility
|
|
|
|
if formname == 'mtimer:set_visibility' then
|
|
|
|
local attr = m.meta.visible
|
|
|
|
if fields.visible then meta:set_string(attr.key, 'true') end
|
|
|
|
if fields.invisible then meta:set_string(attr.key, 'false') end
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-02-17 01:31:10 +01:00
|
|
|
-- Set timer position
|
|
|
|
if formname == 'mtimer:set_position' then
|
|
|
|
local attr = m.meta.position
|
|
|
|
for p,_ in pairs(fields) do
|
|
|
|
if p == 'default' then
|
|
|
|
meta:set_string(attr.key, attr.default)
|
|
|
|
elseif p:gsub('_.*', '') == 'pos' then
|
|
|
|
local new_pos = p:gsub('pos_', '')
|
|
|
|
if new_pos ~= 'xx' then meta:set_string(attr.key, new_pos) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-02-17 03:10:41 +01:00
|
|
|
-- Set timer text color
|
|
|
|
if formname == 'mtimer:set_color' then
|
|
|
|
local attr = m.meta.color
|
|
|
|
local color = ''
|
|
|
|
|
|
|
|
if fields.color then
|
2019-02-22 15:29:08 +01:00
|
|
|
local valid = fields.color:match('^#'..('[0-9a-fA-F]'):rep(6)..'$')
|
2019-02-17 03:10:41 +01:00
|
|
|
local color = valid and fields.color or attr.default
|
|
|
|
meta:set_string(attr.key, color)
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
2019-03-06 18:22:50 +01:00
|
|
|
if not fields.quit then d.set_color(name) end
|
2019-02-17 03:10:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-02-19 13:40:19 +01:00
|
|
|
-- Configure timezone offset
|
|
|
|
if formname == 'mtimer:timezone_offset' then
|
|
|
|
local attr = m.meta.timezone_offset
|
|
|
|
local value = tonumber(fields.offset) or attr.default
|
|
|
|
if math.abs(value) > os.time() then value = 0 end
|
|
|
|
meta:set_string(attr.key, value)
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
2019-03-06 18:22:50 +01:00
|
|
|
if not fields.quit then d.timezone_offset(name) end
|
2019-02-19 13:40:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-02-21 16:16:10 +01:00
|
|
|
-- Set ingame time format
|
|
|
|
if formname == 'mtimer:ingame_time_format' then
|
|
|
|
local attr = m.meta.ingame_time
|
|
|
|
local value = fields.format or attr.default
|
|
|
|
meta:set_string(attr.key, value)
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
2019-03-06 18:22:50 +01:00
|
|
|
if not fields.quit then d.ingame_time_format(name)end
|
2019-02-21 16:16:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-02-21 21:07:35 +01:00
|
|
|
-- Set real-time format
|
|
|
|
if formname == 'mtimer:real_world_time_format' then
|
|
|
|
local attr = m.meta.real_time
|
|
|
|
local value = fields.format or attr.default
|
|
|
|
meta:set_string(attr.key, value)
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
2019-03-06 18:22:50 +01:00
|
|
|
if not fields.quit then d.real_world_time_format(name) end
|
2019-02-21 21:07:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Set session start time format
|
|
|
|
if formname == 'mtimer:session_start_time_format' then
|
|
|
|
local attr = m.meta.session_start_time
|
|
|
|
local value = fields.format or attr.default
|
|
|
|
meta:set_string(attr.key, value)
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
2019-03-06 18:22:50 +01:00
|
|
|
if not fields.quit then d.session_start_time_format(name) end
|
2019-02-21 21:07:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Set session duration format
|
|
|
|
if formname == 'mtimer:session_duration_format' then
|
|
|
|
local attr = m.meta.session_duration
|
|
|
|
local value = fields.format or attr.default
|
|
|
|
meta:set_string(attr.key, value)
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
2019-03-06 18:22:50 +01:00
|
|
|
if not fields.quit then d.session_duration_format(name) end
|
2019-02-21 21:07:35 +01:00
|
|
|
end
|
|
|
|
|
2019-02-19 13:40:19 +01:00
|
|
|
|
2019-02-22 14:08:44 +01:00
|
|
|
-- Set timer text
|
|
|
|
if formname == 'mtimer:timer_format' then
|
|
|
|
local attr = m.meta.timer_format
|
|
|
|
local value = fields.format or attr.default
|
|
|
|
meta:set_string(attr.key, value)
|
|
|
|
if fields.default then meta:set_string(attr.key, attr.default) end
|
2019-03-06 18:22:50 +01:00
|
|
|
if not fields.quit then d.timer_format(name) end
|
2019-02-22 14:08:44 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-02-22 15:29:08 +01:00
|
|
|
-- Back to menu from all formspecs and conditionally update timer
|
2019-03-06 18:22:50 +01:00
|
|
|
if fields.main_menu then d.main_menu(name) end
|
2019-02-22 15:50:32 +01:00
|
|
|
if formname ~= 'mtimer:main_menu' then m.update_timer(name) end
|
|
|
|
|
|
|
|
|
|
|
|
-- Reset everything
|
|
|
|
if fields.reset_everything then
|
|
|
|
for _,def in pairs(m.meta) do
|
|
|
|
meta:set_string(def.key, def.default)
|
|
|
|
end
|
|
|
|
end
|
2019-02-16 16:45:32 +01:00
|
|
|
|
2019-02-22 20:33:42 +01:00
|
|
|
|
2019-02-16 16:45:32 +01:00
|
|
|
end)
|