From f34e91de740954c45f2abe6561798c82c6476599 Mon Sep 17 00:00:00 2001
From: Matthew <22198949+MafewTM@users.noreply.github.com>
Date: Wed, 3 Jan 2024 10:44:57 -0500
Subject: [PATCH] refactor(doors): function organization

---
 client/doors.lua  | 112 ++++++++++++++++++++++++++++++++++++----------
 client/fleeca.lua |  80 +++------------------------------
 2 files changed, 95 insertions(+), 97 deletions(-)

diff --git a/client/doors.lua b/client/doors.lua
index 708ade7..efcc08f 100644
--- a/client/doors.lua
+++ b/client/doors.lua
@@ -1,59 +1,123 @@
-local bigBanks = require 'config.shared'.bigBanks
+local sharedConfig = require 'config.shared'
+
+local function resetDoor(object, bank)
+    local heading = bank.isOpened and bank.heading.open or bank.heading.closed
+    SetEntityHeading(object, heading)
+end
+
+function ResetBankDoors()
+    for k in pairs(sharedConfig.smallBanks) do
+        local coords = sharedConfig.smallBanks[k].coords
+        local object = GetClosestObjectOfType(coords.x, coords.y, coords.z, 5.0, sharedConfig.smallBanks[k].object, false, false, false)
+        resetDoor(object, sharedConfig.smallBanks[k])
+    end
+
+    local paletoCoords = sharedConfig.bigBanks.paleto.coords
+    local paletoObject = GetClosestObjectOfType(paletoCoords.x, paletoCoords.y, paletoCoords.z, 5.0, sharedConfig.bigBanks.paleto.object, false, false, false)
+    resetDoor(paletoObject, sharedConfig.bigBanks.paleto)
+
+    local pacificCoords = sharedConfig.bigBanks.pacific.coords[2]
+    local pacificObject = GetClosestObjectOfType(pacificCoords.x, pacificCoords.y, pacificCoords.z, 20.0, sharedConfig.bigBanks.pacific.object, false, false, false)
+    resetDoor(pacificObject, sharedConfig.bigBanks.pacific)
+end
+
+--- This will open the bank door of any small bank
+--- @param bankId number
+--- @return nil
+function OpenFleecaDoor(bankId)
+    local object = GetClosestObjectOfType(sharedConfig.smallBanks[bankId].coords.x, sharedConfig.smallBanks[bankId].coords.y, sharedConfig.smallBanks[bankId].coords.z, 5.0, sharedConfig.smallBanks[bankId].object, false, false, false)
+    local entHeading = sharedConfig.smallBanks[bankId].heading.closed
+    if object ~= 0 then
+        CreateThread(function()
+            while entHeading ~= sharedConfig.smallBanks[bankId].heading.open do
+                SetEntityHeading(object, entHeading - 10)
+                entHeading -= 0.5
+                Wait(10)
+            end
+        end)
+    end
+end
+
+--- This will open the bank door of the pacific bank
+--- @return nil
+function OpenPacificDoor()
+    local object = GetClosestObjectOfType(sharedConfig.bigBanks.pacific.coords[2].x, sharedConfig.bigBanks.pacific.coords[2].y, sharedConfig.bigBanks.pacific.coords[2].z, 20.0, sharedConfig.bigBanks.pacific.object, false, false, false)
+    local entHeading = sharedConfig.bigBanks.pacific.heading.closed
+    if object ~= 0 then
+        CreateThread(function()
+            while entHeading > sharedConfig.bigBanks.pacific.heading.open do
+                SetEntityHeading(object, entHeading - 10)
+                entHeading -= 0.5
+                Wait(10)
+            end
+        end)
+    end
+end
+
+--- This will open the bank door of the paleto bank
+--- @return nil
+function OpenPaletoDoor()
+    --Config.DoorlockAction(4, false)
+    local object = GetClosestObjectOfType(sharedConfig.bigBanks.paleto.coords.x, sharedConfig.bigBanks.paleto.coords.y, sharedConfig.bigBanks.paleto.coords.z, 5.0, sharedConfig.bigBanks.paleto.object, false, false, false)
+    if object ~= 0 then
+        SetEntityHeading(object, sharedConfig.bigBanks.paleto.heading.open)
+    end
+end
 
 RegisterNetEvent('qb-bankrobbery:client:ClearTimeoutDoors', function()
     --Config.DoorlockAction(4, true)
-    local paletoObject = GetClosestObjectOfType(bigBanks.paleto.coords.x, bigBanks.paleto.coords.y, bigBanks.paleto.coords.z, 5.0, bigBanks.paleto.object, false, false, false)
+    local paletoObject = GetClosestObjectOfType(sharedConfig.bigBanks.paleto.coords.x, sharedConfig.bigBanks.paleto.coords.y, sharedConfig.bigBanks.paleto.coords.z, 5.0, sharedConfig.bigBanks.paleto.object, false, false, false)
     if paletoObject ~= 0 then
         SetEntityHeading(paletoObject, bigBanks.paleto.heading.closed)
     end
-    local object = GetClosestObjectOfType(bigBanks.pacific.coords[2].x, bigBanks.pacific.coords[2].y, bigBanks.pacific.coords[2].z, 20.0, bigBanks.pacific.object, false, false, false)
+    local object = GetClosestObjectOfType(sharedConfig.bigBanks.pacific.coords[2].x, sharedConfig.bigBanks.pacific.coords[2].y, sharedConfig.bigBanks.pacific.coords[2].z, 20.0, sharedConfig.bigBanks.pacific.object, false, false, false)
     if object ~= 0 then
-        SetEntityHeading(object, bigBanks.pacific.heading.closed)
+        SetEntityHeading(object, sharedConfig.bigBanks.pacific.heading.closed)
     end
-    for k in pairs(bigBanks.pacific.lockers) do
-        bigBanks.pacific.lockers[k].isBusy = false
-        bigBanks.pacific.lockers[k].isOpened = false
+    for k in pairs(sharedConfig.bigBanks.pacific.lockers) do
+        sharedConfig.bigBanks.pacific.lockers[k].isBusy = false
+        sharedConfig.bigBanks.pacific.lockers[k].isOpened = false
     end
-    for k in pairs(bigBanks.paleto.lockers) do
-        bigBanks.paleto.lockers[k].isBusy = false
-        bigBanks.paleto.lockers[k].isOpened = false
+    for k in pairs(sharedConfig.bigBanks.paleto.lockers) do
+        sharedConfig.bigBanks.paleto.lockers[k].isBusy = false
+        sharedConfig.bigBanks.paleto.lockers[k].isOpened = false
     end
-    bigBanks.paleto.isOpened = false
-    bigBanks.pacific.isOpened = false
+    sharedConfig.bigBanks.paleto.isOpened = false
+    sharedConfig.bigBanks.pacific.isOpened = false
 end)
 
 CreateThread(function()
     while true do
         local pos = GetEntityCoords(cache.ped)
-        local paletoDist = #(pos - bigBanks.paleto.coords)
-        local pacificDist = #(pos - bigBanks.pacific.coords[2])
+        local paletoDist = #(pos - sharedConfig.bigBanks.paleto.coords)
+        local pacificDist = #(pos - sharedConfig.bigBanks.pacific.coords[2])
         if paletoDist < 15 then
-            if bigBanks.paleto.isOpened then
+            if sharedConfig.bigBanks.paleto.isOpened then
                 --Config.DoorlockAction(4, false)
-                local object = GetClosestObjectOfType(bigBanks.paleto.coords.x, bigBanks.paleto.coords.y, bigBanks.paleto.coords.z, 5.0, bigBanks.paleto.object, false, false, false)
+                local object = GetClosestObjectOfType(sharedConfig.bigBanks.paleto.coords.x, sharedConfig.bigBanks.paleto.coords.y, sharedConfig.bigBanks.paleto.coords.z, 5.0, sharedConfig.bigBanks.paleto.object, false, false, false)
                 if object ~= 0 then
-                    SetEntityHeading(object, bigBanks.paleto.heading.open)
+                    SetEntityHeading(object, sharedConfig.bigBanks.paleto.heading.open)
                 end
             else
                 --Config.DoorlockAction(4, true)
-                local object = GetClosestObjectOfType(bigBanks.paleto.coords.x, bigBanks.paleto.coords.y, bigBanks.paleto.coords.z, 5.0, bigBanks.paleto.object, false, false, false)
+                local object = GetClosestObjectOfType(sharedConfig.bigBanks.paleto.coords.x, sharedConfig.bigBanks.paleto.coords.y, sharedConfig.bigBanks.paleto.coords.z, 5.0, sharedConfig.bigBanks.paleto.object, false, false, false)
                 if object ~= 0 then
-                    SetEntityHeading(object, bigBanks.paleto.heading.closed)
+                    SetEntityHeading(object, sharedConfig.bigBanks.paleto.heading.closed)
                 end
             end
         end
 
         -- Pacific Check
         if pacificDist < 50 then
-            if bigBanks.pacific.isOpened then
-                local object = GetClosestObjectOfType(bigBanks.pacific.coords[2].x, bigBanks.pacific.coords[2].y, bigBanks.pacific.coords[2].z, 20.0, bigBanks.pacific.object, false, false, false)
+            if sharedConfig.bigBanks.pacific.isOpened then
+                local object = GetClosestObjectOfType(sharedConfig.bigBanks.pacific.coords[2].x, sharedConfig.bigBanks.pacific.coords[2].y, sharedConfig.bigBanks.pacific.coords[2].z, 20.0, sharedConfig.bigBanks.pacific.object, false, false, false)
                 if object ~= 0 then
-                    SetEntityHeading(object, bigBanks.pacific.heading.open)
+                    SetEntityHeading(object, sharedConfig.bigBanks.pacific.heading.open)
                 end
             else
-                local object = GetClosestObjectOfType(bigBanks.pacific.coords[2].x, bigBanks.pacific.coords[2].y, bigBanks.pacific.coords[2].z, 20.0, bigBanks.pacific.object, false, false, false)
+                local object = GetClosestObjectOfType(sharedConfig.bigBanks.pacific.coords[2].x, sharedConfig.bigBanks.pacific.coords[2].y, sharedConfig.bigBanks.pacific.coords[2].z, 20.0, sharedConfig.bigBanks.pacific.object, false, false, false)
                 if object ~= 0 then
-                    SetEntityHeading(object, bigBanks.pacific.heading.closed)
+                    SetEntityHeading(object, sharedConfig.bigBanks.pacific.heading.closed)
                 end
             end
         end
diff --git a/client/fleeca.lua b/client/fleeca.lua
index b0ef188..53c4007 100644
--- a/client/fleeca.lua
+++ b/client/fleeca.lua
@@ -10,30 +10,9 @@ local copsCalled = false
 local refreshed = false
 local currentLocker = 0
 
-local function resetDoor(object, bank)
-    local heading = bank.isOpened and bank.heading.open or bank.heading.closed
-    SetEntityHeading(object, heading)
-end
-
-local function resetBankDoors()
-    for k in pairs(sharedConfig.smallBanks) do
-        local coords = sharedConfig.smallBanks[k].coords
-        local object = GetClosestObjectOfType(coords.x, coords.y, coords.z, 5.0, sharedConfig.smallBanks[k].object, false, false, false)
-        resetDoor(object, sharedConfig.smallBanks[k])
-    end
-
-    local paletoCoords = sharedConfig.bigBanks.paleto.coords
-    local paletoObject = GetClosestObjectOfType(paletoCoords.x, paletoCoords.y, paletoCoords.z, 5.0, sharedConfig.bigBanks.paleto.object, false, false, false)
-    resetDoor(paletoObject, sharedConfig.bigBanks.paleto)
-
-    local pacificCoords = sharedConfig.bigBanks.pacific.coords[2]
-    local pacificObject = GetClosestObjectOfType(pacificCoords.x, pacificCoords.y, pacificCoords.z, 20.0, sharedConfig.bigBanks.pacific.object, false, false, false)
-    resetDoor(pacificObject, sharedConfig.bigBanks.pacific)
-end
-
 AddEventHandler('onResourceStop', function(resource)
     if resource ~= cache.resource then return end
-    resetBankDoors()
+    ResetBankDoors()
 end)
 
 RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
@@ -41,7 +20,7 @@ RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
     sharedConfig.powerStations = config1
     sharedConfig.bigBanks = config2
     sharedConfig.smallBanks = config3
-    resetBankDoors()
+    ResetBankDoors()
     isLoggedIn = true
 end)
 
