1
0
Fork 0
forked from Simnation/Main
Main/resources/[standalone]/mh_realtime/server.lua
2025-08-13 21:59:55 +02:00

102 lines
No EOL
3.4 KiB
Lua

local Config = {
Weather = true,
Time = false
}
local apiKey = "8d27737fd1425cd3d243f54e4f80e765"
local city = "Los Angeles"
local weatherType = "CLEAR"
local lastWeatherUpdate = os.time()
function getRealTime()
local now = os.date("*t")
local hour = now.hour
return now.hour, now.min
end
function fetchWeather()
local url = string.format("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s", city, apiKey)
PerformHttpRequest(url, function(statusCode, response, headers)
if statusCode == 200 then
local data = json.decode(response)
if data and data.weather and #data.weather > 0 then
local mainWeather = data.weather[1].main:lower()
print(mainWeather)
if mainWeather == "rain" then
weatherType = "RAIN"
elseif mainWeather == "clouds" then
weatherType = "CLOUDS"
elseif mainWeather == "clear" then
weatherType = "CLEAR"
elseif mainWeather == "snow" then
weatherType = "XMAS"
elseif mainWeather == "fog" or mainWeather == "mist" then
weatherType = "FOGGY"
elseif mainWeather == "thunderstorm" then
weatherType = "THUNDER"
else
weatherType = "CLEAR"
end
print(string.format("[WetterSync] Neues Wetter geladen: %s", weatherType))
end
else
print("[WetterSync] Fehler beim Abrufen des Wetters: Statuscode " .. statusCode)
end
end, 'GET', '', { ["Content-Type"] = 'application/json' })
end
Citizen.CreateThread(function()
while true do
local hour, minute = getRealTime()
local currentTime = os.time()
if currentTime - lastWeatherUpdate >= 600 then
fetchWeather()
lastWeatherUpdate = currentTime
end
if Config.Time then
TriggerClientEvent('wsync:updateTime', -1, hour, minute)
end
if Config.Weather then
TriggerClientEvent('wsync:updateWeather', -1, weatherType)
end
Citizen.Wait(30000)
end
end)
RegisterNetEvent('wsync:requestSync')
AddEventHandler('wsync:requestSync', function()
local hour, minute = getRealTime()
--TriggerClientEvent('wsync:updateTime', source, hour, minute)
TriggerClientEvent('wsync:updateWeather', source, weatherType)
end)
--[[
TimeSync
]]
local function getBerlinTime()
PerformHttpRequest("https://timeapi.io/api/Time/current/zone?timeZone=America/Los_Angeles", function(statusCode, response, headers)
if statusCode == 200 then
local data = json.decode(response)
if data and data.hour and data.minute then
TriggerClientEvent("realtime-sync:updateTime", -1, data.hour, data.minute)
else
print("[RealtimeSync] Fehler beim Parsen der Zeitdaten.")
end
else
print("[RealtimeSync] Fehler beim Abrufen der Zeitdaten: Statuscode " .. tostring(statusCode))
end
end, "GET", "", {["Content-Type"] = "application/json"})
end
Citizen.CreateThread(function()
while true do
Citizen.Wait(360000)
getBerlinTime()
end
end)