From 5d433a094990ee25897d30992468b8d4418b2683 Mon Sep 17 00:00:00 2001 From: raizen Date: Sun, 24 Mar 2024 23:12:11 +0100 Subject: [PATCH] friendship ended with timers, now tick updates is my best friend --- lua/sca_config.lua | 3 -- lua/sca_main.lua | 99 ++++++++++++++++++++++++++++++++-------------- lua/sca_msg.lua | 7 ++++ 3 files changed, 76 insertions(+), 33 deletions(-) delete mode 100644 lua/sca_config.lua create mode 100644 lua/sca_msg.lua diff --git a/lua/sca_config.lua b/lua/sca_config.lua deleted file mode 100644 index 73bdce3..0000000 --- a/lua/sca_config.lua +++ /dev/null @@ -1,3 +0,0 @@ -local conf = {} - -return conf \ No newline at end of file diff --git a/lua/sca_main.lua b/lua/sca_main.lua index 7eabfc8..baf2941 100644 --- a/lua/sca_main.lua +++ b/lua/sca_main.lua @@ -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) \ No newline at end of file diff --git a/lua/sca_msg.lua b/lua/sca_msg.lua new file mode 100644 index 0000000..f8d1d9e --- /dev/null +++ b/lua/sca_msg.lua @@ -0,0 +1,7 @@ +local conf = { + + [71880] = '"Dies ist ein test Text!"' + +} + +return conf \ No newline at end of file