Skip to content

Commit

Permalink
🚨 [v1.1.0] Latest stable
Browse files Browse the repository at this point in the history
  • Loading branch information
imkuroneko authored Mar 25, 2024
1 parent 91588fe commit 946474d
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 104 deletions.
87 changes: 49 additions & 38 deletions client.lua
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
local QBCore = exports['qb-core']:GetCoreObject()
lib.locale()
local defconMenu = {}

-- ===== Armar estructura del menú de alertas
RegisterNetEvent('neko_alertaslspd:client:openMenu')
AddEventHandler('neko_alertaslspd:client:openMenu', function()
exports['qb-menu']:openMenu({
{ header = '⠀⠀⠀⠀⠀⠀⠀🚓 Estado de Alertas', isMenuHeader = true },
{ header = '🟢 ⠀Alerta Verde', txt = '', params = { event = 'neko_alertaslspd:client:setAlertaVerde:Parametro' } },
{ header = '🟡 ⠀Alerta Amarilla', txt = '', params = { event = 'neko_alertaslspd:client:setAlertaAmarilla:Parametro' } },
{ header = '🔴 ⠀Alerta Roja', txt = '', params = { event = 'neko_alertaslspd:client:setAlertaRoja:Parametro' } },
-- ===== Armar estructura del menú
defconMenu = {
id = 'defcon_status',
title = locale('menu_title'),
options = {
{
header = '⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀❌ Cerrar',
params = { event = 'qb-menu:client:closeMenu' }
title = locale('menu_title_green'),
description = locale('menu_description_green'),
icon = 'fas fa-fw fa-circle',
iconColor = '#82ba3d',
serverEvent = 'neko_alertasdefcon:server:change_status',
args = { status = 'g' }
},
})
end)
{
title = locale('menu_title_yellow'),
description = locale('menu_description_yellow'),
icon = 'fas fa-fw fa-circle',
iconColor = '#edcf55',
serverEvent = 'neko_alertasdefcon:server:change_status',
args = { status = 'y' }
},
{
title = locale('menu_title_red'),
description = locale('menu_description_red'),
icon = 'fas fa-fw fa-circle',
iconColor = '#e3203d',
serverEvent = 'neko_alertasdefcon:server:change_status',
args = { status = 'r' }
}
}
}

-- ===== Definir del lado de servidor el estado actual
RegisterNetEvent('neko_alertaslspd:client:setAlertaVerde:Parametro')
AddEventHandler('neko_alertaslspd:client:setAlertaVerde:Parametro', function()
TriggerServerEvent('neko_alertaslspd:server:setAlertaVerde')
end)
lib.registerContext(defconMenu)

RegisterNetEvent('neko_alertaslspd:client:setAlertaAmarilla:Parametro')
AddEventHandler('neko_alertaslspd:client:setAlertaAmarilla:Parametro', function()
TriggerServerEvent('neko_alertaslspd:server:setAlertaAmarilla')
end)

