-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathserver.lua
150 lines (112 loc) · 4.83 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
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
ESX.RegisterServerCallback('esx_documents:submitDocument', function(source, cb, data)
local xPlayer = ESX.GetPlayerFromId(source)
local db_form = nil;
local _data = data;
--local owners = {}
--table.insert(owners, playerId)
--print(data)
--local status_data = { item = "lamp", x = lamp_position.x, y = lamp_position.y, z = lamp_position.z, heading = lamp_heading }
MySQL.Async.insert("INSERT INTO user_documents (owner, data) VALUES (@owner, @data)", {['@owner'] = xPlayer.identifier, ['@data'] = json.encode(data)}, function(id)
if id ~= nil then
MySQL.Async.fetchAll('SELECT * FROM user_documents where id = @id', {['@id']=id}, function (result)
--print("Trying to dump: " .. dump(result))
if(result[1] ~= nil) then
db_form = result[1]
db_form.data = json.decode(result[1].data)
cb(db_form)
end
end)
else
cb(db_form)
end
end)
end)
ESX.RegisterServerCallback('esx_documents:deleteDocument', function(source, cb, id)
local db_document = nil;
local xPlayer = ESX.GetPlayerFromId(source)
MySQL.Async.execute('DELETE FROM user_documents WHERE id = @id AND owner = @owner',
{
['@id'] = id,
['@owner'] = xPlayer.identifier
}, function(rowsChanged)
if rowsChanged >= 1 then
TriggerClientEvent('esx:showNotification', source, _U('document_deleted'))
cb(true)
else
TriggerClientEvent('esx:showNotification', source, _U('document_delete_failed'))
cb(false)
end
end)
end)
ESX.RegisterServerCallback('esx_documents:getPlayerDocuments', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
local forms = {}
if xPlayer ~= nil then
MySQL.Async.fetchAll("SELECT * FROM user_documents WHERE owner = @owner", {['@owner'] = xPlayer.identifier}, function(result)
if #result > 0 then
for i=1, #result, 1 do
local tmp_result = result[i]
tmp_result.data = json.decode(result[i].data)
table.insert(forms, tmp_result)
--print(dump(tmp_result))
end
cb(forms)
end
end)
end
end)
ESX.RegisterServerCallback('esx_documents:getPlayerDetails', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
local cb_data = nil
MySQL.Async.fetchAll("SELECT firstname, lastname, dateofbirth FROM users WHERE identifier = @owner", {['@owner'] = xPlayer.identifier}, function(result)
if result[1] ~= nil then
cb_data = result[1]
cb(cb_data)
else
cb(cb_data)
end
end)
end)
RegisterServerEvent('esx_documents:ShowToPlayer')
AddEventHandler('esx_documents:ShowToPlayer', function(targetID, aForm)
TriggerClientEvent('esx_documents:viewDocument', targetID, aForm)
end)
RegisterServerEvent('esx_documents:CopyToPlayer')
AddEventHandler('esx_documents:CopyToPlayer', function(targetID, aForm)
local _source = source
local _targetid = ESX.GetPlayerFromId(targetID).source
local targetxPlayer = ESX.GetPlayerFromId(_targetid)
local _aForm = aForm
MySQL.Async.insert("INSERT INTO user_documents (owner, data) VALUES (@owner, @data)", {['@owner'] = targetxPlayer.identifier, ['@data'] = json.encode(aForm)}, function(id)
if id ~= nil then
MySQL.Async.fetchAll('SELECT * FROM user_documents where id = @id', {['@id']=id}, function (result)
--print("Trying to dump: " .. dump(result))
if(result[1] ~= nil) then
db_form = result[1]
db_form.data = json.decode(result[1].data)
TriggerClientEvent('esx_documents:copyForm', _targetid, db_form)
TriggerClientEvent('esx:showNotification', _targetid, _U('copy_from_player'))
TriggerClientEvent('esx:showNotification', _source, _U('from_copied_player'))
else
TriggerClientEvent('esx:showNotification', _source, _U('could_not_copy_form_player'))
end
end)
else
TriggerClientEvent('esx:showNotification', _source, _U('could_not_copy_form_player'))
end
end)
end)
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end