]> git.lizzy.rs Git - lmz_opening_hours.git/blob - init.lua
66887c854eae216a29656369f847c951fde05ba3
[lmz_opening_hours.git] / init.lua
1 local modname = minetest.get_current_modname()
2 local storage = minetest.get_mod_storage()
3
4 opening_hours = {}
5
6 local opening_hours_default = {weekday_start = 14, weekday_end = 21, weekend_start = 8, weekend_end = 21, warn_offset = 15, warn_interval = 5}
7
8 local warn_cooldown = 0
9
10 local function get_date()
11         return os.date("*t")
12 end
13
14 local function get_date_formated()
15         return os.date("%d.%m.%y")
16 end
17
18 local function is_weekend()
19         local d = os.date("%w")
20         return d == "0" or d == "6"
21 end
22
23 local function opening_hours_index(t, k)
24         if k:sub(1, 6) == "today_" then 
25                 return t[k:gsub("today_", is_weekend() and "weekend_" or "weekday_")]
26         else
27                 return opening_hours_default[k]
28         end
29 end
30
31 local function save_data()
32         storage:from_table({fields = opening_hours})
33 end
34
35 local function load_data()
36         opening_hours = storage:to_table().fields
37         setmetatable(opening_hours, {__index = opening_hours_index})
38 end
39
40 local function reset_execption()
41         opening_hours.today = nil
42         opening_hours.today_start = nil
43         opening_hours.today_end = nil
44 end
45
46 local function create_exception()
47         opening_hours.today = get_date_formated()
48         opening_hours.today_start = opening_hours.today_start
49         opening_hours.today_end = opening_hours.today_end
50 end
51
52 local function tick(dtime)
53         local d = get_date()
54         if opening_hours.today and opening_hours.today ~= get_date_formated() then
55                 reset_execption()
56         end
57         local diff = tonumber(opening_hours.today_end) - d.hour
58         if diff == 1 then
59                 local minutes_remaining = (60 - d.min)
60                 if minutes_remaining <= tonumber(opening_hours.warn_offset) then
61                         if warn_cooldown <= 0 then
62                                 minetest.chat_send_all(minetest.colorize("#FF4D00", "Der Server schießt in " .. minutes_remaining .. " Minuten."))
63                                 warn_cooldown = tonumber(opening_hours.warn_interval) * 60
64                         else
65                                 warn_cooldown = warn_cooldown - dtime
66                         end
67                 end
68         elseif diff <= 0 then
69                 for _, player in pairs(minetest.get_connected_players()) do
70                         local name = player:get_player_name()
71                         if not minetest.check_player_privs(name, {server = true}) then
72                                 minetest.kick_player(name, "Der Server schließt!")
73                         end
74                 end
75         end
76 end
77
78 local function on_join(name)
79         if minetest.check_player_privs(name, {server = true}) then return end
80         local d = get_date()
81         local diff = tonumber(opening_hours.today_start) - d.hour
82         if diff > 0 then
83                 return "Besuch erfolgte außerhalb der Öffnungszeiten. Der Server hat in " .. math.ceil(diff) .. " Stunde(n) wieder geöffnet."
84         elseif tonumber(opening_hours.today_end) <= d.hour then
85                 return "Besuch erfolgte außerhalb der Öffnungszeiten. Der Server hat bereits geschlossen und hat Morgen wieder geöffnet."
86         end
87 end
88
89 local function show_gui(name)
90         local fld_w = 1.0
91         local fld_h = 0.82429501084599
92         local fld_sz = fld_w .. "," .. fld_h
93         local lab_close_y = 6.3935847420893
94         local fld_close_y = lab_close_y + 0.2427394885132
95         local lab_day1_x = 0.1
96         local fld_day1_f_x = 0.64
97         local fld_day1_t_x = 1.6
98         local time_off = 1.1530125704378
99         local lab_b_y = 0.28175119202427
100         local fld_b_y = lab_b_y + time_off
101         local lab_w_y = 2.0156046814044
102         local fld_w_y = lab_w_y + time_off
103         local lab_e_y = 3.7494581707846
104         local fld_e_y = lab_e_y + time_off
105         local o = opening_hours
106         local formspec = "size[10.01,7.9267895878525]"
107         .. "label[-0.14,-0.23840485478977;Öffnungszeiten]"
108         .. "label[" .. lab_day1_x .. "," .. lab_b_y .. ";Mo.-Fr.]"
109         .. "field[" .. fld_day1_f_x .. "," .. fld_b_y .. ";" .. fld_sz .. ";fld_weekday_start;von;" .. o.weekday_start .. "]"
110         .. "field[" .. fld_day1_t_x .. "," .. fld_b_y .. ";" .. fld_sz .. ";fld_weekday_end;bis;" .. o.weekday_end .. "]"
111         .. "label[" .. lab_day1_x .. "," .. lab_w_y .. ";Sa.-So.]"
112         .. "field[" .. fld_day1_f_x .. "," .. fld_w_y .. ";" .. fld_sz .. ";fld_weekend_start;von;" .. o.weekend_start .. "]"
113         .. "field[" .. fld_day1_t_x .. "," .. fld_w_y .. ";" .. fld_sz .. ";fld_weekend_end;bis;" .. o.weekend_end .. "]"
114         .. "label[" .. lab_day1_x .. "," .. lab_e_y .. ";Heute]"
115         .. (o.today
116                         and ""
117                                 .. "field[" .. fld_day1_f_x .. "," .. fld_e_y .. ";" .. fld_sz .. ";fld_today_start;von;" .. o.today_start .. "]"
118                                 .. "field[" .. fld_day1_t_x .. "," .. fld_e_y .. ";" .. fld_sz .. ";fld_today_end;bis;" .. o.today_end .. "]"
119                         or "image_button[0.34,4.5296922410056;4.205,0.7835;;add_exception;Ausnahmeregelung hinzufügen]"
120                 )
121         .. "label[" .. lab_day1_x .. ",5.4833116601647;Einstellungen]"
122         .. "label[0.34," .. lab_close_y .. ";Spieler ]"
123         .. "field[" .. fld_day1_t_x .. "," .. fld_close_y .. ";" .. fld_sz .. ";fld_warn_offset;;" .. o.warn_offset .. "]"
124         .. "label[2.18," .. lab_close_y .. ";Minuten vor Ablauf der Zeit alle]"
125         .. "field[6.0," .. fld_close_y .. ";" .. fld_sz .. ";fld_warn_interval;;" .. o.warn_interval .. "]"
126         .. "label[6.6," .. lab_close_y .. ";Minuten warnen.]"
127         .. "image_button[5.14,7.6072821846554;2.605,0.7835;;save;Speichern]"
128         .. "image_button_exit[7.62,7.6072821846554;2.605,0.7835;;close;Schließen]"
129         minetest.show_formspec(name, "lmz_opening_hours:gui", formspec)
130 end
131
132 local function progress_gui_input(player, formname, fields)
133         local name = player:get_player_name()
134         if formname ~= "lmz_opening_hours:gui" or not minetest.check_player_privs(name, {server = true}) then return end
135         if fields.add_exception then
136                 create_exception()
137         end
138         for k, v in pairs(fields) do
139                 if k:sub(1, 4) == "fld_" and tonumber(v) then
140                         opening_hours[k:gsub("fld_", "")] = v
141                 end
142         end
143         if not fields.quit and not fields.close then show_gui(name) end
144 end
145
146 load_data()
147
148 minetest.register_globalstep(tick)
149 minetest.register_on_shutdown(save_data)
150 minetest.register_on_prejoinplayer(on_join)
151 minetest.register_chatcommand("opening_hours", {privs = {server = true}, description = "Die Öffnungszeiten konfigurieren", func = show_gui})
152 minetest.register_chatcommand("öffnungszeiten", {privs = {server = true}, description = "Die Öffnungszeiten konfigurieren", func = show_gui})
153 minetest.register_on_player_receive_fields(progress_gui_input)
154
155