Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Saving SQL Stuff
- function new_player( ply )
- local steamID = ply:SteamID()
- sql.Query( "INSERT INTO player_info (`unique_id`, `money`)VALUES ('"..steamID.."', '100')" )
- local result = sql.Query( "SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'" )
- if (result) then
- sql.Query( "INSERT INTO player_stats (`unique_id`, `experience`, `nextexperience`, `level`, `nextlevel`)VALUES ('"..steamID.."', '0', '250', '1', '2')" )
- local result = sql.Query( "SELECT unique_id, experience, nextexperience, level, nextlevel FROM player_stats WHERE unique_id = '"..steamID.."'" )
- if (result) then
- Msg("Player account created !\n")
- sql_value_inventory( ply )
- sql_value_stats( ply )
- else
- Msg("Something went wrong with creating a players stats !\n")
- end
- else
- Msg("Something went wrong with creating a players info !\n")
- end
- end
- function sql_value_inventory ( ply )
- local unique_id = sql.QueryValue("SELECT unique_id FROM player_info WHERE unique_id = '"..steamID.."'")
- local money = sql.QueryValue("SELECT money FROM player_info WHERE unique_id = '"..steamID.."'")
- ply:SetNWString("unique_id", unique_id)
- ply:SetNWInt("money", money)
- end
- function sql_value_stats ( ply )
- local unique_id = sql.QueryValue("SELECT unique_id FROM player_stats WHERE unique_id = '"..steamID.."'")
- local experience = sql.QueryValue("SELECT experience FROM player_stats WHERE unique_id = '"..steamID.."'")
- local nextexperience = sql.QueryValue("SELECT nextexperience FROM player_stats WHERE unique_id = '"..steamID.."'")
- local level = sql.QueryValue("SELECT level FROM player_stats WHERE unique_id = '"..steamID.."'")
- local nextlevel = sql.QueryValue("SELECT nextlevel FROM player_stats WHERE unique_id = '"..steamID.."'")
- ply:SetNWString("unique_id", unique_id)
- ply:SetNWInt("exp", experience)
- ply:SetNetworkedInt("nextexp", nextexperience)
- ply:SetNWInt("lvl", level)
- ply:SetNWInt("nextlvl", nextlevel)
- end
- function saveStat ( ply )
- local money = ply:GetNWInt("money")
- local unique_id = ply:GetNWString ("SteamID")
- local experience = ply:GetNWInt("exp")
- local nextexperience = ply:GetNetworkedInt("nextexp")
- local level = ply:GetNWInt("lvl")
- local nextlevel = ply:GetNWInt("nextlvl")
- sql.Query("UPDATE player_stats SET experience = "..experience..", nextexperience = "..nextexperience..", level = "..level..", nextlevel = "..nextlevel.." WHERE unique_id = '"..unique_id.."'")
- sql.Query("UPDATE player_info SET money = "..money.." WHERE unique_id = '"..unique_id.."'")
- Msg("Stats updated!\n")
- end
- function tables_exist()
- if (sql.TableExists("player_info") && sql.TableExists("player_stats")) then
- Msg("Both tables already exist !")
- else
- if (!sql.TableExists("player_info")) then
- query = "CREATE TABLE player_info ( unique_id varchar(255), money int )"
- result = sql.Query(query)
- if (sql.TableExists("player_info")) then
- Msg("Succes ! table 1 created \n")
- else
- Msg("Somthing went wrong with the player_info query ! \n")
- Msg( sql.LastError( result ) .. "\n" )
- end
- end
- if (!sql.TableExists("player_stats")) then
- local query = "CREATE TABLE player_stats ( unique_id varchar(255), experience int, nextexperience int, level int, nextlevel int )"
- local result = sql.Query(query)
- if (sql.TableExists("player_stats")) then
- Msg("Succes ! table 2 created \n")
- else
- Msg("Something went wrong with the player_stats query ! \n")
- Msg( sql.LastError( result ) .. "\n" )
- end
- end
- end
- end
- function player_exists( ply )
- local steamID = ply:GetNWString("SteamID")
- local result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'")
- if (result) then
- sql_value_inventory( ply ) // We will call this to retrieve the stats
- sql_value_stats( ply ) // We will call this to retrieve the skills
- else
- new_player( steamID, ply ) // Create a new player :D
- end
- end
- function Initialize()
- tables_exist()
- end
- function PlayerInitialSpawn( ply )
- timer.Create("Steam_id_delay", 2, 2, function()
- local SteamID = SteamID
- --ply:SetNWString("SteamID", SteamID)
- timer.Create("SaveStat", 10, 0, function() saveStat( ply ) end)
- player_exists( ply )
- end)
- end
- hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )
- hook.Add( "Initialize", "Initialize", Initialize )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement