mirror of
https://gitlab.com/4w/mtimer.git
synced 2025-01-24 13:31:28 +01:00
d65c1f11ba
Before all variables and “constants” (configuration variables) were stored in one single global table. Now the players get custom attributes set. This allows player-based displaying of the timer information. This will be done in subsequent commits. Tracking issue: https://github.com/dsohler/mtimer/issues/1
77 lines
3.2 KiB
Lua
77 lines
3.2 KiB
Lua
-- Update an idividual player’s timer display
|
||
--
|
||
-- When running the function the player data will be read from the player’s
|
||
-- `player` object and the output will be generated
|
||
--
|
||
-- @param player the player object to update
|
||
mtimer_update = function (player)
|
||
-- Formatting options
|
||
local runtime = player:get_attribute('mtimer:runtime')
|
||
local offset = mtimer_position(player:get_attribute('mtimer:offset'))
|
||
local ingame_time = player:get_attribute('mtimer:ingame_time')
|
||
local current_time = player:get_attribute('mtimer:current_time')
|
||
local font_color = player:get_attribute('mtimer:font_color')
|
||
local start = player:get_attribute('mtimer:start')
|
||
local show = player:get_attribute('mtimer:show')
|
||
local locale = player:get_attribute('mtimer:locale')
|
||
local position = mtimer_position(player:get_attribute('mtimer:position'))
|
||
local format = player:get_attribute('mtimer:format')
|
||
local alignment = mtimer_position(player:get_attribute('mtimer:alignment'))
|
||
|
||
-- Settings
|
||
local joined_timestamp = player:get_attribute('mtimer:joined_timestamp')
|
||
local hud_id = tonumber(player:get_attribute('mtimer:hud_id'))
|
||
|
||
-- Calculate ingame time
|
||
local ingame_timestamp = 24*60*minetest.get_timeofday()
|
||
local ingame_h = tostring((math.floor(ingame_timestamp/60) % 60))
|
||
local ingame_m = tostring((math.floor(ingame_timestamp) % 60))
|
||
|
||
-- Replace variables in output format if wanted by the user
|
||
local rendered_output_format = ''
|
||
if show == 'true' then
|
||
rendered_output_format = format:gsub('+.', {
|
||
['+s'] = os.date(start, joined_timestamp),
|
||
['+c'] = os.date(current_time),
|
||
['+r'] = os.date('!'..runtime:gsub('(+.)', {
|
||
['+h'] = '%H',
|
||
['+m'] = '%M',
|
||
['+s'] = '%S'
|
||
}), os.time() - joined_timestamp),
|
||
['+i'] = ingame_time:gsub('(+.)', {
|
||
['+h'] = string.rep('0', 2-#ingame_h)..ingame_h,
|
||
['+m'] = string.rep('0', 2-#ingame_m)..ingame_m
|
||
}),
|
||
['+n'] = "\n"
|
||
})
|
||
end
|
||
|
||
-- Render the output format as HUD element
|
||
player:hud_change(hud_id, 'text', rendered_output_format)
|
||
player:hud_change(hud_id, 'number', '0x'..font_color)
|
||
player:hud_change(hud_id, 'position', position)
|
||
player:hud_change(hud_id, 'alignment', alignment)
|
||
player:hud_change(hud_id, 'offset', offset)
|
||
end
|
||
|
||
|
||
|
||
-- Self-calling iteration function
|
||
--
|
||
-- The function iterates of all currentlz logged on players, checks if they
|
||
-- have the custom attribute `mtimer:hud_id` set (this is the last value that
|
||
-- will be set when a player joins so the player is set up completely if
|
||
-- `mtimer:hud_id` is present in the custom player attributes) and then runs
|
||
-- the function to update the timer display. After that the function calls
|
||
-- itself again, using the set interval.
|
||
--
|
||
-- @param interval The intervall used to self-calling again after execution
|
||
mtimer_iterate = function(interval)
|
||
for _,player in pairs(minetest.get_connected_players()) do
|
||
if player:get_attribute('mtimer:hud_id') ~= nil then
|
||
mtimer_update(player)
|
||
end
|
||
end
|
||
minetest.after(interval, mtimer_iterate, interval)
|
||
end
|