implement custom timer setting logic

Addresses https://gitlab.com/4w/mtimer/-/issues/10
This commit is contained in:
Dirk Sohler
2021-02-12 12:02:23 +01:00
parent a28f97b135
commit 8b1664111f
5 changed files with 94 additions and 45 deletions

View File

@ -71,11 +71,15 @@ Session Start Time=Sitzungsstart-Zeit
Host Time=Hostzeit
# Custom Timer Things
Timer Mode=Timermodus
Running=Timer läuft
Stopped=Angehalten
Finished=Abgeschlossen
Countdown=Herunterzählen
Timer Mode=Heraufzählen
Continuous Run=Dauerlauf
The timer is stopped=Der Timer ist angehalten
The timer has finished=Der Timer hat sein Ziel erreicht
Used Value=Genutzter Wert
# Default Timer Format
Current Date: @1=Aktuelles Datum: @1

View File

@ -330,12 +330,11 @@ end
mtimer.dialog.custom_timer = function (player_name)
local player_meta = minetest.get_player_by_name(player_name):get_meta()
local ctv = minetest.deserialize(player_meta:get_string(m.meta.custom_timer_settings.key))
local format = player_meta:get_string(m.meta.custom_timer_format.key)
local days = ctv.input_values.days or 0
local hours = ctv.input_values.hours or 0
local minutes = ctv.input_values.minutes or 0
local seconds = ctv.input_values.seconds or 0
local days = ctv.values.days or 0
local hours = ctv.values.hours or 0
local minutes = ctv.values.minutes or 0
local seconds = ctv.values.seconds or 0
local a_countdown = ctv.timer_mode == 'countdown' and 'x' or ''
local a_timer = ctv.timer_mode == 'timer' and 'x' or ''
@ -344,53 +343,58 @@ mtimer.dialog.custom_timer = function (player_name)
mtimer.show_formspec('mtimer:custom_timer', {
title = S('Custom Timer'),
show_to = player_name,
height = 4.25,
height = 5.3,
width = 9,
formspec = {
'field_close_on_enter[days_value;false]',
'field_close_on_enter[hours_value;false]',
'field_close_on_enter[minutes_value;false]',
'field_close_on_enter[seconds_value;false]',
'field_close_on_enter[v_format_running;false]',
'field_close_on_enter[v_format_stopped;false]',
'field_close_on_enter[v_format_finished;false]',
'field_close_on_enter[v_days;false]',
'field_close_on_enter[v_hours;false]',
'field_close_on_enter[v_minutes;false]',
'field_close_on_enter[v_seconds;false]',
'container[0,0]',
' field[0,0;+linewidth,0.5;format;;'..fe(format)..']',
' field[0,0.6;+linewidth,0.5;target_message;;'..fe(ctv.target_message)..']',
' label[0,0.25;'..S('Running')..'] field[2.25,0;6.5,0.5;v_format_running;;'..fe(ctv.format.running)..']',
' label[0,0.85;'..S('Stopped')..'] field[2.25,0.6;6.5,0.5;v_format_stopped;;'..fe(ctv.format.stopped)..']',
' label[0,1.45;'..S('Finished')..'] field[2.25,1.2;6.5,0.5;v_format_finished;;'..fe(ctv.format.finished)..']',
' box[0,2;+linewidth,0.04;#ffffff]',
'container_end[]',
'container[3.75,1.5]',
'container[3.75,2.4]',
' label[0,0;'..S('Information')..']',
' label[1.75,0;'..S('Variable')..']',
' label[3.25,0;'..S('Value')..']',
' label[3.25,0;'..S('Used Value')..']',
' box[0,0.25;5,0.02;#ffffff]',
' label[0,0.5;'..S('Days')..'] label[1.75,0.5;{days}] label[3.25,0.5;'..days..']',
' label[0,0.9;'..S('Hours')..'] label[1.75,0.9;{hours}] label[3.25,0.9;'..hours..']',
' label[0,1.3;'..S('Minutes')..'] label[1.75,1.3;{minutes}] label[3.25,1.3;'..minutes..']',
' label[0,1.7;'..S('Seconds')..'] label[1.75,1.7;{seconds}] label[3.25,1.7;'..seconds..']',
'container_end[]',
'container[0,1.35]',
'container[0,2.3]',
' container[0,0]',
' button[0,0;0.75,0.25;days_add_1;'..S('+')..']',
' field[0,0.25;0.755,0.5;days_value;;'..days..']',
' button[0,0.75;0.75,0.25;days_substract_1;'..S('-')..']',
' button[0,0;0.75,0.25;c_days_p;+]',
' field[0,0.25;0.755,0.5;v_days;;'..days..']',
' button[0,0.75;0.75,0.25;c_days_m;-]',
' container_end[]',
' container[0.9,0]',
' button[0,0;0.75,0.25;hours_add_1;'..S('+')..']',
' field[0,0.25;0.755,0.5;hours_value;;'..hours..']',
' button[0,0.75;0.75,0.25;hours_substract_1;'..S('-')..']',
' button[0,0;0.75,0.25;c_hours_p;+]',
' field[0,0.25;0.755,0.5;v_hours;;'..hours..']',
' button[0,0.75;0.75,0.25;c_hours_m;-]',
' container_end[]',
' container[1.8,0]',
' button[0,0;0.75,0.25;minutes_add_1;'..S('+')..']',
' field[0,0.25;0.755,0.5;minutes_value;;'..minutes..']',
' button[0,0.75;0.75,0.25;minutes_substract_1;'..S('-')..']',
' button[0,0;0.75,0.25;c_minutes_p;+]',
' field[0,0.25;0.755,0.5;v_minutes;;'..minutes..']',
' button[0,0.75;0.75,0.25;c_minutes_m;-]',
' container_end[]',
' container[2.7,0]',
' button[0,0;0.75,0.25;seconds_add_1;'..S('+')..']',
' field[0,0.25;0.755,0.5;seconds_value;;'..seconds..']',
' button[0,0.75;0.75,0.25;seconds_substract_1;'..S('-')..']',
' button[0,0;0.75,0.25;c_seconds_p;+]',
' field[0,0.25;0.755,0.5;v_seconds;;'..seconds..']',
' button[0,0.75;0.75,0.25;c_seconds_m;-]',
' container_end[]',
'container_end[]',
'container[0,2.625]',
' button[0,0;0.25,0.25;type_countdown;'..a_countdown..'] label[0.35,0.125;'..S('Countdown')..']',
' button[0,0.4;0.25,0.25;type_timer;'..a_timer..'] label[0.35,0.525;'..S('Timer Mode')..']',
' button[0,0.8;0.25,0.25;type_continuous;'..a_continuous..'] label[0.35,0.925;'..S('Continuous Run')..']',
'container[0,3.6]',
' button[0,0;0.25,0.25;mode_countdown;'..a_countdown..'] label[0.35,0.125;'..S('Countdown')..']',
' button[0,0.4;0.25,0.25;mode_timer;'..a_timer..'] label[0.35,0.525;'..S('Timer Mode')..']',
' button[0,0.8;0.25,0.25;mode_continuous;'..a_continuous..'] label[0.35,0.925;'..S('Continuous Run')..']',
'container_end[]',
}
})

