Advertisement
Guest User

Untitled

a guest
Mar 11th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. -- Table with all the online users and their data
  2. local onlinePlayers = {}
  3. -- Here are the userids stored with as index the playerElements
  4. local playersID = {}
  5. -- Here are the playerElements stored with as index the userid
  6. local IDPlayers = {}
  7.  
  8. -- When a player quits we want to store the data in the database
  9. addEventHandler( "onPlayerQuit", root,
  10.     function ()
  11.         if ( onlinePlayers[source] ) and ( accountPlayers[source] ) then
  12.             exec( "UPDATE accounts SET username=?, password=?, email=?, userdata=? WHERE userid=?"
  13.                 ,onlinePlayers[source].username
  14.                 ,onlinePlayers[source].password
  15.                 ,onlinePlayers[source].email
  16.                 ,onlinePlayers[source].userdata
  17.                 ,accountPlayers[source]
  18.             )
  19.            
  20.             onlinePlayers[source] = {}         
  21.             IDPlayers[playersID[source]] = {}
  22.             playersID[source] = {}
  23.         end
  24.     end
  25. )
  26.  
  27. -- When a player login add him to the table
  28. addEventHandler( "onPlayerLogin", root, -- [NOTE] CHANGE THIS TO OUR CUSTOM EVENT!!
  29.     function ( userID )
  30.         local userData = querySingle( "SELECT * FROM accounts WHERE userid=? LIMIT 1", userID )
  31.         if ( userData ) then
  32.             onlinePlayers[source] = userData
  33.             playersID[source] = userID
  34.             IDPlayers[userID] = source
  35.         end
  36.     end
  37. )
  38.  
  39. -- Get a playerElement from a userID
  40. function getAccountIDPlayer ( userID )
  41.     if ( IDPlayers[userID] ) then
  42.         return IDPlayers[userID]
  43.     else
  44.         return false
  45.     end
  46. end
  47.  
  48. -- Get the username of a player element
  49. function getPlayerAccountID ( thePlayer )
  50.     if ( playersID[thePlayer] ) then
  51.         return playersID[thePlayer]
  52.     else
  53.         return false
  54.     end
  55. end
  56.  
  57. -- Function that checks whether a username is already being used
  58. function doesUsernameExist ( username )
  59.     if ( querySingle( "SELECT * FROM accounts WHERE username=? LIMIT 1", username ) ) then
  60.         return true
  61.     else
  62.         return false
  63.     end
  64. end
  65.  
  66. -- Function that checks if the username and password are matching
  67. function checkLogin ( username, password )
  68.     if ( querySingle( "SELECT * FROM accounts WHERE username=? AND password=? LIMIT 1", username, sha512 ( password ) ) ) then
  69.         return true
  70.     else
  71.         return false
  72.     end
  73. end
  74.  
  75. -- Function that creates a new account
  76. function createAccount ( username, password, email )
  77.     if ( #password >= 32 ) and ( exec( "INSERT INTO accounts SET username=?, password=?, email=?", username, sha512 ( password ), email ) ) then
  78.         return true
  79.     else
  80.         return false
  81.     end
  82. end
  83.  
  84. -- Function that removed a account
  85. function deleteAccount ( userID )
  86.     local accountData = querySingle( "SELECT * FROM accounts WHERE userid=? LIMIT 1", userID )
  87.     if ( exec( "DELETE FROM accounts WHERE userid=?", userID ) ) then
  88.         return exec( "INSERT INTO accountsTrash SET username=?, password=?, email=?, userdata=?", accountData.username, accountData.password, accountData.email, accountData.userdata ) )
  89.     else
  90.         return false
  91.     end
  92. end
  93.  
  94. -- Function that resotres a account
  95. function restoreAccount ( userID )
  96.     local accountData = querySingle( "SELECT * FROM accountsTrash WHERE userid=? LIMIT 1", userID )
  97.     if ( exec( "DELETE FROM accountsTrash WHERE userid=?", userID ) ) then
  98.         return exec( "INSERT INTO accounts SET username=?, password=?, email=?, userdata=?", accountData.username, accountData.password, accountData.email, accountData.userdata ) )
  99.     else
  100.         return false
  101.     end
  102. end
  103.  
  104. -- Function that gets the account data
  105. function retrieveAccountData ( userID, data )
  106.     if ( IDPlayers[userID] ) then
  107.         return onlinePlayers[IDPlayers[userID]][data]
  108.     else
  109.         return querySingle( "SELECT * FROM accounts WHERE userid=? LIMIT 1", userID )
  110.     end
  111. end
  112.  
  113. -- Function that sets a particular account data
  114. function updateAccountData ( userID, data, value )
  115.     if ( exec( "UPDATE accounts SET `??`=? WHERE userid=?", data, value, userID ) ) then
  116.         if ( onlinePlayers[IDPlayers[userID]] ) then onlinePlayers[IDPlayers[userID]][data] = value end
  117.         return true
  118.     else
  119.         return false
  120.     end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement