Skip to content

Commit

Permalink
feat(server): add CheckIn export
Browse files Browse the repository at this point in the history
* refactor(server): split off hospital related code into its own file

* feat: new server side export to CheckIn to a hospital

* fix: checking in on respawn
  • Loading branch information
Manason authored Dec 1, 2023
1 parent eedf9f6 commit 64c297b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 55 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)

RegisterNetEvent('qbx_ambulancejob:client:putPlayerInBed', function(hospitalName, bedIndex)
putPlayerInBed(hospitalName, bedIndex, false, true)
end)
Expand All @@ -107,20 +103,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
106 changes: 63 additions & 43 deletions server/hospital.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ RegisterNetEvent('qbx_ambulancejob:server:playerLeftBed', function(hospitalName,
hospitalBedsTaken[hospitalName][bedIndex] = false
end)

---@param playerId number
RegisterNetEvent('hospital:server:putPlayerInBed', function(playerId, hospitalName, bedIndex)
if GetInvokingResource() then return end
TriggerClientEvent('qbx_ambulancejob:client:putPlayerInBed', playerId, hospitalName, bedIndex)
end)

lib.callback.register('qbx_ambulancejob:server:isBedTaken', function(_, hospitalName, bedIndex)
return hospitalBedsTaken[hospitalName][bedIndex]
end)
Expand All @@ -64,42 +70,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 +84,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,10 +98,60 @@ lib.callback.register('qbx_ambulancejob:server:canCheckIn', function(source, hos
}) then return false end

return true
end)
end

---@param playerId number
RegisterNetEvent('hospital:server:putPlayerInBed', function(playerId, hospitalName, bedIndex)
if GetInvokingResource() then return end
TriggerClientEvent('qbx_ambulancejob:client:putPlayerInBed', playerId, hospitalName, bedIndex)
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

local bedIndex = getOpenBed(closestHospital)
if not bedIndex then
exports.qbx_core:Notify(src, Lang:t('error.beds_taken'), 'error')
return
end
TriggerClientEvent('qbx_ambulancejob:client:checkedIn', src, closestHospital, bedIndex)

if config.wipeInvOnRespawn then
wipeInventory(player)
end
end

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

0 comments on commit 64c297b

Please sign in to comment.