Skip to content

Commit a1257e7

Browse files
Merge pull request #87 from DispersiaRoleplay/main
Real-Time sync fix + ua.lua translation
2 parents 2076b32 + 85a4954 commit a1257e7

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

fxmanifest.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ game 'gta5'
33
lua54 'yes'
44
author 'Kakarot'
55
description 'Syncs the time & weather for all players on the server and allows editing by command'
6-
version '2.1.0'
6+
version '2.1.1'
77

88
shared_scripts {
99
'config.lua',

locales/ua.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
local Translations = {
2+
weather = {
3+
now_frozen = 'Погода заморожена.',
4+
now_unfrozen = 'Погода більше не заморожена.',
5+
invalid_syntax = 'Невірний синтаксис, правильний: /weather <типпогоди> ',
6+
invalid_syntaxc = 'Невірний синтаксис, використай /weather <типпогоди>!',
7+
updated = 'Погода оновлена.',
8+
invalid = 'Невірний тип погоди. Доступні типи: \nEXTRASUNNY CLEAR NEUTRAL SMOG FOGGY OVERCAST CLOUDS CLEARING RAIN THUNDER SNOW BLIZZARD SNOWLIGHT XMAS HALLOWEEN',
9+
invalidc = 'Невірний тип погоди. Доступні типи: \nEXTRASUNNY CLEAR NEUTRAL SMOG FOGGY OVERCAST CLOUDS CLEARING RAIN THUNDER SNOW BLIZZARD SNOWLIGHT XMAS HALLOWEEN',
10+
willchangeto = 'Погода зміниться на: %{value}.',
11+
accessdenied = 'Доступ до команди /weather заборонено.',
12+
},
13+
dynamic_weather = {
14+
disabled = 'Динамічна зміна погоди вимкнена.',
15+
enabled = 'Динамічна зміна погоди увімкнена.',
16+
},
17+
time = {
18+
frozenc = 'Час заморожено.',
19+
unfrozenc = 'Час більше не заморожено.',
20+
now_frozen = 'Час заморожено.',
21+
now_unfrozen = 'Час більше не заморожено.',
22+
morning = 'Час встановлено на ранок.',
23+
noon = 'Час встановлено на полудень.',
24+
evening = 'Час встановлено на вечір.',
25+
night = 'Час встановлено на ніч.',
26+
change = 'Час змінено на %{value}:%{value2}.',
27+
changec = 'Час змінено на: %{value}!',
28+
invalid = 'Невірний синтаксис. Правильний: time <година> <хвилина> !',
29+
invalidc = 'Невірний синтаксис. Використай /time <година> <хвилина>!',
30+
access = 'Доступ до команди /time заборонено.',
31+
},
32+
blackout = {
33+
enabled = 'Режим блекауту увімкнено.',
34+
enabledc = 'Режим блекауту увімкнено.',
35+
disabled = 'Режим блекауту вимкнено.',
36+
disabledc = 'Режим блекауту вимкнено.',
37+
},
38+
help = {
39+
weathercommand = 'Змінити погоду.',
40+
weathertype = 'типпогоди',
41+
availableweather = 'Доступні типи: extrasunny, clear, neutral, smog, foggy, overcast, clouds, clearing, rain, thunder, snow, blizzard, snowlight, xmas та halloween',
42+
timecommand = 'Змінити час.',
43+
timehname = 'години',
44+
timemname = 'хвилини',
45+
timeh = 'Число від 0 до 23',
46+
timem = 'Число від 0 до 59',
47+
freezecommand = 'Заморозити / розморозити час.',
48+
freezeweathercommand = 'Увімкнути/вимкнути динамічну зміну погоди.',
49+
morningcommand = 'Встановити час на 09:00',
50+
nooncommand = 'Встановити час на 12:00',
51+
eveningcommand = 'Встановити час на 18:00',
52+
nightcommand = 'Встановити час на 23:00',
53+
blackoutcommand = 'Увімкнути/вимкнути режим блекауту.',
54+
},
55+
}
56+
57+
if GetConvar('qb_locale', 'en') == 'ua' then
58+
Lang = Lang or Locale:new({
59+
phrases = Translations,
60+
warnOnMissing = true
61+
})
62+
end

server/server.lua

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,22 @@ local function setDynamicWeather(state)
120120
return Config.DynamicWeather
121121
end
122122

123-
--- Retrieves the current time from worldtimeapi.org
124-
--- @return number - Unix time
123+
--- Retrieves the current time from api.timezonedb.com
125124
local function retrieveTimeFromApi(callback)
126125
Citizen.CreateThread(function()
127-
PerformHttpRequest("http://worldtimeapi.org/api/ip", function(statusCode, response)
128-
if statusCode == 200 then
126+
local apiKey = "REPLACE_ME_TO_YOUR_API" -- 🔐 Replace with your actual key from your email
127+
local zone = "America/Los_Angeles" -- 🔐 Replace with your actual TimeZone, ex: America/Los_Angeles
128+
local url = "http://api.timezonedb.com/v2.1/get-time-zone?key=" .. apiKey .. "&format=json&by=zone&zone=" .. zone
129+
-- print(response) -- 🛠️ Debug: uncomment to inspect raw API response
130+
PerformHttpRequest(url, function(statusCode, response)
131+
if statusCode == 200 and response then
129132
local data = json.decode(response)
130-
if data == nil or data.unixtime == nil then
131-
callback(nil)
132-
else
133-
callback(data.unixtime)
133+
if data and data.timestamp then
134+
callback(data.timestamp)
135+
return
134136
end
135-
else
136-
callback(nil)
137137
end
138+
callback(nil)
138139
end, "GET", nil, nil)
139140
end)
140141
end
@@ -283,34 +284,19 @@ CreateThread(function()
283284
local failedCount = 0
284285

285286
while true do
286-
Wait(0)
287+
Wait(60000) -- ⏱️ Sync server time every 1 minute with real time API. Falls back to OS time if failed.
287288
local newBaseTime = os.time(os.date("!*t")) / 2 + 360 --Set the server time depending of OS time
288289
if Config.RealTimeSync then
289-
newBaseTime = os.time(os.date("!*t")) --Set the server time depending of OS time
290-
if realTimeFromApi == nil then
291-
retrieveTimeFromApi(function(unixTime)
292-
realTimeFromApi = unixTime -- Set the server time depending on real-time retrieved from API
293-
end)
294-
end
295-
while realTimeFromApi == nil do
296-
if failedCount > 10 then
297-
print("Failed to retrieve real time from API, falling back to local time")
298-
break
290+
retrieveTimeFromApi(function(unixTime)
291+
if unixTime then
292+
baseTime = unixTime
293+
else
294+
baseTime = os.time(os.date("!*t"))
299295
end
300-
failedCount = failedCount + 1
301-
Wait(100)
302-
end
303-
if realTimeFromApi ~= nil then
304-
newBaseTime = realTimeFromApi
305-
end
306-
end
307-
if (newBaseTime % 60) ~= previous then --Check if a new minute is passed
308-
previous = newBaseTime % 60 --Only update time with plain minutes, seconds are handled in the client
309-
if freezeTime then
310-
timeOffset = timeOffset + baseTime - newBaseTime
311-
end
312-
baseTime = newBaseTime
313-
end
296+
end)
297+
else
298+
baseTime = os.time(os.date("!*t")) / 2 + 360
299+
end
314300
end
315301
end)
316302

0 commit comments

Comments
 (0)