-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.lua
More file actions
184 lines (167 loc) · 7.6 KB
/
server.lua
File metadata and controls
184 lines (167 loc) · 7.6 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
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
184
local serverTentStates = {} -- ผู้ครองเต็นท์
local campfireFuel = {} -- % เชื้อเพลิง ต่อกองไฟ
local campfireLit = {} -- true/false ไฟติด-ดับ ต่อกองไฟ
-- ========== Tent (เดิม) ==========
RegisterNetEvent('camping:tentEntered')
AddEventHandler('camping:tentEntered', function(tentID)
local src = source
if serverTentStates[tentID] then
TriggerClientEvent('camping:notify', src, { title='Tent', description='This tent is already occupied.', type='error' })
else
serverTentStates[tentID] = src
TriggerClientEvent('camping:updateTentState', -1, tentID, true)
end
end)
RegisterNetEvent('camping:tentExited')
AddEventHandler('camping:tentExited', function(tentID)
local src = source
if serverTentStates[tentID] == src then
serverTentStates[tentID] = nil
TriggerClientEvent('camping:updateTentState', -1, tentID, false)
end
end)
AddEventHandler('playerDropped', function()
local src = source
for tentID, occupant in pairs(serverTentStates) do
if occupant == src then
serverTentStates[tentID] = nil
TriggerClientEvent('camping:updateTentState', -1, tentID, false)
end
end
end)
-- ========== Persistence ==========
RegisterNetEvent('camping:saveCampingData')
AddEventHandler('camping:saveCampingData', function(type, model, x, y, z, stashID, heading)
local query = "INSERT INTO camping (type, model, x, y, z, stashID, heading) VALUES (@type, @model, @x, @y, @z, @stashID, @heading)"
exports.oxmysql:insert(query, {
['@type']=type, ['@model']=model, ['@x']=x, ['@y']=y, ['@z']=z,
['@stashID']=stashID or '', ['@heading']=heading
})
end)
RegisterNetEvent('camping:LoadData')
AddEventHandler('camping:LoadData', function()
local src = source
local result = exports.oxmysql:executeSync("SELECT * FROM camping")
for _, data in ipairs(result) do
if data.type == 'campfire' then
if campfireFuel[data.stashID] == nil then campfireFuel[data.stashID] = 0 end
if campfireLit[data.stashID] == nil then campfireLit[data.stashID] = false end
end
TriggerClientEvent('camping:loadCampingData', src, data)
if data.type == 'campfire' then
TriggerClientEvent('camping:updateFuel', src, data.stashID, campfireFuel[data.stashID] or 0)
TriggerClientEvent('camping:updateLit', src, data.stashID, campfireLit[data.stashID] or false)
end
end
end)
RegisterNetEvent('camping:deleteCampingData')
AddEventHandler('camping:deleteCampingData', function(type, stashID)
local query = "DELETE FROM camping WHERE type = @type AND stashID = @stashID"
exports.oxmysql:execute(query, { ['@type']=type, ['@stashID']=stashID })
if type == 'campfire' then
campfireFuel[stashID] = nil
campfireLit[stashID] = nil
TriggerClientEvent('camping:updateFuel', -1, stashID, 0)
TriggerClientEvent('camping:updateLit', -1, stashID, false)
end
end)
-- ========== Items I/O ==========
RegisterNetEvent('camping:AI', function(itemName, amount, meta)
if not source then return end
exports.ox_inventory:AddItem(source, itemName, amount, meta or nil)
end)
RegisterNetEvent('camping:RI', function(itemName, amount, meta, slot)
if not source then return end
exports.ox_inventory:RemoveItem(source, itemName, amount, meta or nil, slot or nil)
end)
-- ========== Spawn ==========
RegisterNetEvent('camping:server:spawnTent', function(x,y,z,h,randomModel, stashId, slot)
exports.ox_inventory:RemoveItem(source, Config.tentItem, 1, nil, slot or nil)
TriggerClientEvent('camping:client:spawnTent', -1, x,y,z,h,randomModel, stashId)
end)
RegisterNetEvent('camping:server:spawnCampfire', function(x,y,z,h,fireModel,campfireId, slot)
exports.ox_inventory:RemoveItem(source, Config.campfireItem, 1, nil, slot or nil)
campfireFuel[campfireId] = campfireFuel[campfireId] or 0
campfireLit[campfireId] = false
TriggerClientEvent('camping:client:spawnCampfire', -1, x,y,z,h,fireModel,campfireId)
TriggerClientEvent('camping:updateFuel', -1, campfireId, campfireFuel[campfireId])
TriggerClientEvent('camping:updateLit', -1, campfireId, false)
end)
RegisterNetEvent('camping:createTentStash', function(stashId)
exports.ox_inventory:RegisterStash(stashId, "Tent", 10, 10000)
end)
RegisterNetEvent('camping:server:removeTentItem', function(tentId)
TriggerClientEvent('camping:client:removeTentItem', -1, tentId)
end)
RegisterNetEvent('camping:server:removeFireItem', function(fireId)
TriggerClientEvent('camping:client:removeFireItem', -1, fireId)
end)
-- ========== Fuel API (server-authoritative) ==========
lib.callback.register('camping:getFuel', function(_, campfireId)
return campfireFuel[campfireId] or 0
end)
lib.callback.register('camping:addFuel', function(_, campfireId, addPercent)
local cur = campfireFuel[campfireId] or 0
local newv = math.max(0, math.min(100, cur + (addPercent or 0)))
campfireFuel[campfireId] = newv
TriggerClientEvent('camping:updateFuel', -1, campfireId, newv)
return newv
end)
lib.callback.register('camping:tryConsumeFuel', function(_, campfireId, consumePercent)
local cur = campfireFuel[campfireId] or 0
consumePercent = consumePercent or 0
if cur >= consumePercent then
local newv = math.max(0, math.min(100, cur - consumePercent))
campfireFuel[campfireId] = newv
TriggerClientEvent('camping:updateFuel', -1, campfireId, newv)
if newv <= 0 and campfireLit[campfireId] then
campfireLit[campfireId] = false
TriggerClientEvent('camping:updateLit', -1, campfireId, false)
end
return { ok = true, fuel = newv }
else
return { ok = false, fuel = cur }
end
end)
-- ========== จุดไฟ / ดับไฟ ==========
RegisterNetEvent('camping:ignite', function(campfireId)
local src = source
local cur = campfireFuel[campfireId] or 0
if cur <= 0 then
return TriggerClientEvent('camping:notify', src, { title='Campfire', description='No fuel to ignite.', type='error' })
end
if not campfireLit[campfireId] then
campfireLit[campfireId] = true
TriggerClientEvent('camping:updateLit', -1, campfireId, true)
end
end)
RegisterNetEvent('camping:extinguish', function(campfireId)
if campfireLit[campfireId] then
campfireLit[campfireId] = false
TriggerClientEvent('camping:updateLit', -1, campfireId, false)
end
end)
-- ========== Auto-drain: ลดเชื้อเพลิง 0.1%/วิ เมื่อไฟติด ==========
CreateThread(function()
while true do
Wait(1000)
local drain = (Config and Config.fuelDrainPerSecond) or 0.1
if drain <= 0 then goto continue end
for id, lit in pairs(campfireLit) do
if lit then
local cur = campfireFuel[id] or 0
if cur > 0 then
local newv = cur - drain
if newv <= 0 then
newv = 0
campfireLit[id] = false
TriggerClientEvent('camping:updateLit', -1, id, false)
end
campfireFuel[id] = newv
TriggerClientEvent('camping:updateFuel', -1, id, newv)
end
end
end
::continue::
end
end)