Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions sonorancad/core/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,61 @@ CreateThread(function()
end
end
end)

RegisterCommand('time', function()
local hours = GetClockHours()
local minutes = GetClockMinutes()
TriggerEvent('chat:addMessage', {
args = {
'SonoranCAD',
('Current time: %s:%s'):format(hours, minutes)
}
})
end)

-- Jordan - Time Utils
local isSendingTime = false

local function getFormattedGameTime()
-- Get in-game date and time
local year, month, day = GetClockYear(), GetClockMonth(), GetClockDayOfMonth()
local hour, minute, second = GetClockHours(), GetClockMinutes(), GetClockSeconds()

-- Format the in-game time as "YYYY-MM-DD HH:MM:SS"
return string.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second)
end

local function getSecondsPerInGameHour()
-- Get milliseconds per in-game minute
local msPerMinute = GetMillisecondsPerGameMinute()

-- Convert to seconds per in-game hour
local secondsPerHour = (msPerMinute / 1000) * 60
return secondsPerHour
end

-- Event to start sending time to the server
RegisterNetEvent("SonoranCad:time:requestSendTime", function()
debugLog("You have been selected to send the time.")
isSendingTime = true

Citizen.CreateThread(function()
while isSendingTime do
local inGameTime = getFormattedGameTime()
local secondsPerHour = getSecondsPerInGameHour()
local timeData = {
currentGame = inGameTime,
secondsPerHour = secondsPerHour
}
TriggerServerEvent("SonoranCad:time:sendTime", timeData)
debugLog("Time sent to server: " .. json.encode(timeData))
Citizen.Wait(60000) -- Wait 60 seconds before sending again
end
end)
end)

-- Event to stop sending time (optional cleanup)
RegisterNetEvent("SonoranCad:time:stopSendingTime", function()
debugLog("You are no longer required to send the time.")
isSendingTime = false
end)
4 changes: 3 additions & 1 deletion sonorancad/core/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ function dumpInfo()
local version = GetResourceMetadata(GetCurrentResourceName(), "version", 0)
local pluginList, loadedPlugins, disabledPlugins = GetPluginLists()
local pluginVersions = {}
local versionFile = LoadVersionFile()
local versions = json.decode(versionFile)
local cadVariables = { ["netPort"] = GetConvar("netPort", "Unknown")}
local variableList = ""
for k, v in pairs(cadVariables) do
variableList = ("%s%s = %s\n"):format(variableList, k, v)
end
for k, v in pairs(pluginList) do
if Config.plugins[v] then
table.insert(pluginVersions, ("%s [%s/%s]"):format(v, Config.plugins[v].version, Config.plugins[v].latestVersion))
table.insert(pluginVersions, ("%s [%s/%s]"):format(v, Config.plugins[v].configVersion, versions.submoduleConfigs[v].version))
end
end
local coreConfig = {}
Expand Down
2 changes: 1 addition & 1 deletion sonorancad/core/plugin_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Provides logic for checking loaded plugins after startup
]]

local function LoadVersionFile()
function LoadVersionFile()
local f = LoadResourceFile(GetCurrentResourceName(), ("version.json"))
if f then
return f
Expand Down
79 changes: 78 additions & 1 deletion sonorancad/core/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,81 @@ exports('setCallPostal', setCallPostal)
exports('performLookup', performLookup)
exports('checkCADSubscriptionType', checkCADSubscriptionType)
exports('getDispatchStatus', getDispatchStatus)
-- Jordan - CAD Utils
-- Jordan - CAD Utils

-- Jordan - Time Utils

local currentTimeClient = nil -- Stores the Player ID of the current time reporter
local lastSendTime = nil -- Stores the last time the time was sent

-- Function to select a new client
local function selectNewClient()
local players = GetPlayers()
if #players == 0 then
if currentTimeClient then
TriggerClientEvent("SonoranCad:time:stopSendingTime", currentTimeClient)
end
currentTimeClient = nil
debugLog("No players available to send time.")
return
end
local newClient = players[math.random(1, #players)]
-- Inform the previous client to stop sending time
if currentTimeClient and currentTimeClient ~= newClient then
TriggerClientEvent("SonoranCad:time:stopSendingTime", currentTimeClient)
end
currentTimeClient = newClient
debugLog("Selected client " .. currentTimeClient .. " to send time.")
TriggerClientEvent("SonoranCad:time:requestSendTime", currentTimeClient)
end

-- Event to handle received time from the client
RegisterNetEvent("SonoranCad:time:sendTime", function(timeData)
local source = source
if tonumber(source) == tonumber(currentTimeClient) then
debugLog("Received time from client " .. source .. ": " .. json.encode(timeData))
lastSendTime = os.time()
-- Send the time to the API
exports['sonorancad']:performApiRequest({
{
['serverId'] = GetConvar('sonoran_serverId', 1),
['currentUtc'] = os.date("!%Y-%m-%d %H:%M:%S"),
['currentGame'] = timeData.currentGame,
['secondsPerHour'] = timeData.secondsPerHour
}
}, 'SET_CLOCK', function(_)
end)
else
debugLog("Unexpected client sent time. Ignored.")
end
end)

-- Event when a player disconnects
AddEventHandler("playerDropped", function(reason)
local playerId = source
if tonumber(playerId) == tonumber(currentTimeClient) then
debugLog("Client " .. playerId .. " disconnected. Selecting a new client.")
selectNewClient()
end
end)

-- Initial selection when server starts
AddEventHandler("onResourceStart", function(resourceName)
if resourceName == GetCurrentResourceName() then
selectNewClient()
end
end)

Citizen.CreateThread(function()
while true do
Citizen.Wait(65000)
if currentTimeClient then
if lastSendTime and os.time() - lastSendTime > 70 then
debugLog("Client " .. currentTimeClient .. " failed to report time in 60 seconds.")
selectNewClient()
end
elseif not currentTimeClient and #GetPlayers() > 0 then
selectNewClient()
end
end
end)