From c498a321ca962324ef60353346b8eefed01b8db2 Mon Sep 17 00:00:00 2001 From: 0xDEMXN <15928886+0xDEMXN@users.noreply.github.com> Date: Sun, 20 Feb 2022 20:12:07 +0100 Subject: [PATCH] refactor: major js rewrite + improvements everywhere --- .editorconfig | 12 ++ .prettierrc | 8 ++ client.lua | 192 ++++++++++++++-------------- config.lua | 6 +- fxmanifest.lua | 12 +- html/{ => css}/style.css | 2 + html/{ => images}/logo.png | Bin html/{ui.html => index.html} | 20 +-- html/js/script.js | 192 ++++++++++++++++++++++++++++ html/script.js | 238 ----------------------------------- 10 files changed, 333 insertions(+), 349 deletions(-) create mode 100644 .editorconfig create mode 100644 .prettierrc rename html/{ => css}/style.css (96%) rename html/{ => images}/logo.png (100%) rename html/{ui.html => index.html} (68%) create mode 100644 html/js/script.js delete mode 100644 html/script.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..88bf97c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = crlf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = false \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..ad35042 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "semi": true, + "trailingComma": "all", + "singleQuote": true, + "printWidth": 80, + "tabWidth": 2, + "arrowParens": "avoid" +} diff --git a/client.lua b/client.lua index 2ce8347..fa471d3 100644 --- a/client.lua +++ b/client.lua @@ -1,110 +1,118 @@ local playerId = PlayerId() +local maxUnderwaterTime = 10.0 local GeneralLoop = function() - CreateThread(function() - while ESX.PlayerLoaded do - local underwaterTime = GetPlayerUnderwaterTimeRemaining(playerId) * 10 - local isDriving = IsPedInAnyVehicle(ESX.PlayerData.ped, true) - local veh = isDriving and GetVehiclePedIsUsing(ESX.PlayerData.ped, false) - local speedMultiplier = isDriving and dx.metricSystem and 3.6 or 2.236936 - - SendNUIMessage({ - action = "general", - -- ped - hp = (GetEntityHealth(ESX.PlayerData.ped) - 100) * 100 / (GetEntityMaxHealth(ESX.PlayerData.ped) - 100), - armour = GetPedArmour(ESX.PlayerData.ped), - oxygen = IsPedSwimmingUnderWater(ESX.PlayerData.ped) and underwaterTime or 100, - -- vehicles - speedometer = isDriving and - { - speed = math.floor(GetEntitySpeed(veh) * speedMultiplier), - maxspeed = GetVehicleModelMaxSpeed(GetEntityModel(veh)) * speedMultiplier - }, - fuel = dx.showFuel and isDriving and GetVehicleFuelLevel(veh), - -- voice - showVoice = dx.showVoice, - voiceConnected = dx.showVoice and MumbleIsConnected(), - voiceTalking = dx.showVoice and NetworkIsPlayerTalking(playerId), - }) - - DisplayRadar(dx.persistentRadar or isDriving) - SetRadarZoom(1150) - SendNUIMessage({showUi = not IsPauseMenuActive()}) - - Wait(dx.generalRefreshRate) - end - SendNUIMessage({showUi = false}) - end) + CreateThread(function() + while ESX.PlayerLoaded do + SendNUIMessage({ + action = 'general', + visible = not IsPauseMenuActive() + }) + + SetRadarZoom(1150) + DisplayRadar(dx.persistentRadar or isDriving) + + local health = (GetEntityHealth(ESX.PlayerData.ped) - 100) / (GetEntityMaxHealth(ESX.PlayerData.ped) - 100) + local underwaterTime = GetPlayerUnderwaterTimeRemaining(playerId) / maxUnderwaterTime + local isDriving = IsPedInAnyVehicle(ESX.PlayerData.ped, true) + local veh = isDriving and GetVehiclePedIsUsing(ESX.PlayerData.ped, false) + local speedMultiplier = isDriving and dx.metricSystem and 3.6 or 2.236936 + local maxSpeed = isDriving and GetVehicleModelMaxSpeed(GetEntityModel(veh)) * speedMultiplier + + SendNUIMessage({ + action = 'base', + hp = health > 0 and health or 0, + armour = GetPedArmour(ESX.PlayerData.ped) / 100, + oxygen = underwaterTime >= 1 and IsPedSwimmingUnderWater(ESX.PlayerData.ped) and 0.99 or underwaterTime, + speed = isDriving and GetEntitySpeed(veh) * speedMultiplier / maxSpeed * 1.3, + fuel = dx.fuel and isDriving and GetVehicleFuelLevel(veh) / 100, + voice = { + toggled = dx.voice, + connected = dx.voice and MumbleIsConnected(), + talking = dx.voice and NetworkIsPlayerTalking(playerId), + } + }) + + Wait(dx.generalRefreshRate) + end + SendNUIMessage({ + action = 'general', + visible = false + }) + end) end local StatusLoop = function() - CreateThread(function() - local hunger, thirst, stress - - while ESX.PlayerLoaded do - - TriggerEvent('esx_status:getStatus', 'hunger', function(status) hunger = status.val / 10000 end) - TriggerEvent('esx_status:getStatus', 'thirst', function(status) thirst = status.val / 10000 end) - if dx.showStress then - TriggerEvent('esx_status:getStatus', 'stress', function(status) stress = status.val / 10000 end) - end - - SendNUIMessage({ - action = "status", - -- status - hunger = hunger, - thirst = thirst, - stress = dx.showStress and stress, - }) - - Wait(dx.statusRefreshRate) - end - end) + CreateThread(function() + local hunger, thirst, stress + + while ESX.PlayerLoaded do + + TriggerEvent('esx_status:getStatus', 'hunger', function(status) hunger = status.val / 10000 end) + TriggerEvent('esx_status:getStatus', 'thirst', function(status) thirst = status.val / 10000 end) + if dx.stress then TriggerEvent('esx_status:getStatus', 'stress', function(status) stress = status.val / 10000 end) end + + SendNUIMessage({ + action = 'status', + hunger = hunger, + thirst = thirst, + stress = dx.stress and stress, + }) + + Wait(dx.statusRefreshRate) + end + end) end local InitHUD = function () - GeneralLoop() - StatusLoop() - SendNUIMessage({playerId = GetPlayerServerId(playerId)}) + GeneralLoop() + StatusLoop() + SendNUIMessage({ + action = 'general', + visible = true, + playerId = GetPlayerServerId(playerId) + }) + while IsPedSwimmingUnderWater(ESX.PlayerData.ped) do Wait(5000) end + maxUnderwaterTime = GetPlayerUnderwaterTimeRemaining(playerId) end if dx.circleMap then - CreateThread(function() - RequestStreamedTextureDict("circlemap", false) - repeat Wait(100) until HasStreamedTextureDictLoaded("circlemap") - - AddReplaceTexture("platform:/textures/graphics", "radarmasksm", "circlemap", "radarmasksm") - - SetMinimapClipType(1) - SetMinimapComponentPosition('minimap', 'L', 'B', -0.017, 0.021, 0.207, 0.32) - SetMinimapComponentPosition('minimap_mask', 'L', 'B', 0.06, 0.05, 0.132, 0.260) - SetMinimapComponentPosition('minimap_blur', 'L', 'B', 0.005, -0.01, 0.166, 0.257) - - Wait(500) - SetRadarBigmapEnabled(true, false) - Wait(500) - SetRadarBigmapEnabled(false, false) - - local minimap = RequestScaleformMovie("minimap") - repeat Wait(100) until HasScaleformMovieLoaded(minimap) - - while true do - Wait(0) - BeginScaleformMovieMethod(minimap, "SETUP_HEALTH_ARMOUR") - ScaleformMovieMethodAddParamInt(3) - EndScaleformMovieMethod() - end - end) + CreateThread(function() + RequestStreamedTextureDict('circlemap', false) + repeat Wait(100) until HasStreamedTextureDictLoaded('circlemap') + + AddReplaceTexture('platform:/textures/graphics', 'radarmasksm', 'circlemap', 'radarmasksm') + + SetMinimapClipType(1) + SetMinimapComponentPosition('minimap', 'L', 'B', -0.017, 0.021, 0.207, 0.32) + SetMinimapComponentPosition('minimap_mask', 'L', 'B', 0.06, 0.05, 0.132, 0.260) + SetMinimapComponentPosition('minimap_blur', 'L', 'B', 0.005, -0.01, 0.166, 0.257) + + Wait(500) + SetRadarBigmapEnabled(true, false) + Wait(500) + SetRadarBigmapEnabled(false, false) + + local minimap = RequestScaleformMovie('minimap') + repeat Wait(100) until HasScaleformMovieLoaded(minimap) + + while true do + Wait(0) + BeginScaleformMovieMethod(minimap, 'SETUP_HEALTH_ARMOUR') + ScaleformMovieMethodAddParamInt(3) + EndScaleformMovieMethod() + end + end) end AddEventHandler('pma-voice:setTalkingMode', function(mode) - SendNUIMessage({action = "voice_range", voiceRange = mode}) + SendNUIMessage({action = 'voice', voiceRange = mode}) end) RegisterNetEvent('esx:playerLoaded') AddEventHandler('esx:playerLoaded', function() ESX.PlayerLoaded = true - InitHUD() + InitHUD() end) RegisterNetEvent('esx:onPlayerLogout') @@ -113,10 +121,10 @@ AddEventHandler('esx:onPlayerLogout', function() end) AddEventHandler('onResourceStart', function(resourceName) - if (resourceName == GetCurrentResourceName()) then - if ESX.PlayerLoaded then - Citizen.Wait(50) - InitHUD() - end + if (resourceName == GetCurrentResourceName()) then + if ESX.PlayerLoaded then + Citizen.Wait(50) + InitHUD() end + end end) \ No newline at end of file diff --git a/config.lua b/config.lua index a657277..4cb474b 100644 --- a/config.lua +++ b/config.lua @@ -13,13 +13,13 @@ dx = { -- true: speed will be kmh -- false: speed will be mph - showStress = false, + stress = true, -- setting this to true requires you to add a stress status - showFuel = false, + fuel = true, -- setting this to true requires you to add a fuel managment resource - showVoice = false, + voice = true, -- enabling this requires pma-voice circleMap = true diff --git a/fxmanifest.lua b/fxmanifest.lua index a2764fa..c8ed7b3 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -1,7 +1,7 @@ fx_version 'cerulean' game 'gta5' author '0xDEMXN' -version '1.0.8' +version '1.0.9' lua54 'yes' use_fxv2_oal 'yes' @@ -16,13 +16,13 @@ client_scripts { } files { - 'html/ui.html', - 'html/script.js', - 'html/style.css', - 'html/logo.png', + 'html/index.html', + 'html/js/script.js', + 'html/css/style.css', + 'html/images/logo.png', } -ui_page 'html/ui.html' +ui_page 'html/index.html' dependencies { 'es_extended', diff --git a/html/style.css b/html/css/style.css similarity index 96% rename from html/style.css rename to html/css/style.css index 6a93a88..900e4f9 100644 --- a/html/style.css +++ b/html/css/style.css @@ -10,6 +10,7 @@ body { .Container { margin-top: auto; margin-bottom: 1.5rem; + display: none; } .Icons { @@ -24,6 +25,7 @@ body { height: 2.5rem; border-radius: 50%; background: rgb(49, 49, 49); + display: none; } .Icon i { diff --git a/html/logo.png b/html/images/logo.png similarity index 100% rename from html/logo.png rename to html/images/logo.png diff --git a/html/ui.html b/html/index.html similarity index 68% rename from html/ui.html rename to html/index.html index ea19d94..32f0f36 100644 --- a/html/ui.html +++ b/html/index.html @@ -4,13 +4,13 @@ - + -