friendship ended with timers, now tick updates is my best friend
This commit is contained in:
@@ -1,3 +0,0 @@
|
|||||||
local conf = {}
|
|
||||||
|
|
||||||
return conf
|
|
||||||
101
lua/sca_main.lua
101
lua/sca_main.lua
@@ -1,43 +1,82 @@
|
|||||||
local conf = include("sca_config.lua")
|
local msg = include("sca_msg.lua")
|
||||||
local timeSynced = false
|
local isRunning = false
|
||||||
|
local summerTime = nil
|
||||||
|
|
||||||
local function sca_loop()
|
local function sca_setup_timezone()
|
||||||
local intervalOffset = os.time() % 60
|
local time = os.time() // Year in seconds
|
||||||
print("intervalOffset: ", intervalOffset)
|
local currentYear = 1970 + (math.floor(time / 31536000))
|
||||||
-- If the function isn't called at HH:MM:00 then readjust the interval
|
local leapYears = 0
|
||||||
if intervalOffset > 0 then
|
local isLeapYear = tobool((currentYear % 4 == 0) and (currentYear % 100 != 0 and currentYear % 400 == 0))
|
||||||
timeSynced = false
|
|
||||||
|
|
||||||
print("[BuildBox] serverChatAdverts: Warning! Timer has not been called at HH:MM:00!! Readjusting interval...")
|
for (i = 1970, currentYear, 1) do
|
||||||
timer.Adjust("sca_chatPrintService", 60 - intervalOffset)
|
if ((i % 4 == 0) and (i % 100 != 0 and i % 400 == 0)) then
|
||||||
-- Otherwise, if timeSynced is false (interval was shorter than 60 seconds) then readjust interval to 60 seconds
|
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
|
else
|
||||||
if !timeSynced then
|
local currentDay = (yearBeginTimestamp + (daysInTotal * 86400)) - yearBeginTimestamp
|
||||||
timeSynced = true
|
|
||||||
|
|
||||||
timer.Adjust("sca_chatPrintService", 60)
|
|
||||||
-- else check config and print text to players if there is any
|
|
||||||
else
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sca_setup()
|
local function sca_msg_to_player(time)
|
||||||
print("[BuildBox] serverChatAdverts: Started!")
|
if (msg[time] != nil) then
|
||||||
|
for k, v in ipairs(player.GetHumans()) do
|
||||||
-- Calculate an interval that will call a function on HH:MM:00
|
v:SendLua('chat.AddText(' .. msg[time] .. ')')
|
||||||
print("setup time: ", os.time())
|
end
|
||||||
print(os.time() % 60)
|
end
|
||||||
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!")
|
|
||||||
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
7
lua/sca_msg.lua
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
local conf = {
|
||||||
|
|
||||||
|
[71880] = '"Dies ist ein test Text!"'
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return conf
|
||||||
Reference in New Issue
Block a user