-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.lua
117 lines (106 loc) · 3.56 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
local syncing = true
Citizen.CreateThread(function()
updated = false
while true do
if not updated then
if syncing then
local hour, minute = os.date("%H"), os.date("%M")
for i=1, #Config.Updates, 1 do
local time = Config.Updates[i]
if tonumber(hour) == tonumber(string.sub(time, 1, 2)) then
if tonumber(minute) == tonumber(string.sub(time, 4, 5)) then
Update()
updated = true
end
end
end
end
else
Wait(65000)
updated = false
end
Citizen.Wait(1000)
end
end)
function Update()
local link = 'http://api.weatherapi.com/v1/current.json?key=' .. Config.APIkey .. '&q=' .. Config.City
PerformHttpRequest(link, function(err, result, headers)
if checkErrorCode(err) then
local r = '[ ' .. result .. ' ]'
local rt = json.decode(r)
local hour = tonumber(string.sub(rt[1].current.last_updated, 12, 13))
for i=1, #Config.Weathers, 1 do
if Config.Weathers[i].code == rt[1].current.condition.code then
if hour >= Config.NightStart or hour <= Config.NightEnd then
TriggerClientEvent('some_weathersync:UpdateWeather', -1, Config.Weathers[i].night)
else
TriggerClientEvent('some_weathersync:UpdateWeather', -1, Config.Weathers[i].day)
end
end
end
end
end)
end
function checkErrorCode(err)
if err == 401 then
print('Your api key is wrong! Please fix it')
return false
elseif err == 400 then
print('City name is wrong! Please fix it')
return false
elseif err == 403 then
print('Your api key has exceeded calls per month quota')
return false
elseif err == 403 then
print('Your api key has been disabled!')
return false
else
return true
end
end
RegisterCommand('syncWeather', function(source, args, raw)
if checkPermissions(source) then
Update()
print('weather synced')
end
end)
RegisterCommand('stopWeatherSync', function(source, args, raw)
if checkPermissions(source) then
syncing = false
print('syncing stopped')
end
end)
RegisterCommand('resumeWeatherSync', function(source, args, raw)
if checkPermissions(source) then
syncing = true
Update()
print('syncing resumed')
end
end)
RegisterCommand('setSyncingCity', function(source, args, raw)
if checkPermissions(source) then
if args[1] then
local cityName = ''
if #args == 1 then
cityName = args[1]
else
for i=1, #args-1, 1 do
cityName = cityName .. args[i] .. '_'
end
cityName = cityName .. args[#args]
end
Config.City = cityName
Update()
print('New city is - ' .. Config.City)
else
print('City name is incorrect. Skipped')
end
end
end)
function checkPermissions(src)
return IsPlayerAceAllowed(src, "weatherSyncSettings")
end
RegisterServerEvent('some_weathersync:beginUpdate')
AddEventHandler('some_weathersync:beginUpdate', function(source)
Update()
end)