From 3aeae8435664e074bb91f9f25f5588cb63d2b315 Mon Sep 17 00:00:00 2001 From: Dirk Sohler Date: Fri, 22 Feb 2019 14:08:44 +0100 Subject: [PATCH] implement timer format setting --- init.lua | 20 +++++++++++-- locale/mtimer.de.tr | 2 +- system/chat_command.lua | 6 ++-- system/formspec/formspec_creation.lua | 41 ++++++++++++++++++++++++++- system/get_timer_data.lua | 28 ++++++++++++++++++ system/on_receive_fields.lua | 11 +++++++ 6 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 system/get_timer_data.lua diff --git a/init.lua b/init.lua index 91dc993..e7ec27a 100644 --- a/init.lua +++ b/init.lua @@ -10,8 +10,14 @@ mtimer = { position = { key = 'mtimer:position', default = 'bl' }, color = { key = 'mtimer:color', default = '#ffffffFF' }, timezone_offset = { key = 'mtimer:timezone_offset', default = '0' }, - ingame_time = {key='mtimer:ingame_time_format',default='{24h}:{min}'}, - real_time = { key='mtimer:real_time_format', default='{24h}:{min}' }, + ingame_time = { + key = 'mtimer:ingame_time_format', + default = '{24h}:{min}' + }, + real_time = { + key = 'mtimer:real_time_format', + default = '{24h}:{min} ({isodate})' + }, session_start_time = { key = 'mtimer:session_start_time_format', default = '{isodate}T{isotime}' @@ -19,7 +25,14 @@ mtimer = { session_duration = { key = 'mtimer:session_duration_format', default = '{hours}:{minutes}' - } + }, + timer_format = { + key = 'mtimer:timer_format', + default = 'Current Date: {rd}\n'.. + 'Ingame Time: {it}\n'.. + 'Session Start: {st}\n'.. + 'Session Duration: {sd}' + }, } } @@ -35,4 +48,5 @@ dofile(syspath..'timer_update.lua') dofile(syspath..'on_receive_fields.lua') dofile(syspath..'on_joinplayer.lua') dofile(syspath..'get_times.lua') +dofile(syspath..'get_timer_data.lua') diff --git a/locale/mtimer.de.tr b/locale/mtimer.de.tr index 9c0dfa0..57a49a5 100644 --- a/locale/mtimer.de.tr +++ b/locale/mtimer.de.tr @@ -10,7 +10,7 @@ Ingame Time Format=Spielzeit-Format Real-World Time Format=Realzeit-Format Session Start Time Format=Session-Startzeit-Format Session Duration Format=Session-Dauer-Format -Set Timer Text=Timertext einstellen +Timer Format=Timerformat Open Main Menu=Hauptmenü öffnen # Visibility diff --git a/system/chat_command.lua b/system/chat_command.lua index 1f3e6c4..2880715 100644 --- a/system/chat_command.lua +++ b/system/chat_command.lua @@ -10,7 +10,7 @@ end minetest.register_chatcommand('mtimer', { description = S('Configure timer display'), - params = '', + params = '', func = function(name, parameters) local action = parameters:match('%a+') @@ -25,7 +25,7 @@ minetest.register_chatcommand('mtimer', { if action == 're' then fs.real_world_time_format(name) end if action == 'st' then fs.session_start_time_format(name) end if action == 'sd' then fs.session_duration_format(name) end - if action == 'te' then fs.timer_text(name) end + if action == 'tf' then fs.timer_format(name) end if action == 'help' then local message = { @@ -37,7 +37,7 @@ minetest.register_chatcommand('mtimer', { command('re')..S('Real-World Time Format'), command('st')..S('Session Start Time Format'), command('sd')..S('Session Duration Format'), - command('te')..S('Set Timer Text'), + command('tf')..S('Timer Format'), command(' ')..S('Open Main Menu') } cs(name, table.concat(message, '\n')) diff --git a/system/formspec/formspec_creation.lua b/system/formspec/formspec_creation.lua index 8966c93..a9f843f 100644 --- a/system/formspec/formspec_creation.lua +++ b/system/formspec/formspec_creation.lua @@ -17,7 +17,7 @@ mtimer.show_formspec.main_menu = function (player_name) button[0,5.5;4,1;real_world_time_format;]]..S('Real-World Time Format')..[[] button[0,6.5;4,1;session_start_time_format;]]..S('Session Start Time Format')..[[] button[0,7.5;4,1;session_duration_format;]]..S('Session Duration Format')..[[] - button[0,8.5;4,1;timer_text;]]..S('Set Timer Text')..[[] + button[0,8.5;4,1;timer_format;]]..S('Timer Format')..[[] button_exit[0,9.5;4,1;exit;]]..S('Exit')..[[] ]]) end @@ -196,3 +196,42 @@ mtimer.show_formspec.session_duration_format = function (player_name) container_end[] ]]) end + + +mtimer.show_formspec.timer_format = function (player_name) + local timer_data = mtimer.get_timer_data(player_name) + minetest.show_formspec(player_name, 'mtimer:timer_format', + build_frame(9, 5.8, S('Timer Format'))..[[ + textarea[0.25,0.5;6,2.5;format;;]]..fe(timer_data.format)..[[] + + container[0,2.785] + + label[2.5,0;]]..S('Variable')..[[] + label[4,0;]]..S('Current value')..[[] + + box[0,0.45;8.75,0.02;#ffffff] + + label[0,0.5;]]..S('Real-World Date')..[[] + label[2.5,0.5;{rd}] + label[4,0.5;]]..fe(timer_data.real_world_date)..[[] + + label[0,0.9;]]..S('In-Game Time')..[[] + label[2.5,0.9;{it}] + label[4,0.9;]]..fe(timer_data.ingame_time)..[[] + + label[0,1.3;]]..S('Session Start Time')..[[] + label[2.5,1.3;{st}] + label[4,1.3;]]..fe(timer_data.session_start_time)..[[] + + label[0,1.7;]]..S('Session Duration')..[[] + label[2.5,1.7;{sd}] + label[4,1.7;]]..fe(timer_data.session_duration)..[[] + + container_end[] + + container[6,0.45] + button[0,0;3,1;apply;]]..S('Apply')..[[] + container_end[] + + ]]) +end diff --git a/system/get_timer_data.lua b/system/get_timer_data.lua new file mode 100644 index 0000000..ba15219 --- /dev/null +++ b/system/get_timer_data.lua @@ -0,0 +1,28 @@ +local m = mtimer +local S = m.translator + + +mtimer.get_timer_data = function (player_name) + local player_meta = minetest.get_player_by_name(player_name):get_meta() + local time_data = mtimer.get_times(player_name) + local ingame_time = time_data.ingame_time.formatted + local session_start_time = time_data.session_start_time.formatted + local session_duration = time_data.session_duration.formatted + + local values = { + format = player_meta:get_string(m.meta.timer_format.key), + real_world_date = time_data.real_time.formatted, + ingame_time = time_data.ingame_time.formatted, + session_start_time = time_data.session_start_time.formatted, + session_duration = time_data.session_duration.formatted + } + + values['formatted'] = values.format:gsub('{[0-9a-z]+}', { + ['{rd}'] = values.real_world_date, + ['{it}'] = values.ingame_time, + ['{st}'] = values.session_start_time, + ['{sd}'] = values.session_duration, + }) + + return values +end diff --git a/system/on_receive_fields.lua b/system/on_receive_fields.lua index 7673a37..8eedb7f 100644 --- a/system/on_receive_fields.lua +++ b/system/on_receive_fields.lua @@ -20,6 +20,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.session_duration_format then f.session_duration_format(name) end + if fields.timer_format then f.timer_format(name) end end @@ -119,6 +120,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end + -- 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 + if not fields.quit then mtimer.show_formspec.timer_format(name) end + end + + -- Back to menu from all formspecs if fields.main_menu then f.main_menu(name) end