forked from DevTestingPizza/vSync
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.lua
361 lines (335 loc) · 12.3 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
------------------ change this -------------------
admins = {
'steam:110000105959047',
--'license:1234975143578921327',
}
-- Set this to false if you don't want the weather to change automatically every 10 minutes.
DynamicWeather = true
--------------------------------------------------
debugprint = false -- don't touch this unless you know what you're doing or you're being asked by Vespura to turn this on.
--------------------------------------------------
-------------------- DON'T CHANGE THIS --------------------
AvailableWeatherTypes = {
'EXTRASUNNY',
'CLEAR',
'NEUTRAL',
'SMOG',
'FOGGY',
'OVERCAST',
'CLOUDS',
'CLEARING',
'RAIN',
'THUNDER',
'SNOW',
'BLIZZARD',
'SNOWLIGHT',
'XMAS',
'HALLOWEEN',
}
CurrentWeather = "EXTRASUNNY"
local baseTime = 0
local timeOffset = 0
local freezeTime = false
local blackout = false
local newWeatherTimer = 10
RegisterServerEvent('weathertimesync:requestSync')
AddEventHandler('weathertimesync:requestSync', function()
TriggerClientEvent('weathertimesync:updateWeather', -1, CurrentWeather, blackout)
TriggerClientEvent('weathertimesync:updateTime', -1, baseTime, timeOffset, freezeTime)
end)
function isAllowedToChange(player)
local allowed = false
for i,id in ipairs(admins) do
for x,pid in ipairs(GetPlayerIdentifiers(player)) do
if debugprint then print('admin id: ' .. id .. '\nplayer id:' .. pid) end
if string.lower(pid) == string.lower(id) then
allowed = true
end
end
end
return allowed
end
RegisterCommand('freezetime', function(source, args)
if source ~= 0 then
if isAllowedToChange(source) then
freezeTime = not freezeTime
if freezeTime then
TriggerClientEvent('weathertimesync:notify', source, 'Time is now ~b~frozen~s~.')
else
TriggerClientEvent('weathertimesync:notify', source, 'Time is ~y~no longer frozen~s~.')
end
else
TriggerClientEvent('chatMessage', source, '', {255,255,255}, '^8Error: ^1You are not allowed to use this command.')
end
else
freezeTime = not freezeTime
if freezeTime then
print("Time is now frozen.")
else
print("Time is no longer frozen.")
end
end
end)
RegisterCommand('freezeweather', function(source, args)
if source ~= 0 then
if isAllowedToChange(source) then
DynamicWeather = not DynamicWeather
if not DynamicWeather then
TriggerClientEvent('weathertimesync:notify', source, 'Dynamic weather changes are now ~r~disabled~s~.')
else
TriggerClientEvent('weathertimesync:notify', source, 'Dynamic weather changes are now ~b~enabled~s~.')
end
else
TriggerClientEvent('chatMessage', source, '', {255,255,255}, '^8Error: ^1You are not allowed to use this command.')
end
else
DynamicWeather = not DynamicWeather
if not DynamicWeather then
print("Weather is now frozen.")
else
print("Weather is no longer frozen.")
end
end
end)
RegisterCommand('weather', function(source, args)
if source == 0 then
local validWeatherType = false
if args[1] == nil then
print("Invalid syntax, correct syntax is: /weather <weathertype> ")
return
else
for i,wtype in ipairs(AvailableWeatherTypes) do
if wtype == string.upper(args[1]) then
validWeatherType = true
end
end
if validWeatherType then
print("Weather has been updated.")
CurrentWeather = string.upper(args[1])
newWeatherTimer = 10
TriggerEvent('weathertimesync:requestSync')
else
print("Invalid weather type, valid weather types are: \nEXTRASUNNY CLEAR NEUTRAL SMOG FOGGY OVERCAST CLOUDS CLEARING RAIN THUNDER SNOW BLIZZARD SNOWLIGHT XMAS HALLOWEEN ")
end
end
else
if isAllowedToChange(source) then
local validWeatherType = false
if args[1] == nil then
TriggerClientEvent('chatMessage', source, '', {255,255,255}, '^8Error: ^1Invalid syntax, use ^0/weather <weatherType> ^1instead!')
else
for i,wtype in ipairs(AvailableWeatherTypes) do
if wtype == string.upper(args[1]) then
validWeatherType = true
end
end
if validWeatherType then
TriggerClientEvent('weathertimesync:notify', source, 'Weather will change to: ~y~' .. string.lower(args[1]) .. "~s~.")
CurrentWeather = string.upper(args[1])
newWeatherTimer = 10
TriggerEvent('weathertimesync:requestSync')
else
TriggerClientEvent('chatMessage', source, '', {255,255,255}, '^8Error: ^1Invalid weather type, valid weather types are: ^0\nEXTRASUNNY CLEAR NEUTRAL SMOG FOGGY OVERCAST CLOUDS CLEARING RAIN THUNDER SNOW BLIZZARD SNOWLIGHT XMAS HALLOWEEN ')
end
end
else
TriggerClientEvent('chatMessage', source, '', {255,255,255}, '^8Error: ^1You do not have access to that command.')
print('Access for command /weather denied.')
end
end
end, false)
RegisterCommand('blackout', function(source)
if source == 0 then
blackout = not blackout
if blackout then
print("Blackout is now enabled.")
else
print("Blackout is now disabled.")
end
else
if isAllowedToChange(source) then
blackout = not blackout
if blackout then
TriggerClientEvent('weathertimesync:notify', source, 'Blackout is now ~b~enabled~s~.')
else
TriggerClientEvent('weathertimesync:notify', source, 'Blackout is now ~r~disabled~s~.')
end
TriggerEvent('weathertimesync:requestSync')
end
end
end)
RegisterCommand('morning', function(source)
if source == 0 then
print("For console, use the \"/time <hh> <mm>\" command instead!")
return
end
if isAllowedToChange(source) then
ShiftToMinute(0)
ShiftToHour(9)
TriggerClientEvent('weathertimesync:notify', source, 'Time set to ~y~morning~s~.')
TriggerEvent('weathertimesync:requestSync')
end
end)
RegisterCommand('noon', function(source)
if source == 0 then
print("For console, use the \"/time <hh> <mm>\" command instead!")
return
end
if isAllowedToChange(source) then
ShiftToMinute(0)
ShiftToHour(12)
TriggerClientEvent('weathertimesync:notify', source, 'Time set to ~y~noon~s~.')
TriggerEvent('weathertimesync:requestSync')
end
end)
RegisterCommand('evening', function(source)
if source == 0 then
print("For console, use the \"/time <hh> <mm>\" command instead!")
return
end
if isAllowedToChange(source) then
ShiftToMinute(0)
ShiftToHour(18)
TriggerClientEvent('weathertimesync:notify', source, 'Time set to ~y~evening~s~.')
TriggerEvent('weathertimesync:requestSync')
end
end)
RegisterCommand('night', function(source)
if source == 0 then
print("For console, use the \"/time <hh> <mm>\" command instead!")
return
end
if isAllowedToChange(source) then
ShiftToMinute(0)
ShiftToHour(23)
TriggerClientEvent('weathertimesync:notify', source, 'Time set to ~y~night~s~.')
TriggerEvent('weathertimesync:requestSync')
end
end)
function ShiftToMinute(minute)
timeOffset = timeOffset - ( ( (baseTime+timeOffset) % 60 ) - minute )
end
function ShiftToHour(hour)
timeOffset = timeOffset - ( ( ((baseTime+timeOffset)/60) % 24 ) - hour ) * 60
end
RegisterCommand('time', function(source, args, rawCommand)
if source == 0 then
if tonumber(args[1]) ~= nil and tonumber(args[2]) ~= nil then
local argh = tonumber(args[1])
local argm = tonumber(args[2])
if argh < 24 then
ShiftToHour(argh)
else
ShiftToHour(0)
end
if argm < 60 then
ShiftToMinute(argm)
else
ShiftToMinute(0)
end
print("Time has changed to " .. argh .. ":" .. argm .. ".")
TriggerEvent('weathertimesync:requestSync')
else
print("Invalid syntax, correct syntax is: time <hour> <minute> !")
end
elseif source ~= 0 then
if isAllowedToChange(source) then
if tonumber(args[1]) ~= nil and tonumber(args[2]) ~= nil then
local argh = tonumber(args[1])
local argm = tonumber(args[2])
if argh < 24 then
ShiftToHour(argh)
else
ShiftToHour(0)
end
if argm < 60 then
ShiftToMinute(argm)
else
ShiftToMinute(0)
end
local newtime = math.floor(((baseTime+timeOffset)/60)%24) .. ":"
local minute = math.floor((baseTime+timeOffset)%60)
if minute < 10 then
newtime = newtime .. "0" .. minute
else
newtime = newtime .. minute
end
TriggerClientEvent('weathertimesync:notify', source, 'Time was changed to: ~y~' .. newtime .. "~s~!")
TriggerEvent('weathertimesync:requestSync')
else
TriggerClientEvent('chatMessage', source, '', {255,255,255}, '^8Error: ^1Invalid syntax. Use ^0/time <hour> <minute> ^1instead!')
end
else
TriggerClientEvent('chatMessage', source, '', {255,255,255}, '^8Error: ^1You do not have access to that command.')
print('Access for command /time denied.')
end
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
local newBaseTime = os.time(os.date("!*t"))/2 + 360
if freezeTime then
timeOffset = timeOffset + baseTime - newBaseTime
end
baseTime = newBaseTime
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(5000)
TriggerClientEvent('weathertimesync:updateTime', -1, baseTime, timeOffset, freezeTime)
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(300000)
TriggerClientEvent('weathertimesync:updateWeather', -1, CurrentWeather, blackout)
end
end)
Citizen.CreateThread(function()
while true do
newWeatherTimer = newWeatherTimer - 1
Citizen.Wait(60000)
if newWeatherTimer == 0 then
if DynamicWeather then
NextWeatherStage()
end
newWeatherTimer = 10
end
end
end)
function NextWeatherStage()
if CurrentWeather == "CLEAR" or CurrentWeather == "CLOUDS" or CurrentWeather == "EXTRASUNNY" then
local new = math.random(1,2)
if new == 1 then
CurrentWeather = "CLEARING"
else
CurrentWeather = "OVERCAST"
end
elseif CurrentWeather == "CLEARING" or CurrentWeather == "OVERCAST" then
local new = math.random(1,6)
if new == 1 then
if CurrentWeather == "CLEARING" then CurrentWeather = "FOGGY" else CurrentWeather = "RAIN" end
elseif new == 2 then
CurrentWeather = "CLOUDS"
elseif new == 3 then
CurrentWeather = "CLEAR"
elseif new == 4 then
CurrentWeather = "EXTRASUNNY"
elseif new == 5 then
CurrentWeather = "SMOG"
else
CurrentWeather = "FOGGY"
end
elseif CurrentWeather == "THUNDER" or CurrentWeather == "RAIN" then
CurrentWeather = "CLEARING"
elseif CurrentWeather == "SMOG" or CurrentWeather == "FOGGY" then
CurrentWeather = "CLEAR"
end
TriggerEvent("weathertimesync:requestSync")
if debugprint then
print("[weathertimesync] New random weather type has been generated: " .. CurrentWeather .. ".\n")
print("[weathertimesync] Resetting timer to 10 minutes.\n")
end
end