@@ -49,34 +28,6 @@ RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
     isLoggedIn = false
 end)
 
--- Functions
-
---- This will open the bank door of the paleto bank
---- @return nil
-local function openPaletoDoor()
-    --Config.DoorlockAction(4, false)
-    local object = GetClosestObjectOfType(sharedConfig.bigBanks.paleto.coords.x, sharedConfig.bigBanks.paleto.coords.y, sharedConfig.bigBanks.paleto.coords.z, 5.0, sharedConfig.bigBanks.paleto.object, false, false, false)
-    if object ~= 0 then
-        SetEntityHeading(object, sharedConfig.bigBanks.paleto.heading.open)
-    end
-end
-
---- This will open the bank door of the pacific bank
---- @return nil
-local function openPacificDoor()
-    local object = GetClosestObjectOfType(sharedConfig.bigBanks.pacific.coords[2].x, sharedConfig.bigBanks.pacific.coords[2].y, sharedConfig.bigBanks.pacific.coords[2].z, 20.0, sharedConfig.bigBanks.pacific.object, false, false, false)
-    local entHeading = sharedConfig.bigBanks.pacific.heading.closed
-    if object ~= 0 then
-        CreateThread(function()
-            while entHeading > sharedConfig.bigBanks.pacific.heading.open do
-                SetEntityHeading(object, entHeading - 10)
-                entHeading -= 0.5
-                Wait(10)
-            end
-        end)
-    end
-end
-
 --- This is triggered once the hack at a small bank is done
 --- @param success boolean
 --- @return nil
