-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvs_server.lua
198 lines (181 loc) · 5.54 KB
/
vs_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
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
------------------ change this -------------------
-- 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('vSync:requestSync')
AddEventHandler('vSync:requestSync', function()
TriggerClientEvent('vSync:updateWeather', -1, CurrentWeather, blackout)
TriggerClientEvent('vSync:updateTime', -1, baseTime, timeOffset, freezeTime)
end)
function ShiftToMinute(minute)
timeOffset = timeOffset - ( ( (baseTime+timeOffset) % 60 ) - minute )
end
function ShiftToHour(hour)
timeOffset = timeOffset - ( ( ((baseTime+timeOffset)/60) % 24 ) - hour ) * 60
end
ESX.RunCustomFunction("AddCommand", "time", 5, function(xPlayer, args)
if tonumber(args.hour) ~= nil and tonumber(args.min) ~= nil then
local argh = tonumber(args.hour)
local argm = tonumber(args.min)
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('vSync:notify', xPlayer.source, 'Time was changed to: ~y~' .. newtime .. "~s~!")
TriggerEvent('vSync:requestSync')
else
TriggerClientEvent('chatMessage', xPlayer.source, '', {255,255,255}, '^8Error: ^1Invalid syntax. Use ^0/time <hour> <minute> ^1instead!')
end
end, {
{name = 'hour', type = 'number'},
{name = 'min', type = 'number'}
}, '.time hour min', '.')
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('vSync:updateTime', -1, baseTime, timeOffset, freezeTime)
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(300000)
TriggerClientEvent('vSync: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 = 30
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("vSync:requestSync")
if debugprint then
print("[vSync] New random weather type has been generated: " .. CurrentWeather .. ".\n")
print("[vSync] Resetting timer to 10 minutes.\n")
end
end
AddEventHandler('vSync:setWeather', function(weather)
for i,wtype in ipairs(AvailableWeatherTypes) do
if wtype == string.upper(weather) then
validWeatherType = true
end
end
freezeTime = true
DynamicWeather = true
if validWeatherType then
CurrentWeather = string.upper(weather)
newWeatherTimer = 10
TriggerEvent('vSync:requestSync')
end
end)
AddEventHandler('vSync:setTime', function(hour, minutes)
freezeTime = true
DynamicWeather = true
if tonumber(hour) ~= nil and tonumber(minutes) ~= nil then
local argh = tonumber(hour)
local argm = tonumber(minutes)
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
TriggerEvent('vSync:requestSync')
end
end)