diff --git a/README.md b/README.md index 11c78be..fb3c8b0 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,34 @@ Where needed, apply buttons are placed. So instead of having to leave the formsp In the timer format configuration formspec shown above a text area is used where pressing enter goes to a new line so a button for applying the changes is shown. +## Default configuration (interesting for server admins) + +The default configuration is loaded on server start and applied when a new player joins or an already existing player sets any (or all) of the mTimer values to the default value via the main menu or one of the specific dialog formspecs. + +The mod tries to load the configuration from least to most mod specific locations: + +1. Default `minetest.conf` that is used to load the server +2. From `_mtimer.conf` in the served world’s directory +3. Built-in default setting defined by the mod’s author + +There is intentionally no `settingtypes.txt` because the mod is meant to be configured by the individual players. The following options can be set in either `1.` or `2.` without changing the mod’s code. After that a server restart is necessary. + +```ini +mtimer_color = #ffffff +mtimer_hud_element_size = 1 +mtimer_position = bl +mtimer_timezone_offset = 0 +mtimer_visible = true + +mtimer_host_time_format = {24h}:{min} ({isodate}) +mtimer_ingame_time_format = {24h}:{min} +mtimer_real_time_format = {24h}:{min} ({isodate}) +mtimer_session_duration_format = {hours}:{minutes} +mtimer_session_start_time_format = {isodate}T{isotime} +``` + +The timer format and the HUD element’s offset can’t be set because those values need special formatting that can’t be done using Minetest’s settings API right now. Namely a serialized table for the offset and a string with newlines + ## Known Issues ### Formspecs Look Weird @@ -58,4 +86,4 @@ mTimer was tested with resolutions from 1024×768 up to 2560×1440 with 20px fon Simple: The mod was not translated to the language you’re using. Feel free to translate it and file a pull request. I’d be happy to merge it. -Currently German (`language = de` in `minetest.conf`) and Italian (`language = it`) are supported as translation language and English being the default. +Currently German (`language = de` in `minetest.conf`) and Italian (`language = it`) are supported as translation language and English being the default and Italian being slightly out of date. diff --git a/init.lua b/init.lua index ccb08f5..3fc047f 100644 --- a/init.lua +++ b/init.lua @@ -1,64 +1,22 @@ local modpath = minetest.get_modpath('mtimer')..DIR_DELIM local syspath = modpath..'system'..DIR_DELIM -local S = minetest.get_translator('mtimer') -- Set initial global `mtimer` table -- --- The sub-table `dialog` is filled programmatically and is used for the --- functions that show the formspecs to the player. --- --- In sub-table `meta` the meta keys and their default values are defined. Those --- are iterated over when a player joins. The names are searched for whenever --- somewhere in the code a meta information is to be loaded. --- -- @see ./system/formspec/formspec_creation.lua +-- @see ./system/load_configuration.lua mtimer = { - translator = S, + translator = minetest.get_translator('mtimer'), dialog = {}, - meta = { - visible = { key = 'mtimer:visible', default = 'true' }, - position = { key = 'mtimer:position', default = 'bl' }, - color = { key = 'mtimer:color', default = '#ffffff' }, - hud_element_size = { key = 'mtimer:hud_element_size', default = '1' }, - timezone_offset = { key = 'mtimer:timezone_offset', default = '0' }, - hud_element_offset = { - key = 'mtimer:hud_element_offset', - default = minetest.serialize({ x = 0, y = 0 }) - }, - ingame_time = { - key = 'mtimer:ingame_time_format', - default = '{24h}:{min}' - }, - real_time = { - key = 'mtimer:real_time_format', - default = '{24h}:{min} ({isodate})' - }, - host_time = { - key = 'mtimer:host_time_format', - default = '{24h}:{min} ({isodate})' - }, - session_start_time = { - key = 'mtimer:session_start_time_format', - default = '{isodate}T{isotime}' - }, - session_duration = { - key = 'mtimer:session_duration_format', - default = '{hours}:{minutes}' - }, - timer_format = { - key = 'mtimer:timer_format', - default = table.concat({ - S('Current Date: @1', '{rd}'), - S('Ingame Time: @1', '{it}'), - S('Session Start: @1', '{st}'), - S('Session Duration: @1', '{sd}') - }, '\n') - } - } + meta = {} } +-- Load configuration +dofile(syspath..'load_configuration.lua') + + -- Load formspec functionality dofile(syspath..'formspec'..DIR_DELIM..'formspec_helpers.lua') dofile(syspath..'formspec'..DIR_DELIM..'real_time_universal.lua') diff --git a/system/get_times.lua b/system/get_times.lua index a89ea0c..125bfb5 100644 --- a/system/get_times.lua +++ b/system/get_times.lua @@ -82,15 +82,15 @@ local get_real_time_universal = function (player_name, time_type) if time_type == 'real' then server_timestamp = os.time() local_timestamp = server_timestamp + ((timezone_offset*60)*60) - format = player_meta:get_string(m_meta.real_time.key) + format = player_meta:get_string(m_meta.real_time_format.key) elseif time_type == 'session' then server_timestamp = player_meta:get('mtimer:session_start') local_timestamp = server_timestamp + ((timezone_offset*60)*60) - format = player_meta:get_string(m_meta.session_start_time.key) + format = player_meta:get_string(m_meta.session_start_time_format.key) elseif time_type == 'host' then server_timestamp = os.time() local_timestamp = server_timestamp - format = player_meta:get_string(m_meta.host_time.key) + format = player_meta:get_string(m_meta.host_time_format.key) force_utc = '' end @@ -173,7 +173,7 @@ end -- @return table The table as described local get_ingame_time = function (player_name) local player = minetest.get_player_by_name(player_name) - local format = player:get_meta():get_string(m.meta.ingame_time.key) + local format = player:get_meta():get_string(m.meta.ingame_time_format.key) local time_of_day = tostring((minetest.get_timeofday() * 24000) * 3.6) local ingame_timestamp = tonumber(string.format('%.0f', time_of_day)) @@ -218,7 +218,7 @@ end local get_session_duration = function (player_name) local player = minetest.get_player_by_name(player_name) local player_meta = player:get_meta() - local format = player_meta:get_string(m.meta.session_duration.key) + local format = player_meta:get_string(m.meta.session_duration_format.key) local start_timestamp = player_meta:get_string('mtimer:session_start') local current_timestamp = os.time() local difference = current_timestamp - start_timestamp diff --git a/system/load_configuration.lua b/system/load_configuration.lua new file mode 100644 index 0000000..ef14fe8 --- /dev/null +++ b/system/load_configuration.lua @@ -0,0 +1,67 @@ +local S = mtimer.translator +local worldpath = minetest.get_worldpath()..DIR_DELIM +local worldconfig = Settings(worldpath..DIR_DELIM..'_mtimer.conf') + + +-- Set an option in mtimer.meta configuration table. +-- +-- The function takes an unprefixed key name and tries to get this key’s +-- configuration option and sets the table entry with that option and the +-- meta key name for that key. Because the meta settings system only allows +-- to write strings all values are converted to strings. +-- +-- set('my_cool_key', 1337) +-- +-- This setting creates the following table entry: +-- +-- mtimer.meta.my_cool_key = { +-- key = 'mtimer:my_cool_key', +-- default = '1337' +-- } +-- +-- The default value is searched in the following order When the setting is +-- not found in any of the locations an empty string is used +-- +-- 1. Standard `minetest.conf` file that is used for the server +-- 2. `_mtimer.conf` in the loaded world’s directory +-- 3. Provided default value when calling the function +local set = function (key_name, default_value, changeable) + local meta_key = 'mtimer:'..key_name + local config_option = 'mtimer_'..key_name + local value = default_value + + if changeable ~= false then + local global_setting = minetest.settings:get(config_option) + local world_setting = worldconfig:get(config_option) + value = world_setting or global_setting or default_value or '' + end + + mtimer.meta[key_name] = { + key = meta_key, + default = tostring(value) + } +end + + +-- Display settings +set('color', '#ffffff') +set('hud_element_offset', minetest.serialize({ x = 0, y = 0 }), false) +set('hud_element_size', 1) +set('position', 'bl') +set('timezone_offset', 0) +set('visible', true) + +-- Formatting settings +set('host_time_format', '{24h}:{min} ({isodate})') +set('ingame_time_format', '{24h}:{min}') +set('real_time_format', '{24h}:{min} ({isodate})') +set('session_duration_format', '{hours}:{minutes}') +set('session_start_time_format', '{isodate}T{isotime}') + +-- Timer display format (the HUD element’s content) +set('timer_format', table.concat({ + S('Current Date: @1', '{rd}'), + S('Ingame Time: @1', '{it}'), + S('Session Start: @1', '{st}'), + S('Session Duration: @1', '{sd}') +}, '\n'), false) diff --git a/system/on_receive_fields.lua b/system/on_receive_fields.lua index aa853b5..a1f0b24 100644 --- a/system/on_receive_fields.lua +++ b/system/on_receive_fields.lua @@ -84,7 +84,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- Set ingame time format if formname == 'mtimer:ingame_time_format' then - local attr = m.meta.ingame_time + local attr = m.meta.ingame_time_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 @@ -94,7 +94,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- Set real-time format if formname == 'mtimer:real_world_time_format' then - local attr = m.meta.real_time + local attr = m.meta.real_time_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 @@ -104,7 +104,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- Set host time format if formname == 'mtimer:host_time_format' then - local attr = m.meta.host_time + local attr = m.meta.host_time_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 @@ -114,7 +114,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- Set session start time format if formname == 'mtimer:session_start_time_format' then - local attr = m.meta.session_start_time + local attr = m.meta.session_start_time_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 @@ -124,7 +124,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- Set session duration format if formname == 'mtimer:session_duration_format' then - local attr = m.meta.session_duration + local attr = m.meta.session_duration_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