Update main.lua

* Added a version checker.
* Updated the method to find identifiers to make sure it always uses the license.
* Updated "exitGame" event so it's vulnerable to modders.
* Made code cleaner and optimized a little more.
This commit is contained in:
Andyyy7666
2021-12-24 22:43:27 +01:00
committed by GitHub
parent 92b054f630
commit da19b16394

View File

@@ -6,24 +6,78 @@
------------------------------------------------------------------------
------------------------------------------------------------------------
expectedName = "ND_Core" -- This is the resource and is not suggested to be changed.
resource = GetCurrentResourceName()
-- check if resource is renamed
if resource ~= expectedName then
print("^1[^4" .. expectedName .. "^1] WARNING^0")
print("Change the resource name to ^4" .. expectedName .. " ^0or else it won't work!")
end
-- check if resource version is up to date
PerformHttpRequest("https://raw.githubusercontent.com/Andyyy7666/ND_Framework/main/ND_Core/fxmanifest.lua", function(error, res, head)
i, j = string.find(tostring(res), "version")
res = string.sub(tostring(res), i, j + 6)
res = string.gsub(res, "version ", "")
res = string.gsub(res, '"', "")
resp = tonumber(res)
verFile = GetResourceMetadata(expectedName, "version", 0)
if verFile then
if tonumber(verFile) < resp then
print("^1[^4" .. expectedName .. "^1] WARNING^0")
print("^4" .. expectedName .. " ^0is outdated. Please update it from ^5https://github.com/Andyyy7666/ND_Framework ^0| Current Version: ^1" .. verFile .. " ^0| New Version: ^2" .. resp .. " ^0|")
elseif tonumber(verFile) > tonumber(resp) then
print("^1[^4" .. expectedName .. "^1] WARNING^0")
print("^4" .. expectedName .. "s ^0version number is higher than we expected. | Current Version: ^3" .. verFile .. " ^0| Expected Version: ^2" .. resp .. " ^0|")
else
print("^1[^4" .. expectedName .. "^1] WARNING^0")
print("^4" .. expectedName .. " ^0is up to date | Current Versin: ^2" .. verFile .. " ^0|")
end
else
print("^1[^4" .. expectedName .. "^1] WARNING^0")
print("You may not have the latest version of ^4" .. expectedName .. "^0. A newer, improved version may be present at ^5https://github.com/Andyyy7666/ND_Framework^0")
end
end)
-- Get player identifier function (This will not change the identifier all the time)
function GetPlayerIdentifierFromType(type, source) -- Credits: xander1998, Post: https://forum.cfx.re/t/solved-is-there-a-better-way-to-get-lic-steam-and-ip-than-getplayeridentifiers/236243/2?u=andyyy7666
local identifiers = {}
local identifierCount = GetNumPlayerIdentifiers(source)
for a = 0, identifierCount do
table.insert(identifiers, GetPlayerIdentifier(source, a))
end
for b = 1, #identifiers do
if string.find(identifiers[b], type, 1) then
return identifiers[b]
end
end
return nil
end
-- Using the IsRolePresent function from discord.lua to check if the player has the roles.
RegisterNetEvent("checkPerms")
AddEventHandler("checkPerms", function(role)
local player = source
local role = role
local rolePermission = IsRolePresent(player, role)
TriggerClientEvent("permsChecked", player, role, rolePermission)
end)
-- Disconnecting a player
RegisterNetEvent("exitGame")
AddEventHandler("exitGame", function(Player)
DropPlayer(Player, "Disconnected using framework.")
AddEventHandler("exitGame", function()
local player = source
DropPlayer(player, "Disconnected using framework.")
end)
-- Inserting the players characters into characters table
RegisterNetEvent("getCharacters")
AddEventHandler("getCharacters", function()
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("SELECT * FROM characters WHERE license = ?", {license}, function(result)
exports.oxmysql:execute("SELECT * FROM characters WHERE license = ?", {GetPlayerIdentifierFromType("license", player)}, function(result)
if result then
characters = {}
for i = 1, #result do
@@ -35,14 +89,15 @@ AddEventHandler("getCharacters", function()
end)
end)
-- Creating a new character and increasing the character id.
RegisterNetEvent("newCharacter")
AddEventHandler("newCharacter", function(newCharacter)
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("SELECT MAX(character_id) AS nextID FROM characters WHERE license LIKE ?", {license}, function(result)
local license = GetPlayerIdentifierFromType("license", player)
exports.oxmysql:execute("SELECT MAX(character_id) AS nextID FROM characters WHERE license = ?", {license}, function(result)
if result then
character_id = result[1].nextID
if character_id == nil then
if not character_id then
character_id = 1
else
character_id = character_id + 1
@@ -56,99 +111,59 @@ AddEventHandler("newCharacter", function(newCharacter)
end)
end)
-- Delete character from database.
RegisterNetEvent("delCharacter")
AddEventHandler("delCharacter", function(character_id)
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("DELETE FROM characters WHERE license LIKE ? AND character_id LIKE ?", {license, character_id}, function(id)
if id then
print(id)
end
end)
exports.oxmysql:execute("DELETE FROM characters WHERE license = ? AND character_id = ?", {GetPlayerIdentifierFromType("license", player), character_id})
end)
-- Update the character info when edited.
RegisterNetEvent("editCharacter")
AddEventHandler("editCharacter", function(newCharacter)
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("UPDATE characters SET first_name = ?, last_name = ?, dob = ?, gender = ?, twt = ?, department = ? WHERE license = ? AND character_id = ?", {newCharacter.firstName, newCharacter.lastName, newCharacter.dateOfBirth, newCharacter.gender, newCharacter.twtName, newCharacter.department, license, newCharacter.id}, function(id)
if id then
print(id)
end
end)
exports.oxmysql:execute("UPDATE characters SET first_name = ?, last_name = ?, dob = ?, gender = ?, twt = ?, department = ? WHERE license = ? AND character_id = ?", {newCharacter.firstName, newCharacter.lastName, newCharacter.dateOfBirth, newCharacter.gender, newCharacter.twtName, newCharacter.department, GetPlayerIdentifierFromType("license", player), newCharacter.id})
end)
if config.shotSpotterEnabled then
RegisterNetEvent("shotSpotterActive")
AddEventHandler("shotSpotterActive", function(x, y, z, postal)
TriggerClientEvent("shotSpotterReport", -1, x, y, z, postal)
end)
end
-- Planning on moving everything money related to server.
RegisterNetEvent("bankPay")
AddEventHandler("bankPay", function(characterid, playerid, amount, playerSending, name)
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("UPDATE characters SET bank = bank - ? WHERE license = ? AND character_id = ?", {amount, license, characterid}, function(bank)
if bank then
print(bank)
end
end)
exports.oxmysql:execute("UPDATE characters SET bank = bank - ? WHERE license = ? AND character_id = ?", {amount, GetPlayerIdentifierFromType("license", player), characterid})
TriggerClientEvent("receiveBank", playerid, amount, playerSending, name)
TriggerClientEvent("updateMoney", player)
end)
RegisterNetEvent("addBank")
AddEventHandler("addBank", function(amount, characterid)
local player = source
license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("UPDATE characters SET bank = bank + ? WHERE license = ? AND character_id = ?", {amount, license, characterid}, function(bank)
if bank then
print(bank)
end
end)
exports.oxmysql:execute("UPDATE characters SET bank = bank + ? WHERE license = ? AND character_id = ?", {amount, GetPlayerIdentifierFromType("license", player), characterid})
TriggerClientEvent("updateMoney", player)
end)
RegisterNetEvent("cashPay")
AddEventHandler("cashPay", function(characterid, playerid, amount, playerSending, name)
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("UPDATE characters SET cash = cash - ? WHERE license = ? AND character_id = ?", {amount, license, characterid}, function(cash)
if cash then
print(cash)
end
end)
exports.oxmysql:execute("UPDATE characters SET cash = cash - ? WHERE license = ? AND character_id = ?", {amount, GetPlayerIdentifierFromType("license", player), characterid})
TriggerClientEvent("receiveCash", playerid, amount, playerSending, name)
TriggerClientEvent("updateMoney", player)
end)
RegisterNetEvent("addCash")
AddEventHandler("addCash", function(amount, characterid)
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("UPDATE characters SET cash = cash + ? WHERE license = ? AND character_id = ?", {amount, license, characterid}, function(cash)
if cash then
print(cash)
end
end)
exports.oxmysql:execute("UPDATE characters SET cash = cash + ? WHERE license = ? AND character_id = ?", {amount, GetPlayerIdentifierFromType("license", player), characterid})
TriggerClientEvent("updateMoney", player)
end)
-- This is used to find out how much money the player has and use it in the client to show it on the ui.
RegisterNetEvent("getMoney")
AddEventHandler("getMoney", function(characterid)
local player = source
local license = GetPlayerIdentifier(player, 1)
exports.oxmysql:execute("SELECT cash, bank FROM characters WHERE license = ? AND character_id = ?", {license, characterid}, function(result)
exports.oxmysql:execute("SELECT cash, bank FROM characters WHERE license = ? AND character_id = ?", {GetPlayerIdentifierFromType("license", player), characterid}, function(result)
if result then
for i = 1, #result do
cash = result[i].cash
bank = result[i].bank
print(cash)
print(bank)
end
TriggerClientEvent("returnMoney", player, cash, bank)
end
end)
Citizen.Wait(300)
TriggerClientEvent("returnMoney", player, cash, bank)
end)