Advertisement
PsychVexis

Untitled

Apr 30th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. -- Edit this table to all the database tables and columns
  2. -- where identifiers are used (such as users, owned_vehicles, owned_properties etc.)
  3. ---------------------------------------------------------------------------------------
  4. local IdentifierTables = {
  5. {table = "users", column = "identifier"},
  6. {table = "owned_vehicles", column = "owner"},
  7. {table = "user_accounts", column = "identifier"},
  8. }
  9.  
  10. RegisterServerEvent("kashactersS:SetupCharacters")
  11. AddEventHandler('kashactersS:SetupCharacters', function()
  12. local src = source
  13. local LastCharId = GetLastCharacter(src)
  14. SetIdentifierToChar(GetPlayerIdentifiers(src)[1], LastCharId)
  15. local Characters = GetPlayerCharacters(src)
  16. TriggerClientEvent('kashactersC:SetupUI', src, Characters)
  17. end)
  18.  
  19. RegisterServerEvent("kashactersS:CharacterChosen")
  20. AddEventHandler('kashactersS:CharacterChosen', function(charid, ischar)
  21. local src = source
  22. local spawn = {}
  23. SetLastCharacter(src, tonumber(charid))
  24. SetCharToIdentifier(GetPlayerIdentifiers(src)[1], tonumber(charid))
  25. if ischar == "true" then
  26. spawn = GetSpawnPos(src)
  27. else
  28. TriggerClientEvent('skinchanger:loadDefaultModel', src, true, cb)
  29. spawn = { x = 195.55, y = -933.36, z = 29.90 } -- DEFAULT SPAWN POSITION
  30. end
  31. TriggerClientEvent("kashactersC:SpawnCharacter", src, spawn)
  32. end)
  33.  
  34. RegisterServerEvent("kashactersS:DeleteCharacter")
  35. AddEventHandler('kashactersS:DeleteCharacter', function(charid)
  36. local src = source
  37. DeleteCharacter(GetPlayerIdentifiers(src)[1], charid)
  38. TriggerClientEvent("kashactersC:ReloadCharacters", src)
  39. end)
  40.  
  41. function GetPlayerCharacters(source)
  42. local identifier = GetIdentifierWithoutSteam(GetPlayerIdentifiers(source)[1])
  43. local Chars = MySQLAsyncExecute("SELECT * FROM `users` WHERE identifier LIKE '%"..identifier.."%'")
  44. return Chars
  45. end
  46.  
  47. function GetLastCharacter(source)
  48. local LastChar = MySQLAsyncExecute("SELECT `charid` FROM `user_lastcharacter` WHERE `steamid` = '"..GetPlayerIdentifiers(source)[1].."'")
  49. if LastChar[1] ~= nil and LastChar[1].charid ~= nil then
  50. return tonumber(LastChar[1].charid)
  51. else
  52. MySQLAsyncExecute("INSERT INTO `user_lastcharacter` (`steamid`, `charid`) VALUES('"..GetPlayerIdentifiers(source)[1].."', 1)")
  53. return 1
  54. end
  55. end
  56.  
  57. function SetLastCharacter(source, charid)
  58. MySQLAsyncExecute("UPDATE `user_lastcharacter` SET `charid` = '"..charid.."' WHERE `steamid` = '"..GetPlayerIdentifiers(source)[1].."'")
  59. end
  60.  
  61. function SetIdentifierToChar(identifier, charid)
  62. for _, itable in pairs(IdentifierTables) do
  63. MySQLAsyncExecute("UPDATE `"..itable.table.."` SET `"..itable.column.."` = 'Char"..charid..GetIdentifierWithoutSteam(identifier).."' WHERE `"..itable.column.."` = '"..identifier.."'")
  64. end
  65. end
  66.  
  67. function SetCharToIdentifier(identifier, charid)
  68. for _, itable in pairs(IdentifierTables) do
  69. MySQLAsyncExecute("UPDATE `"..itable.table.."` SET `"..itable.column.."` = '"..identifier.."' WHERE `"..itable.column.."` = 'Char"..charid..GetIdentifierWithoutSteam(identifier).."'")
  70. end
  71. end
  72.  
  73. function DeleteCharacter(identifier, charid)
  74. for _, itable in pairs(IdentifierTables) do
  75. MySQLAsyncExecute("DELETE FROM `"..itable.table.."` WHERE `"..itable.column.."` = 'Char"..charid..GetIdentifierWithoutSteam(identifier).."'")
  76. end
  77. end
  78.  
  79. function GetSpawnPos(source)
  80. local SpawnPos = MySQLAsyncExecute("SELECT `position` FROM `users` WHERE `identifier` = '"..GetPlayerIdentifiers(source)[1].."'")
  81. return json.decode(SpawnPos[1].position)
  82. end
  83.  
  84. function GetIdentifierWithoutSteam(Identifier)
  85. return string.gsub(Identifier, "steam", "")
  86. end
  87.  
  88. function MySQLAsyncExecute(query)
  89. local IsBusy = true
  90. local result = nil
  91. MySQL.Async.fetchAll(query, {}, function(data)
  92. result = data
  93. IsBusy = false
  94. end)
  95. while IsBusy do
  96. Citizen.Wait(0)
  97. end
  98. return result
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement