Skip to content

Commit

Permalink
feat: auto-run SQL & restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgrimshaw committed Sep 5, 2024
1 parent d497444 commit c845ff7
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 13 deletions.
6 changes: 4 additions & 2 deletions client.lua → client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local function distanceCheck()
lastLocation = GetEntityCoords(vehicle)
end

local plate = string.gsub(GetVehicleNumberPlateText(GetVehiclePedIsIn(PlayerPedId(), false)), '^%s*(.-)%s*$', '%1')
local plate = string.gsub(GetVehicleNumberPlateText(GetVehiclePedIsIn(PlayerPedId(), false)), "^%s*(.-)%s*$", "%1")

if plate == currentVehPlate and not currentVehOwned and recheckCurrentVeh > 0 then
recheckCurrentVeh -= 1000
Expand Down Expand Up @@ -64,12 +64,14 @@ local function distanceCheck()

if roundedMileage ~= lastUpdatedMileage then
Entity(vehicle).state:set("vehicleMileage", roundedMileage)
TriggerServerEvent('jg-vehiclemileage:server:update-mileage', currentVehPlate, roundedMileage)
TriggerServerEvent("jg-vehiclemileage:server:update-mileage", currentVehPlate, roundedMileage)
lastUpdatedMileage = roundedMileage
end
end

CreateThread(function()
Wait(2000)

while true do
distanceCheck()
Wait(1000)
Expand Down
2 changes: 2 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Config = {}
Config.AutoRunSQL = true

Config.Framework = "auto" -- or "QBCore", "Qbox", "ESX"
Config.ShowMileage = true
Config.Unit = "miles" -- "miles" or "kilometers"
Expand Down
8 changes: 4 additions & 4 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ lua54 "yes"

author "JG Scripts"
description "Tracks vehicle mileage with UI"
version "v1.1.1"

client_script "client.lua"
version "v1.2"

shared_scripts {
"@ox_lib/init.lua",
"config.lua",
"main.lua"
}

client_script "client/*.lua"

server_scripts {
"@oxmysql/lib/MySQL.lua",
"server.lua"
"server/*.lua"
}

ui_page "web/index.html"
Expand Down
1 change: 1 addition & 0 deletions install/run-esx.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE owned_vehicles ADD COLUMN IF NOT EXISTS mileage FLOAT DEFAULT 0 NOT NULL
1 change: 1 addition & 0 deletions install/run-qb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE player_vehicles ADD COLUMN IF NOT EXISTS mileage FLOAT DEFAULT 0 NOT NULL
2 changes: 2 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Framework = {}

if (Config.Framework == "auto" and (GetResourceState("qbx_core") == "started" or GetResourceState("qb-core") == "started")) or Config.Framework == "Qbox" or Config.Framework == "QBCore" then
Config.Framework = "QBCore"
Framework.VehiclesTable = "player_vehicles"
elseif (Config.Framework == "auto" and GetResourceState("es_extended") == "started") or Config.Framework == "ESX" then
Config.Framework = "ESX"
Framework.VehiclesTable = "owned_vehicles"
else
error("You haven't set a valid framework. Valid options can be found in main.lua!")
Expand Down
5 changes: 0 additions & 5 deletions run.sql

This file was deleted.

4 changes: 2 additions & 2 deletions server.lua → server/server.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
lib.callback.register('jg-vehiclemileage:server:get-mileage', function(_, plate)
lib.callback.register("jg-vehiclemileage:server:get-mileage", function(_, plate)
local vehicle = MySQL.single.await("SELECT mileage FROM " .. Framework.VehiclesTable .. " WHERE plate = ?", {plate})
if not vehicle then return { error = true } end
return { mileage = vehicle.mileage }
end)

RegisterNetEvent('jg-vehiclemileage:server:update-mileage', function(plate, mileage)
RegisterNetEvent("jg-vehiclemileage:server:update-mileage", function(plate, mileage)
MySQL.update("UPDATE " .. Framework.VehiclesTable .. " SET mileage = ? WHERE plate = ?", {mileage, plate})
end)

Expand Down
14 changes: 14 additions & 0 deletions server/sv-initsql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if Config.AutoRunSQL then
if not pcall(function()
local fileName = (Config.Framework == "QBCore" or Config.Framework == "Qbox") and "run-qb.sql" or "run-esx.sql"

-- Open & read file
local file = assert(io.open(GetResourcePath(GetCurrentResourceName()) .. "/install/" .. fileName, "rb"))
local sql = file:read("*all")
file:close()

MySQL.query.await(sql)
end) then
print("^1[SQL ERROR] There was an error while automatically running the required SQL. Don't worry, you just need to run the SQL file for your framework, found in the 'install' folder manually. If you've already ran the SQL code previously, and this error is annoying you, set Config.AutoRunSQL = false^0")
end
end

0 comments on commit c845ff7

Please sign in to comment.