Skip to content

Commit

Permalink
fix: remove unit from call on unit delete
Browse files Browse the repository at this point in the history
  • Loading branch information
BubbleDK committed Aug 24, 2024
1 parent e22fd45 commit 1d190a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 16 additions & 2 deletions server/calls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ end
exports('updateCallCoords', updateCallCoords)

local function removeExpiredCalls()
local currentTime = os.time() * 1000 -- Får nuværende tid i millisekunder
local currentTime = os.time() * 1000
local amountRemoved = 0
for id, call in pairs(activeCalls) do
if currentTime - call.time >= 1800000 then -- 1800000 millisekunder svarer til 30 minutter
if currentTime - call.time >= 1800000 then
activeCalls[id] = nil
amountRemoved += 1
end
Expand All @@ -51,6 +51,10 @@ local function removeExpiredCalls()
officers.triggerEvent('mdt:updateCalls', { calls = utils.cleanTable(activeCalls) })
end

function getActiveCalls()
return activeCalls
end

utils.registerCallback('mdt:getCalls', function(source, data)
return activeCalls
end)
Expand All @@ -71,6 +75,16 @@ utils.registerCallback('mdt:respondToCall', function(source, id)
return true
end)

function detachFromCall(unitId, id)
if not activeCalls[id].units[unitId] then return false end

activeCalls[id].units[unitId] = nil

officers.triggerEvent('mdt:editCallUnits', { id = id, units = activeCalls[id].units })

return true
end

utils.registerCallback('mdt:detachFromCall', function(source, id)
local playerUnitId = Player(source).state.mdtUnitId
if not playerUnitId then return false end
Expand Down
8 changes: 6 additions & 2 deletions server/units.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ local function removePlayerFromUnit(officer, state)

officers.triggerEvent('mdt:refreshUnits', units)

return true
end

for i = 1, #unit.members do
Expand All @@ -35,7 +34,12 @@ local function removePlayerFromUnit(officer, state)

if #unit.members == 0 then
units[unitId] = nil
-- TODO: Remove unit from all calls it's attached to
local activeCalls = getActiveCalls()
for id, v in pairs(activeCalls) do
if (activeCalls[id].units[unitId]) then
detachFromCall(unitId, id)
end
end
end

officers.triggerEvent('mdt:refreshUnits', units)
Expand Down

0 comments on commit 1d190a3

Please sign in to comment.