Skip to content

Commit

Permalink
feat: start adding make shift radio channel name support
Browse files Browse the repository at this point in the history
  • Loading branch information
AvarianKnight committed Dec 8, 2021
1 parent 23919f9 commit 5b9db46
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
12 changes: 9 additions & 3 deletions client/module/radio.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local radioChannel = 0
local radioNames = {}

--- event syncRadioData
--- syncs the current players on the radio to the client
---@param radioTable table the table of the current players on the radio
function syncRadioData(radioTable)
---@param localPlyRadioName string the local players name
function syncRadioData(radioTable, localPlyRadioName)
radioData = radioTable
logger.info('[radio] Syncing radio table.')
if GetConvarInt('voice_debugMode', 0) >= 4 then
Expand All @@ -16,6 +18,7 @@ function syncRadioData(radioTable)
toggleVoice(tgt, enabled, 'radio')
end
end
radioNames[playerServerId] = localPlyRadioName
end
RegisterNetEvent('pma-voice:syncRadioData', syncRadioData)

Expand All @@ -33,8 +36,9 @@ RegisterNetEvent('pma-voice:setTalkingOnRadio', setTalkingOnRadio)
--- event addPlayerToRadio
--- adds a player onto the radio.
---@param plySource number the players server id to add to the radio.
function addPlayerToRadio(plySource)
function addPlayerToRadio(plySource, plyRadioName)
radioData[plySource] = false
radioNames[plySource] = plyRadioName
if radioPressed then
logger.info('[radio] %s joined radio %s while we were talking, adding them to targets', plySource, radioChannel)
playerTargets(radioData, MumbleIsPlayerTalking(PlayerId()) and callData or {})
Expand All @@ -55,17 +59,19 @@ function removePlayerFromRadio(plySource)
toggleVoice(tgt, false, 'radio')
end
end
radioNames = {}
radioData = {}
playerTargets(MumbleIsPlayerTalking(PlayerId()) and callData or {})
else
radioData[plySource] = nil
toggleVoice(plySource, false)
if radioPressed then
logger.info('[radio] %s left radio %s while we were talking, updating targets.', plySource, radioChannel)
playerTargets(radioData, MumbleIsPlayerTalking(PlayerId()) and callData or {})
else
logger.info('[radio] %s has left radio %s', plySource, radioChannel)
end
radioData[plySource] = nil
radioNames[plySourse] = nil
end
end
RegisterNetEvent('pma-voice:removePlayerFromRadio', removePlayerFromRadio)
Expand Down
80 changes: 53 additions & 27 deletions server/module/radio.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
local radioChecks = {}

AddEventHandler("onResourceStop", function(resource)
for channel, cfxFunctionRef in pairs(radioChecks) do
local functionRef = cfxFunctionRef.__cfx_functionReference
local functionResource = string.match(functionRef, resource)
if functionResource then
radioChecks[channel] = nil
logger.warn('Channel %s had its radio check removed because the resource that gave the checks stopped', channel)
end
end
end)

--- removes a player from the specified channel
---@param source number the player to remove
---@param radioChannel number the current channel to remove them from
function removePlayerFromRadio(source, radioChannel)
logger.verbose('[radio] Removed %s from radio %s', source, radioChannel)
radioData[radioChannel] = radioData[radioChannel] or {}
for player, _ in pairs(radioData[radioChannel]) do
TriggerClientEvent('pma-voice:removePlayerFromRadio', player, source)
end
radioData[radioChannel][source] = nil
voiceData[source] = voiceData[source] or defaultTable(source)
voiceData[source].radio = 0
end

--- checks if the player can join the channel specified
--- @param source number the source of the player
--- @param radioChannel number the channel they're trying to join
Expand Down Expand Up @@ -53,6 +28,23 @@ function addChannelCheck(channel, cb)
end
exports('addChannelCheck', addChannelCheck)

local function radioNameGetter_orig(source)
return GetPlayerName(source)
end
local radioNameGetter = radioNameGetter_orig

--- adds a check to the channel, function is expected to return a boolean of true or false
---@param cb function the function to execute the check on
function overrideRadioNameGetter(channel, cb)
local cbType = type(cb)
if cbType == 'table' and not cb.__cfx_functionReference then
error(("'cb' expected 'function' got '%s'"):format(cbType))
end
radioNameGetter = cb
logger.info("%s added a check to channel %s", GetInvokingResource(), channel)
end
exports('overrideRadioNameGetter', overrideRadioNameGetter)

--- adds a player to the specified radion channel
---@param source number the player to add to the channel
---@param radioChannel number the channel to set them to
Expand All @@ -66,14 +58,29 @@ function addPlayerToRadio(source, radioChannel)
-- check if the channel exists, if it does set the varaible to it
-- if not create it (basically if not radiodata make radiodata)
radioData[radioChannel] = radioData[radioChannel] or {}
local plyName = radioNameGetter(source)
for player, _ in pairs(radioData[radioChannel]) do
TriggerClientEvent('pma-voice:addPlayerToRadio', player, source)
TriggerClientEvent('pma-voice:addPlayerToRadio', player, source, plyName)
end
voiceData[source] = voiceData[source] or defaultTable(source)

voiceData[source].radio = radioChannel
radioData[radioChannel][source] = false
TriggerClientEvent('pma-voice:syncRadioData', source, radioData[radioChannel])
TriggerClientEvent('pma-voice:syncRadioData', source, radioData[radioChannel], plyName)
end

--- removes a player from the specified channel
---@param source number the player to remove
---@param radioChannel number the current channel to remove them from
function removePlayerFromRadio(source, radioChannel)
logger.verbose('[radio] Removed %s from radio %s', source, radioChannel)
radioData[radioChannel] = radioData[radioChannel] or {}
for player, _ in pairs(radioData[radioChannel]) do
TriggerClientEvent('pma-voice:removePlayerFromRadio', player, source)
end
radioData[radioChannel][source] = nil
voiceData[source] = voiceData[source] or defaultTable(source)
voiceData[source].radio = 0
end

-- TODO: Implement this in a way that allows players to be on multiple channels
Expand Down Expand Up @@ -134,3 +141,22 @@ function setTalkingOnRadio(talking)
end
RegisterNetEvent('pma-voice:setTalkingOnRadio', setTalkingOnRadio)

AddEventHandler("onResourceStop", function(resource)
for channel, cfxFunctionRef in pairs(radioChecks) do
local functionRef = cfxFunctionRef.__cfx_functionReference
local functionResource = string.match(functionRef, resource)
if functionResource then
radioChecks[channel] = nil
logger.warn('Channel %s had its radio check removed because the resource that gave the checks stopped', channel)
end
end
local radioRef = radioNameGetter.__cfx_functionReference
if radioRef then
local isResource = string.match(functionRef, resource)
if isResource then
radioNameGetter = radioNameGetter_orig
logger.warn('Radio name getter is resetting to default because the resource that gave the cb got turned off')
end
end

end)

0 comments on commit 5b9db46

Please sign in to comment.