-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.lua
241 lines (207 loc) · 8.99 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
for item, vehData in pairs(Config.Vehicles) do
ESX.RegisterUsableItem(item, function(source)
TriggerClientEvent('msk_givevehicle:giveVehicle', source, item, vehData)
end)
end
DoesVehicleWithPlateExist = function(plate)
local alreadyExists = MySQL.scalar.await('SELECT COUNT(plate) FROM owned_vehicles WHERE plate = @plate', {
['@plate'] = MSK.String.Trim(plate)
}) > 0
return alreadyExists
end
RegisterServerEvent('msk_givevehicle:setVehicle', function(item, props, vehicleType)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local alreadyExists = DoesVehicleWithPlateExist(props.plate)
if alreadyExists then
return Config.Notification(src, Translation[Config.Locale]['item_already_exist'], 'error') -- Plate already exist
end
MySQL.query('INSERT INTO owned_vehicles (owner, plate, vehicle, stored, type) VALUES (@owner, @plate, @vehicle, @stored, @type)', {
['@owner'] = xPlayer.identifier,
['@plate'] = MSK.String.Trim(props.plate),
['@vehicle'] = json.encode(props),
['@stored'] = 1,
['type'] = vehicleType
})
if (GetResourceState("msk_vehiclekeys") == "started") then
exports.msk_vehiclekeys:AddPrimaryKey({source = src}, {plate = MSK.String.Trim(props.plate), model = props.model})
end
xPlayer.removeInventoryItem(item, 1)
Config.Notification(src, Translation[Config.Locale]['item_success'], 'success')
end)
----------------------------------------------------------------
-- Ingame Commands
----------------------------------------------------------------
ESX.RegisterCommand(Config.Commands.giveveh, Config.AdminGroups, function(xPlayer, args, showError) -- /giveveh <playerID> <categorie> <carModel> <plate>
TriggerClientEvent('msk_givevehicle:giveVehicleCommand', xPlayer.source, args.player, args.type, args.vehicle, args.plate)
end, false, {help = 'Give someone a Vehicle', arguments = {
{name = 'player', help = 'PlayerID', type = 'player'},
{name = 'type', help = 'Categorie', type = 'string'},
{name = 'vehicle', help = 'Vehiclename', type = 'string'},
{name = 'plate', help = '"Plate" (optional)', type = 'string'}
}})
ESX.RegisterCommand(Config.Commands.delveh, Config.AdminGroups, function(xPlayer, args, showError) -- /delveh <plate>
if args.plate then
MySQL.query('DELETE FROM owned_vehicles WHERE plate = @plate', {
['@plate'] = args.plate
}, function(result)
if result.affectedRows == 1 then
logging('debug', 'Deleted: ' .. args.plate)
Config.Notification(src, Translation[Config.Locale]['deleted']:format(args.plate))
else
logging('debug', 'Error while deleting: ' .. args.plate)
Config.Notification(src, Translation[Config.Locale]['delete_failed']:format(args.plate))
end
end)
end
end, false, {help = 'Delete a Vehicle from Database', arguments = {
{name = 'plate', help = '"Plate"', type = 'string'}
}})
ESX.RegisterCommand(Config.Commands.givejobveh, Config.AdminGroups, function(xPlayer, args, showError) -- /givejobveh <playerID> <categorie> <carModel> <job> <bool> <plate>
TriggerClientEvent('msk_givevehicle:giveVehicleCommand', xPlayer.source, args.player, args.type, args.vehicle, args.plate, nil, args.job, args.identifier)
end, false, {help = 'Give someone a Vehicle', arguments = {
{name = 'player', help = 'PlayerID', type = 'player'},
{name = 'type', help = 'Categorie', type = 'string'},
{name = 'vehicle', help = 'Vehiclename', type = 'string'},
{name = 'job', help = 'Job', type = 'string'},
{name = 'identifier', help = 'Identifier', type = 'number'},
{name = 'plate', help = '"Plate" (optional)', type = 'string'}
}})
-- You can use this command at the console too
ESX.RegisterCommand(Config.Commands.spawnveh, Config.AdminGroups, function(xPlayer, args, showError) -- /spawnveh <playerID> <plate>
MySQL.query("SELECT * FROM owned_vehicles WHERE plate = @plate", {
['@plate'] = args.plate
}, function(result)
if result and result[1] then
TriggerClientEvent('msk_givevehicle:spawnVehicle', args.player.source, json.decode(result[1].vehicle))
MySQL.query("UPDATE owned_vehicles SET stored = 0 WHERE plate = @plate", {['@plate'] = args.plate})
end
end)
end, true, {help = 'Spawn Owned Vehicle by Plate', arguments = {
{name = 'player', help = 'PlayerID', type = 'player'},
{name = 'plate', help = '"Plate"', type = 'string'}
}})
----------------------------------------------------------------
-- Console Commands
----------------------------------------------------------------
RegisterCommand(Config.ConsoleCommands.giveveh, function(source, args, rawCommand) -- _giveveh <playerID> <categorie> <carModel> <plate>
if (source == 0) then
local playerID = args[1]
if args[1] and args[2] and args[3] and args[4] then
local plate = args[4]
if #args > 4 then
for i=5, #args do
plate = plate.." "..args[i]
end
end
plate = string.upper(plate)
TriggerClientEvent('msk_givevehicle:giveVehicleCommand', playerID, playerID, args[2], args[3], plate, true)
elseif args[1] and args[2] and args[3] then
TriggerClientEvent('msk_givevehicle:giveVehicleCommand', playerID, playerID, args[2], args[3], nil, true)
else
print('^1SYNTAX ERROR: ^5_giveveh <playerID> <categorie> <carModel> <plate> ^0| Plate is optional')
end
end
end)
RegisterCommand(Config.ConsoleCommands.delveh, function(source, args, rawCommand) -- _delveh <plate>
if (source == 0) then
if args[1] then
local plate = args[1]
if #args > 1 then
for i=2, #args do
plate = plate.." "..args[i]
end
end
plate = string.upper(plate)
MySQL.query('DELETE FROM owned_vehicles WHERE plate = @plate', {
['@plate'] = plate
}, function(result)
if result == 1 then
logging('debug', 'Deleted', plate)
else
logging('debug', 'Error while deleting', plate)
end
end)
else
print('^1SYNTAX ERROR: ^5_delveh <plate>')
end
end
end)
RegisterCommand(Config.ConsoleCommands.givejobveh, function(source, args, rawCommand) -- _givejobveh <playerID> <categorie> <carModel> <job> <bool> <plate>
if (source == 0) then
local playerID = args[1]
if args[1] and args[2] and args[3] and args[4] and args[5] and args[6] then
local plate = args[6]
if #args > 6 then
for i=7, #args do
plate = plate.." "..args[i]
end
end
plate = string.upper(plate)
TriggerClientEvent('msk_givevehicle:giveVehicleCommand', playerID, playerID, args[2], args[3], plate, true, args[4], args[5])
elseif args[1] and args[2] and args[3] and args[4] and args[5] and not args[6] then
TriggerClientEvent('msk_givevehicle:giveVehicleCommand', playerID, playerID, args[2], args[3], nil, true, args[4], args[5])
else
print('^1SYNTAX ERROR: ^5_givejobveh <playerID> <categorie> <carModel> <job> <bool> <plate> ^0| Plate is optional')
end
end
end)
RegisterServerEvent('msk_givevehicle:setVehicleCommand', function(xTarget, categorie, model, plate, props, console, job, bool)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local alreadyExists = DoesVehicleWithPlateExist(plate)
if console then
xTarget = ESX.GetPlayerFromId(xTarget)
end
if alreadyExists then
logging('debug', Translation[Config.Locale]['vehicle_already_exist']:format(plate))
if xPlayer and not console then
Config.Notification(src, Translation[Config.Locale]['vehicle_already_exist']:format(plate))
end
else
if job then
local identifier
if tostring(bool) == '1' then
identifier = job
elseif tostring(bool) == '0' then
identifier = xTarget.identifier
end
MySQL.query('INSERT INTO owned_vehicles (owner, plate, vehicle, stored, type, job) VALUES (@owner, @plate, @vehicle, @stored, @type, @job)', {
['@owner'] = identifier,
['@plate'] = plate,
['@vehicle'] = json.encode(props),
['@stored'] = 1,
['type'] = categorie,
['job'] = job
})
if (GetResourceState("msk_vehiclekeys") == "started") then
exports.msk_vehiclekeys:AddPrimaryKey({identifier = identifier}, {plate = MSK.String.Trim(plate), model = props.model})
end
else
MySQL.query('INSERT INTO owned_vehicles (owner, plate, vehicle, stored, type) VALUES (@owner, @plate, @vehicle, @stored, @type)', {
['@owner'] = xTarget.identifier,
['@plate'] = plate,
['@vehicle'] = json.encode(props),
['@stored'] = 1,
['type'] = categorie
})
if (GetResourceState("msk_vehiclekeys") == "started") then
exports.msk_vehiclekeys:AddPrimaryKey({identifier = xTarget.identifier}, {plate = MSK.String.Trim(plate), model = props.model})
end
end
logging('debug', Translation[Config.Locale]['vehicle_successfully_added']:format(model, plate, xTarget.source))
if xPlayer and not console then
Config.Notification(src, Translation[Config.Locale]['vehicle_successfully_added']:format(model, plate, xTarget.source))
if xPlayer.source == xTarget.source then
Config.Notification(src, Translation[Config.Locale]['got_vehicle']:format(model, plate))
end
end
if xPlayer and console then
Config.Notification(src, Translation[Config.Locale]['got_vehicle']:format(model, plate))
end
end
end)
logging = function(code, ...)
if not Config.Debug then return end
MSK.Logging(code, ...)
end