Jump to content

ajuda SQLite gang


Recommended Posts

bom pessoal estou fazendo um systema de gang usando sqlite para salvar os dados, a parte de criar a gang está funcionando mais ainda vou fazer atualizações futuras, sou novo usando SQLite e nao estou consigo fazer algumas funções que eu gostaria como puxar o Level do jogador na função getPlayerGangLevel, nao estou sabendo verificar a tabela.

está apresentando o seguinte erro:

ERROR: gangsystem\server.lua:33: attempt to call upvalue 'db' (a userdata value)

 

local db = dbConnect("sqlite", "/testegang.db")
dbExec(db, "CREATE TABLE IF NOT EXISTS gangues (name TEXT, leader TEXT, gangcolor TEXT)")
dbExec(db, "CREATE TABLE IF NOT EXISTS ganguemembers (account TEXT, gangName TEXT, level TEXT)")

addEventHandler ( "onResourceStart", resourceRoot, function ( ) 
	exports.scoreboard:scoreboardAddColumn ( "Gang Level", getRootElement ( ), 90, "Gang Level", 12 )

	for i, v in pairs ( getElementsByType ( "player" ) ) do
		local gangTeam = getElementData ( v, "Gang" )
		if ( not gangTeam ) then
			setElementData ( v, "Gang", nil )
			setElementData ( v, "Gang Level", nil)
		end

		if ( not getElementData ( v, "Gang Level" ) ) then
			setElementData ( v, "Gang Level", nil )
		end
	end
end )

local maxStr = 20 

function gangIsLeader ( player, team ) 
    local select = dbExec(db "SELECT leader FROM gangues WHERE name = '" ..team.. "' " ); 
    if ( select[1]["leader"] == player ) then 
        return true; 
    else 
        return false; 
    end 
end

function getPlayerGangLevel ( player ) 
    local select = dbExec(db "SELECT account FROM ganguemembers WHERE account = '" ..player.. "' " ); 
    if ( select[1]["account"] == player ) then 
        return select["level"]
    else 
        return false; 
    end 
end

function testePlayerLevel(player, commandName)
	mylevel = getPlayerGangLevel(player)
	outputChatBox("Meu level é: "..myLevel, player, 255, 255, 0, false)
end
addCommandHandler("getlevel", testePlayerLevel)

function setPlayerGang ( player, team, level ) 
    dbExec(db, "INSERT INTO ganguemembers VALUES (?, ?, ?)", getPlayerName(player), tostring(team), tostring(level))
	setPlayerTeam(player, getTeamFromName(team))
	setElementData(player, "Gang", getTeamFromName(team))
	outputDebugString(getPlayerName(player).." entrou na gang "..team.." como: "..level)
end

function createGang(name, owner)
	local color = {math.random(255), math.random(255), math.random(255)}
	r, g, b = unpack(color)
	createTeam(name, r, g, b)
	dbExec(db, "INSERT INTO gangues VALUES (?, ?, ?)", name, getPlayerName(owner), toJSON(color))
	outputDebugString(getPlayerName(owner).." criou a gang: "..name)
end

function criarGang ( source, commandName, teamName ) 
    local getMoney = getPlayerMoney ( source ) 
    if ( teamName ) then 
        if ( getMoney < 400000 ) then 
            outputChatBox ( "#F4A460[GANG]#F08080 Você não tem dinheiro suficiente para criar uma gang Valor 400000$", source, 255, 255, 255, true ) 
        elseif ( string.len ( teamName ) > maxStr ) then 
                outputChatBox ( "#1e90ff[GANG] #ffffff- O Nome da Gang nao pode ter mais de " .. maxStr .. " caracteres!", uPed, 255, 255, 255, true ); 
		else 
            createGang ( teamName, source ) 
            takePlayerMoney ( source, 400000 ) 
            setPlayerGang ( source, teamName, "Lider" )
			setElementData(source, "Gang Level", "Lider")
        end 
    else 
        outputChatBox ( "#F4A460[GANG]#F08080 Uso correto: /fundargang [nome]", source, 255, 255, 255, true ) 
    end 
end 
addCommandHandler ( "gangcriar", criarGang )

Link to comment