RegisterNetEvent('neko_alertaslspd:client:setAlertaRoja:Parametro')
AddEventHandler('neko_alertaslspd:client:setAlertaRoja:Parametro', function()
TriggerServerEvent('neko_alertaslspd:server:setAlertaRoja')
-- ===== Armar estructura del menú de alertas
RegisterNetEvent('neko_alertas_defcon:client:open_menu')
AddEventHandler('neko_alertas_defcon:client:open_menu', function()
QBCore.Functions.GetPlayerData(function(PlayerData)
if PlayerData.job.name ~= 'police' then
lib.notify({ description = locale('player_not_police') , type = 'error' })
else
if not PlayerData.job.onduty then
lib.notify({ description = locale('player_not_duty') , type = 'error' })
else
lib.showContext(defconMenu.id)
end
end
end)
end)

-- ===== Enviar el evento de estado al cliente de los jugadores
RegisterNetEvent('neko_alertaslspd:client:setAlertaVerde:Jugadores')
AddEventHandler('neko_alertaslspd:client:setAlertaVerde:Jugadores', function()
SendNUIMessage({ type = 'logo', display = 'alerta_verde' })
end)

RegisterNetEvent('neko_alertaslspd:client:setAlertaAmarilla:Jugadores')
AddEventHandler('neko_alertaslspd:client:setAlertaAmarilla:Jugadores', function()
SendNUIMessage({ type = 'logo', display = 'alerta_amarilla' })
end)

RegisterNetEvent('neko_alertaslspd:client:setAlertaRoja:Jugadores')
AddEventHandler('neko_alertaslspd:client:setAlertaRoja:Jugadores', function()
SendNUIMessage({ type = 'logo', display = 'alerta_roja' })
RegisterNetEvent('neko_alertas_defcon:client:set_current_status')
AddEventHandler('neko_alertas_defcon:client:set_current_status', function(data)
SendNUIMessage({ alert = data.status })
end)
9 changes: 9 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Config = {}

Config.Command = 'alertaslspd'

-- Send public alerts on discord
Config.webhookPublic = ''

-- Send details of who changed the DEFCON status
Config.webhookLogs = ''
33 changes: 14 additions & 19 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
fx_version 'cerulean'
game 'gta5'
fx_version 'cerulean'
game 'gta5'
lua54 'yes'
-- ===========================================================
description 'Sistemas de alertas (DEFCON) para la policía'
author 'KuroNeko'
-- ===========================================================
version '1.1.0'

author 'imkuroneko'
description 'Sistema de notificación (UI) de estado de alertas'
version '1.0.0'
-- ===========================================================
shared_scripts { '@ox_lib/init.lua' }
server_scripts { 'config.lua', 'server.lua' }
client_scripts { 'client.lua' }

client_scripts {
'client.lua',
}
server_scripts {
'server.lua',
}

ui_page 'ui/index.html'

files {
'ui/index.html',
'ui/img/*.png',
'ui/script.js'
}
ui_page 'ui/index.html'
files { 'locales/es.json', 'ui/**' }
29 changes: 29 additions & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"menu_title" : "🚓 Alertas DEFCON",
"menu_title_green" : "Alerta Verde",
"menu_description_green" : "Cambiar el estado DEFCON a alerta verde",
"menu_title_yellow" : "Alerta Amarilla",
"menu_description_yellow" : "Cambiar el estado DEFCON a alerta amarilla",
"menu_title_red" : "Alerta Roja",
"menu_description_red" : "Cambiar el estado DEFCON a alerta roja",

"command_help" : "Abrir menú de estado DEFCON",

"player_not_duty" : "No te encuentras en servicio",
"player_not_police" : "No perteneces al cuerpo policial",

"update_status_current" : "Este ya es el estado actual del DEFCON",
"update_status_success" : "Estado DEFCON actualizado exitosamente",

"webhook_public_title" : "Nuevo estado de Alerta DEFCON",

"webhook_public_title_green" : "Se ha establecido la alerta verde",
"webhook_public_description_green" : "webhook_public_description_green...",
"webhook_public_title_yellow" : "Se ha establecido la alerta amarilla",
"webhook_public_description_yellow" : "webhook_public_description_yellow...",
"webhook_public_title_red" : "Se ha establecido la alerta roja",
"webhook_public_description_red" : "webhook_public_description_red...",

"webhook_log_title" : "Actualización de estado DEFCON",
"webhook_log_description" : "El oficial `%s` (CID: `%s`) ha cambiado el estado DEFCON."
}
121 changes: 84 additions & 37 deletions server.lua
Original file line number Diff line number Diff line change
@@ -1,56 +1,103 @@
local QBCore = exports['qb-core']:GetCoreObject()
local nivelAlertaActual = 0
PlayerData = nil
lib.locale()
SendedWebhookRestart = false
DefconCurrentStatus = 'g'

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
QBCore.Functions.GetPlayerData(function(PlayerData)
PlayerData = PlayerData.job
end)
end)

RegisterNetEvent('QBCore:Client:OnJobUpdate', function(JobInfo)
PlayerData = JobInfo
end)


