-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJAM_Garage_Server.lua
More file actions
112 lines (93 loc) · 3.67 KB
/
Copy pathJAM_Garage_Server.lua
File metadata and controls
112 lines (93 loc) · 3.67 KB
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
local JAG = JAM.Garage
function JAG:GetPlayerVehicles(identifier)
local playerVehicles = {}
local data = MySQL.Sync.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier",{['@identifier'] = identifier})
for key,val in pairs(data) do
if (not val.job or val.job == nil) and (val.type and val.type == "car") then
local playerVehicle = json.decode(val.vehicle)
table.insert(playerVehicles, {owner = val.owner, veh = val.vehicle, vehicle = playerVehicle, plate = val.plate, state = val.jamstate})
end
end
return playerVehicles
end
ESX.RegisterServerCallback('JAG:StoreVehicle', function(source, cb, vehicleProps)
local isFound = false
local xPlayer = ESX.GetPlayerFromId(source)
while not xPlayer do
xPlayer = ESX.GetPlayerFromId(source)
Citizen.Wait(0)
end
local playerVehicles = JAG:GetPlayerVehicles(xPlayer.getIdentifier())
local plate = vehicleProps.plate
for key,val in pairs(playerVehicles) do
if(plate == val.plate) then
local vehProps = json.encode(vehicleProps)
MySQL.Sync.execute("UPDATE owned_vehicles SET vehicle=@vehProps WHERE plate=@plate",{['@vehProps'] = vehProps, ['@plate'] = val.plate})
isFound = true
break
end
end
cb(isFound)
end)
ESX.RegisterServerCallback('JAG:GetVehicles', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
while not xPlayer do
xPlayer = ESX.GetPlayerFromId(source)
Citizen.Wait(0)
end
local vehicles = JAG:GetPlayerVehicles(xPlayer.getIdentifier())
cb(vehicles)
end)
RegisterNetEvent('JAG:FinePlayer')
AddEventHandler('JAG:FinePlayer', function(amount)
local xPlayer = ESX.GetPlayerFromId(source)
while not xPlayer do
xPlayer = ESX.GetPlayerFromId(source)
Citizen.Wait(0)
end
xPlayer.removeMoney(amount)
end)
RegisterNetEvent('JAG:ChangeState')
AddEventHandler('JAG:ChangeState', function(plate, state)
local xPlayer = ESX.GetPlayerFromId(source)
while not xPlayer do
xPlayer = ESX.GetPlayerFromId(source)
Citizen.Wait(0)
end
local vehicles = JAG:GetPlayerVehicles(xPlayer.getIdentifier())
for key,val in pairs(vehicles) do
if(plate == val.plate) then
MySQL.Sync.execute("UPDATE owned_vehicles SET jamstate=@state WHERE plate=@plate",{['@state'] = state , ['@plate'] = plate})
break
end
end
end)
function JAG.Startup()
while not JAM.SQLReady do
Citizen.Wait(0)
end
local dbconvar = GetConvar('mysql_connection_string', 'Empty')
if dbconvar == "Empty" then print("JAG.Startup(): Error: local dbconvar is empty."); return; end
local strStart,strEnd = string.find(dbconvar, "database=")
local dbStart,dbEnd = string.find(dbconvar,";",strEnd)
local dbName = string.sub(dbconvar, strEnd + 1, dbEnd - 1)
local dbconfig =
{
["@dbname@"] = dbName,
["@dbtable@"] = "owned_vehicles",
["@dbfield@"] = "jamstate",
["@dbfieldconf@"] = "int(11) NOT NULL DEFAULT 0",
}
local query1 = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA ='@dbname@' and COLUMN_NAME='@dbfield@' and TABLE_NAME='@dbtable@';"
local query2 = "ALTER TABLE `@dbtable@` ADD COLUMN `@dbfield@` @dbfieldconf@;"
local curquery1 = JAG.Replace(dbconfig,query1)
local curquery2 = JAG.Replace(dbconfig,query2)
local data = MySQL.Sync.fetchAll( curquery1 )
if #data == 0 then MySQL.Sync.fetchAll( curquery2 ); end;
end
function JAG.Replace(c,q)
for repThis,repWith in pairs(type(c) == "table" and c or {}) do q = tostring(q):gsub(repThis,repWith); end;
return q
end
RegisterNetEvent('JAG:Startup')
AddEventHandler('JAG:Startup', JAG.Startup)