Skip to content

Commit

Permalink
feat: new server side export to CheckIn to a hospital
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason committed Dec 1, 2023
1 parent 660755e commit 11f43e7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 50 deletions.
17 changes: 5 additions & 12 deletions client/hospital.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ local function putPlayerInBed(hospitalName, bedIndex, isRevive, skipOpenCheck)
TriggerServerEvent('qbx_ambulancejob:server:playerEnteredBed', hospitalName, bedIndex)
end

RegisterNetEvent('qbx_ambulancejob:client:onPlayerRespawn', function(hospitalName, bedIndex)
putPlayerInBed(hospitalName, bedIndex, true, true)
end)

---Notifies doctors, and puts player in a hospital bed.
local function checkIn(hospitalName)
local canCheckIn = lib.callback.await('qbx_ambulancejob:server:canCheckIn', false, hospitalName)
Expand All @@ -103,20 +99,17 @@ local function checkIn(hospitalName)
})
then
exports.scully_emotemenu:cancelEmote()
--- ask server for first non taken bed
local bedIndex = lib.callback.await('qbx_ambulancejob:server:getOpenBed', false, hospitalName)
if not bedIndex then
exports.qbx_core:Notify(Lang:t('error.beds_taken'), 'error')
return
end

putPlayerInBed(hospitalName, bedIndex, true, true)
lib.callback('qbx_ambulancejob:server:checkIn', false, nil, cache.serverId, hospitalName)
else
exports.scully_emotemenu:cancelEmote()
exports.qbx_core:Notify(Lang:t('error.canceled'), 'error')
end
end

RegisterNetEvent('qbx_ambulancejob:client:checkedIn', function(hospitalName, bedIndex)
putPlayerInBed(hospitalName, bedIndex, true, true)
end)

---Set up check-in and getting into beds using either target or zones
if config.useTarget then
CreateThread(function()
Expand Down
94 changes: 56 additions & 38 deletions server/hospital.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,6 @@ lib.callback.register('qbx_ambulancejob:server:spawnVehicle', function(source, v
return netId
end)

local function respawn(src)
local player = exports.qbx_core:GetPlayer(src)
local closestHospital = nil
if player.PlayerData.metadata.injail > 0 then
closestHospital = "jail"
else
local coords = GetEntityCoords(GetPlayerPed(src))
local closest = nil

for hospitalName, hospital in pairs(sharedConfig.locations.hospitals) do
if hospitalName ~= 'jail' then
if not closest or #(coords - hospital.coords) < #(coords - closest) then
closest = hospital.coords
closestHospital = hospitalName
end
end
end
end

local bedIndex = getOpenBed(closestHospital)
if not bedIndex then
---TODO: handle hospital being out of beds. Could send them to backup hospital or notify to wait.
return
end

if config.wipeInvOnRespawn then
wipeInventory(player)
end
TriggerClientEvent('qbx_ambulancejob:client:onPlayerRespawn', src, closestHospital, bedIndex)
end

AddEventHandler('qbx_medical:server:playerRespawned', function(source)
respawn(source)
end)


local function sendDoctorAlert()
if doctorCalled then return end
doctorCalled = true
Expand All @@ -114,7 +78,7 @@ local function sendDoctorAlert()
end)
end

lib.callback.register('qbx_ambulancejob:server:canCheckIn', function(source, hospitalName)
local function canCheckIn(source, hospitalName)
local numDoctors = exports.qbx_core:GetDutyCountType('ems')
if numDoctors >= config.minForCheckIn then
TriggerClientEvent('ox_lib:notify', source, { description = Lang:t('info.dr_alert'), type = 'inform' })
Expand All @@ -128,4 +92,58 @@ lib.callback.register('qbx_ambulancejob:server:canCheckIn', function(source, hos
}) then return false end

return true
end)
end

lib.callback.register('qbx_ambulancejob:server:canCheckIn', canCheckIn)

---Sends the patient to an open bed within the hospital
---@param src number the player doing the checking in
---@param patientSrc number the player being checked in
---@param hospitalName string name of the hospital matching the config where player should be placed
local function checkIn(src, patientSrc, hospitalName)
if not canCheckIn(patientSrc, hospitalName) then return false end

local bedIndex = getOpenBed(hospitalName)
if not bedIndex then
exports.qbx_core:Notify(src, Lang:t('error.beds_taken'), 'error')
return false
end

TriggerClientEvent('qbx_ambulancejob:client:checkedIn', patientSrc, hospitalName, bedIndex)
return true
end

lib.callback.register('qbx_ambulancejob:server:checkIn', checkIn)

exports('CheckIn', checkIn)

local function respawn(src)
local player = exports.qbx_core:GetPlayer(src)
local closestHospital
if player.PlayerData.metadata.injail > 0 then
closestHospital = "jail"
else
local coords = GetEntityCoords(GetPlayerPed(src))
local closest = nil

for hospitalName, hospital in pairs(sharedConfig.locations.hospitals) do
if hospitalName ~= 'jail' then
if not closest or #(coords - hospital.coords) < #(coords - closest) then
closest = hospital.coords
closestHospital = hospitalName
end
end
end
end

if not checkIn(src, src, closestHospital) then
lib.print.error(src, 'unable to check-in at hospital. Likely due to no available beds.')
return
end

if config.wipeInvOnRespawn then
wipeInventory(player)
end
end

AddEventHandler('qbx_medical:server:playerRespawned', respawn)

0 comments on commit 11f43e7

Please sign in to comment.