RegisterCommand("alertaslspd", function(source)
-- ===== Registrar comando
lib.addCommand(Config.Command, { help = locale('command_help'), params = {} }, function(source, args)
local Player = QBCore.Functions.GetPlayer(source)

if Player and Player.PlayerData.job.name == 'police' then
if Player.PlayerData.job.onduty then
TriggerClientEvent("neko_alertaslspd:client:openMenu", source)
TriggerClientEvent('neko_alertas_defcon:client:open_menu', source)
else
TriggerClientEvent('QBCore:Notify', source, 'No te encuentras de servicio')
TriggerClientEvent('ox_lib:notify', source, { description = locale('player_not_duty') , type = 'error' })
end
else
TriggerClientEvent('QBCore:Notify', source, 'No eres del cuerpo policial')
TriggerClientEvent('ox_lib:notify', source, { description = locale('player_not_police') , type = 'error' })
end
end, false)

RegisterServerEvent('neko_alertaslspd:server:setAlertaVerde')
AddEventHandler('neko_alertaslspd:server:setAlertaVerde', function()
nivelAlertaActual = 0
end)

RegisterServerEvent('neko_alertaslspd:server:setAlertaAmarilla')
AddEventHandler('neko_alertaslspd:server:setAlertaAmarilla', function()
nivelAlertaActual = 1
end)
-- ===== Evento
RegisterServerEvent('neko_alertasdefcon:server:change_status')
AddEventHandler('neko_alertasdefcon:server:change_status', function(data)
local src = source
if data.status ~= 'g' and data.status ~= 'y' and data.status ~= 'r' then return end

RegisterServerEvent('neko_alertaslspd:server:setAlertaRoja')
AddEventHandler('neko_alertaslspd:server:setAlertaRoja', function()
nivelAlertaActual = 2
if data.status == DefconCurrentStatus then
TriggerClientEvent('ox_lib:notify', source, { description = locale('update_status_current') , type = 'info' })
else
TriggerClientEvent('ox_lib:notify', source, { description = locale('update_status_success') , type = 'success' })
DefconCurrentStatus = data.status
SendWebhookPublic()
SendWebhookLog(src)
end
end)

Citizen.CreateThread(function()
if not SendedWebhookRestart then
SendWebhookPublic()
SendedWebhookRestart = true
end

while true do
Citizen.Wait(0)
if nivelAlertaActual == 0 then
TriggerClientEvent('neko_alertaslspd:client:setAlertaVerde:Jugadores', -1)
elseif nivelAlertaActual == 1 then
TriggerClientEvent('neko_alertaslspd:client:setAlertaAmarilla:Jugadores', -1)
elseif nivelAlertaActual == 2 then
TriggerClientEvent('neko_alertaslspd:client:setAlertaRoja:Jugadores', -1)
end
Citizen.Wait(5000)
TriggerClientEvent('neko_alertas_defcon:client:set_current_status', -1, { status = DefconCurrentStatus })
end
end)

-- ===== Funciones
function SendWebhookPublic()
local t, m, c = '', '', 0
if DefconCurrentStatus == 'r' then
t = 'webhook_public_title_red'
m = 'webhook_public_description_red'
c = 14884925
elseif DefconCurrentStatus == 'y' then
t = 'webhook_public_title_yellow'
m = 'webhook_public_description_yellow'
c = 15585109
else
t = 'webhook_public_title_green'
m = 'webhook_public_description_green'
c = 8567357
end

SendContentToDiscord(Config.webhookPublic, true, {
{ ['title'] = locale(t), ['description'] = locale(m), ['color'] = c, ['timestamp'] = os.date("!%Y-%m-%dT%H:%M:%SZ", os.time()) }
})
end

function SendWebhookLog(source)
local officer = QBCore.Functions.GetPlayer(source)

local name = officer.PlayerData.charinfo.firstname..' '..officer.PlayerData.charinfo.lastname
local cid = officer.PlayerData.citizenid
local title = 'placeholder'

if DefconCurrentStatus == 'r' then
title = locale('webhook_public_title_red')
elseif DefconCurrentStatus == 'y' then
title = locale('webhook_public_title_yellow')
else
title = locale('webhook_public_title_green')
end


SendContentToDiscord(Config.webhookLogs, false, {
{ ['title'] = locale(title), ['description'] = locale('webhook_log_description', name, cid), ['color'] = 3191259, ['timestamp'] = os.date("!%Y-%m-%dT%H:%M:%SZ", os.time()) }
})
end

function SendContentToDiscord(webhookUrl, tagEveryone, embedContent)
if DefconCurrentStatus ~= 'r' and DefconCurrentStatus ~= 'g' and DefconCurrentStatus ~= 'y' then return end
if not webhookUrl or webhookUrl == '' or webhookUrl == nil then return end

local contentData = {}

contentData['embeds'] = embedContent

if tagEveryone then contentData['content'] = '@everyone' end

PerformHttpRequest(webhookUrl, function(e, t, h) end, 'POST', json.encode(contentData), { ['Content-Type'] = 'application/json' })
end
4 changes: 2 additions & 2 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
body { margin: 0; padding: 20px; }
#logo { position: absolute; top: 0; right: 0; opacity: 1; padding: unset; margin: unset; }
#alerta_defcon { position: absolute; top: 0; right: 0; opacity: 1; padding: unset; margin: unset; }
</style>
<script src="nui://game/ui/jquery.js" type="text/javascript"></script>
<script src="./script.js" type="text/javascript"></script>
</head>

<body>
<div id="logo"> <img id="img" src="./img/alerta_verde.png"> </div>
<div id="alerta_defcon"> <img id="defcon" src="./img/alerta_verde.png"> </div>
</body>
16 changes: 8 additions & 8 deletions ui/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ $(function () {
window.onload = function (e) {
window.addEventListener('message', function (event) {
var item = event.data;
if(item !== undefined && item.type === "logo") {
if(item !== undefined) {
const img = document.getElementById('img');
if(item.display === "alerta_verde") {
img.src = 'img/alerta_verde.png'
} else if(item.display === "alerta_amarilla") {
img.src = 'img/alerta_amarilla.png'
} else if(item.display === "alerta_roja") {
img.src = 'img/alerta_roja.png'
}

switch(item.alert) {
case 'g': $('#defcon').attr('src', 'img/alerta_verde.png'); break;
case 'y': $('#defcon').attr('src', 'img/alerta_amarilla.png'); break;
case 'r': $('#defcon').attr('src', 'img/alerta_roja.png'); break;
default: $('#defcon').attr('src', 'img/alerta_verde.png'); break;
}
}
});
};
Expand Down

0 comments on commit 946474d

Please sign in to comment.