Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Table with all the online users and their data
- local onlinePlayers = {}
- -- Here are the userids stored with as index the playerElements
- local playersID = {}
- -- Here are the playerElements stored with as index the userid
- local IDPlayers = {}
- -- When a player quits we want to store the data in the database
- addEventHandler( "onPlayerQuit", root,
- function ()
- if ( onlinePlayers[source] ) and ( accountPlayers[source] ) then
- exec( "UPDATE accounts SET username=?, password=?, email=?, userdata=? WHERE userid=?"
- ,onlinePlayers[source].username
- ,onlinePlayers[source].password
- ,onlinePlayers[source].email
- ,onlinePlayers[source].userdata
- ,accountPlayers[source]
- )
- onlinePlayers[source] = {}
- IDPlayers[playersID[source]] = {}
- playersID[source] = {}
- end
- end
- )
- -- When a player login add him to the table
- addEventHandler( "onPlayerLogin", root, -- [NOTE] CHANGE THIS TO OUR CUSTOM EVENT!!
- function ( userID )
- local userData = querySingle( "SELECT * FROM accounts WHERE userid=? LIMIT 1", userID )
- if ( userData ) then
- onlinePlayers[source] = userData
- playersID[source] = userID
- IDPlayers[userID] = source
- end
- end
- )
- -- Get a playerElement from a userID
- function getAccountIDPlayer ( userID )
- if ( IDPlayers[userID] ) then
- return IDPlayers[userID]
- else
- return false
- end
- end
- -- Get the username of a player element
- function getPlayerAccountID ( thePlayer )
- if ( playersID[thePlayer] ) then
- return playersID[thePlayer]
- else
- return false
- end
- end
- -- Function that checks whether a username is already being used
- function doesUsernameExist ( username )
- if ( querySingle( "SELECT * FROM accounts WHERE username=? LIMIT 1", username ) ) then
- return true
- else
- return false
- end
- end
- -- Function that checks if the username and password are matching
- function checkLogin ( username, password )
- if ( querySingle( "SELECT * FROM accounts WHERE username=? AND password=? LIMIT 1", username, sha512 ( password ) ) ) then
- return true
- else
- return false
- end
- end
- -- Function that creates a new account
- function createAccount ( username, password, email )
- if ( #password >= 32 ) and ( exec( "INSERT INTO accounts SET username=?, password=?, email=?", username, sha512 ( password ), email ) ) then
- return true
- else
- return false
- end
- end
- -- Function that removed a account
- function deleteAccount ( userID )
- local accountData = querySingle( "SELECT * FROM accounts WHERE userid=? LIMIT 1", userID )
- if ( exec( "DELETE FROM accounts WHERE userid=?", userID ) ) then
- return exec( "INSERT INTO accountsTrash SET username=?, password=?, email=?, userdata=?", accountData.username, accountData.password, accountData.email, accountData.userdata ) )
- else
- return false
- end
- end
- -- Function that resotres a account
- function restoreAccount ( userID )
- local accountData = querySingle( "SELECT * FROM accountsTrash WHERE userid=? LIMIT 1", userID )
- if ( exec( "DELETE FROM accountsTrash WHERE userid=?", userID ) ) then
- return exec( "INSERT INTO accounts SET username=?, password=?, email=?, userdata=?", accountData.username, accountData.password, accountData.email, accountData.userdata ) )
- else
- return false
- end
- end
- -- Function that gets the account data
- function retrieveAccountData ( userID, data )
- if ( IDPlayers[userID] ) then
- return onlinePlayers[IDPlayers[userID]][data]
- else
- return querySingle( "SELECT * FROM accounts WHERE userid=? LIMIT 1", userID )
- end
- end
- -- Function that sets a particular account data
- function updateAccountData ( userID, data, value )
- if ( exec( "UPDATE accounts SET `??`=? WHERE userid=?", data, value, userID ) ) then
- if ( onlinePlayers[IDPlayers[userID]] ) then onlinePlayers[IDPlayers[userID]][data] = value end
- return true
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement