Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #45 from FiveM-Scripts/1.3
Browse files Browse the repository at this point in the history
1.3
  • Loading branch information
ghermans authored Dec 9, 2017
2 parents 8009325 + 1b3446f commit 965d4da
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## v1.3
- Added mysql-async support.
- Removed the stats preview since this will get replaced in the future.

## v1.2.4
This update contains a hotfix regarding issue #42, no other changes have been made.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# fs_freemode
[![Version](https://img.shields.io/badge/Version-1.2.3-brightgreen.svg)]()
[![Version](https://img.shields.io/badge/Version-1.3-brightgreen.svg)]()
<a href="https://discord.gg/Cgr5FU6" title="Chat on Discord"><img alt="Discord Status" src="https://discordapp.com/api/guilds/285462938691567627/widget.png"></a>

## Requirements
Expand Down
1 change: 1 addition & 0 deletions __resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ client_scripts {
'ext/trains.lua'
}

server_script '@mysql-async/lib/MySQL.lua'
server_scripts {
'config.lua',
'database.lua',
Expand Down
2 changes: 1 addition & 1 deletion config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Setup.ServerName = "My Server"
Setup.DisplayWelcomeNotification = true
Setup.blips = true
Setup.Money = 500000
Setup.NativeMoney = true
Setup.NativeMoney = false
Setup.Currency = "$"
Setup.WantedMusicDisabled = true
Setup.WantedHeadDisplay = true
Expand Down
3 changes: 2 additions & 1 deletion database.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
database = {
driver = GetConvar('database_driver', 'mysql-async'),
host = GetConvar('es_couchdb_url', '127.0.0.1'),
port = GetConvar('es_couchdb_port', '5984'),
username = GetConvar('es_couchdb_password', 'admin:changeme')
}
}
48 changes: 36 additions & 12 deletions int/weapons_s.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,46 @@ AddEventHandler('fivem-stores:weapon-menu:item-selected', function(s)
local player = user.getIdentifier()
if user.getMoney() >= tonumber(s.Price) then
TriggerClientEvent('fivem-stores:giveWeapon', src, s.Name, s.FriendlyName)
TriggerEvent('es:exposeDBFunctions', function(db)
db.getDocumentByRow('fs_freemode', 'identifier', player, function(freemodeuser)
local myWeapons = false
for k,v in ipairs(freemodeuser.weapons) do
if (v == s.Name) then
myWeapons = true
if(database.driver == "couchdb") then
TriggerEvent('es:exposeDBFunctions', function(db)
db.getDocumentByRow('fs_freemode', 'identifier', player, function(freemodeuser)
local myWeapons = false
for k,v in ipairs(freemodeuser.weapons) do
if (v == s.Name) then
myWeapons = true
end
end

if not myWeapons then
freemodeuser.weapons[#freemodeuser.weapons+1] = s.Name
db.updateDocument('fs_freemode', freemodeuser._id, {weapons = freemodeuser.weapons}, function() end)
user.removeMoney(tonumber(s.Price))
end
end)
end)
elseif(database.driver == "mysql-async") then
MySQL.Async.fetchScalar("SELECT weapons FROM fs_freemode WHERE identifier = '"..player.."'", { ['@identifier'] = player}, function (weapons)
if(weapons) then
local arrayWeapons = json.decode(weapons)
local myWeapons = false
for k,v in ipairs(arrayWeapons) do
if (v == s.Name) then
print(k,v)
myWeapons = true
end
end
end

if not myWeapons then
freemodeuser.weapons[#freemodeuser.weapons+1] = s.Name
db.updateDocument('fs_freemode', freemodeuser._id, {weapons = freemodeuser.weapons}, function() end)
user.removeMoney(tonumber(s.Price))
if not myWeapons then
arrayWeapons[#arrayWeapons+1] = s.Name
local update = json.encode(arrayWeapons)
MySQL.Async.execute("UPDATE fs_freemode SET weapons='"..update.."' WHERE identifier = '"..player.."'", {})
user.removeMoney(tonumber(s.Price))
end
end
end)
end)
else
print("Error : database driver not recognized!")
end
end
end)
end)
81 changes: 54 additions & 27 deletions libs/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,68 @@ pbytes =''

local auth = toBase64Enc(pbytes)

if(database.driver == "couchdb") then
TriggerEvent('es:exposeDBFunctions', function(DB)
db.createDocument = DB.createDocument
DB.createDatabase('fs_freemode', function()end)
end)
else
MySQL.ready(function()
MySQL.Async.execute("CREATE TABLE IF NOT EXISTS fs_freemode (identifier varchar(255), weapons json DEFAULT NULL)")
end)
end

function db.getUser(identifier, callback)
local qu = {selector = {["identifier"] = identifier}}
PerformHttpRequest("http://" .. database['host'] .. ":" .. database['port'] .. "/fs_freemode/_find", function(err, rText, headers)
local t = json.decode(rText)
if(t.docs[1])then
callback(t.docs[1])
else
callback(false)
end
if(database.driver == "couchdb") then
local qu = {selector = {["identifier"] = identifier}}
PerformHttpRequest("http://" .. database['host'] .. ":" .. database['port'] .. "/fs_freemode/_find", function(err, rText, headers)
local t = json.decode(rText)
if(t.docs[1])then
callback(t.docs[1])
else
callback(false)
end
end, "POST", json.encode(qu), {["Content-Type"] = 'application/json', Authorization = "Basic " .. auth})
end

function db.updateUser(identifier, update, callback)
db.getUser(identifier, function(user)
for i in pairs(update)do
user[i] = update[i]
end

PerformHttpRequest("http://" .. database['host'] .. ":" .. database['port'] .. "/fs_freemode/" .. user._id, function(err, rText, headers)
callback((err or true))
end, "PUT", json.encode(user), {["Content-Type"] = 'application/json', Authorization = "Basic " .. auth})
end)
elseif(database.driver == "mysql-async") then
print("Loading data from mysql-server for: " .. identifier)
MySQL.Async.fetchAll("SELECT * FROM fs_freemode WHERE identifier = '"..identifier.."'", { ['@identifier'] = identifier}, function (result)
if (result) then
local user = {
identifier = result[1].identifier,
weapons = json.decode(result[1].weapons)
}
callback(user)
else
callback(false)
end
end)
else
print("Error: database driver not recognized!")
end
end

TriggerEvent('es:exposeDBFunctions', function(DB)
db.createDocument = DB.createDocument
DB.createDatabase('fs_freemode', function()end)
end)

local created = {}
AddEventHandler('es:newPlayerLoaded', function(source, user)
TriggerEvent('es:exposeDBFunctions', function(db)
db.createDocument('fs_freemode', {identifier = user.get("identifier"), weapons = {}}, function()
local src = source
if(database.driver == "couchdb") then
TriggerEvent('es:exposeDBFunctions', function(db)
db.createDocument('fs_freemode', {identifier = user.get("identifier"), weapons = {}}, function()
end)
end)
created[source] = true
created[source] = true
elseif(database.driver == "mysql-async") then
local identifier = tostring(user.get("identifier"))
print("Checking if player exists " ..identifier)

MySQL.Async.fetchScalar("SELECT identifier FROM fs_freemode WHERE identifier = '"..identifier.."'", { ['@identifier'] = identifier}, function (userIdentifier)
if(userIdentifier == nil) then
print("Adding identifier " .. identifier)
MySQL.Async.execute("INSERT INTO fs_freemode (`identifier`, `weapons`) VALUES (@identifier, @weapons)", { ['@identifier'] = identifier, ['@weapons'] = json.encode({"WEAPON_PISTOL", "WEAPON_KNIFE"}), })
end
end)
created[source] = true
else
print("Error : driver not recognize !")
end
end)
21 changes: 7 additions & 14 deletions server.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local version = 'v1.2.4'
local version = 'v1.3'

TriggerEvent("es:setDefaultSettings", {
debugInformation = false,
Expand All @@ -18,21 +18,14 @@ AddEventHandler("fs_freemode:playerSpawned", function(spawn)
TriggerEvent('fs_freemode:loadWeapons', source)
end)

RegisterServerEvent("fs_freemode:initStats")
AddEventHandler("fs_freemode:initStats", function()
local src = source
TriggerEvent('es:getPlayerFromId', src, function(user)
TriggerClientEvent('fs_freemode:initStats', src, user.getMoney())
end)
end)

RegisterServerEvent("fs_freemode:loadWeapons")
AddEventHandler("fs_freemode:loadWeapons", function(source)
TriggerEvent('es:getPlayerFromId', source, function(user)
db.getUser(user.getIdentifier(), function(freemodeuser)
for i=1, #freemodeuser.weapons do
TriggerClientEvent('fs_freemode:spawnWeapons', source, freemodeuser.weapons[i])
end
local src = source
TriggerEvent('es:getPlayerFromId', src, function(user)
db.getUser(user.getIdentifier(), function(freemodeuser)
for i=1, #freemodeuser.weapons do
TriggerClientEvent('fs_freemode:spawnWeapons', source, freemodeuser.weapons[i])
end
end)
end)
end)
Expand Down

0 comments on commit 965d4da

Please sign in to comment.