implement ingame time format setting

Convert ingame time to millihours and calculate the timestamp from this.
It results in a default timestampo that is parsed by default date and
time functions.

Replacement variables:

{24h} = 24 hours time
{12h} = 12 hours time
{min} = minutes
{its} = ingame timestamp

For some reason I was not able to get the am/pm indicator (`%p`) so 12
hours format is only the time and no optional indicator.
This commit is contained in:
Dirk Sohler 2019-02-21 16:16:10 +01:00
parent e84fa467db
commit 89cd1f6212
No known key found for this signature in database
GPG Key ID: B9751241BD7D4E1A
7 changed files with 87 additions and 9 deletions

@ -10,6 +10,7 @@ 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}'},
}
}

@ -6,8 +6,8 @@ Set Visibility=Sichtbarkeit einstellen
Set Position=Position setzen
Set Color=Farbe einstellen
Timezone Offset=Zeitzonenunterschied
Ingame Time Representation=Spielzeit-Repräsentation
Real World Time Representation=Realzeit-Repräsentation
Ingame Time Format=Spielzeit-Format
Real-World Time Format=Realzeit-Format
Set Timer Text=Timertext einstellen
Open Main Menu=Hauptmenü öffnen

@ -21,8 +21,8 @@ minetest.register_chatcommand('mtimer', {
if action == 'po' then fs.set_position(name) end
if action == 'co' then fs.set_color(name) end
if action == 'tz' then fs.timezone_offset(name) end
if action == 'in' then fs.ingame_time(name) end
if action == 're' then fs.real_time(name) end
if action == 'in' then fs.ingame_time_format(name) end
if action == 're' then fs.real_time_format(name) end
if action == 'te' then fs.timer_text(name) end
if action == 'help' then
@ -31,8 +31,8 @@ minetest.register_chatcommand('mtimer', {
command('po')..S('Set Position'),
command('co')..S('Set Color'),
command('tz')..S('Timezone Offset'),
command('in')..S('Ingame Time Representation'),
command('re')..S('Real World Time Representation'),
command('in')..S('Ingame Time Format'),
command('re')..S('Real-World Time Format'),
command('te')..S('Set Timer Text'),
command(' ')..S('Open Main Menu')
}

@ -37,8 +37,8 @@ mtimer.show_formspec.main_menu = function (player_name)
button[0,1.5;4,1;set_position;]]..S('Set Position')..[[]
button[0,2.5;4,1;set_color;]]..S('Set Color')..[[]
button[0,3.5;4,1;timezone_offset;]]..S('Timezone Offset')..[[]
button[0,4.5;4,1;ingame_time;]]..S('Ingame Time Representation')..[[]
button[0,5.5;4,1;real_time;]]..S('Real World Time Representation')..[[]
button[0,4.5;4,1;ingame_time_format;]]..S('Ingame Time Format')..[[]
button[0,5.5;4,1;real_world_time_format;]]..S('Real-World Time Format')..[[]
button[0,6.5;4,1;timer_text;]]..S('Set Timer Text')..[[]
button_exit[0,7.5;4,1;exit;]]..S('Exit')..[[]
]])
@ -122,3 +122,42 @@ mtimer.show_formspec.timezone_offset = function (player_name)
label[0,1.45;]]..time_information..[[]
]])
end
mtimer.show_formspec.ingame_time_format = function (player_name)
local time_data = mtimer.get_times(player_name).ingame_time
minetest.show_formspec(player_name, 'mtimer:ingame_time_format',
build_frame(7, 4.85, S('Ingame Time Format'))..[[
field_close_on_enter[format;false]
field[0.25,0.75;7,1;format;;]]..time_data.format..[[]
container[0,1.425]
label[2.5,0;]]..S('Variable')..[[]
label[4.25,0;]]..S('Current value')..[[]
box[0,0.45;6.75,0.02;#ffffff]
label[0,0.5;]]..S('Hours (24h)')..[[]
label[2.5,0.5;{24h}]
label[4.25,0.5;]]..time_data.hours_24..[[]
label[0,0.9;]]..S('Hours (12h)')..[[]
label[2.5,0.9;{12h}]
label[4.25,0.9;]]..time_data.hours_12..[[]
label[0,1.3;]]..S('Minutes')..[[]
label[2.5,1.3;{min}]
label[4.25,1.3;]]..time_data.minutes..[[]
label[0,1.7;]]..S('Ingame Timestamp')..[[]
label[2.5,1.7;{its}]
label[4.25,1.7;]]..time_data.ingame_timestamp..[[]
box[0,2.2;6.75,0.02;#ffffff]
label[0,2.25;]]..S('Current Result')..[[]
label[2.5,2.25;]]..time_data.formatted..[[]
container_end[]
]])
end

@ -14,8 +14,34 @@ local get_real_time = function (player_name)
end
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 time_of_day = tostring((minetest.get_timeofday() * 24000) * 3.6)
local ingame_timestamp = tonumber(string.format("%.0f", time_of_day))
local values = {
hours_24 = os.date('!%H', ingame_timestamp),
hours_12 = os.date('!%I', ingame_timestamp),
minutes = os.date('!%M', ingame_timestamp),
ingame_timestamp = ingame_timestamp,
format = format
}
values['formatted'] = format:gsub('{[a-z0-9]+}', {
['{24h}'] = values.hours_24,
['{12h}'] = values.hours_12,
['{min}'] = values.minutes,
['{its}'] = values.ingame_timestamp
})
return values
end
mtimer.get_times = function (player_name)
return {
real_time = get_real_time(player_name)
real_time = get_real_time(player_name),
ingame_time = get_ingame_time(player_name)
}
end

@ -6,4 +6,5 @@ minetest.register_on_joinplayer(function(player)
local current = meta:get_string(def.key)
if current == '' then meta:set_string(def.key, def.default) end
end
meta:set_string('mtimer:session_start', os.time())
end)

@ -12,6 +12,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.set_position then f.set_position(name) end
if fields.set_color then f.set_color(name) end
if fields.timezone_offset then f.timezone_offset(name) end
if fields.ingame_time_format then f.ingame_time_format(name) end
end
@ -65,6 +66,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
-- Set ingame time format
if formname == 'mtimer:ingame_time_format' then
local attr = m.meta.ingame_time
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.ingame_time_format(name)end
end
-- Back to menu from all formspecs
if fields.main_menu then f.main_menu(name) end