1 local modname = minetest.get_current_modname()
2 local storage = minetest.get_mod_storage()
6 local opening_hours_default = {
13 day1_start_minute = 0,
17 day2_start_minute = 0,
21 day3_start_minute = 0,
25 day4_start_minute = 0,
29 day5_start_minute = 0,
33 day6_start_minute = 0,
40 local warn_cooldown = 0
42 local function get_date()
46 local function get_date_formated()
47 return os.date("%d.%m.%y")
50 local function is_weekend()
51 local d = os.date("%w")
52 return d == "0" or d == "6"
55 local function upgrade_configuration(old)
58 day0_start_hour = old.weekend_start,
59 day0_start_minute = 0,
60 day0_end_hour = old.weekend_end,
62 day1_start_hour = old.weekday_start,
63 day1_start_minute = 0,
64 day1_end_hour = old.weekday_end,
66 day2_start_hour = old.weekday_start,
67 day2_start_minute = 0,
68 day2_end_hour = old.weekday_end,
70 day3_start_hour = old.weekday_start,
71 day3_start_minute = 0,
72 day3_end_hour = old.weekday_end,
74 day4_start_hour = old.weekday_start,
75 day4_start_minute = 0,
76 day4_end_hour = old.weekday_end,
78 day5_start_hour = old.weekday_start,
79 day5_start_minute = 0,
80 day5_end_hour = old.weekday_end,
82 day6_start_hour = old.weekend_start,
83 day6_start_minute = 0,
84 day6_end_hour = old.weekend_end,
86 warn_offset = old.warn_offset,
87 warn_interval = old.warn_interval,
90 new.exception_today = old.today
91 new.exception_start_hour = old.today_start
92 new.exception_start_minute = 0
93 new.exception_end_hour = old.today_end
94 new.exception_end_minute = 0
99 local function save_data()
100 storage:from_table({fields = opening_hours})
103 local function load_data()
104 opening_hours = storage:to_table().fields
105 if opening_hours.weekday_start then
106 opening_hours = upgrade_configuration(opening_hours)
107 elseif not opening_hours.version then
108 opening_hours = opening_hours_default
112 local function reset_execption()
113 opening_hours.exception_today = nil
116 local function opening_today()
117 local exception = opening_hours.exception_today
119 if exception and exception == get_date_formated() then
120 day_key = "exception"
121 elseif is_weekend() then
126 return opening_hours[day_key .. "_start_hour"], opening_hours[day_key .. "_end_hour"]
129 local function create_exception()
130 local today_start, today_end = opening_today()
131 opening_hours.exception_today = get_date_formated()
132 opening_hours.exception_start_hour = today_start
133 opening_hours.exception_start_minute = 0
134 opening_hours.exception_end_hour = today_end
135 opening_hours.exception_end_minute = 0
138 local function tick(dtime)
140 local exception = opening_hours.exception_today
141 if exception and exception ~= get_date_formated() then
144 local today_start, today_end = opening_today()
145 local diff = tonumber(today_end) - d.hour
147 local minutes_remaining = (60 - d.min)
148 if minutes_remaining <= tonumber(opening_hours.warn_offset) then
149 if warn_cooldown <= 0 then
150 minetest.chat_send_all(minetest.colorize("#FF4D00", "Der Server schießt in " .. minutes_remaining .. " Minuten."))
151 warn_cooldown = tonumber(opening_hours.warn_interval) * 60
153 warn_cooldown = warn_cooldown - dtime
156 elseif diff <= 0 then
157 for _, player in pairs(minetest.get_connected_players()) do
158 local name = player:get_player_name()
159 if not minetest.check_player_privs(name, {server = true}) then
160 minetest.kick_player(name, "Der Server schließt!")
166 local function on_join(name)
167 if minetest.check_player_privs(name, {server = true}) then return end
168 local today_start, today_end = opening_today()
170 local diff = tonumber(today_start) - d.hour
172 return "Besuch erfolgte außerhalb der Öffnungszeiten. Der Server hat in " .. math.ceil(diff) .. " Stunde(n) wieder geöffnet."
173 elseif tonumber(today_end) <= d.hour then
174 return "Besuch erfolgte außerhalb der Öffnungszeiten. Der Server hat bereits geschlossen und hat Morgen wieder geöffnet."
178 local function show_gui(name)
180 local fld_h = 0.82429501084599
181 local fld_sz = fld_w .. "," .. fld_h
182 local inline_off = 0.2427394885132
183 local lab_close_y = 6.3935847420893
184 local fld_close_y = lab_close_y + 0.2427394885132
185 local lab_day1_x = 0.1
186 local pre_colon_off = 0.4
187 local fld_day1_f_hour_x = 0.64
188 local minute_off = 0.04
190 local fld_day1_f_minute_x = fld_day1_f_hour_x + fld_w - minute_off
191 local lab_day1_f_colon_x = fld_day1_f_minute_x - pre_colon_off
192 local fld_day1_t_hour_x = lab_day1_f_colon_x + to_off
193 local fld_day1_t_minute_x = fld_day1_t_hour_x + fld_w - minute_off
194 local lab_day1_t_colon_x = fld_day1_t_minute_x - pre_colon_off
195 local below_off = 1.1530125704378
196 local lab_b_y = 0.28175119202427
197 local fld_b_y = lab_b_y + below_off
198 local lab_b_colon_y = fld_b_y - inline_off
199 local lab_w_y = 2.0156046814044
200 local fld_w_y = lab_w_y + below_off
201 local lab_w_colon_y = fld_w_y - inline_off
202 local lab_e_y = 3.7494581707846
203 local fld_e_y = lab_e_y + below_off
204 local lab_e_colon_y = fld_e_y - inline_off
205 local o = opening_hours
206 local formspec = "size[10.01,7.9267895878525]"
207 .. "label[-0.14,-0.23840485478977;Öffnungszeiten]"
208 .. "label[" .. lab_day1_x .. "," .. lab_b_y .. ";Mo.-Fr.]"
209 .. "field[" .. fld_day1_f_hour_x .. "," .. fld_b_y .. ";" .. fld_sz .. ";fld_day1_start_hour;von;" .. string.format("%02d", o.day1_start_hour) .. "]"
210 .. "label[" .. lab_day1_f_colon_x .. "," .. lab_b_colon_y .. ";:]"
211 .. "field[" .. fld_day1_f_minute_x .. "," .. fld_b_y .. ";" .. fld_sz .. ";fld_day1_start_minute;;" .. string.format("%02d", o.day1_start_minute) .. "]"
212 .. "field[" .. fld_day1_t_hour_x .. "," .. fld_b_y .. ";" .. fld_sz .. ";fld_day1_end_hour;bis;" .. string.format("%02d", o.day1_end_hour) .. "]"
213 .. "label[" .. lab_day1_t_colon_x .. "," .. lab_b_colon_y .. ";:]"
214 .. "field[" .. fld_day1_t_minute_x .. "," .. fld_b_y .. ";" .. fld_sz .. ";fld_day1_end_minute;;" .. string.format("%02d", o.day1_end_minute) .. "]"
215 .. "label[" .. lab_day1_x .. "," .. lab_w_y .. ";Sa.-So.]"
216 .. "field[" .. fld_day1_f_hour_x .. "," .. fld_w_y .. ";" .. fld_sz .. ";fld_day0_start_hour;von;" .. string.format("%02d", o.day0_start_hour) .. "]"
217 .. "label[" .. lab_day1_f_colon_x .. "," .. lab_w_colon_y .. ";:]"
218 .. "field[" .. fld_day1_f_minute_x .. "," .. fld_w_y .. ";" .. fld_sz .. ";fld_day0_start_minute;;" .. string.format("%02d", o.day0_start_minute) .. "]"
219 .. "field[" .. fld_day1_t_hour_x .. "," .. fld_w_y .. ";" .. fld_sz .. ";fld_day0_end_hour;bis;" .. string.format("%02d", o.day0_end_hour) .. "]"
220 .. "label[" .. lab_day1_t_colon_x .. "," .. lab_w_colon_y .. ";:]"
221 .. "field[" .. fld_day1_t_minute_x .. "," .. fld_w_y .. ";" .. fld_sz .. ";fld_day0_end_minute;;" .. string.format("%02d", o.day0_end_minute) .. "]"
222 .. "label[" .. lab_day1_x .. "," .. lab_e_y .. ";Heute]"
223 .. (o.exception_today
225 .. "field[" .. fld_day1_f_hour_x .. "," .. fld_e_y .. ";" .. fld_sz .. ";fld_exception_start_hour;von;" .. string.format("%02d", o.exception_start_hour) .. "]"
226 .. "label[" .. lab_day1_f_colon_x .. "," .. lab_e_colon_y .. ";:]"
227 .. "field[" .. fld_day1_f_minute_x .. "," .. fld_e_y .. ";" .. fld_sz .. ";fld_exception_start_minute;;" .. string.format("%02d", o.exception_start_minute) .. "]"
228 .. "field[" .. fld_day1_t_hour_x .. "," .. fld_e_y .. ";" .. fld_sz .. ";fld_exception_end_hour;bis;" .. string.format("%02d", o.exception_end_hour) .. "]"
229 .. "label[" .. lab_day1_t_colon_x .. "," .. lab_e_colon_y .. ";:]"
230 .. "field[" .. fld_day1_t_minute_x .. "," .. fld_e_y .. ";" .. fld_sz .. ";fld_exception_end_minute;;" .. string.format("%02d", o.exception_end_minute) .. "]"
231 or "image_button[0.34,4.5296922410056;4.205,0.7835;;add_exception;Ausnahmeregelung hinzufügen]"
233 .. "label[" .. lab_day1_x .. ",5.4833116601647;Einstellungen]"
234 .. "label[0.34," .. lab_close_y .. ";Spieler ]"
235 .. "field[1.6," .. fld_close_y .. ";" .. fld_sz .. ";fld_warn_offset;;" .. o.warn_offset .. "]"
236 .. "label[2.18," .. lab_close_y .. ";Minuten vor Ablauf der Zeit alle]"
237 .. "field[6.0," .. fld_close_y .. ";" .. fld_sz .. ";fld_warn_interval;;" .. o.warn_interval .. "]"
238 .. "label[6.6," .. lab_close_y .. ";Minuten warnen.]"
239 .. "image_button[5.14,7.6072821846554;2.605,0.7835;;save;Speichern]"
240 .. "image_button_exit[7.62,7.6072821846554;2.605,0.7835;;close;Schließen]"
241 minetest.show_formspec(name, "lmz_opening_hours:gui", formspec)
244 local function progress_gui_input(player, formname, fields)
245 local name = player:get_player_name()
246 if formname ~= "lmz_opening_hours:gui" or not minetest.check_player_privs(name, {server = true}) then return end
247 if fields.add_exception then
250 for k, v in pairs(fields) do
251 if k:sub(1, 4) == "fld_" then
252 local field = k:gsub("fld_", "")
253 local old = opening_hours[field]
254 opening_hours[field] = tonumber(v) or old
257 if not fields.quit and not fields.close then show_gui(name) end
262 minetest.register_globalstep(tick)
263 minetest.register_on_shutdown(save_data)
264 minetest.register_on_prejoinplayer(on_join)
265 minetest.register_chatcommand("opening_hours", {privs = {server = true}, description = "Die Öffnungszeiten konfigurieren", func = show_gui})
266 minetest.register_chatcommand("öffnungszeiten", {privs = {server = true}, description = "Die Öffnungszeiten konfigurieren", func = show_gui})
267 minetest.register_on_player_receive_fields(progress_gui_input)