Jump to content

هوية شخصية / قاعدة بيانات


Markn1

Recommended Posts

مرحبا بالجميع! أريد الحفاظ على معرف لكل لاعب. بحيث أنه في كل مرة يدخل الحساب كان لديه عدد.

على سبيل المثال: إذا كان لدي 50 معرف بحيث بعد إعادة الدخول إلى الملقم سيكون هناك 50. 

 

-- server

ids = {}

function displayLoadedRes ( res )
	max_players = getServerConfigSetting ( "maxplayers" )
	exports.scoreboard:scoreboardAddColumn("playerid", root, 30, "ID", 1)
	for i = 1, max_players do
		table.insert ( ids, false )
	end
	for i, v in ipairs ( getElementsByType ( 'player' )) do
		assignPlayerFreeID ( v )
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), displayLoadedRes )

function assignPlayerFreeID ( player )
	local assigned = false
	if isElement ( player ) then
		for i, v in ipairs (ids) do
			if not isElement(v) then
				assigned = i
				ids[i] = player
				break
			end
		end
		if assigned then
			setElementData ( player, "playerid", assigned )
			return true
		else	
			outputChatBox ( 'Невозможно подобрать свободный ид для игрока '..getPlayerName ( player ))
			kickPlayer ( player )
			return false
		end
	else
		return false
	end
end

function getPlayerFromID ( id )
	for i, v in ipairs ( getElementsByType ( 'player' )) do
		local playerid = getElementData ( v, "playerid" )
		if playerid and playerid == tonumber(id) then
			return v
		end
	end
	return false
end

-- SAVE ( DB )

createDB = dbConnect( "sqlite", "save.db" ) 
if createDB then
	outputDebugString("DB ON")
else
	outputDebugString("DB Fail")
end

addEventHandler( "onResourceStart", getRootElement(), function()
	if createDB then
		local tabela = dbExec( createDB, "CREATE TABLE IF NOT EXISTS save ( login, playerid )" )
	else
	    return false
    end   
end )

addEventHandler( "onPlayerLogin", getRootElement(), function( pre, cur )
	if not isGuestAccount ( cur ) then assignPlayerFreeID ( source ) end
	local login = getAccountName (getPlayerAccount(source))
    local q = dbQuery( createDB, "SELECT * FROM save WHERE login=?", login ) 
	local result = dbPoll( q, -1 )
	if result then
		for _, row in ipairs(result) do
		setElementData ( source, "playerid", row["playerid"] )
		end
    end
end )

function onPlayerQuit ( thePlayer )
	local lp = source 
	local pl = getPlayerAccount (lp) 
	if isGuestAccount(pl) then return end 
	local login = getAccountName (pl)
    local playerid = getElementData ( lp, "playerid" );
	local q = dbQuery( createDB, "SELECT * FROM save WHERE login=?", login ) 
	local result = dbPoll( q, -1 )
	dbFree(q)
    if #result == 0 then
		dbExec( createDB, "INSERT INTO save (login, playerid) VALUES (?,?)", login, playerid ) 
	else
	dbExec( createDB, "UPDATE save SET login=?, playerid=?", login, playerid ) 
	end
end
addEventHandler( "onPlayerQuit", getRootElement(), onPlayerQuit )
--addEventHandler( "onPlayerWanted", getRootElement(), onPlayerQuit )

addEventHandler ( "onResourceStop", resourceRoot, function () 
for _,v in ipairs ( getElementsByType ( "player" ) ) do
	local pl = getPlayerAccount (v) 
	if isGuestAccount(pl) then return end 
	local login = getAccountName (pl)
    local playerid = getElementData (v, "playerid" );
	local q = dbQuery( createDB, "SELECT * FROM save WHERE login=?", login ) 
	local result = dbPoll( q, -1 )
	dbFree(q)
        if #result == 0 then 
		    dbExec( createDB, "INSERT INTO save (login, playerid) VALUES (?,?)", login, playerid ) 
	    else 
	        dbExec( createDB, "UPDATE save SET login=?, playerid=?", login, playerid ) 
	    end
    end
end )

 

Link to comment

قم بتجربة هذا 

local ID_Database = dbConnect ( "sqlite", "ID_Database.db" )
dbExec ( ID_Database, "CREATE TABLE IF NOT EXISTS `ID` (`Account`,`Number`)")

getLastUsedID = function ( )
	local id = 0
	local db = dbPoll ( dbQuery ( ID_Database, "SELECT * FROM `ID`" ), -1 )
	if #db > 0 then
		for i, v in ipairs ( db ) do
			id = v.Number
		end
	end
	return tonumber(id)
end

getAccountID = function ( acc )
	if acc then
		local db = dbPoll ( dbQuery ( ID_Database, "SELECT * FROM `ID` WHERE `Account`=?", tostring(acc) ), -1 )
		if #db > 0 then		
			return tostring(db[1].Number)
		end
	end
	return false
end

giveAccountID = function ( acc )
	if acc then
		if not getAccountID ( acc ) then
			local id = getLastUsedID ( ) +1
			dbExec ( ID_Database, "INSERT INTO `ID` VALUES(?,?)", tostring(acc), tostring(id) )
			return tostring(id)
		end
	end
	return false
end

loadPlayerID = function ( player ) 
	local account = getPlayerAccount ( player )
	if not isGuestAccount ( account ) then
		local acc = getAccountName ( account )
		local id = getAccountID ( acc )
		if id then
			setElementData ( source, "playerid", id )
			return id
		else
			local newID = giveAccountID ( acc )
			if newID then
				setElementData ( source, "playerid", newID )
				return newID
			end
		end
	end
	return false
end

addEventHandler ( "onPlayerLogin", root,
	function ( )
		loadPlayerID ( source )
	end
)

addEventHandler ( "onResourceStart", resourceRoot,
	function ( )
		for i, v in ipairs ( getElementsByType ( "player" ) ) do
			loadPlayerID ( v )
		end
	end
)

 

  • Like 1
  • Thanks 1
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...