From eedf9f6293303dbe444138bcb41f4d0a3dcf9804 Mon Sep 17 00:00:00 2001 From: Manason Date: Fri, 1 Dec 2023 08:31:16 -0800 Subject: [PATCH] refactor(server): split off hospital related code into its own file --- server/hospital.lua | 137 ++++++++++++++++++++++++++++++++++++++++++++ server/main.lua | 134 ------------------------------------------- 2 files changed, 137 insertions(+), 134 deletions(-) create mode 100644 server/hospital.lua diff --git a/server/hospital.lua b/server/hospital.lua new file mode 100644 index 0000000..cba5f84 --- /dev/null +++ b/server/hospital.lua @@ -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> +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) \ No newline at end of file diff --git a/server/main.lua b/server/main.lua index 9cfe850..1e981ee 100644 --- a/server/main.lua +++ b/server/main.lua @@ -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> -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) @@ -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 @@ -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 @@ -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', {