Repository navigation
Expand file tree
/
Copy pathserver.lua
More file actions
72 lines (62 loc) · 2.79 KB
/
Copy pathserver.lua
File metadata and controls
72 lines (62 loc) · 2.79 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
local ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
ESX.RegisterServerCallback('drugsystem:getPlayerPlants', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
local identifier = xPlayer.identifier
MySQL.Async.fetchAll('SELECT * FROM drug_plants WHERE owner = @owner', {
['@owner'] = identifier
}, function(result)
cb(result)
end)
end)
RegisterServerEvent('drugsystem:plant')
AddEventHandler('drugsystem:plant', function(plantType, plantCoords)
local xPlayer = ESX.GetPlayerFromId(source)
local identifier = xPlayer.identifier
MySQL.Async.execute('INSERT INTO drug_plants (owner, type, plant_time, grow_time, harvest_time, position) VALUES (@owner, @type, @plant_time, @grow_time, @harvest_time, @position)', {
['@owner'] = identifier,
['@type'] = plantType,
['@plant_time'] = os.time(),
['@grow_time'] = os.time() + Config.Drugs[plantType].growTime / 1000,
['@harvest_time'] = os.time() + (Config.Drugs[plantType].growTime + Config.Drugs[plantType].harvestTime) / 1000,
['@position'] = json.encode(plantCoords)
}, function(insertId)
TriggerClientEvent('drugsystem:plantSuccess', source, insertId, plantType, plantCoords)
end)
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)
MySQL.Async.fetchAll('SELECT * FROM drug_plants', {}, function(result)
for _, plant in pairs(result) do
local currentTime = os.time()
local plantTime = plant.plant_time
local growTime = plant.grow_time
local harvestTime = plant.harvest_time
if currentTime >= growTime and currentTime < harvestTime then
TriggerClientEvent('drugsystem:updatePlant', -1, plant.id, plant.type, json.decode(plant.position), 'uv')
elseif currentTime >= harvestTime then
TriggerClientEvent('drugsystem:updatePlant', -1, plant.id, plant.type, json.decode(plant.position), 'crate')
end
end
end)
end
end)
RegisterServerEvent('drugsystem:harvestPlant')
AddEventHandler('drugsystem:harvestPlant', function(plantId)
local xPlayer = ESX.GetPlayerFromId(source)
local identifier = xPlayer.identifier
MySQL.Async.fetchScalar('SELECT type FROM drug_plants WHERE id = @id AND owner = @owner', {
['@id'] = plantId,
['@owner'] = identifier
}, function(plantType)
if plantType then
MySQL.Async.execute('DELETE FROM drug_plants WHERE id = @id', {
['@id'] = plantId
}, function()
TriggerClientEvent('drugsystem:harvest', source, plantId)
xPlayer.addInventoryItem(plantType, 1)
end)
end
end)
end)