refactor(esx_banking)

This commit is contained in:
P Gergő
2023-01-14 22:39:06 +01:00
parent 00c0006e93
commit c70cd30922
3 changed files with 257 additions and 259 deletions
+211 -213
View File
@@ -1,182 +1,242 @@
local ActivateBlips = {} local BANK = {
local PlayerLoaded = true Data = {}
local isInMarker, isInAtmMarker, isInMenu, isMarkerShowed = false, false, false, false }
local _GetEntityCoords, _PlayerPedId
-- Functions local activeBlips, bankPoints, atmPoints, markerPoints = {}, {}, {}, {}
local playerLoaded, uiActive, inMenu = false, false, false
-- Listen for keypress while player inside the marker --Functions
local function Listen4Key() -- General data collecting thread
CreateThread(function() function BANK:Thread()
while (isInMarker or isInAtmMarker) and not isInMenu do self:CreateBlips()
if IsControlJustReleased(0, 38) then local data = self.Data
OpenUi(isInAtmMarker) data.ped = PlayerPedId()
end data.coord = GetEntityCoords(data.Ped)
Wait(0) playerLoaded = true
end
end)
end
-- Create Blips CreateThread(function ()
local function CreateBlips() while playerLoaded do
local tmpActiveBlips = {} data.coord = GetEntityCoords(data.ped)
for i = 1, #Config.Banks do data.ped = PlayerPedId()
if type(Config.Banks[i].Blip) == 'table' and Config.Banks[i].Blip.Enabled then bankPoints, atmPoints, markerPoints = {}, {}, {}
local blip = AddBlipForCoord(Config.Banks[i].Position.xy)
SetBlipSprite(blip, Config.Banks[i].Blip.Sprite)
SetBlipScale(blip, Config.Banks[i].Blip.Scale)
SetBlipColour(blip, Config.Banks[i].Blip.Color)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName(Config.Banks[i].Blip.Label)
EndTextCommandSetBlipName(blip)
tmpActiveBlips[#tmpActiveBlips + 1] = blip
end
end
ActivateBlips = tmpActiveBlips if IsPedOnFoot(data.ped) and not inMenu then
end for i = 1, #Config.AtmModels do
local atm = GetClosestObjectOfType(data.coord.x, data.coord.y, data.coord.z, 0.7, Config.AtmModels[i], false, false, false)
if atm ~= 0 then
atmPoints[#atmPoints+1] = GetEntityCoords(atm)
end
end
-- Remove blips for i = 1, #Config.Banks do
local function RemoveBlips() local bankDistance = #(data.coord - Config.Banks[i].Position.xyz)
for i = 1, #ActivateBlips do if bankDistance <= 0.7 then
if DoesBlipExist(ActivateBlips[i]) then bankPoints[#bankPoints+1] = Config.Banks[i].Position.xyz
RemoveBlip(ActivateBlips[i]) end
end if Config.ShowMarker and bankDistance <= Config.DrawMarker then
end markerPoints[#markerPoints+1] = Config.Banks[i].Position.xyz
ActivateBlips = {}
end
function OpenUi(atm)
atm = atm or false
isInMenu = true
ESX.HideUI()
ESX.TriggerServerCallback('esx_banking:getPlayerData', function(data)
SendNUIMessage({
showMenu = true,
openATM = atm,
datas = {
your_money_panel = {
accountsData = {{
name = "cash",
amount = data.money
}, {
name = "bank",
amount = data.bankMoney
}}
},
bankCardData = {
bankName = TranslateCap('bank_name'),
cardNumber = "2232 2222 2222 2222",
createdDate = "08/08",
name = data.playerName
},
transactionsData = data.transactionHistory
}
})
end)
SetNuiFocus(true, true)
end
local function CloseUi()
SetNuiFocus(false, false)
isInMenu = false
SendNUIMessage({
showMenu = false
})
if (isInMarker or isInAtmMarker) then
ESX.TextUI(TranslateCap('press_e_banking'))
Listen4Key()
end
end
local function ShowMarker(coord)
CreateThread(function()
while isMarkerShowed do
DrawMarker(20, coord.x, coord.y, coord.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.2, 0.2, 187, 255, 0, 255, false, true, 2, false, nil, nil, false)
Wait(0)
end
end)
end
local function StartThread()
CreateThread(function()
CreateBlips()
while PlayerLoaded do
_PlayerPedId = PlayerPedId()
_GetEntityCoords = GetEntityCoords(_PlayerPedId)
local closestBank = {}
if IsPedOnFoot(PlayerPedId()) then
for i = 1, #Config.AtmModels do
local atm = GetClosestObjectOfType(_GetEntityCoords, 8.0, Config.AtmModels[i], false)
if atm ~= 0 then
local atmOffset = GetOffsetFromEntityInWorldCoords(atm, 0.0, -0.7, 0.0)
local atmDistance = #(_GetEntityCoords - atmOffset)
if not isInAtmMarker and atmDistance <= 1.5 then
isInAtmMarker = true
ESX.TextUI(TranslateCap('press_e_banking'))
Listen4Key()
elseif isInAtmMarker and atmDistance > 1.5 then
isInAtmMarker = false
ESX.HideUI()
end end
end end
end end
for i = 1, #Config.Banks do if next(bankPoints) and not uiActive then
local bankDistance = #(_GetEntityCoords - Config.Banks[i].Position.xyz) self:TextUi(true)
if bankDistance <= Config.DrawMarker then
closestBank = {Config.Banks[i].Position, bankDistance}
end
end end
if not isMarkerShowed and next(closestBank) then if next(atmPoints) and not uiActive then
isMarkerShowed = true self:TextUi(true, true)
ShowMarker(closestBank[1].xyz)
elseif isMarkerShowed and not next(closestBank) then
isMarkerShowed = false
end end
if next(closestBank) then if not next(bankPoints) and not next(atmPoints) and uiActive then
if not isInMarker and closestBank[2] <= 1.0 then self:TextUi(false)
isInMarker = true
ESX.TextUI(TranslateCap('press_e_banking'))
Listen4Key()
elseif isInMarker and closestBank[2] > 1.0 then
isInMarker = false
ESX.HideUI()
end
elseif not next(closestBank) and isInMarker then
isInMarker = false
ESX.HideUI()
end end
Wait(1000)
end end
Wait(1000) end)
end
end)
end
-- NuiCallbacks if not Config.ShowMarker then return end
CreateThread(function()
local wait = 1000
while playerLoaded do
if next(markerPoints) then
for i = 1, #markerPoints do
DrawMarker(20, markerPoints[i].x, markerPoints[i].y, markerPoints[i].z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.2, 0.2, 187, 255, 0, 255, false, true, 2, false, nil, nil, false)
end
wait = 0
end
Wait(wait)
end
end)
end
-- Handle text ui / Keypress
function BANK:TextUi(state, atm)
uiActive = state
if state then
ESX.TextUI(TranslateCap('press_e_banking'))
CreateThread(function()
while uiActive do
if IsControlJustReleased(0, 38) then
self:HandleUi(true, atm)
self:TextUi(false)
end
Wait(0)
end
end)
else
ESX.HideUI()
end
end
-- Create Blips
function BANK:CreateBlips()
local tmpActiveBlips = {}
for i = 1, #Config.Banks do
if type(Config.Banks[i].Blip) == 'table' and Config.Banks[i].Blip.Enabled then
local position = Config.Banks[i].Position
local bInfo = Config.Banks[i].Blip
local blip = AddBlipForCoord(position.x, position.y, position.z)
SetBlipSprite(blip, bInfo.Sprite)
SetBlipScale(blip, bInfo.Scale)
SetBlipColour(blip, bInfo.Color)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName(bInfo.Label)
EndTextCommandSetBlipName(blip)
tmpActiveBlips[#tmpActiveBlips + 1] = blip
end
end
activeBlips = tmpActiveBlips
end
-- Remove blips
function BANK:RemoveBlips()
for i = 1, #activeBlips do
if DoesBlipExist(activeBlips[i]) then
RemoveBlip(activeBlips[i])
end
end
activeBlips = {}
end
-- Open / Close ui
function BANK:HandleUi(state, atm)
atm = atm or false
SetNuiFocus(state, state)
inMenu = state
if state then
ESX.TriggerServerCallback('esx_banking:getPlayerData', function(data)
SendNUIMessage({
showMenu = true,
openATM = atm,
datas = {
your_money_panel = {
accountsData = {{
name = "cash",
amount = data.money
}, {
name = "bank",
amount = data.bankMoney
}}
},
bankCardData = {
bankName = TranslateCap('bank_name'),
cardNumber = "2232 2222 2222 2222",
createdDate = "08/08",
name = data.playerName
},
transactionsData = data.transactionHistory
}
})
end)
else
SendNUIMessage({
showMenu = false
})
end
end
function BANK:LoadNpc(index, netID)
CreateThread(function()
while not NetworkDoesEntityExistWithNetworkId(netID) do
Wait(200)
end
local npc = NetworkGetEntityFromNetworkId(netID)
TaskStartScenarioInPlace(npc, Config.Peds[index].Scenario, 0, true)
SetEntityProofs(npc, true, true, true, true, true, true, true, true)
SetBlockingOfNonTemporaryEvents(npc, true)
FreezeEntityPosition(npc, true)
SetPedCanRagdollFromPlayerImpact(npc, false)
SetPedCanRagdoll(npc, false)
SetEntityAsMissionEntity(npc, true, true)
SetEntityDynamic(npc, false)
end)
end
-- Events
RegisterNetEvent('esx_banking:closebanking', function()
BANK:HandleUi(false)
end)
RegisterNetEvent('esx_banking:pedHandler', function(netIdTable)
for i = 1, #netIdTable do
BANK:LoadNpc(i, netIdTable[i])
end
end)
RegisterNetEvent('esx_banking:updateMoneyInUI', function(doingType, bankMoney, money)
SendNUIMessage({
updateData = true,
data = {
type = doingType,
bankMoney = bankMoney,
money = money
}
})
end)
-- Handlers
-- Resource starting
AddEventHandler('onResourceStart', function(resource)
if resource ~= GetCurrentResourceName() then return end
BANK:Thread()
end)
-- Enable the script on player loaded
RegisterNetEvent('esx:playerLoaded', function()
BANK:Thread()
end)
-- Disable the script on player logout
RegisterNetEvent('esx:onPlayerLogout', function()
playerLoaded = false
end)
-- Resource stopping
AddEventHandler('onResourceStop', function(resource)
if resource ~= GetCurrentResourceName() then return end
BANK:RemoveBlips()
if uiActive then BANK:TextUi(false) end
end)
-- Nui Callbacks
RegisterNUICallback('close', function(data, cb) RegisterNUICallback('close', function(data, cb)
CloseUi() BANK:HandleUi(false)
cb('ok') cb('ok')
end) end)
RegisterNUICallback('clickButton', function(data, cb) RegisterNUICallback('clickButton', function(data, cb)
if data ~= nil and isInMenu then if data ~= nil and inMenu then
TriggerServerEvent("esx_banking:doingType", data) TriggerServerEvent("esx_banking:doingType", data)
end end
cb('ok') cb('ok')
end) end)
RegisterNUICallback('checkPincode', function(data, cb) RegisterNUICallback('checkPincode', function(data, cb)
if data ~= nil and isInMenu then if data ~= nil and inMenu then
ESX.TriggerServerCallback("esx_banking:checkPincode", function(pincode) ESX.TriggerServerCallback("esx_banking:checkPincode", function(pincode)
if pincode then if pincode then
cb({ cb({
@@ -193,65 +253,3 @@ RegisterNUICallback('checkPincode', function(data, cb)
end, data) end, data)
end end
end) end)
-- Events
RegisterNetEvent('esx_banking:closebanking', function()
CloseUi()
end)
local function loadNPC(index, netID)
CreateThread(function()
while not NetworkDoesEntityExistWithNetworkId(netID) do
Wait(200)
end
local npc = NetworkGetEntityFromNetworkId(netID)
TaskStartScenarioInPlace(npc, Config.Peds[index].Scenario, 0, true)
SetEntityProofs(npc, true, true, true, true, true, true, true, true)
SetBlockingOfNonTemporaryEvents(npc, true)
FreezeEntityPosition(npc, true)
SetPedCanRagdollFromPlayerImpact(npc, false)
SetPedCanRagdoll(npc, false)
SetEntityAsMissionEntity(npc, true, true)
SetEntityDynamic(npc, false)
end)
end
RegisterNetEvent('esx_banking:PedHandler', function(netIdTable)
for i = 1, #netIdTable do
loadNPC(i, netIdTable[i])
end
end)
RegisterNetEvent('esx_banking:updateMoneyInUI')
AddEventHandler('esx_banking:updateMoneyInUI', function(doingType, bankMoney, money)
SendNUIMessage({
updateData = true,
data = {
type = doingType,
bankMoney = bankMoney,
money = money
}
})
end)
-- Resource starting
AddEventHandler('onResourceStart', function(resource)
if resource ~= GetCurrentResourceName() then return end
StartThread()
end)
-- Enables it on player loaded
RegisterNetEvent('esx:playerLoaded', function()
StartThread()
end)
-- Resource stopping
AddEventHandler('onResourceStop', function(resource)
if resource ~= GetCurrentResourceName() then return end
RemoveBlips()
ESX.HideUI()
if isInMenu then
CloseUi()
end
end)
+1 -1
View File
@@ -1,6 +1,6 @@
Config = { Config = {
Debug = false,
DrawMarker = 10, DrawMarker = 10,
ShowMarker = true,
Locale = GetConvar('esx:locale', 'en'), Locale = GetConvar('esx:locale', 'en'),
EnablePeds = true, EnablePeds = true,
AtmModels = {`prop_fleeca_atm`, `prop_atm_01`, `prop_atm_02`, `prop_atm_03`}, AtmModels = {`prop_fleeca_atm`, `prop_atm_01`, `prop_atm_02`, `prop_atm_03`},
+1 -1
View File
@@ -24,7 +24,7 @@ end)
if Config.EnablePeds then if Config.EnablePeds then
AddEventHandler('esx:playerLoaded', function(playerId) AddEventHandler('esx:playerLoaded', function(playerId)
TriggerClientEvent('esx_banking:PedHandler', playerId, netIdTable) TriggerClientEvent('esx_banking:pedHandler', playerId, netIdTable)
end) end)
end end