Corrigi vários erros no código, pode ser que ainda não funcione 100% porque não testei  ele, mas tente isto:


local db = dbConnect("sqlite", "testegang.db")
if db then
	outputChatBox( "@dbConnect: Conexão feita com sucesso!" )
else
	outputChatBox( "@dbConnect: Falha na conexão com o banco de dados!" )
end
dbExec(db, "CREATE TABLE IF NOT EXISTS gangues (name TEXT, leader TEXT, gangcolor TEXT)")
dbExec(db, "CREATE TABLE IF NOT EXISTS ganguemembers (account TEXT, gangName TEXT, level TEXT)")

addEventHandler ( "onResourceStart", resourceRoot, function ( ) 
	exports.scoreboard:scoreboardAddColumn ( "Gang Level", getRootElement ( ), 90, "Gang Level", 12 )

	for i, v in pairs ( getElementsByType ( "player" ) ) do
		local gangTeam = getElementData ( v, "Gang" )
		if ( not gangTeam ) then
			setElementData ( v, "Gang", nil )
			setElementData ( v, "Gang Level", nil)
		elseif gangTeam then
			setPlayerTeam( v, tostring(gangTeam) )
		end

		if ( not getElementData ( v, "Gang Level" ) ) then
			setElementData ( v, "Gang Level", nil )
		end
	end
end )

local maxStr = 20 
function gangIsLeader ( player, team ) 
	local poll = dbPoll( dbQuery( db, "SELECT leader FROM gangues WHERE name=?", team ), -1 )
	if ( poll[1]["leader"] == player ) then 
		return true; 
	else 
		return false; 
	end 
end

function getPlayerGangLevel ( player ) 
	local poll = dbPoll( dbQuery( db, "SELECT account FROM ganguemembers WHERE account=?", player ), -1 )
	if ( poll[1]["account"] == player ) then
		return poll[1]["level"]
	else 
		return false; 
	end
end

function testePlayerLevel(player, commandName)
	local account = getAccountName( getPlayerAccount(player) )
	local mylevel = getPlayerGangLevel( account )
	outputChatBox("Meu level é: "..myLevel, player, 255, 255, 0, false)
end
addCommandHandler("getlevel", testePlayerLevel)

function setPlayerGang ( player, team, level )
	local account = getAccountName( getPlayerAccount(player) )
	dbExec(db, "INSERT INTO ganguemembers VALUES (?, ?, ?)", account, tostring(team), tostring(level))
	setPlayerTeam(player, getTeamFromName(tostring(team)))
	setElementData(player, "Gang", tostring(team))
	outputDebugString(getPlayerName(player).." entrou na gang "..tostring(team).." como: "..tostring(level))
end

function createGang(name, owner)
	local color = {math.random(255), math.random(255), math.random(255)}
	local r, g, b = unpack(color)
	local account = getAccountName( getPlayerAccount(owner) )
	createTeam(tostring(name), r, g, b)
	dbExec(db, "INSERT INTO gangues VALUES (?, ?, ?)", name, account, toJSON(color))
	outputDebugString(getPlayerName(owner).." criou a gang: "..tostring(name))
end

function criarGang ( source, commandName, teamName ) 
	local getMoney = getPlayerMoney ( source )
	if ( teamName ) then 
		if ( getMoney < 400000 ) then 
			outputChatBox ( "#F4A460[GANG]#F08080 Você não tem dinheiro suficiente para criar uma gang Valor 400000$", source, 255, 255, 255, true ) 
		elseif ( string.len ( teamName ) > maxStr ) then 
			outputChatBox ( "#1e90ff[GANG] #ffffff- O Nome da Gang nao pode ter mais de " .. maxStr .. " caracteres!", source, 255, 255, 255, true ); 
		else 
			createGang ( tostring(teamName), source ) 
			takePlayerMoney ( source, 400000 ) 
			setPlayerGang ( source, tostring(teamName), "Lider" )
			setElementData(source, "Gang Level", "Lider")
		end 
	else 
		outputChatBox ( "#F4A460[GANG]#F08080 Uso correto: /gangcriar [nome]", source, 255, 255, 255, true ) 
	end 
end 
addCommandHandler ( "gangcriar", criarGang )

 

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...