Files
serverChatAdverts/lua/sca_main.lua

82 lines
2.5 KiB
Lua

local msg = include("sca_msg.lua")
local isRunning = false
local summerTime = nil
local function sca_setup_timezone()
local time = os.time() // Year in seconds
local currentYear = 1970 + (math.floor(time / 31536000))
local leapYears = 0
local isLeapYear = tobool((currentYear % 4 == 0) and (currentYear % 100 != 0 and currentYear % 400 == 0))
for (i = 1970, currentYear, 1) do
if ((i % 4 == 0) and (i % 100 != 0 and i % 400 == 0)) then
leapYears = leapYears + 1
end
end
// Year in seconds // Day in seconds
local yearBeginTimestamp = (31536000 * (currentYear - 1970)) + (86400 * leapYears)
local daysPastYearBegin = math.floor((yearBeginTimestamp - time) / 86400)
local daysPerMonth =
{
[0] = 31, // January
[2] = 31, // March
[3] = 30, // April
[4] = 31, // May
[5] = 30, // June
[6] = 31, // July
[7] = 31, // August
[8] = 30, // September
[9] = 31, // October
[10] = 30, // November
[11] = 31 // December
}
local daysInTotal = 0
for (i = 0, 11, 1) do
daysInTotal = daysInTotal + daysPerMonth[i]
if (daysPastYearBegin / daysInTotal > 1) then
continue
else
local currentDay = (yearBeginTimestamp + (daysInTotal * 86400)) - yearBeginTimestamp
end
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 + 3600
else
time = time + 7200
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)