From 717fdd843e959ebad16bce78a78194d702519cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20N=27gadi?= Date: Tue, 19 Sep 2017 10:23:34 +0200 Subject: [PATCH] chore(repo): Initial commit --- README.txt | 25 ++++++ __resource.lua | 11 +++ esx_license.sql | 10 +++ server/main.lua | 206 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 252 insertions(+) create mode 100644 README.txt create mode 100644 __resource.lua create mode 100644 esx_license.sql create mode 100644 server/main.lua diff --git a/README.txt b/README.txt new file mode 100644 index 00000000..3fbb1f02 --- /dev/null +++ b/README.txt @@ -0,0 +1,25 @@ +# esx_license +FXServer ESX License + +[INSTALLATION] + +[INSTALLATION] + +1) Using [fvm](https://github.com/qlaffont/fvm-installer) +``` +fvm install --save --folder=esx esx-org/esx_license + +``` + +2) Manually + +- Download https://github.com/ESX-Org/esx_license/releases/latest +- Put it in [esx] directory + + +1) Import esx_license.sql in your database +2) Add this in your server.cfg : + +``` +start esx_license +``` diff --git a/__resource.lua b/__resource.lua new file mode 100644 index 00000000..69621592 --- /dev/null +++ b/__resource.lua @@ -0,0 +1,11 @@ +resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937' + +description 'ESX License' + +version '1.0.0' + +server_scripts { + '@async/async.lua', + '@mysql-async/lib/MySQL.lua', + 'server/main.lua' +} \ No newline at end of file diff --git a/esx_license.sql b/esx_license.sql new file mode 100644 index 00000000..162637f1 --- /dev/null +++ b/esx_license.sql @@ -0,0 +1,10 @@ +USE `essentialmode`; + +CREATE TABLE `licenses` ( + + `id` int(11) NOT NULL AUTO_INCREMENT, + `type` varchar(255) NOT NULL, + `label` varchar(255) NOT NULL, + + PRIMARY KEY (`id`) +); \ No newline at end of file diff --git a/server/main.lua b/server/main.lua new file mode 100644 index 00000000..119eee0e --- /dev/null +++ b/server/main.lua @@ -0,0 +1,206 @@ +ESX = nil + +TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) + +function AddLicense(target, type, cb) + + local xPlayer = ESX.GetPlayerFromId(target) + + MySQL.Async.execute( + 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', + { + ['@type'] = type, + ['@owner'] = xPlayer.identifier + }, + function(rowsChanged) + if cb ~= nil then + cb() + end + end + ) + +end + +function RemoveLicense(target, type, cb) + + local xPlayer = ESX.GetPlayerFromId(target) + + MySQL.Async.execute( + 'DELETE FROM user_licenses WHERE type = @type AND owner = @owner', + { + ['@type'] = type, + ['@owner'] = xPlayer.identifier + }, + function(rowsChanged) + if cb ~= nil then + cb() + end + end + ) + +end + +function GetLicense(type, cb) + + MySQL.Async.fetchAll( + 'SELECT * FROM licenses WHERE type = @type', + { + ['@type'] = type + }, + function(result) + + local data = { + type = type, + label = result[1].label + } + + cb(data) + + end + ) + +end + +function GetLicenses(target, cb) + + local xPlayer = ESX.GetPlayerFromId(target) + + MySQL.Async.fetchAll( + 'SELECT * FROM user_licenses WHERE owner = @owner', + { + ['@owner'] = xPlayer.identifier + }, + function(result) + + local licenses = {} + local asyncTasks = {} + + for i=1, #result, 1 do + + local scope = function(type) + + table.insert(asyncTasks, function(cb) + + MySQL.Async.fetchAll( + 'SELECT * FROM licenses WHERE type = @type', + { + ['@type'] = type + }, + function(result2) + + table.insert(licenses, { + type = type, + label = result2[1].label + }) + + cb() + + end + ) + + end) + + end + + scope(result[i].type) + + end + + Async.parallel(asyncTasks, function(results) + cb(licenses) + end) + + end + ) + +end + +function CheckLicense(target, type, cb) + + local xPlayer = ESX.GetPlayerFromId(target) + + MySQL.Async.fetchAll( + 'SELECT COUNT(*) as count FROM user_licenses WHERE type = @type AND owner = @owner', + { + ['@type'] = type, + ['@owner'] = xPlayer.identifier + }, + function(result) + + if tonumber(result[1].count) > 0 then + cb(true) + else + cb(false) + end + + end + ) + +end + +function GetLicensesList(cb) + + MySQL.Async.fetchAll( + 'SELECT * FROM licenses', + { + ['@type'] = type + }, + function(result) + + local licenses = {} + + for i=1, #result, 1 do + table.insert(licenses, { + type = result[i].type, + label = result[i].label + }) + end + + cb(licenses) + + end + ) + +end + +RegisterNetEvent('esx_license:addLicense') +AddEventHandler('esx_license:addLicense', function(target, type, cb) + AddLicense(target, type, cb) +end) + +RegisterNetEvent('esx_license:removeLicense') +AddEventHandler('esx_license:removeLicense', function(target, type, cb) + RemoveLicense(target, type, cb) +end) + +AddEventHandler('esx_license:getLicense', function(type, cb) + GetLicense(type, cb) +end) + +AddEventHandler('esx_license:getLicenses', function(target, cb) + GetLicenses(target, cb) +end) + +AddEventHandler('esx_license:checkLicense', function(target, type, cb) + CheckLicense(target, type, cb) +end) + +AddEventHandler('esx_license:getLicensesList', function(cb) + GetLicensesList(cb) +end) + +ESX.RegisterServerCallback('esx_license:getLicense', function(source, cb, type) + GetLicense(type, cb) +end) + +ESX.RegisterServerCallback('esx_license:getLicenses', function(source, cb, target) + GetLicenses(target, cb) +end) + +ESX.RegisterServerCallback('esx_license:checkLicense', function(source, cb, target, type) + CheckLicense(target, type, cb) +end) + +ESX.RegisterServerCallback('esx_license:getLicensesList', function(source, cb) + GetLicensesList(cb) +end) \ No newline at end of file