Jump to content

Business system bank withdraw bug


Recommended Posts

Hi guys! 

I try to make a business  system from scratch. Everything is fine until i got to the point of making the withdraw part  for example my payout is 5500$ and my bank is 11.000$ i withdraw 11k now and my db updates and the bank is 0 but when the payout time ends and my db gets updated my bank is not 5500$ is 16.500$.

Also no errors in debugscript 3 

Server Side

local seconds = 1000 
local minute = 60 * seconds
local hour = 60* minute

local conexiune = dbConnect( "sqlite", "afacere.db" )
if conexiune then
    outputDebugString( "Connection with AFACERE database was successfully established." )
else
    outputDebugString( "Connection with database couldn't be established." )
end

afacerebase = function()
    dbExec(conexiune,"CREATE TABLE IF NOT EXISTS afacere (NR INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,x INTEGER,y INTEGER,z INTEGER,cost INT,owner TEXT,payout INT, time INT,balance INT)")
end 
addEventHandler("onResourceStart",getRootElement(),afacerebase)

-- s 
-- m 
-- h
createAfacere = function(player,commandName,cost,payout,type,time)
    local owner = "DE VANZARE"
    local x,y,z = getElementPosition(player)
    local balance = 0
    if time == nil then return end
        if tostring(type) == "h" then 
            dbExec(conexiune,"INSERT INTO afacere (x,y,z,cost,owner,payout,time,balance) VALUES (?,?,?,?,?,?,?,?)",x,y,z,cost,owner,payout,tonumber(hour*time),balance)
            outputDebugString("1")
                elseif tostring(type) == "s" then
                    dbExec(conexiune,"INSERT INTO afacere (x,y,z,cost,owner,payout,time,balance) VALUES (?,?,?,?,?,?,?,?)",x,y,z,cost,owner,payout,tonumber(seconds * time),balance)
                    outputDebugString("2")        
                elseif tostring(type) == "m" then 
                                dbExec(conexiune,"INSERT INTO afacere (x,y,z,cost,owner,payout,time,balance) VALUES (?,?,?,?,?,?,?,?)",x,y,z,cost,owner,payout,tonumber(minute * time),balance)
                                outputDebugString("3")    
                            end
                        createPickup(x,y,z,3,1274,1)
                        createBlip(x,y,z,52,2.5,150,150,150,255,0,200)
                    end
addCommandHandler("afacere",createAfacere,false,false)

deleteAfacere = function(player,commandName,cod)
    if cod == nil then return outputChatBox("[AFACERE] Nu ai introdus un ID syntax error <cod>",source) end
        local query = dbQuery(conexiune,"SELECT NR FROM afacere WHERE NR=? LIMIT 1",tonumber(cod))
            local result = dbPoll(query,-1)
                    if result then 
                            for k,v in ipairs(result) do 
                                    if tonumber(v.NR) ~= tonumber(cod) then
                                outputChatBox("EROR",source,255,0,0)
                            else 
                        dbExec(conexiune,"DELETE FROM afacere WHERE NR=?",tonumber(cod))
                    outputChatBox("Succes",source)
                    end
            end
        else 
            outputChatBox("NOT FOUND 404",source,255,10,10)
        end 
end
addCommandHandler("killafacere",deleteAfacere,false,false)

startloading = function()
    local query = dbQuery(conexiune,"SELECT * FROM afacere")
    local result = dbPoll(query,-1)
    if result then 
    for k,v in ipairs(result) do
    afacereglobala = createPickup(v.x,v.y,v.z,3,1274,1)
    createBlip(v.x,v.y,v.z,52,2.5,150,150,150,255,0,200) 
    setElementData(afacereglobala,"afacere_ID",v.NR)
    setElementData(afacereglobala,"x",v.x)
    setElementData(afacereglobala,"y",v.y)
    setElementData(afacereglobala,"z",v.z)
    setElementData(afacereglobala,"afacere_cost",v.cost)
    setElementData(afacereglobala,"afacere_owner",tostring(v.owner))
    setElementData(afacereglobala,"afacere_payout",v.payout)
    setElementData(afacereglobala,"afacere_time",v.time)
    setElementData(afacereglobala,"afacere_balance",v.balance)
    local TIMP = setTimer(start_money,v.time,1,afacereglobala);
        end
    end
