Skip to content

Commit

Permalink
Merge pull request #47 from Cocodrulo/main
Browse files Browse the repository at this point in the history
Fix radio nui not updating and restricted channels
  • Loading branch information
GhzGarage authored Nov 12, 2024
2 parents 278be86 + 7e0dee3 commit 94de37e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 67 deletions.
51 changes: 31 additions & 20 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ local function SplitStr(inputstr, sep)
end

local function connecttoradio(channel)
if channel > Config.MaxFrequency or channel <= 0 then QBCore.Functions.Notify(Lang:t('restricted_channel_error'), 'error') return false end
if Config.RestrictedChannels[channel] ~= nil then
if not Config.RestrictedChannels[channel][PlayerData.job.name] or not PlayerData.job.onduty then
QBCore.Functions.Notify(Lang:t('restricted_channel_error'), 'error')
return false
end
end
RadioChannel = channel
if onRadio then
exports["pma-voice"]:setRadioChannel(0)
Expand All @@ -42,6 +49,7 @@ local function connecttoradio(channel)
else
QBCore.Functions.Notify(Lang:t('joined_to_radio', {channel = channel .. '.00 MHz'}), 'success')
end
return true
end

local function closeEvent()
Expand All @@ -61,9 +69,9 @@ local function toggleRadioAnimation(pState)
LoadAnimDic("cellphone@")
if pState then
TriggerEvent("attachItemRadio","radio01")
TaskPlayAnim(PlayerPedId(), "cellphone@", "cellphone_text_read_base", 2.0, 3.0, -1, 49, 0, 0, 0, 0)
radioProp = CreateObject(`prop_cs_hand_radio`, 1.0, 1.0, 1.0, 1, 1, 0)
AttachEntityToEntity(radioProp, PlayerPedId(), GetPedBoneIndex(PlayerPedId(), 57005), 0.14, 0.01, -0.02, 110.0, 120.0, -15.0, 1, 0, 0, 0, 2, 1)
TaskPlayAnim(PlayerPedId(), "cellphone@", "cellphone_text_read_base", 2.0, 3.0, -1, 49, 0, false, false, false)
radioProp = CreateObject(`prop_cs_hand_radio`, 1.0, 1.0, 1.0, true, true, false)
AttachEntityToEntity(radioProp, PlayerPedId(), GetPedBoneIndex(PlayerPedId(), 57005), 0.14, 0.01, -0.02, 110.0, 120.0, -15.0, true, false, false, false, 2, true)
else
StopAnimTask(PlayerPedId(), "cellphone@", "cellphone_text_read_base", 1.0)
ClearPedTasks(PlayerPedId())
Expand Down Expand Up @@ -149,17 +157,13 @@ end)
RegisterNUICallback('joinRadio', function(data, cb)
local rchannel = tonumber(data.channel)
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel <= Config.MaxFrequency and rchannel > 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Lang:t('restricted_channel_error'), 'error')
end
else
connecttoradio(rchannel)
end
local canaccess = connecttoradio(rchannel)
cb({
canaccess = canaccess,
channel = RadioChannel
})
else
QBCore.Functions.Notify(Lang:t('you_on_radio') , 'error')
end
Expand All @@ -169,7 +173,10 @@ RegisterNUICallback('joinRadio', function(data, cb)
else
QBCore.Functions.Notify(Lang:t('invalid_radio') , 'error')
end
cb("ok")
cb({
canaccess = false,
channel = RadioChannel
})
end)

RegisterNUICallback('leaveRadio', function(_, cb)
Expand Down Expand Up @@ -205,18 +212,22 @@ end)

RegisterNUICallback("increaseradiochannel", function(_, cb)
local newChannel = RadioChannel + 1
connecttoradio(newChannel)
QBCore.Functions.Notify(Lang:t("increase_decrease_radio_channel", {value = newChannel}), "success")
cb("ok")
local canaccess = connecttoradio(newChannel)
cb({
canaccess = canaccess,
channel = newChannel
})
end)

RegisterNUICallback("decreaseradiochannel", function(_, cb)
if not onRadio then return end
local newChannel = RadioChannel - 1
if newChannel >= 1 then
connecttoradio(newChannel)
QBCore.Functions.Notify(Lang:t("increase_decrease_radio_channel", {value = newChannel}), "success")
cb("ok")
local canaccess = connecttoradio(newChannel)
cb({
canaccess = canaccess,
channel = newChannel
})
end
end)

Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ files {
'html/ui.html',
'html/js/script.js',
'html/css/style.css',
'html/img/radio.png'
'html/img/radio*.png'
}

