-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.lua
More file actions
236 lines (189 loc) · 7.64 KB
/
server.lua
File metadata and controls
236 lines (189 loc) · 7.64 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
local activeCalls = {}
local activePayphones = {}
local QBCore = nil
local ESX = nil
Citizen.CreateThread(function()
if Config.Framework == "esx" then
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
elseif Config.Framework == "esxnew" then
ESX = exports["es_extended"]:getSharedObject()
elseif Config.Framework == "qbcore" then
QBCore = exports['qb-core']:GetCoreObject()
end
end)
function ResetPlayerCallState(src)
if not activeCalls[src] then return end
Config.PhoneIntegration.EndCall(src)
if activeCalls[src].payphoneCoords then
activePayphones[activeCalls[src].payphoneCoords] = nil
end
activeCalls[src] = nil
TriggerClientEvent('src-payphone:callEnded', src)
end
function GetPlayerPhoneNumber(src)
local playerNumber = nil
local success = false
success, playerNumber = pcall(function()
return Config.PhoneIntegration.GetPhoneNumber(src)
end)
if not success or not playerNumber then
playerNumber = "000-0000"
end
return playerNumber
end
---@param source number - player id
---@param amount number - amount of money to remove
---@param removeHandler string - handler to remove money
---@return boolean - if money was removed
lib.callback.register('src-payphone:removeMoney', function(source, amount, removeHandler)
if removeHandler == "ox_inventory" then
return exports.ox_inventory:RemoveItem(source, "cash", amount)
elseif removeHandler == "esx" or removeHandler == "esxnew" then
if ESX then
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer then
xPlayer.removeMoney(amount)
return true --removeMoney in esx dont return anything
end
end
elseif removeHandler == "qbcore" then
if QBCore then
local Player = QBCore.Functions.GetPlayer(source)
if Player then
return Player.Functions.RemoveMoney('cash', amount, "payphone-call")
end
end
elseif removeHandler == "qbox" then
local Player = exports.qbx_core:GetPlayer(source)
if Player then
return Player.Functions.RemoveMoney('cash', amount, "payphone-call")
end
else
warn("[src-payphone] Create standalone implementation for removeMoney. Money not removed")
return true
end
end)
RegisterNetEvent('src-payphone:startCall')
AddEventHandler('src-payphone:startCall', function(number, company, payphoneCoords)
local src = source
if activePayphones[payphoneCoords] and activePayphones[payphoneCoords] ~= src then
TriggerClientEvent('src-payphone:notifyClient', src, _('phone_unavailable'), 'error')
return
end
activePayphones[payphoneCoords] = src
if activeCalls[src] then
ResetPlayerCallState(src)
Citizen.Wait(500)
end
local callId = Config.PhoneIntegration.CreateCall(src, number, company)
if not callId then
TriggerClientEvent('src-payphone:callEnded', src)
activePayphones[payphoneCoords] = nil
return
end
activeCalls[src] = {
callId = callId,
number = number,
company = company,
active = true,
answered = false,
started = os.time(),
nextPaymentDue = 0,
timeUntilNextPayment = 0,
payphoneCoords = payphoneCoords
}
TriggerClientEvent('src-payphone:callStatus', src, activeCalls[src])
Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)
if not DoesPlayerExist(src) then
ResetPlayerCallState(src)
break
end
local inCall = Config.PhoneIntegration.IsInCall(src)
if not inCall then
ResetPlayerCallState(src)
break
end
if activeCalls[src] then
local call = Config.PhoneIntegration.GetCall(activeCalls[src].callId)
if call then
if call.answered and not activeCalls[src].answered then
activeCalls[src].answered = true
activeCalls[src].nextPaymentDue = os.time() + Config.CheckPaymentInterval
end
if activeCalls[src].answered then
activeCalls[src].timeUntilNextPayment = activeCalls[src].nextPaymentDue - os.time()
if activeCalls[src].timeUntilNextPayment <= 0 then
local success = lib.callback.await('src-payphone:requestPayment', src, Config.CallCostPer30Seconds)
if not success then
ResetPlayerCallState(src)
break
end
activeCalls[src].nextPaymentDue = os.time() + Config.CheckPaymentInterval
activeCalls[src].timeUntilNextPayment = Config.CheckPaymentInterval
end
end
TriggerClientEvent('src-payphone:callStatus', src, activeCalls[src])
else
ResetPlayerCallState(src)
break
end
else
break
end
end
end)
end)
RegisterNetEvent('src-payphone:endCall')
AddEventHandler('src-payphone:endCall', function()
local src = source
ResetPlayerCallState(src)
end)
RegisterNetEvent('src-payphone:checkCallStatus')
AddEventHandler('src-payphone:checkCallStatus', function()
local src = source
local inCall = Config.PhoneIntegration.IsInCall(src)
TriggerClientEvent('src-payphone:callStatusCheck', src, inCall)
if not inCall and activeCalls[src] then
ResetPlayerCallState(src)
end
end)
AddEventHandler('playerDropped', function()
local src = source
ResetPlayerCallState(src)
end)
function DoesPlayerExist(playerId)
return GetPlayerPing(playerId) > 0
end
---@param src number - player id
---@return table - contacts
lib.callback.register('src-payphone:getContacts', function(src)
local playerNumber = GetPlayerPhoneNumber(src)
if not playerNumber or playerNumber == "000-0000" then
return {}
end
return MySQL.query.await('SELECT * FROM ' .. Config.DatabaseTable.Contacts .. ' WHERE ' .. Config.DatabaseTable.PhoneNumber .. ' = ? ORDER BY favourite DESC, firstname ASC', { playerNumber }) or {}
end)
RegisterNetEvent('src-payphone:forceReset')
AddEventHandler('src-payphone:forceReset', function()
local src = source
ResetPlayerCallState(src)
end)
RegisterNetEvent('src-payphone:checkPayphoneAvailability')
AddEventHandler('src-payphone:checkPayphoneAvailability', function(coords, company)
local src = source
local isAvailable = not activePayphones[coords]
TriggerClientEvent('src-payphone:payphoneAvailabilityResult', src, isAvailable, company)
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(5000)
for src, _ in pairs(activeCalls) do
local inCall = Config.PhoneIntegration.IsInCall(src)
if not inCall then
ResetPlayerCallState(src)
end
end
end
end)