forked from snyx95/esx_eden_garage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
183 lines (142 loc) · 4.81 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
RegisterServerEvent('eden_garage:debug')
RegisterServerEvent('eden_garage:modifystate')
RegisterServerEvent('eden_garage:pay')
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
--Recupere les véhicules
ESX.RegisterServerCallback('eden_garage:getVehicles', function(source, cb)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local vehicules = {}
MySQL.Async.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier",{['@identifier'] = xPlayer.getIdentifier()}, function(data)
for _,v in pairs(data) do
local vehicle = json.decode(v.vehicle)
table.insert(vehicules, {vehicle = vehicle, state = v.state})
end
cb(vehicules)
end)
end)
-- Fin --Recupere les véhicules
--Stock les véhicules
ESX.RegisterServerCallback('eden_garage:stockv',function(source,cb, vehicleProps)
local isFound = false
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local vehicules = getPlayerVehicles(xPlayer.getIdentifier())
local plate = vehicleProps.plate
for _,v in pairs(vehicules) do
if(plate == v.plate)then
local idveh = v.id
local vehprop = json.encode(vehicleProps)
MySQL.Sync.execute("UPDATE owned_vehicles SET vehicle =@vehprop WHERE id=@id",{['@vehprop'] = vehprop, ['@id'] = v.id})
isFound = true
break
end
end
cb(isFound)
end)
--Fin stock les vehicules
--Change le state du véhicule
AddEventHandler('eden_garage:modifystate', function(vehicleProps, state)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local vehicules = getPlayerVehicles(xPlayer.getIdentifier())
local plate = vehicleProps.plate
local state = state
for _,v in pairs(vehicules) do
if(plate == v.plate)then
local idveh = v.id
MySQL.Sync.execute("UPDATE owned_vehicles SET state =@state WHERE id=@id",{['@state'] = state , ['@id'] = v.id})
break
end
end
end)
--Fin change le state du véhicule
--Fonction qui récupere les plates
-- Fin Fonction qui récupere les plates
ESX.RegisterServerCallback('eden_garage:getOutVehicles',function(source, cb)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local vehicules = {}
MySQL.Async.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier AND state=false",{['@identifier'] = xPlayer.getIdentifier()}, function(data)
for _,v in pairs(data) do
local vehicle = json.decode(v.vehicle)
table.insert(vehicules, vehicle)
end
cb(vehicules)
end)
end)
--Foonction qui check l'argent
ESX.RegisterServerCallback('eden_garage:checkMoney', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer.get('money') >= Config.Price then
cb(true)
else
cb(false)
end
end)
--Fin Foonction qui check l'argent
--fonction qui retire argent
AddEventHandler('eden_garage:pay', function()
local xPlayer = ESX.GetPlayerFromId(source)
xPlayer.removeMoney(Config.Price)
TriggerClientEvent('esx:showNotification', source, 'Vous avez payé ' .. Config.Price)
end)
--Fin fonction qui retire argent
--Recupere les vehicules
function getPlayerVehicles(identifier)
local vehicles = {}
local data = MySQL.Sync.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier",{['@identifier'] = identifier})
for _,v in pairs(data) do
local vehicle = json.decode(v.vehicle)
table.insert(vehicles, {id = v.id, plate = vehicle.plate})
end
return vehicles
end
--Fin Recupere les vehicules
--Debug
AddEventHandler('eden_garage:debug', function(var)
print(to_string(var))
end)
function table_print (tt, indent, done)
done = done or {}
indent = indent or 0
if type(tt) == "table" then
local sb = {}
for key, value in pairs (tt) do
table.insert(sb, string.rep (" ", indent)) -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
table.insert(sb, "{\n");
table.insert(sb, table_print (value, indent + 2, done))
table.insert(sb, string.rep (" ", indent)) -- indent it
table.insert(sb, "}\n");
elseif "number" == type(key) then
table.insert(sb, string.format("\"%s\"\n", tostring(value)))
else
table.insert(sb, string.format(
"%s = \"%s\"\n", tostring (key), tostring(value)))
end
end
return table.concat(sb)
else
return tt .. "\n"
end
end
function to_string( tbl )
if "nil" == type( tbl ) then
return tostring(nil)
elseif "table" == type( tbl ) then
return table_print(tbl)
elseif "string" == type( tbl ) then
return tbl
else
return tostring(tbl)
end
end
--Fin Debug
-- Fonction qui change les etats sorti en rentré lors d'un restart
AddEventHandler('onMySQLReady', function()
MySQL.Sync.execute("UPDATE owned_vehicles SET state=true WHERE state=false", {})
end)
-- Fin Fonction qui change les etats sorti en rentré lors d'un restart