end

addEventHandler("onResourceStart",getRootElement(),startloading)


      addEventHandler("onResourceStart", resourceRoot, function() 
        for k, v in ipairs(getElementsByType("player")) do
          bindKey(v, "1", "down", "afacere")
        end
    end)
    
    addEventHandler("onPlayerJoin", root, function() 
        bindKey(source, "1", "down", "afacere")
    end)
    
    function open_menu(p)
        if getElementData(p,"open_w") == false then 
        local x,y,z = getElementPosition(p)
        local query = dbQuery(conexiune,"SELECT * FROM afacere")
        local result = dbPoll(query,-1)
        if result then 
        for k,v in ipairs(result) do
        if getDistanceBetweenPoints3D(x,y,z,v.x,v.y,v.z) <= 2.5 then 
        triggerClientEvent(p,"afacere_menu",p,v.NR,v.owner,v.payout,v.cost,v.balance,v.time,v.x,v.y,v.z)
                    end
                end
            end
            else
        end
    end
    addCommandHandler("afacere", open_menu)

addEvent("buy_afacere",true)

buy_now = function(ID,payout,balance,timer,nume)
    local nume = nume
        dbExec(conexiune,"UPDATE afacere SET owner=? WHERE NR=?",tostring(nume),tonumber(ID))
        setTimer(start_money,timer,1,afacereglobala)    
end
addEventHandler("buy_afacere",getRootElement(),buy_now)


start_money = function(afacereglobala)
    local ID = getElementData(afacereglobala,"afacere_ID")
    local x= getElementData(afacereglobala,"x")
    local y= getElementData(afacereglobala,"y")
    local z= getElementData(afacereglobala,"z")
    local cost getElementData(afacereglobala,"afacere_cost")
    local owner = getElementData(afacereglobala,"afacere_owner")
    local payout = getElementData(afacereglobala,"afacere_payout")
    local time = getElementData(afacereglobala,"afacere_time")
    local lock = getElementData(afacereglobala,"afacere_balance_lock")
    local balance = getElementData(afacereglobala,"afacere_balance")
        if owner ~= "DE VANZARE" then  
    balance = balance + payout
    dbExec(conexiune,"UPDATE afacere SET balance=? WHERE NR=?",balance,ID)
end
    local TIMP = setTimer(start_money,time,1,afacereglobala)
    setElementData(afacereglobala,"afacere_balance",balance)
end


addEvent("get_money",true)

get_money = function(name,ids,acc_balance)
    local bank = tonumber(getElementData(afacereglobala,"afacere_balance"))
    if name ~= "DE VANZARE" then 
    givePlayerMoney(source,acc_balance)
    setElementData(afacereglobala,"afacere_balance",0)
    dbExec(conexiune,"UPDATE afacere SET balance=? where NR=?",bank - bank,ids)
    end
end
addEventHandler("get_money",getRootElement(),get_money)

Client Side

local client = getLocalPlayer()
local info = {}

addEvent("afacere_menu",true)

