Skip to content

Commit

Permalink
support of negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tazi0 committed Apr 30, 2020
1 parent e155b78 commit 41d8677
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion esx_invest/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ Config.Keys = {
Config.InvestRateTime = 10 -- Minutes

-- Stock settings
Config.StockLimit = 5 -- %
-- in %
Config.Stock = {
Minimum = -5,
Maximum = 5
}

-- Debug mode
Config.Debug = false
13 changes: 9 additions & 4 deletions esx_invest/server/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ AddEventHandler("invest:sell", function(job)
for k, v in pairs(result) do result = v end

local amount = result.amount
local sellRate = result.investRate - result.rate
local sellRate = math.abs(result.investRate - result.rate)
local addMoney = amount + ((amount * sellRate) / 100)


Expand Down Expand Up @@ -178,7 +178,7 @@ AddEventHandler('onResourceStart', function(resourceName)

local companies = MySQL.Sync.fetchAll("SELECT * FROM `companies`")
for k, v in pairs(companies) do
newRate = genRand(0, Config.StockLimit, 2)
newRate = genRand(Config.Stock.Minimum, Config.Stock.Maximum, 2)

local rate = "stale"
if newRate > 1 then
Expand Down Expand Up @@ -206,7 +206,7 @@ AddEventHandler('onResourceStart', function(resourceName)
local companies = MySQL.Sync.fetchAll("SELECT * FROM `companies`")
for k, v in pairs(companies) do
if(v.investRate == nil) then
v.investRate = genRand(0, Config.StockLimit, 2)
v.investRate = genRand(Config.Stock.Minimum, Config.Stock.Maximum, 2)

MySQL.Sync.execute("UPDATE companies SET investRate=@rate WHERE label=@label", {
["@rate"] = v.investRate,
Expand All @@ -219,5 +219,10 @@ AddEventHandler('onResourceStart', function(resourceName)
loopUpdate()
end)

-- v1.2 >
-- print(genRand(0, 2, 2)) -- from 0.00 to 2.00
-- print(genRand(1, 2, 2)) -- from 1.00 to 2.00
-- print(genRand(1, 2, 2)) -- from 1.00 to 2.00

-- v1.2 <
-- print(genRand(-5, 5, 2)) -- from -5% to 5%
-- print(math.abs(-1 - -2.95)) -- difference between -1% and -2.95%

0 comments on commit 41d8677

Please sign in to comment.