Files
serverChatAdverts/lua/sca_main.lua
2024-03-28 18:30:31 +01:00

110 lines
3.4 KiB
Lua

local msg = include("sca_msg.lua")
local isRunning = false
local summerTime = nil
local function sca_setup_timezone()
local time = os.time()
local daysPastUnixYear = math.floor(time / 86400) // Unix began on 01.01.1970
local year = 1970
local month = 0
local day = 0
local weekDay = daysPastUnixYear % 7 // 0 = thursday, 1 = friday, 2 = saturday, 3 = sunday, 4 = monday, 5 = tuesday, 6 = wensday. 01.01.1970 was on thursday
local daysPerMonth = {
[0] = 31, // January
[1] = 28, // February
[2] = 31, // March
[3] = 30, // April
[4] = 31, // May
[5] = 30, // June
[6] = 31, // July
[7] = 31, // August
[8] = 30, // September
[9] = 31, // Ocotober
[10] = 30, // Novmeber
[11] = 31 // December
}
while (daysPastUnixYear > 364) do
if (year % 4 == 0 && (year % 400 == 0 && year % 100 != 0)) then
daysPastUnixYear = daysPastUnixYear - 366
else
daysPastUnixYear = daysPastUnixYear - 365
end
year = year + 1
end
for i = 0, 11, 1 do
if (daysPastUnixYear - daysPerMonth[i] >= daysPerMonth[i]) then
if (i == 2 && (year % 4 == 0 && (year % 400 == 0 && year % 100 != 0))) then
daysPastUnixYear = daysPastUnixYear - (daysPerMonth[i] + 1)
else
daysPastUnixYear = daysPastUnixYear - daysPerMonth[i]
end
month = month + 1
else
day = daysPastUnixYear - 1
break
end
end
local hourInSeconds = time % 86400
if (month > 2 && month < 9) then
summerTime = true
elseif (month == 2) then
// If its less than 6 days until end of the month (aka last Weekday in the month) and its sunday and its 3 am or later....
if ((daysPerMonth[month] - day <= 6) && weekDay == 3 && hoursInSeconds >= 7200) then
summerTime = true
else
summerTime = false
end
elseif (month == 9) then
// The same action again but for winter time
if ((daysPerMonth[month] - day <= 6) && weekDay == 3 && hoursInSeconds >= 10800) then
summerTime = false
else
summerTime = true
end
else
summerTime = false
end
end
local function sca_msg_to_player(time)
if (msg[time] != nil) then
for k, v in ipairs(player.GetHumans()) do
v:SendLua('chat.AddText(' .. msg[time] .. ')')
end
end
end
local function sca_timer_loop()
// If timezone has been setup then...
if summerTime != nil then
local time = os.time()
// Germany timezone in summer is UTC+1, in winter is UTC+2
if summerTime then
time = time + 7200
else
time = time + 3600
end
local timeModulo = time % 60
if timeModulo == 0 then
if !isRunning then
// 86400 seconds are 24 hours. We will get time between 00:00 - 23:59 (current time) in seconds from that modulo operation. 0 = 00:00, 86340 = 23:59
sca_msg_to_player(time % 86400)
isRunning = true
end
else
if isRunning then
isRunning = false
end
end
end
end
hook.Add("Tick", "sca_timer_loop", sca_timer_loop)
hook.Add("Initialize", "sca_setup_timezone", sca_setup_timezone)