open_afacere_menu = function(ID,owner,payout,cost,balance,timer,x_aff,y_aff,z_aff)
        info[1] = ID 
        info[2] = owner 
        info[3] = payout 
        info[4] = cost 
        info[5] = balance 
        info[6] = timer
        setElementData(getLocalPlayer(),"open_w",true)
        local screenW, screenH = guiGetScreenSize()
                afacerewindow = guiCreateWindow((screenW - 500) / 2, (screenH - 300) / 2, 500, 300, "AFACERE", false)
                guiWindowSetMovable(afacerewindow, false)
                guiWindowSetSizable(afacerewindow, false)
        
                BuyAfacere = guiCreateButton(15, 247, 119, 34, "BUY", false, afacerewindow)
                guiSetProperty(BuyAfacere, "NormalTextColour", "FFAAAAAA")
                SELL_afacere = guiCreateButton(154, 247, 119, 34, "SELL", false, afacerewindow)
                guiSetProperty(SELL_afacere, "NormalTextColour", "FFAAAAAA")
                GET_MONEY = guiCreateButton(293, 247, 119, 34, "GET MONEY", false, afacerewindow)
                guiSetProperty(GET_MONEY, "NormalTextColour", "FFAAAAAA")
                cancel_afacere = guiCreateButton(423, 247, 67, 34, "X", false, afacerewindow)
                guiSetProperty(cancel_afacere, "NormalTextColour", "FFAAAAAA")
                Info_afacere = guiCreateMemo(154, 15, 340, 230, "AFACERE ID : "..ID.."\nSTATUS: "..owner.."\nPAY_OUT: "..payout.."\nCOST: "..cost.."\nBALANCE: "..tostring(balance).."\nTimer for payout in minutes: "..(timer/60000).."\nX: "..x_aff.."\nY: "..y_aff.."\nZ: "..z_aff.."\n------------------------------------------------------------------------------", false,afacerewindow) 
                guiCreateStaticImage(15,15, 128, 128,"$.png",false,afacerewindow)
                guiCreateStaticImage(15,120,128, 128,"money2.png",false,afacerewindow)
                guiMemoSetReadOnly(Info_afacere, true)    
                showCursor(true)
                addEventHandler ( "onClientGUIClick",cancel_afacere, close_afacere_menu,false)
                addEventHandler ( "onClientGUIClick",BuyAfacere, buy_afacere_menu,false)  
                addEventHandler("onClientGUIClick",GET_MONEY,get_money_menu,false)    
            end

addEventHandler("afacere_menu",getRootElement(),open_afacere_menu)


close_afacere_menu = function(button,state)
        if (button == "left") and (state == "up") then 
                showCursor (false)
                guiSetVisible(afacerewindow,false)
                setElementData(getLocalPlayer(),"open_w",false)
        end
end

buy_afacere_menu = function(button,state)
        if (button == "left") and (state == "up") then 
                if  info[2] == "DE VANZARE" and getPlayerMoney(getLocalPlayer()) >= info[4] then 
                        local nume = getPlayerName(getLocalPlayer())
                        showCursor (false)
                        guiSetVisible(afacerewindow,false)
                        outputChatBox("[AFACERE] Ai cumparat o afacere :D",0,50,255)
                        setElementData(getLocalPlayer(),"open_w",false)
                        takePlayerMoney(info[4])
                        triggerServerEvent("buy_afacere",getLocalPlayer(),info[1],info[3],info[5],info[6],nume)
                                elseif info[2] ~= "DE VANZARE" then 
                                outputChatBox("[AFACERE] Aceasta afacere este detinuta deja de catre cineva",150,0,0)
                elseif info[2] == "DE VANZARE" and getPlayerMoney(getLocalPlayer()) < info[4] then 
                outputChatBox("[AFACERE] Nu ai destui bani pentru a cumpara aceasta afacere",150,0,0)
                end 
        end
end

get_money_menu = function(button,state)
        local name = getPlayerName(client)
        if (button == "left") and (state == "up") then 
                if  info[2] == "DE VANZARE" then 
                   outputChatBox("[AFACERE] Aceasta afacere nu este detinuta de tine ")
                     elseif name == info[2] and info[5] >0 and info[5] >=16500 then 
                        guiSetVisible(afacerewindow,false)
                                setElementData(getLocalPlayer(),"open_w",false)
                                        showCursor (false)
                                triggerServerEvent("get_money",client,info[2],info[1],info[5])
                        elseif name ~= info[2] then  
                        outputChatBox("[AFACERE] Aceasta afacere nu este detinuta de tine ")
                end 
        end
end

Here is the full code.

Link to comment
  • Moderators
3 hours ago, FlorinSzasz said:
local balance = getElementData(afacereglobala,"afacere_balance")

The database should to be considered the truth of what the balance actually is, not your elementdata.

See here how to update the balance:

dbExec(conexiune,"UPDATE afacere SET balance = balance + ? WHERE NR=? LIMIT 1", payout, ID)

 

You need a second 'SELECT' query for updating the elementdata.

 

  • Thanks 1
Link to comment
  • Moderators
9 hours ago, FlorinSzasz said:

remove the "LIMIT 1" otherwise it will give me a syntax error

Indeed, my bad.

According to the docs, you need to use ORDER BY, before you can use LIMIT in an UPDATE query.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...