dependency 'pma-voice'
Expand Down
125 changes: 82 additions & 43 deletions html/js/script.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,127 @@
$(function() {
window.addEventListener('message', function(event) {
$(function () {
window.addEventListener("message", function (event) {
if (event.data.type == "open") {
QBRadio.SlideUp()
QBRadio.SlideUp();
}

if (event.data.type == "close") {
QBRadio.SlideDown()
QBRadio.SlideDown();
}
});

document.onkeyup = function (data) {
if (data.key == "Escape") { // Escape key
$.post('https://qb-radio/escape', JSON.stringify({}));
} else if (data.key == "Enter") { // Enter key
$.post('https://qb-radio/joinRadio', JSON.stringify({
channel: $("#channel").val()
}));
if (data.key == "Escape") {
// Escape key
$.post("https://qb-radio/escape", JSON.stringify({}));
} else if (data.key == "Enter") {
// Enter key
$.post(
"https://qb-radio/joinRadio",
JSON.stringify({
channel: $("#channel").val(),
})
).then((data) => {
if (data.canaccess) {
$("#channel").val(data.channel);
}
});
}
};
});

QBRadio = {}
QBRadio = {};

$(document).on('click', '#submit', function(e){
$(document).on("click", "#submit", function (e) {
e.preventDefault();

$.post('https://qb-radio/joinRadio', JSON.stringify({
channel: $("#channel").val()
}));
$.post(
"https://qb-radio/joinRadio",
JSON.stringify({
channel: $("#channel").val(),
})
).then((data) => {
if (data.canaccess) {
$("#channel").val(data.channel);
}
});
});

$(document).on('click', '#disconnect', function(e){
$(document).on("click", "#disconnect", function (e) {
e.preventDefault();

$.post('https://qb-radio/leaveRadio');
$.post("https://qb-radio/leaveRadio");
});

$(document).on('click', '#volumeUp', function(e){
$(document).on("click", "#volumeUp", function (e) {
e.preventDefault();

$.post('https://qb-radio/volumeUp', JSON.stringify({
channel: $("#channel").val()
}));
$.post(
"https://qb-radio/volumeUp",
JSON.stringify({
channel: $("#channel").val(),
})
);
});

$(document).on('click', '#volumeDown', function(e){
$(document).on("click", "#volumeDown", function (e) {
e.preventDefault();

$.post('https://qb-radio/volumeDown', JSON.stringify({
channel: $("#channel").val()
}));
$.post(
"https://qb-radio/volumeDown",
JSON.stringify({
channel: $("#channel").val(),
})
);
});

$(document).on('click', '#decreaseradiochannel', function(e){
$(document).on("click", "#decreaseradiochannel", function (e) {
e.preventDefault();

$.post('https://qb-radio/decreaseradiochannel', JSON.stringify({
channel: $("#channel").val()
}));
$.post(
"https://qb-radio/decreaseradiochannel",
JSON.stringify({
channel: $("#channel").val(),
})
).then((data) => {
if (data.canaccess) {
$("#channel").val(data.channel);
}
});
});

$(document).on('click', '#increaseradiochannel', function(e){
$(document).on("click", "#increaseradiochannel", function (e) {
e.preventDefault();

$.post('https://qb-radio/increaseradiochannel', JSON.stringify({
channel: $("#channel").val()
}));
$.post(
"https://qb-radio/increaseradiochannel",
JSON.stringify({
channel: $("#channel").val(),
})
).then((data) => {
if (data.canaccess) {
$("#channel").val(data.channel);
}
});
});

$(document).on('click', '#poweredOff', function(e){
$(document).on("click", "#poweredOff", function (e) {
e.preventDefault();

$.post('https://qb-radio/poweredOff', JSON.stringify({
channel: $("#channel").val()
}));
$.post(
"https://qb-radio/poweredOff",
JSON.stringify({
channel: $("#channel").val(),
})
);
});

QBRadio.SlideUp = function() {
QBRadio.SlideUp = function () {
$(".container").css("display", "block");
$(".radio-container").animate({bottom: "6vh",}, 250);
}
$(".radio-container").animate({ bottom: "6vh" }, 250);
};

QBRadio.SlideDown = function() {
$(".radio-container").animate({bottom: "-110vh",}, 400, function(){
QBRadio.SlideDown = function () {
$(".radio-container").animate({ bottom: "-110vh" }, 400, function () {
$(".container").css("display", "none");
});
}
};
1 change: 0 additions & 1 deletion locales/de.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local Translations ={
['volume_radio'] = 'Neue Lautstärke %{value}',
['decrease_radio_volume'] = 'Das Radio ist bereits auf maximaler Lautstärke eingestellt',
['increase_radio_volume'] = 'Das Radio ist bereits auf der niedrigsten Lautstärke eingestellt',
['increase_decrease_radio_channel'] = 'Neuer Kanal %{value}',
}

if GetConvar('qb_locale', 'en') == 'de' then
Expand Down
1 change: 0 additions & 1 deletion locales/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local Translations ={
['volume_radio'] = 'New volume %{value}',
['decrease_radio_volume'] = 'The radio is already set to maximum volume',
['increase_radio_volume'] = 'The radio is already set to the lowest volume',
['increase_decrease_radio_channel'] = 'New channel %{value}',
}

Lang = Lang or Locale:new({
Expand Down
1 change: 0 additions & 1 deletion locales/pt-br.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local Translations = {
['volume_radio'] = 'Novo volume %{value}',
['decrease_radio_volume'] = 'O rádio já está ajustado para o volume máximo',
['increase_radio_volume'] = 'O rádio já está ajustado para o volume mais baixo',
['increase_decrease_radio_channel'] = 'Novo canal %{value}',
}

if GetConvar('qb_locale', 'en') == 'pt-br' then
Expand Down

0 comments on commit 94de37e

Please sign in to comment.