Markn1 Posted November 1, 2017 Share Posted November 1, 2017 مرحبا بالجميع! أريد الحفاظ على معرف لكل لاعب. بحيث أنه في كل مرة يدخل الحساب كان لديه عدد. على سبيل المثال: إذا كان لدي 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
3NAD Posted November 2, 2017 Share Posted November 2, 2017 يبدو انك تستخدم مترجم قوقل أليس كذلك؟ -- أرجو توضيح المشكلة .. Link to comment
Markn1 Posted November 2, 2017 Author Share Posted November 2, 2017 40 minutes ago, 3NAD said: يبدو انك تستخدم مترجم قوقل أليس كذلك؟ نعم. أنا بحاجة للحفاظ على معرف اللاعب. ساعدني من فضلك Link to comment
3NAD Posted November 2, 2017 Share Posted November 2, 2017 هل تقصد انك تريد ان تقوم بحفظ معرف واحد لكل لاعب إلى الأبد ؟ Link to comment
Markn1 Posted November 2, 2017 Author Share Posted November 2, 2017 5 minutes ago, 3NAD said: هل تقصد انك تريد ان تقوم بحفظ معرف واحد لكل لاعب إلى الأبد ؟ نعم Link to comment
3NAD Posted November 2, 2017 Share Posted November 2, 2017 قم بتجربة هذا 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 ) 1 1 Link to comment
Markn1 Posted November 2, 2017 Author Share Posted November 2, 2017 21 minutes ago, 3NAD said: قم بتجربة هذا شكرا جزيلا لك! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now