View File

@ -248,17 +248,10 @@ end
local get_custom_timer = 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.custom_timer_format.key)
local ctv = ds(player_meta:get_string(m.meta.custom_timer_settings.key))
local values = {}
values['formatted'] = format:gsub('{[a-z0-9]+}', {
['{days}'] = 0,
['{hours}'] = 0,
['{minutes}'] = 0,
['{seconds}'] = 0
})
values['formatted'] = 'PLACEHOLDER'
return values
end

View File

@ -63,7 +63,6 @@ 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
@ -82,9 +81,13 @@ set('custom_timer_format', 'd: {days}, h: {hours}, m: {minutes}, s: {seconds}')
-- 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 },
values = { days = 0, hours = 0, minutes = 0, seconds = 0 },
start_timestamp = 0,
target_message = S('The timer has finished'),
format = {
running = 'd: {days}, h: {hours}, m: {minutes}, s: {seconds}',
stopped = S('The timer is stopped'),
finished = S('The timer has finished')
},
timer_mode = 'countdown',
running = false
}), false)

View File

@ -182,9 +182,54 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
-- TODO: Implement configure custom timer
-- Custom timer setting and configuration
if formname == 'mtimer:custom_timer' then
print(dump(fields))
local attr = m.meta.custom_timer_settings
local ctv = minetest.deserialize(meta:get_string(attr.key))
local entered = fields.key_enter_field or ''
-- Set mode
if fields.mode_countdown then ctv.timer_mode = 'countdown' end
if fields.mode_timer then ctv.timer_mode = 'timer' end
if fields.mode_continuous then ctv.timer_mode = 'continuous' end
-- Validate direct input
local days = tonumber(fields.v_days) or 0
local hours = tonumber(fields.v_hours) or 0
local minutes = tonumber(fields.v_minutes) or 0
local seconds = tonumber(fields.v_seconds) or 0
-- Set values from plus/minus input
if fields.c_days_p then days = days + 1 end
if fields.c_hours_p then hours = hours + 1 end
if fields.c_minutes_p then minutes = minutes + 1 end
if fields.c_seconds_p then seconds = seconds + 1 end
if fields.c_days_m then days = days - 1 end
if fields.c_hours_m then hours = hours - 1 end
if fields.c_minutes_m then minutes = minutes - 1 end
if fields.c_seconds_m then seconds = seconds - 1 end
-- Validate values
if seconds >= 60 then seconds = 0 minutes = minutes + 1 end
if minutes >= 60 then minutes = 0 hours = hours + 1 end
if hours >= 24 then hours = 0 days = days + 1 end
if seconds < 0 then seconds = 59 minutes = minutes - 1 end
if minutes < 0 then minutes = 59 hours = hours - 1 end
if hours < 0 then hours = 23 days = days - 1 end
if days < 0 then days = 0 end
-- set formats
ctv.format = {
running = fields.v_format_running,
stopped = fields.v_format_stopped,
finished = fields.v_format_finished
}
-- (re)set (and quit)
ctv.values = {days=days,hours=hours,minutes=minutes,seconds=seconds}
meta:set_string(attr.key, minetest.serialize(table.copy(ctv)))
if fields.default then meta:set_string(attr.key, attr.default) end
if not fields.quit then d.custom_timer(name) end
end