-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
343 lines (303 loc) · 9.31 KB
/
server.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
local json = require "json"
local weatherFile = "weather_data.json"
local translationsFile = "translations.json"
local defaultWeather = { weather = "CLEAR", hour = 12, minute = 0, specialWeather = nil, specialPersistent = false }
local currentWeather = {}
local translations = {}
local lang = "en"
local function loadTranslations()
local file = LoadResourceFile(GetCurrentResourceName(), translationsFile)
if file then
translations = json.decode(file)
if translations then
print("Translations loaded successfully.")
else
print("Failed to decode translations.")
end
else
print("Failed to open translations file.")
end
end
local function translate(lang, key)
return translations[lang] and translations[lang][key] or key
end
local function loadWeather()
local file = io.open(weatherFile, "r")
if file then
local content = file:read("*a")
local data = json.decode(content)
if data and data.weather and data.hour and data.minute then
currentWeather = data
else
currentWeather = defaultWeather
end
file:close()
else
currentWeather = defaultWeather
end
end
local function saveWeather()
local file = io.open(weatherFile, "w+")
if file then
local dataToSave = {
weather = currentWeather.weather,
hour = currentWeather.hour,
minute = currentWeather.minute,
specialWeather = currentWeather.specialWeather,
specialPersistent = currentWeather.specialPersistent
}
file:write(json.encode(dataToSave))
file:close()
end
end
local function syncWeather()
TriggerClientEvent("weather:update", -1, currentWeather.weather, currentWeather.specialWeather, currentWeather.specialPersistent)
end
local function syncTime()
TriggerClientEvent("time:update", -1, currentWeather.hour, currentWeather.minute)
end
local function addCustomNotification(playerId, header, title, subtitle, content, duration, sticky)
TriggerClientEvent('fb_ui:addNotification', playerId, "", "", title, subtitle, content, duration or 10000, sticky or false)
end
local validWeather = {
"CLEAR", "EXTRASUNNY", "CLOUDS", "OVERCAST", "RAIN",
"CLEARING", "THUNDER", "SMOG", "FOGGY", "SNOW",
"BLIZZARD", "SNOWLIGHT", "XMAS", "HALLOWEEN"
}
local validSpecialWeather = {
"SNOWLIGHT", "XMAS", "HALLOWEEN", "BLACKOUT"
}
function table.contains(tbl, value)
for _, v in pairs(tbl) do
if v == value then
return true
end
end
return false
end
function math.clamp(val, min, max)
return math.min(math.max(val, min), max)
end
RegisterCommand("weather", function(source, args)
if not IsPlayerAceAllowed(source, "command.weather") then
addCustomNotification(
source,
translate(lang, "accessDenied"),
translate(lang, "accessDenied"),
"",
"",
10000
)
return
end
if #args < 1 then
addCustomNotification(
source,
translate(lang, "weatherCommandUsage"),
translate(lang, "weatherCommandExample") .. table.concat(validWeather, ", "),
"",
"",
12000
)
return
end
local newWeather = args[1]:upper()
if not table.contains(validWeather, newWeather) then
addCustomNotification(
source,
translate(lang, "invalidWeatherType"),
translate(lang, "invalidWeatherType") .. table.concat(validWeather, ", "),
"",
"",
12000
)
return
end
currentWeather.weather = newWeather
syncWeather()
saveWeather()
local playerName = GetPlayerName(source) or "Console"
addCustomNotification(
source,
translate(lang, "weatherChanged"),
translate(lang, "weatherChanged") .. newWeather,
"",
"",
10000
)
end, false)
RegisterCommand("weatherreset", function(source)
if not IsPlayerAceAllowed(source, "command.weatherreset") then
addCustomNotification(
source,
translate(lang, "accessDenied"),
translate(lang, "accessDenied"),
"",
"",
10000
)
return
end
currentWeather = defaultWeather
syncWeather()
saveWeather()
local playerName = GetPlayerName(source) or "Console"
addCustomNotification(
source,
translate(lang, "weatherReset"),
translate(lang, "weatherReset"),
"",
"",
10000
)
end, false)
RegisterCommand("time", function(source, args)
if not IsPlayerAceAllowed(source, "command.time") then
addCustomNotification(
source,
translate(lang, "accessDenied"),
translate(lang, "accessDenied"),
"",
"",
10000
)
return
end
if #args < 2 then
addCustomNotification(
source,
translate(lang, "timeCommandUsage"),
translate(lang, "timeCommandExample"),
"",
"",
12000
)
return
end
local hour = tonumber(args[1])
local minute = tonumber(args[2])
if hour and minute then
currentWeather.hour = math.clamp(hour, 0, 23)
currentWeather.minute = math.clamp(minute, 0, 59)
syncTime()
saveWeather()
local playerName = GetPlayerName(source) or "Console"
addCustomNotification(
source,
translate(lang, "timeChanged"),
translate(lang, "timeChanged") .. hour .. "h" .. string.format("%02d", minute),
"",
"",
10000
)
else
addCustomNotification(
source,
translate(lang, "invalidTimeValues"),
translate(lang, "invalidTimeValues"),
"",
"",
10000
)
end
end, false)
RegisterCommand("syncWeather", function(source)
if not IsPlayerAceAllowed(source, "command.syncWeather") then
addCustomNotification(
source,
translate(lang, "accessDenied"),
translate(lang, "accessDenied"),
"",
"",
10000
)
return
end
syncWeather()
syncTime()
addCustomNotification(
source,
translate(lang, "syncSuccess"),
translate(lang, "syncSuccess"),
"",
"",
10000
)
end, false)
RegisterCommand("weatherspecial", function(source, args)
if not IsPlayerAceAllowed(source, "command.weatherspecial") then
addCustomNotification(
source,
translate(lang, "accessDenied"),
translate(lang, "accessDenied"),
"",
"",
10000
)
return
end
if #args < 2 then
addCustomNotification(
source,
translate(lang, "specialWeatherCommandUsage"),
translate(lang, "specialWeatherCommandExample") .. table.concat(validSpecialWeather, ", "),
"",
"",
12000
)
return
end
local specialWeather = args[1]:upper()
local persistent = args[2]:lower() == "true"
if not table.contains(validSpecialWeather, specialWeather) then
addCustomNotification(
source,
translate(lang, "invalidSpecialWeatherType"),
translate(lang, "invalidSpecialWeatherType") .. table.concat(validSpecialWeather, ", "),
"",
"",
12000
)
return
end
currentWeather.specialWeather = specialWeather
currentWeather.specialPersistent = persistent
syncWeather()
saveWeather()
local playerName = GetPlayerName(source) or "Console"
addCustomNotification(
source,
translate(lang, "specialWeatherChanged"),
translate(lang, "specialWeatherChanged") .. specialWeather .. " (Persistent: " .. tostring(persistent) .. ")",
"",
"",
10000
)
end, false)
AddEventHandler("onResourceStart", function(resourceName)
if resourceName == GetCurrentResourceName() then
loadTranslations()
loadWeather()
syncWeather()
syncTime()
addCustomNotification(
-1,
translate(lang, "weatherLoading"),
translate(lang, "weatherLoaded") .. currentWeather.weather .. " at " .. currentWeather.hour .. "h" .. string.format("%02d", currentWeather.minute),
"",
"",
10000
)
end
end)
AddEventHandler("onResourceStop", function(resourceName)
if resourceName == GetCurrentResourceName() then
saveWeather()
end
end)
AddEventHandler("txAdmin:events:scheduledRestart", function()
saveWeather()
end)
AddEventHandler("txAdmin:events:serverShuttingDown", function()
saveWeather()
end)