diff --git a/client/hospital.lua b/client/hospital.lua index 6867e52..4be6986 100644 --- a/client/hospital.lua +++ b/client/hospital.lua @@ -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) @@ -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() diff --git a/server/hospital.lua b/server/hospital.lua index cba5f84..d480c31 100644 --- a/server/hospital.lua +++ b/server/hospital.lua @@ -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) @@ -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 @@ -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' }) @@ -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) \ No newline at end of file +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)