Skip to content

Commit

Permalink
refactor(server): split off hospital related code into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason authored Dec 1, 2023
1 parent b6cb941 commit eedf9f6
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 134 deletions.
137 changes: 137 additions & 0 deletions server/hospital.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
--- Contains code relevant to the physical hospital building. Things like checking in, beds, spawning vehicles, etc.

---@class Player object from core

local config = require 'config.server'
local sharedConfig = require 'config.shared'
local triggerEventHooks = require '@qbx_core.modules.hooks'
local doctorCalled = false

---@type table<string, table<number, boolean>>
local hospitalBedsTaken = {}

for hospitalName, hospital in pairs(sharedConfig.locations.hospitals) do
hospitalBedsTaken[hospitalName] = {}
for i = 1, #hospital.beds do
hospitalBedsTaken[hospitalName][i] = false
end
end

local function getOpenBed(hospitalName)
local beds = hospitalBedsTaken[hospitalName]
for i = 1, #beds do
local isTaken = beds[i]
if not isTaken then return i end
end
end

lib.callback.register('qbx_ambulancejob:server:getOpenBed', function(_, hospitalName)
return getOpenBed(hospitalName)
end)

---@param player Player
local function billPlayer(player)
player.Functions.RemoveMoney("bank", sharedConfig.checkInCost, "respawned-at-hospital")
config.depositSociety("ambulance", sharedConfig.checkInCost)
TriggerClientEvent('hospital:client:SendBillEmail', player.PlayerData.source, sharedConfig.checkInCost)
end

RegisterNetEvent('qbx_ambulancejob:server:playerEnteredBed', function(hospitalName, bedIndex)
if GetInvokingResource() then return end
local src = source
local player = exports.qbx_core:GetPlayer(src)
billPlayer(player)
hospitalBedsTaken[hospitalName][bedIndex] = true
end)

RegisterNetEvent('qbx_ambulancejob:server:playerLeftBed', function(hospitalName, bedIndex)
if GetInvokingResource() then return end
hospitalBedsTaken[hospitalName][bedIndex] = false
end)

lib.callback.register('qbx_ambulancejob:server:isBedTaken', function(_, hospitalName, bedIndex)
return hospitalBedsTaken[hospitalName][bedIndex]
end)

---@param player Player
local function wipeInventory(player)
player.Functions.ClearInventory()
TriggerClientEvent('ox_lib:notify', player.PlayerData.source, { description = Lang:t('error.possessions_taken'), type = 'error' })
end

lib.callback.register('qbx_ambulancejob:server:spawnVehicle', function(source, vehicleName, vehicleCoords)
local netId = SpawnVehicle(source, vehicleName, vehicleCoords, true)
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
local _, doctors = exports.qbx_core:GetDutyCountType('ems')
for i = 1, #doctors do
local doctor = doctors[i]
TriggerClientEvent('ox_lib:notify', doctor, { description = Lang:t('info.dr_needed'), type = 'inform' })
end

SetTimeout(config.doctorCallCooldown * 60000, function()
doctorCalled = false
end)
end

lib.callback.register('qbx_ambulancejob:server:canCheckIn', function(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' })
sendDoctorAlert()
return false
end

if not triggerEventHooks('checkIn', {
source = source,
hospitalName = hospitalName,
}) then return false end

return true
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)
134 changes: 0 additions & 134 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,90 +1,5 @@
---@class Player object from core

---@alias source number

local config = require 'config.server'
local sharedConfig = require 'config.shared'
local triggerEventHooks = require '@qbx_core.modules.hooks'
local doctorCalled = false

---@type table<string, table<number, boolean>>
local hospitalBedsTaken = {}
for hospitalName, hospital in pairs(sharedConfig.locations.hospitals) do
hospitalBedsTaken[hospitalName] = {}
for i = 1, #hospital.beds do
hospitalBedsTaken[hospitalName][i] = false
end
end

---@param player Player
local function billPlayer(player)
player.Functions.RemoveMoney("bank", sharedConfig.checkInCost, "respawned-at-hospital")
config.depositSociety("ambulance", sharedConfig.checkInCost)
TriggerClientEvent('hospital:client:SendBillEmail', player.PlayerData.source, sharedConfig.checkInCost)
end

---@param player Player
local function wipeInventory(player)
player.Functions.ClearInventory()
TriggerClientEvent('ox_lib:notify', player.PlayerData.source, { description = Lang:t('error.possessions_taken'), type = 'error' })
end

local function getOpenBed(hospitalName)
local beds = hospitalBedsTaken[hospitalName]
for i = 1, #beds do
local isTaken = beds[i]
if not isTaken then return i end
end
end

lib.callback.register('qbx_ambulancejob:server:getOpenBed', function(_, hospitalName)
return getOpenBed(hospitalName)
end)

lib.callback.register('qbx_ambulancejob:server:spawnVehicle', function(source, vehicleName, vehicleCoords)
local netId = SpawnVehicle(source, vehicleName, vehicleCoords, true)
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)

lib.callback.register('qbx_ambulancejob:server:isBedTaken', function(_, hospitalName, bedIndex)
return hospitalBedsTaken[hospitalName][bedIndex]
end)

lib.callback.register('qbx_ambulancejob:server:getPlayerStatus', function(_, targetSrc)
return exports.qbx_medical:GetPlayerStatus(targetSrc)
end)
Expand Down Expand Up @@ -112,19 +27,6 @@ RegisterNetEvent('qbx_medical:server:onPlayerLaststand', function(text)
alertAmbulance(src, text)
end)

RegisterNetEvent('qbx_ambulancejob:server:playerEnteredBed', function(hospitalName, bedIndex)
if GetInvokingResource() then return end
local src = source
local player = exports.qbx_core:GetPlayer(src)
billPlayer(player)
hospitalBedsTaken[hospitalName][bedIndex] = true
end)

RegisterNetEvent('qbx_ambulancejob:server:playerLeftBed', function(hospitalName, bedIndex)
if GetInvokingResource() then return end
hospitalBedsTaken[hospitalName][bedIndex] = false
end)

---@param playerId number
RegisterNetEvent('hospital:server:TreatWounds', function(playerId)
if GetInvokingResource() then return end
Expand All @@ -150,26 +52,6 @@ RegisterNetEvent('hospital:server:RevivePlayer', function(playerId)
TriggerClientEvent('hospital:client:Revive', patient.PlayerData.source)
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)

local function sendDoctorAlert()
if doctorCalled then return end
doctorCalled = true
local _, doctors = exports.qbx_core:GetDutyCountType('ems')
for i = 1, #doctors do
local doctor = doctors[i]
TriggerClientEvent('ox_lib:notify', doctor, { description = Lang:t('info.dr_needed'), type = 'inform' })
end

SetTimeout(config.doctorCallCooldown * 60000, function()
doctorCalled = false
end)
end

---@param targetId number
RegisterNetEvent('hospital:server:UseFirstAid', function(targetId)
if GetInvokingResource() then return end
Expand All @@ -193,22 +75,6 @@ lib.callback.register('qbx_ambulancejob:server:getNumDoctors', function()
return count
end)

lib.callback.register('qbx_ambulancejob:server:canCheckIn', function(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' })
sendDoctorAlert()
return false
end

if not triggerEventHooks('checkIn', {
source = source,
hospitalName = hospitalName,
}) then return false end

return true
end)

-- Commands

lib.addCommand('911e', {
Expand Down

0 comments on commit eedf9f6

Please sign in to comment.