mtimer/init.lua
2017-07-27 21:20:11 +02:00

113 lines
3.5 KiB
Lua

-- Convert a given x,y string into a table {x=given_x, y=given_y} and return
-- it to be used of the screen location parameters in the HUD definition
local _mtimer_location_value = function (o)
local x = o:gsub(',[^,]+$', '')
local y = o:gsub('^[^,]+,', '')
return {x=x, y=y}
end
local _mtimer_get_options = function ()
local path = minetest.get_modpath(minetest.get_current_modname())
local file = io.open(path..DIR_DELIM..'settingtypes.txt', 'rb')
local result = {}
for line in file:lines() do
if line:match("^([a-zA-Z])") then
local name = line:gsub(' .+', '')
local default_value = line:gsub('^[^ ]+ %b() %a+ ', '')
local set_value = minetest.setting_get(name)
result[name] = set_value or default_value
end
end
return result
end
local _options = _mtimer_get_options()
local interval = _options['mtimer_update_interval']
local placeholder = _options['mtimer_placeholder']
local font_color = _options['mtimer_font_color']
local format = {
locale = _options['mtimer_locale'],
start = _options['mtimer_session_start'],
runtime = _options['mtimer_session_runtime'],
current = _options['mtimer_current_time'],
ingame = _options['mtimer_ingame_time'],
output = _options['mtimer_output_format']
}
local location = {
position = _mtimer_location_value(_options['mtimer_position']),
alignment = _mtimer_location_value(_options['mtimer_alignment']),
offset = _mtimer_location_value(_options['mtimer_offset'])
}
local playerData = {}
minetest.register_on_joinplayer(function(player)
minetest.after(1, function(player)
local playerName = player:get_player_name()
local _hudID = player:hud_add({
hud_elem_type = 'text',
position = location.position,
alignment = location.alignment,
offset = location.offset,
text = placeholder,
number = '0x'..font_color
})
playerData[playerName] = {
startTime = os.time(),
hudID = _hudID
}
minetest.after(5, _mtimer_changeText, player)
end, player)
end)
minetest.register_on_leaveplayer(function(player)
local playerName = player:get_player_name()
playerData[playerName] = nil
end)
function _mtimer_changeText(player)
local playerName = player:get_player_name()
if playerName == '' then return end
-- Save the game-detected locale. Fun fact: changing the locale on runtime
-- from within a mod causes the UI to glitch out completely.
local currentLocale = os.setlocale(nil)
os.setlocale(format.locale)
local startTime = playerData[playerName].startTime
local hudID = playerData[playerName].hudID
local time = 24*60*minetest.get_timeofday()
local h = tostring((math.floor(time/60) % 60))
local m = tostring((math.floor(time) % 60))
local res = format.output:gsub('(+.)', {
['+s'] = os.date(format.start, startTime),
['+c'] = os.date(format.current),
['+r'] = os.date('!'..format.runtime:gsub('(+.)', {
['+h'] = '%H',
['+m'] = '%M',
['+s'] = '%S'
}), os.time() - startTime),
['+i'] = format.ingame:gsub('(+.)', {
['+h'] = string.rep('0', 2-#h)..h,
['+m'] = string.rep('0', 2-#m)..m
}),
['+n'] = "\n"
})
os.setlocale(currentLocale) -- Restore game-detected locale
player:hud_change(hudID, 'text', res)
minetest.after(interval, _mtimer_changeText, player)
end