Repository navigation
Expand file tree
/
Copy pathserver.lua
More file actions
60 lines (54 loc) · 2.16 KB
/
Copy pathserver.lua
File metadata and controls
60 lines (54 loc) · 2.16 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
local ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
ESX.RegisterServerCallback('adoptme:getPlayerPets', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
MySQL.Async.fetchAll('SELECT * FROM adoptme_pets WHERE owner = @owner', {
['@owner'] = xPlayer.identifier
}, function(result)
cb(result)
end)
end)
RegisterNetEvent('adoptme:buyPet')
AddEventHandler('adoptme:buyPet', function(petModel)
local xPlayer = ESX.GetPlayerFromId(source)
local petPrice = 0
for _, pet in ipairs(Config.Pets) do
if pet.model == petModel then
petPrice = pet.price
break
end
end
if xPlayer.getMoney() >= petPrice then
xPlayer.removeMoney(petPrice)
MySQL.Async.execute('INSERT INTO adoptme_pets (owner, pet_name, pet_model) VALUES (@owner, @pet_name, @pet_model)', {
['@owner'] = xPlayer.identifier,
['@pet_name'] = 'Pet',
['@pet_model'] = petModel
}, function(rowsChanged)
TriggerClientEvent('adoptme:spawnPet', source, petModel)
end)
else
TriggerClientEvent('esx:showNotification', source, 'You do not have enough money!')
end
end)
RegisterNetEvent('adoptme:tradePet')
AddEventHandler('adoptme:tradePet', function(targetId, petId)
local xPlayer = ESX.GetPlayerFromId(source)
local xTarget = ESX.GetPlayerFromId(targetId)
MySQL.Async.fetchScalar('SELECT COUNT(*) FROM adoptme_pets WHERE id = @id AND owner = @owner', {
['@id'] = petId,
['@owner'] = xPlayer.identifier
}, function(count)
if count > 0 then
MySQL.Async.execute('UPDATE adoptme_pets SET owner = @new_owner WHERE id = @id', {
['@new_owner'] = xTarget.identifier,
['@id'] = petId
}, function(rowsChanged)
TriggerClientEvent('esx:showNotification', source, 'Pet traded successfully!')
TriggerClientEvent('esx:showNotification', targetId, 'You received a pet!')
end)
else
TriggerClientEvent('esx:showNotification', source, 'You do not own this pet!')
end
end)
end)