@@ -86,23 +37,6 @@ local function onHackDone(success)
     TriggerServerEvent('qb-bankrobbery:server:setBankState', closestBank)
 end
 
---- This will open the bank door of any small bank
---- @param bankId number
---- @return nil
-local function openBankDoor(bankId)
-    local object = GetClosestObjectOfType(sharedConfig.smallBanks[bankId].coords.x, sharedConfig.smallBanks[bankId].coords.y, sharedConfig.smallBanks[bankId].coords.z, 5.0, sharedConfig.smallBanks[bankId].object, false, false, false)
-    local entHeading = sharedConfig.smallBanks[bankId].heading.closed
-    if object ~= 0 then
-        CreateThread(function()
-            while entHeading ~= sharedConfig.smallBanks[bankId].heading.open do
-                SetEntityHeading(object, entHeading - 10)
-                entHeading -= 0.5
-                Wait(10)
-            end
-        end)
-    end
-end
-
 --- This will trigger the sequence of opening a safe locker of a bank
 --- @param bankId string | number
 --- @param lockerId number
@@ -294,13 +228,13 @@ end)
 RegisterNetEvent('qb-bankrobbery:client:setBankState', function(bankId)
     if bankId == 'paleto' then
         sharedConfig.bigBanks.paleto.isOpened = true
-        openPaletoDoor()
+        OpenPaletoDoor()
     elseif bankId == 'pacific' then
         sharedConfig.bigBanks.pacific.isOpened = true
-        openPacificDoor()
+        OpenPacificDoor()
     else
         sharedConfig.smallBanks[bankId].isOpened = true
-        openBankDoor(bankId)
+        OpenFleecaDoor(bankId)
     end
 end)
 
@@ -399,7 +333,7 @@ CreateThread(function()
     while true do
         if closestBank ~= 0 then
             if not refreshed then
-                resetBankDoors()
+                ResetBankDoors()
                 refreshed = true
             end
         else
@@ -410,7 +344,7 @@ CreateThread(function()
 end)
 
 CreateThread(function()
-    while true do -- This is kept for the resetBankDoors function to be executed outside of the polyzone
+    while true do -- This is kept for the ResetBankDoors function to be executed outside of the polyzone
         local pos = GetEntityCoords(cache.ped)
         local inRange = false
         if isLoggedIn then