-
Notifications
You must be signed in to change notification settings - Fork 29
/
server.lua
102 lines (92 loc) · 2.59 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
-- Make sure you set this convar:
-- set es_enableCustomData 1
AddEventHandler('es_db:doesUserExist', function(identifier, callback)
MySQL.Async.fetchScalar('SELECT COUNT(1) FROM users WHERE identifier = @identifier', { ['@identifier'] = identifier }, function(users)
if users > 0 then
callback(true)
else
callback(false)
end
end)
end)
AddEventHandler('es_db:retrieveUser', function(identifier, callback)
local Callback = callback
MySQL.Async.fetchAll('SELECT * FROM users WHERE `identifier`=@identifier;', {identifier = identifier}, function(users)
if users[1] then
Callback(users[1])
else
Callback(false)
end
end)
end)
AddEventHandler('es_db:createUser', function(identifier, license, cash, bank, callback)
local user = {
identifier = identifier,
money = cash or 0,
bank = bank or 0,
license = license,
group = 'user',
permission_level = 0
}
MySQL.Async.execute('INSERT INTO users (`identifier`, `money`, `bank`, `group`, `permission_level`, `license`) VALUES (@identifier, @money, @bank, @group, @permission_level, @license);',
{
identifier = user.identifier,
money = user.money,
bank = user.bank,
permission_level = user.permission_level,
group = user.group,
license = user.license
}, function(rowsChanged)
callback()
end)
end)
AddEventHandler('es_db:retrieveLicensedUser', function(license, callback)
MySQL.Async.fetchAll('SELECT * FROM users WHERE `license`=@license;', {license = license}, function(users)
if users[1] then
callback(users[1])
else
callback(false)
end
end)
end)
AddEventHandler('es_db:doesLicensedUserExist', function(license, callback)
MySQL.Async.fetchScalar('SELECT COUNT(1) FROM users WHERE license = @license', { ['@license'] = license }, function(users)
if users > 0 then
callback(true)
else
callback(false)
end
end)
end)
AddEventHandler('es_db:updateUser', function(identifier, new, callback)
Citizen.CreateThread(function()
--if(#new ~= 0)then
local updateString = ''
local params = {identifier = identifier}
local length = tLength(new)
local cLength = 1
for k,v in pairs(new) do
if (type(k) == 'string') then
updateString = updateString .. '`' .. k .. '`=@' .. k
params[k] = v
if cLength < length then
updateString = updateString .. ', '
end
end
cLength = cLength + 1
end
MySQL.Async.execute('UPDATE users SET ' .. updateString .. ' WHERE `identifier`=@identifier', params, function(rowsChanged)
if callback then
callback(true)
end
end)
--end
end)
end)
function tLength(t)
local l = 0
for k,v in pairs(t)do
l = l + 1
end
return l
end