From 19d0253f8d04fd474c622fa0e1746dee465d245b Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Sun, 30 Jun 2024 20:29:29 +0200 Subject: [PATCH] Update v2.1.5 * Added MSK.DeleteCron --- fxmanifest.lua | 2 +- server/sv_cronjobs.lua | 51 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index 62f2c38..c0d277d 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -4,7 +4,7 @@ games { 'gta5' } author 'Musiker15 - MSK Scripts' name 'msk_core' description 'Core functions for MSK Scripts' -version '2.1.4' +version '2.1.5' lua54 'yes' diff --git a/server/sv_cronjobs.lua b/server/sv_cronjobs.lua index 06d0199..e618478 100644 --- a/server/sv_cronjobs.lua +++ b/server/sv_cronjobs.lua @@ -1,4 +1,17 @@ local CronJobs, CronJobsAt = {}, {} +local CronJobUniqueIds = {} + +local createUniqueId = function() + local id = math.random(1, 999999999999) + + if CronJobUniqueIds[id] then + return createUniqueId() + end + + createUniqueId[id] = id + + return createUniqueId[id] +end local getTime = function(time, date) if date.m then @@ -33,9 +46,8 @@ tickCronJob = function() local m = tonumber(os.date('%M', timestamp)) if currD == d and currH == h and currM == m then - logging('debug', 'tickCronJob', os.date('%d.%m.%Y %H:%M:%S', currTime)) CronJobs[i].timestamp = getTime(timestamp, CronJobs[i].date) - CronJobs[i].cb(CronJobs[i].data, {timestamp = currTime, d = currD, h = currH, m = currM}) + CronJobs[i].cb(CronJobs[i].id, CronJobs[i].data, {timestamp = currTime, d = currD, h = currH, m = currM}) end end @@ -51,7 +63,7 @@ tickCronJobAt = function() for i=1, #CronJobsAt, 1 do if (not CronJobsAt[i].date.atD or CronJobsAt[i].date.atD and currD == CronJobsAt[i].date.atD) and currH == CronJobsAt[i].date.atH and currM == CronJobsAt[i].date.atM then - CronJobsAt[i].cb(CronJobsAt[i].data, {timestamp = currTime, d = currD, h = currH, m = currM}) + CronJobsAt[i].cb(CronJobs[i].id, CronJobsAt[i].data, {timestamp = currTime, d = currD, h = currH, m = currM}) end end @@ -77,6 +89,7 @@ MSK.CreateCron = function(date, data, cb) end CronJobsAt[#CronJobsAt + 1] = { + uniqueId = createUniqueId(), date = date, data = data, cb = cb @@ -85,6 +98,7 @@ MSK.CreateCron = function(date, data, cb) logging('debug', 'Created CronJobAT at: ' .. os.date('%d.%m.%Y %H:%M:%S', os.time()), 'Will be executed at: ' .. ('%s:%s'):format(date.atH, date.atM) .. ' ' .. ('Day %s (1-7 = Mo - Su)'):format(date.atD or 'everyday')) else CronJobs[#CronJobs + 1] = { + uniqueId = createUniqueId(), timestamp = timestamp, date = date, data = data, @@ -95,4 +109,33 @@ MSK.CreateCron = function(date, data, cb) end end exports('CreateCron', MSK.CreateCron) -RegisterNetEvent('msk_core:createCron', MSK.CreateCron) \ No newline at end of file +RegisterNetEvent('msk_core:createCron', MSK.CreateCron) + +MSK.DeleteCron = function(id) + if not id then return end + if not CronJobUniqueIds[id] then return end + local found = false + + for i=1, #CronJobs, 1 do + if CronJobs[i].uniqueId == id then + CronJobs[i] = nil + CronJobUniqueIds[id] = nil + found = true + break + end + end + + if found then return end + + for i=1, #CronJobsAt, 1 do + if CronJobsAt[i].uniqueId == id then + CronJobsAt[i] = nil + CronJobUniqueIds[id] = nil + found = true + break + end + end + + return found +end +exports('DeleteCron', MSK.DeleteCron) \ No newline at end of file