From fbca7a64fa5ec0cd36d876a59a927571d9e865b9 Mon Sep 17 00:00:00 2001 From: Dirk Sohler Date: Tue, 19 Feb 2019 13:40:19 +0100 Subject: [PATCH] implement timezone offset configuration --- init.lua | 1 + locale/mtimer.de.tr | 7 +++++++ system/formspec_creation.lua | 32 ++++++++++++++++++++++++++++++++ system/on_receive_fields.lua | 13 +++++++++++++ 4 files changed, 53 insertions(+) diff --git a/init.lua b/init.lua index d241f3b..7ffeb74 100644 --- a/init.lua +++ b/init.lua @@ -9,6 +9,7 @@ mtimer = { visible = { key = 'mtimer:visible', default = 'true' }, position = { key = 'mtimer:position', default = 'bl' }, color = { key = 'mtimer:color', default = '#ffffffFF' }, + timezone_offset = { key = 'mtimer:timezone_offset', default = '0' }, } } diff --git a/locale/mtimer.de.tr b/locale/mtimer.de.tr index 219f052..ea3b066 100644 --- a/locale/mtimer.de.tr +++ b/locale/mtimer.de.tr @@ -24,6 +24,13 @@ Click the position you want to place the timer at.=Auf die Stelle klicken, an de mTimer Color=mTimer-Farbe Use `@1` format only!=Ausschließlich `@1`-Format benutzen! +# Timezone Offset +mTimer Timezone Offset=mTimer-Zeitzonenunterschied +30 minutes @= 0.5, 60 minutes @= 1=30 Minuten @= 0.5, 60 Minuten @= 1 +“Arbitrary” values are possible.=Es sind „beliebige“ Werte möglich. +Server Time: @1=Serverzeit: @1 +Local Time: @1=Lokale Zeit: @1 + # Generic Formspec Strings Default=Standard Exit=Verlassen diff --git a/system/formspec_creation.lua b/system/formspec_creation.lua index 351aa67..d5621f4 100644 --- a/system/formspec_creation.lua +++ b/system/formspec_creation.lua @@ -12,6 +12,9 @@ local build_frame = function (width, height, title) container_end[] ]] + local width = width < 6 and 6 or width + local height = height < 2 and 2 or height + return formspec_frame:gsub('%+%a+', { ['+width'] = width, ['+height'] = height, @@ -94,3 +97,32 @@ mtimer.show_formspec.set_color = function (player_name) label[-0.05,1.45;]]..S('Use `@1` format only!', hexcolor)..[[] ]]) end + + +mtimer.show_formspec.timezone_offset = function (player_name) + local player = minetest.get_player_by_name(player_name) + local offset = player:get_meta():get_string(m.meta.timezone_offset.key) + + local server_timestamp = os.time() + local local_timestamp = server_timestamp + ((offset*60)*60) + local server_time = os.date('%Y-%m-%dT%T', server_timestamp) + local local_time = os.date('%Y-%m-%dT%T', local_timestamp) + + local format_information = table.concat({ + S('30 minutes @= 0.5, 60 minutes @= 1'), + S('“Arbitrary” values are possible.') + }, '\n') + + local time_information = table.concat({ + S('Server Time: @1', server_time), + S('Local Time: @1', local_time) + }, '\n') + + minetest.show_formspec(player_name, 'mtimer:timezone_offset', + build_frame(7, 3, S('mTimer Timezone Offset'))..[[ + field_close_on_enter[offset;false] + field[0.25,0.75;3,1;offset;;]]..offset..[[] + label[3,0.425;]]..format_information..[[] + label[0,1.45;]]..time_information..[[] + ]]) +end diff --git a/system/on_receive_fields.lua b/system/on_receive_fields.lua index 74afd7b..975410b 100644 --- a/system/on_receive_fields.lua +++ b/system/on_receive_fields.lua @@ -11,6 +11,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.set_visibility then f.set_visibility(name) end if fields.set_position then f.set_position(name) end if fields.set_color then f.set_color(name) end + if fields.timezone_offset then f.timezone_offset(name) end end @@ -53,6 +54,18 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end + -- 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 + if not fields.quit then mtimer.show_formspec.timezone_offset(name) end + end + + + -- Back to menu from all formspecs if fields.main_menu then f.main_menu(name) end