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