Skip to content

Commit

Permalink
Money reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
GhzGarage committed Jan 15, 2024
1 parent 1dffb57 commit 36e4267
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Variables
local coin = Crypto.Coin
local QBCore = exports['qb-core']:GetCoreObject()
local bannedCharacters = {'%','$',';'}
local bannedCharacters = { '%', '$', ';' }

-- Function
local function RefreshCrypto()
Expand All @@ -28,13 +28,13 @@ end

local function GetTickerPrice() -- Touch = no help
local ticker_promise = promise.new()
PerformHttpRequest("https://min-api.cryptocompare.com/data/price?fsym=" .. Ticker.coin .. "&tsyms=" .. Ticker.currency .. '&api_key=' .. Ticker.Api_key, function(Error, Result, _)
PerformHttpRequest('https://min-api.cryptocompare.com/data/price?fsym=' .. Ticker.coin .. '&tsyms=' .. Ticker.currency .. '&api_key=' .. Ticker.Api_key, function(Error, Result, _)
local result_obj = json.decode(Result)
if not result_obj['Response'] then
local this_resolve = {error = Error, response_data = result_obj[string.upper(Ticker.currency)]}
local this_resolve = { error = Error, response_data = result_obj[string.upper(Ticker.currency)] }
ticker_promise:resolve(this_resolve) --- Could resolve Error aswell for more accurate Error messages? Solved in else
else
local this_resolve = {error = result_obj['Message']}
local this_resolve = { error = result_obj['Message'] }
ticker_promise:resolve(this_resolve)
end
end, 'GET')
Expand All @@ -54,8 +54,8 @@ end
local function HandlePriceChance()
local currentValue = Crypto.Worth[coin]
local prevValue = Crypto.Worth[coin]
local trend = math.random(0,100)
local event = math.random(0,100)
local trend = math.random(0, 100)
local event = math.random(0, 100)
local chance = event - Crypto.ChanceOfCrashOrLuck

if event > chance then
Expand All @@ -80,13 +80,13 @@ local function HandlePriceChance()

if Crypto.History[coin][4] then
-- Shift array index 1 to 3
for k=3,1,-1 do
Crypto.History[coin][k] = Crypto.History[coin][k+1]
for k = 3, 1, -1 do
Crypto.History[coin][k] = Crypto.History[coin][k + 1]
end
-- Assign array index 4 to the latest result
Crypto.History[coin][4] = {PreviousWorth = prevValue, NewWorth = currentValue}
Crypto.History[coin][4] = { PreviousWorth = prevValue, NewWorth = currentValue }
else
Crypto.History[coin][#Crypto.History[coin] + 1] = {PreviousWorth = prevValue, NewWorth = currentValue}
Crypto.History[coin][#Crypto.History[coin] + 1] = { PreviousWorth = prevValue, NewWorth = currentValue }
end

Crypto.Worth[coin] = currentValue
Expand All @@ -102,7 +102,7 @@ local function HandlePriceChance()
props,
function(affectedRows)
if affectedRows < 1 then
print("Crypto not found, inserting new record for " .. coin)
print('Crypto not found, inserting new record for ' .. coin)
MySQL.insert('INSERT INTO crypto (crypto, worth, history) VALUES (:crypto, :worth, :history)', props)
end
RefreshCrypto()
Expand All @@ -112,7 +112,7 @@ end

-- Commands

QBCore.Commands.Add("setcryptoworth", "Set crypto value", {{name="crypto", help="Name of the crypto currency"}, {name="Value", help="New value of the crypto currency"}}, false, function(source, args)
QBCore.Commands.Add('setcryptoworth', 'Set crypto value', { { name = 'crypto', help = 'Name of the crypto currency' }, { name = 'Value', help = 'New value of the crypto currency' } }, false, function(source, args)
local src = source
local crypto = tostring(args[1])

Expand All @@ -122,58 +122,58 @@ QBCore.Commands.Add("setcryptoworth", "Set crypto value", {{name="crypto", help=

if NewWorth ~= nil then
local PercentageChange = math.ceil(((NewWorth - Crypto.Worth[crypto]) / Crypto.Worth[crypto]) * 100)
local ChangeLabel = "+"
local ChangeLabel = '+'

if PercentageChange < 0 then
ChangeLabel = "-"
ChangeLabel = '-'
PercentageChange = (PercentageChange * -1)
end

if Crypto.Worth[crypto] == 0 then
PercentageChange = 0
ChangeLabel = ""
ChangeLabel = ''
end

Crypto.History[crypto][#Crypto.History[crypto]+1] = {
Crypto.History[crypto][#Crypto.History[crypto] + 1] = {
PreviousWorth = Crypto.Worth[crypto],
NewWorth = NewWorth
}

TriggerClientEvent('QBCore:Notify', src, "You have changed the value of "..Crypto.Labels[crypto].." from: $"..Crypto.Worth[crypto].." to: $"..NewWorth.." ("..ChangeLabel.." "..PercentageChange.."%)")
TriggerClientEvent('QBCore:Notify', src, 'You have changed the value of ' .. Crypto.Labels[crypto] .. ' from: $' .. Crypto.Worth[crypto] .. ' to: $' .. NewWorth .. ' (' .. ChangeLabel .. ' ' .. PercentageChange .. '%)')
Crypto.Worth[crypto] = NewWorth
TriggerClientEvent('qb-crypto:client:UpdateCryptoWorth', -1, crypto, NewWorth)
MySQL.insert('INSERT INTO crypto (worth, history) VALUES (:worth, :history) ON DUPLICATE KEY UPDATE worth = :worth, history = :history', {
['worth'] = NewWorth,
['history'] = json.encode(Crypto.History[crypto]),
})
else
TriggerClientEvent('QBCore:Notify', src, Lang:t('text.you_have_not_given_a_new_value', {crypto = Crypto.Worth[crypto]}))
TriggerClientEvent('QBCore:Notify', src, Lang:t('text.you_have_not_given_a_new_value', { crypto = Crypto.Worth[crypto] }))
end
else
TriggerClientEvent('QBCore:Notify', src, Lang:t('text.this_crypto_does_not_exist'))
end
else
TriggerClientEvent('QBCore:Notify', src, Lang:t('text.you_have_not_provided_crypto_available_qbit'))
end
end, "admin")
end, 'admin')

QBCore.Commands.Add("checkcryptoworth", "", {}, false, function(source)
QBCore.Commands.Add('checkcryptoworth', '', {}, false, function(source)
local src = source
TriggerClientEvent('QBCore:Notify', src, Lang:t('text.the_qbit_has_a_value_of', {crypto = Crypto.Worth["qbit"]}))
TriggerClientEvent('QBCore:Notify', src, Lang:t('text.the_qbit_has_a_value_of', { crypto = Crypto.Worth['qbit'] }))
end)

QBCore.Commands.Add("crypto", "", {}, false, function(source)
QBCore.Commands.Add('crypto', '', {}, false, function(source)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local MyPocket = math.ceil(Player.PlayerData.money.crypto * Crypto.Worth["qbit"])
local MyPocket = math.ceil(Player.PlayerData.money.crypto * Crypto.Worth['qbit'])

TriggerClientEvent('QBCore:Notify', src, Lang:t('text.you_have_with_a_value_of',{playerPlayerDataMoneyCrypto = Player.PlayerData.money.crypto,mypocket = MyPocket}))
TriggerClientEvent('QBCore:Notify', src, Lang:t('text.you_have_with_a_value_of', { playerPlayerDataMoneyCrypto = Player.PlayerData.money.crypto, mypocket = MyPocket }))
end)

-- Events

RegisterServerEvent('qb-crypto:server:FetchWorth', function()
for name,_ in pairs(Crypto.Worth) do
for name, _ in pairs(Crypto.Worth) do
local result = MySQL.query.await('SELECT * FROM crypto WHERE crypto = ?', { name })
if result[1] ~= nil then
Crypto.Worth[name] = result[1].worth
Expand All @@ -190,11 +190,11 @@ end)
RegisterServerEvent('qb-crypto:server:ExchangeFail', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local ItemData = Player.Functions.GetItemByName("cryptostick")
local ItemData = Player.Functions.GetItemByName('cryptostick')

if ItemData ~= nil then
Player.Functions.RemoveItem("cryptostick", 1)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items["cryptostick"], "remove")
Player.Functions.RemoveItem('cryptostick', 1)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items['cryptostick'], 'remove')
TriggerClientEvent('QBCore:Notify', src, Lang:t('error.cryptostick_malfunctioned'), 'error')
end
end)
Expand All @@ -216,7 +216,7 @@ end)
RegisterServerEvent('qb-crypto:server:ExchangeSuccess', function(LuckChance)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local ItemData = Player.Functions.GetItemByName("cryptostick")
local ItemData = Player.Functions.GetItemByName('cryptostick')

if ItemData ~= nil then
local LuckyNumber = math.random(1, 10)
Expand All @@ -226,19 +226,19 @@ RegisterServerEvent('qb-crypto:server:ExchangeSuccess', function(LuckChance)
Amount = (math.random(1599999, 2599999) / DeelNumber)
end

Player.Functions.RemoveItem("cryptostick", 1)
Player.Functions.AddMoney('crypto', Amount)
TriggerClientEvent('QBCore:Notify', src, Lang:t('success.you_have_exchanged_your_cryptostick_for',{amount = Amount}), "success", 3500)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items["cryptostick"], "remove")
TriggerClientEvent('qb-phone:client:AddTransaction', src, Player, {}, Lang:t('credit.there_are_amount_credited',{amount = Amount}), "Credit")
Player.Functions.RemoveItem('cryptostick', 1)
Player.Functions.AddMoney('crypto', Amount, 'crypto stick exchange')
TriggerClientEvent('QBCore:Notify', src, Lang:t('success.you_have_exchanged_your_cryptostick_for', { amount = Amount }), 'success', 3500)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items['cryptostick'], 'remove')
TriggerClientEvent('qb-phone:client:AddTransaction', src, Player, {}, Lang:t('credit.there_are_amount_credited', { amount = Amount }), 'Credit')
end
end)

-- Callbacks

QBCore.Functions.CreateCallback('qb-crypto:server:HasSticky', function(source, cb)
local Player = QBCore.Functions.GetPlayer(source)
local Item = Player.Functions.GetItemByName("cryptostick")
local Item = Player.Functions.GetItemByName('cryptostick')

if Item ~= nil then
cb(true)
Expand All @@ -253,25 +253,25 @@ QBCore.Functions.CreateCallback('qb-crypto:server:GetCryptoData', function(sourc
History = Crypto.History[name],
Worth = Crypto.Worth[name],
Portfolio = Player.PlayerData.money.crypto,
WalletId = Player.PlayerData.metadata["walletid"],
WalletId = Player.PlayerData.metadata['walletid'],
}

cb(CryptoData)
end)

QBCore.Functions.CreateCallback('qb-crypto:server:BuyCrypto', function(source, cb, data)
local Player = QBCore.Functions.GetPlayer(source)
local total_price = math.floor(tonumber(data.Coins) * tonumber(Crypto.Worth["qbit"]))
local total_price = math.floor(tonumber(data.Coins) * tonumber(Crypto.Worth['qbit']))
if Player.PlayerData.money.bank >= total_price then
local CryptoData = {
History = Crypto.History["qbit"],
Worth = Crypto.Worth["qbit"],
History = Crypto.History['qbit'],
Worth = Crypto.Worth['qbit'],
Portfolio = Player.PlayerData.money.crypto + tonumber(data.Coins),
WalletId = Player.PlayerData.metadata["walletid"],
WalletId = Player.PlayerData.metadata['walletid'],
}
Player.Functions.RemoveMoney('bank', total_price)
TriggerClientEvent('qb-phone:client:AddTransaction', source, Player, data, Lang:t('credit.you_have_qbit_purchased',{dataCoins = tonumber(data.Coins)}), "Credit")
Player.Functions.AddMoney('crypto', tonumber(data.Coins))
Player.Functions.RemoveMoney('bank', total_price, 'bought crypto')
TriggerClientEvent('qb-phone:client:AddTransaction', source, Player, data, Lang:t('credit.you_have_qbit_purchased', { dataCoins = tonumber(data.Coins) }), 'Credit')
Player.Functions.AddMoney('crypto', tonumber(data.Coins), 'bought crypto')
cb(CryptoData)
else
cb(false)
Expand All @@ -283,15 +283,15 @@ QBCore.Functions.CreateCallback('qb-crypto:server:SellCrypto', function(source,

if Player.PlayerData.money.crypto >= tonumber(data.Coins) then
local CryptoData = {
History = Crypto.History["qbit"],
Worth = Crypto.Worth["qbit"],
History = Crypto.History['qbit'],
Worth = Crypto.Worth['qbit'],
Portfolio = Player.PlayerData.money.crypto - tonumber(data.Coins),
WalletId = Player.PlayerData.metadata["walletid"],
WalletId = Player.PlayerData.metadata['walletid'],
}
Player.Functions.RemoveMoney('crypto', tonumber(data.Coins))
local amount = math.floor(tonumber(data.Coins) * tonumber(Crypto.Worth["qbit"]))
TriggerClientEvent('qb-phone:client:AddTransaction', source, Player, data, Lang:t('debit.you_have_sold',{dataCoins = tonumber(data.Coins)}), "Debit")
Player.Functions.AddMoney('bank', amount)
Player.Functions.RemoveMoney('crypto', tonumber(data.Coins), 'sold crypto')
local amount = math.floor(tonumber(data.Coins) * tonumber(Crypto.Worth['qbit']))
TriggerClientEvent('qb-phone:client:AddTransaction', source, Player, data, Lang:t('debit.you_have_sold', { dataCoins = tonumber(data.Coins) }), 'Debit')
Player.Functions.AddMoney('bank', amount, 'sold crypto')
cb(CryptoData)
else
cb(false)
Expand All @@ -313,37 +313,37 @@ QBCore.Functions.CreateCallback('qb-crypto:server:TransferCrypto', function(sour
local result = MySQL.query.await('SELECT * FROM `players` WHERE `metadata` LIKE ?', { query })
if result[1] ~= nil then
local CryptoData = {
History = Crypto.History["qbit"],
Worth = Crypto.Worth["qbit"],
History = Crypto.History['qbit'],
Worth = Crypto.Worth['qbit'],
Portfolio = Player.PlayerData.money.crypto - tonumber(data.Coins),
WalletId = Player.PlayerData.metadata["walletid"],
WalletId = Player.PlayerData.metadata['walletid'],
}
Player.Functions.RemoveMoney('crypto', tonumber(data.Coins))
TriggerClientEvent('qb-phone:client:AddTransaction', source, Player, data, "You have "..tonumber(data.Coins).." Qbit('s) transferred!", "Debit")
Player.Functions.RemoveMoney('crypto', tonumber(data.Coins), 'transfer crypto')
TriggerClientEvent('qb-phone:client:AddTransaction', source, Player, data, 'You have ' .. tonumber(data.Coins) .. " Qbit('s) transferred!", 'Debit')
local Target = QBCore.Functions.GetPlayerByCitizenId(result[1].citizenid)

if Target ~= nil then
Target.Functions.AddMoney('crypto', tonumber(data.Coins))
TriggerClientEvent('qb-phone:client:AddTransaction', Target.PlayerData.source, Player, data, "There are "..tonumber(data.Coins).." Qbit('s) credited!", "Credit")
Target.Functions.AddMoney('crypto', tonumber(data.Coins), 'transfer crypto')
TriggerClientEvent('qb-phone:client:AddTransaction', Target.PlayerData.source, Player, data, 'There are ' .. tonumber(data.Coins) .. " Qbit('s) credited!", 'Credit')
else
local MoneyData = json.decode(result[1].money)
MoneyData.crypto = MoneyData.crypto + tonumber(data.Coins)
MySQL.update('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(MoneyData), result[1].citizenid })
end
cb(CryptoData)
else
cb("notvalid")
cb('notvalid')
end
else
cb("notenough")
cb('notenough')
end
end)

-- Threads

CreateThread(function()
while true do
Wait(Crypto.RefreshTimer*60000)
Wait(Crypto.RefreshTimer * 60000)
HandlePriceChance()
end
end)
Expand All @@ -355,10 +355,10 @@ if Ticker.Enabled then
if Ticker.tick_time < 2 then
Interval = 120000
end
while(true) do
while (true) do
local get_coin_price = GetTickerPrice()
if type(get_coin_price) == 'number' then
Crypto.Worth["qbit"] = get_coin_price
Crypto.Worth['qbit'] = get_coin_price
else
print('\27[31m' .. get_coin_price .. '\27[0m')
Ticker.Enabled = false
Expand Down

0 comments on commit 36e4267

Please sign in to comment.