Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. -- Saving SQL Stuff
  2. function new_player( ply )
  3.  
  4. local steamID = ply:SteamID()
  5. sql.Query( "INSERT INTO player_info (`unique_id`, `money`)VALUES ('"..steamID.."', '100')" )
  6. local result = sql.Query( "SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'" )
  7. if (result) then
  8.  
  9. sql.Query( "INSERT INTO player_stats (`unique_id`, `experience`, `nextexperience`, `level`, `nextlevel`)VALUES ('"..steamID.."', '0', '250', '1', '2')" )
  10. local result = sql.Query( "SELECT unique_id, experience, nextexperience, level, nextlevel FROM player_stats WHERE unique_id = '"..steamID.."'" )
  11. if (result) then
  12. Msg("Player account created !\n")
  13. sql_value_inventory( ply )
  14. sql_value_stats( ply )
  15. else
  16. Msg("Something went wrong with creating a players stats !\n")
  17. end
  18.  
  19. else
  20. Msg("Something went wrong with creating a players info !\n")
  21. end
  22. end
  23.  
  24. function sql_value_inventory ( ply )
  25. local unique_id = sql.QueryValue("SELECT unique_id FROM player_info WHERE unique_id = '"..steamID.."'")
  26. local money = sql.QueryValue("SELECT money FROM player_info WHERE unique_id = '"..steamID.."'")
  27. ply:SetNWString("unique_id", unique_id)
  28. ply:SetNWInt("money", money)
  29. end
  30.  
  31. function sql_value_stats ( ply )
  32. local unique_id = sql.QueryValue("SELECT unique_id FROM player_stats WHERE unique_id = '"..steamID.."'")
  33. local experience = sql.QueryValue("SELECT experience FROM player_stats WHERE unique_id = '"..steamID.."'")
  34. local nextexperience = sql.QueryValue("SELECT nextexperience FROM player_stats WHERE unique_id = '"..steamID.."'")
  35. local level = sql.QueryValue("SELECT level FROM player_stats WHERE unique_id = '"..steamID.."'")
  36. local nextlevel = sql.QueryValue("SELECT nextlevel FROM player_stats WHERE unique_id = '"..steamID.."'")
  37. ply:SetNWString("unique_id", unique_id)
  38. ply:SetNWInt("exp", experience)
  39. ply:SetNetworkedInt("nextexp", nextexperience)
  40. ply:SetNWInt("lvl", level)
  41. ply:SetNWInt("nextlvl", nextlevel)
  42. end
  43.  
  44. function saveStat ( ply )
  45. local money = ply:GetNWInt("money")
  46. local unique_id = ply:GetNWString ("SteamID")
  47. local experience = ply:GetNWInt("exp")
  48. local nextexperience = ply:GetNetworkedInt("nextexp")
  49. local level = ply:GetNWInt("lvl")
  50. local nextlevel = ply:GetNWInt("nextlvl")
  51. sql.Query("UPDATE player_stats SET experience = "..experience..", nextexperience = "..nextexperience..", level = "..level..", nextlevel = "..nextlevel.." WHERE unique_id = '"..unique_id.."'")
  52. sql.Query("UPDATE player_info SET money = "..money.." WHERE unique_id = '"..unique_id.."'")
  53. Msg("Stats updated!\n")
  54. end
  55.  
  56. function tables_exist()
  57.  
  58. if (sql.TableExists("player_info") && sql.TableExists("player_stats")) then
  59. Msg("Both tables already exist !")
  60. else
  61. if (!sql.TableExists("player_info")) then
  62. query = "CREATE TABLE player_info ( unique_id varchar(255), money int )"
  63. result = sql.Query(query)
  64. if (sql.TableExists("player_info")) then
  65. Msg("Succes ! table 1 created \n")
  66. else
  67. Msg("Somthing went wrong with the player_info query ! \n")
  68. Msg( sql.LastError( result ) .. "\n" )
  69. end
  70. end
  71. if (!sql.TableExists("player_stats")) then
  72. local query = "CREATE TABLE player_stats ( unique_id varchar(255), experience int, nextexperience int, level int, nextlevel int )"
  73. local result = sql.Query(query)
  74. if (sql.TableExists("player_stats")) then
  75. Msg("Succes ! table 2 created \n")
  76. else
  77. Msg("Something went wrong with the player_stats query ! \n")
  78. Msg( sql.LastError( result ) .. "\n" )
  79. end
  80. end
  81. end
  82.  
  83. end
  84.  
  85.  
  86. function player_exists( ply )
  87.  
  88. local steamID = ply:GetNWString("SteamID")
  89.  
  90. local result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'")
  91. if (result) then
  92. sql_value_inventory( ply ) // We will call this to retrieve the stats
  93. sql_value_stats( ply ) // We will call this to retrieve the skills
  94. else
  95. new_player( steamID, ply ) // Create a new player :D
  96. end
  97. end
  98.  
  99. function Initialize()
  100. tables_exist()
  101. end
  102.  
  103. function PlayerInitialSpawn( ply )
  104.  
  105. timer.Create("Steam_id_delay", 2, 2, function()
  106. local SteamID = SteamID
  107. --ply:SetNWString("SteamID", SteamID)
  108. timer.Create("SaveStat", 10, 0, function() saveStat( ply ) end)
  109. player_exists( ply )
  110. end)
  111.  
  112. end
  113.  
  114. hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )
  115. hook.Add( "Initialize", "Initialize", Initialize )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement