mtimer/system/load_configuration.lua
Dirk Sohler a28f97b135
build custom timer formspec with options parsing
No functionality yet and the fields are just dumped. Translation to
German is done and the meta data is loaded properly.

Addresses https://gitlab.com/4w/mtimer/-/issues/10
2021-02-11 21:49:26 +01:00

101 lines
3.5 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 keys
-- 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 worlds directory
-- 3. Provided default value when calling the function
--
-- @param key_name The unprefixed name of the key to get
-- @param default_value What to return when the configuration option is missing
-- @param changeable If the option is changeable via the config files
-- @return string Either the configuration options value or an empty string
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}')
set('custom_timer_format', 'd: {days}, h: {hours}, m: {minutes}, s: {seconds}')
-- Custom timer settings
--
-- `timer_mode` can be one of the following:
--
-- 'countdown': Counting backwards from the calculated starting point to the
-- `start_timestamp` value. The starting point is calculated
-- using the input values and `start_timestamp`.
--
-- 'timer': Counting up from the `start_timestamp` value to the calculated
-- target. The target is calculated by the `start_timestamp` and the
-- given `input_values`.
--
-- 'continuous': The timer shows the difference between the current timestamp
-- and the stored `start_timestamp`. Here the `target_message`
-- is ignored and will never be shown.
set('custom_timer_settings', minetest.serialize({
input_values = { days = 0, hours = 0, minutes = 0, seconds = 0 },
start_timestamp = 0,
target_message = S('The timer has finished'),
timer_mode = 'countdown',
running = false
}), false)
-- Timer display format (the HUD elements 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}'),
S('Custom Timer: @1', '{ct}')
}, '\n'), false)