friendship ended with timers, now tick updates is my best friend

This commit is contained in:
2024-03-24 23:12:11 +01:00
parent 80e5906e5d
commit 5d433a0949
3 changed files with 76 additions and 33 deletions

View File

@@ -1,3 +0,0 @@
local conf = {}
return conf

View File

@@ -1,43 +1,82 @@
local conf = include("sca_config.lua")
local timeSynced = false
local msg = include("sca_msg.lua")
local isRunning = false
local summerTime = nil
local function sca_loop()
local intervalOffset = os.time() % 60
print("intervalOffset: ", intervalOffset)
-- If the function isn't called at HH:MM:00 then readjust the interval
if intervalOffset > 0 then
timeSynced = false
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))
print("[BuildBox] serverChatAdverts: Warning! Timer has not been called at HH:MM:00!! Readjusting interval...")
timer.Adjust("sca_chatPrintService", 60 - intervalOffset)
-- Otherwise, if timeSynced is false (interval was shorter than 60 seconds) then readjust interval to 60 seconds
else
if !timeSynced then
timeSynced = true
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)
timer.Adjust("sca_chatPrintService", 60)
-- else check config and print text to players if there is any
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_setup()
print("[BuildBox] serverChatAdverts: Started!")
-- Calculate an interval that will call a function on HH:MM:00
print("setup time: ", os.time())
print(os.time() % 60)
print(60 - (os.time() % 60))
local interval = 60 - (os.time() % 60)
print("setup interval: ", interval)
timer.Create("sca_chatPrintService", interval, 0, sca_loop)
print("[BuildBox] serverChatAdverts: Initialization completed!")
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("Initialize", "sca_setup", sca_setup)
hook.Add("Tick", "sca_timer_loop", sca_timer_loop)
hook.Add("Initialize", "sca_setup_timezone", sca_setup_timezone)

7
lua/sca_msg.lua Normal file
View File

@@ -0,0 +1,7 @@
local conf = {
[71880] = '"Dies ist ein test Text!"'
}
return conf