Coding help (200-300 lines of mysql stuff needs converrsion)


require( "mysql" )

function VE.HandleError ( error, func, user )
	if error == "" then return end
	if string.find(error, "gone away") then
		VE.ConnectToMySQL ( )
		if user then
			func(user)
		else
			func()
		end
	end
	Msg("-----MySQL Error-----
" .. error .. "
---------------------
")
end

function VE.ConnectToMySQL ( )
	if VE.db then
		mysql.disconnect(VE.db)
	end
	
	Msg("Connecting to MySQL database...
")
	
	VE.db, error = mysql.connect(VE.Config.MySQLServer, VE.Config.MySQLUser, VE.Config.MySQLPass, VE.Config.MySQLDatabase)
	
	if (VE.db == 0) then VE.HandleError(error, VE.ConnectToMySQL) return end
	
	Msg("Successfully connected to database!
")
end
VE.ConnectToMySQL()

function GM:PlayerInitialSpawn ( user )
	VE.LoadUser ( user )
	
	timer.Simple(31 - user:GetNWInt("SkillLevel_4"), RegenHealth, user)
	
	user:PrintMessage(HUD_PRINTTALK, "If you ever spawn with only a knife, disconnect and contact an administrator immediately.")
	if VE.Config.AllowDownload == "0" then
		user:PrintMessage(HUD_PRINTTALK, "The server administrator has opted to NOT allow you to download materials and sounds needed for this gamemode")
	end
	if VE.Config.HideMaterials == "1" then
		user:PrintMessage(HUD_PRINTTALK, "The server administrator has opted to NOT display materials in the menu and scoreboard. This will improve your visual performance if you do not have the materials.")
	end
	VE.SetTeam(user)
end

function util.booltonum ( bool )
	if bool then
		return 1
	else
		return 0
	end
end

function VE.CalculatePoints ( user, trues )
	if not user then return end
	local Points = 0
	for i = 1, 9 do
		Points = Points + user:GetNWInt("SkillLevel_" .. tostring(i))
	end
	
	for i = 1 ,14 do
		if user:GetNWBool("OwnsWeapon_" .. tostring(i)) then
			Points = Points + 2
		end
	end
	
	Points = Points + (user:GetNWInt("Kills") * .025)
	Points = Points - (user:GetNWInt("Deaths") * .025)
	Points = Points + (user:GetNWInt("Headshots") * .05)
	Points = Points + (user:GetNWInt("TimesWon") * 2)
	
	user:SetNWInt("Points", Points)
	
	if trues then return end
	VE.SaveUser ( user )
end

function GM:PlayerDisconnected ( user )
	VE.SaveUser(user)
end

function VE.SaveUser ( user )

	Msg("Saving " .. user:Nick() .. "...
")

	VE.CalculatePoints ( user, true )

	local SaveString = [[UPDATE `ve_profiles` SET ]]
		
	local StartTotal = 0
	for i = 1 ,9 do
		SaveString = SaveString .. [[`SkillLevel_]] .. tostring(i) .. [[` = ']] .. user:GetNWInt("SkillLevel_" .. tostring(i)) .. [[', ]]
		StartTotal = StartTotal + user:GetNWInt("SkillLevel_" .. tostring(i))
	end
	
	for i = 1 ,14 do
		SaveString = SaveString .. [[`OwnsWeapon_]] .. tostring(i) .. [[` = ']] .. util.booltonum(user:GetNWBool("OwnsWeapon_" .. tostring(i))) .. [[', ]]
		StartTotal = StartTotal + 1
	end
	
	SaveString = SaveString .. [[`Name` = ']] .. user:Nick() .. [[', ]]
	SaveString = SaveString .. [[`EquipedWep` = ']] .. tostring(user:GetNWInt("EquipedWep")) .. [[', ]]
	SaveString = SaveString .. [[`EquipedWep_Side` = ']] .. tostring(user:GetNWInt("EquipedWep_Side")) .. [[', ]]
	SaveString = SaveString .. [[`Cash` = ']] .. tostring(user:GetNWInt("Cash")) .. [[', ]]
	SaveString = SaveString .. [[`Kills` = ']] .. tostring(user:GetNWInt("Kills")) .. [[', ]]
	SaveString = SaveString .. [[`Deaths` = ']] .. tostring(user:GetNWInt("Deaths")) .. [[', ]]
	SaveString = SaveString .. [[`Headshots` = ']] .. tostring(user:GetNWInt("Headshots")) .. [[', ]]
	SaveString = SaveString .. [[`TimeSpent` = ']] .. tostring(user:GetNWInt("TimeSpent")) .. [[', ]]
	SaveString = SaveString .. [[`TimesPlayed` = ']] .. tostring(user:GetNWInt("TimesPlayed")) .. [[', ]]
	SaveString = SaveString .. [[`TimesWon` = ']] .. tostring(user:GetNWInt("TimesWon")) .. [[', ]]
	SaveString = SaveString .. [[`Points` = ']] .. tostring(user:GetNWInt("Points")) .. [[', ]]	
	
	SaveString = SaveString .. [[`OwnsIronsights` = ']] .. util.booltonum(user:GetNWBool("OwnsIronsights")) .. [[', ]]
	SaveString = SaveString .. [[`OwnsFrag` = ']] .. util.booltonum(user:GetNWBool("OwnsFrag")) .. [[', ]]
	SaveString = SaveString .. [[`OwnsSmoke` = ']] .. util.booltonum(user:GetNWBool("OwnsSmoke")) .. [[', ]]
	
	SaveString = SaveString .. [[`CustTitle` = ']] .. user:GetNWString("CustTitle") .. [[' ]]
	
	SaveString = SaveString .. [[WHERE `SteamID` = ']] .. user:SteamID() .. [[']]
	
	if user:GetNWInt("TimesPlayed") == 0 then return end
	if user:GetNWInt("Points") == 0 then return end
	if StartTotal == 0 and user:GetNWInt("Cash") == 0 then return end
	
	test, er, error = mysql.query(VE.db, SaveString)

	if not er then VE.HandleError(error, VE.SaveUser, user) end
	
	Msg("Saved " .. user:Nick() .. "
")

end
concommand.Add("mysql_save", VE.SaveUser)

function TimeSpentUp ( )
	for k, user in pairs(player.GetAll()) do
		user:SetNWInt("TimeSpent", user:GetNWInt("TimeSpent") + 1)
	end
end
timer.Create("TimeSpentUp", 1, 0, TimeSpentUp)

function VE.NewUser ( user )
	Msg("Creating account for " .. user:Nick() .. "...
")
	user:PrintMessage(HUD_PRINTTALK, "Creating account... If this is not your first time playing then please contact a administrator and disconnect immediately.")

	 test, isok, error = mysql.query(VE.db, [[INSERT INTO `ve_profiles` (
`SkillLevel_1` ,
`SkillLevel_2` ,
`SkillLevel_3` ,
`SkillLevel_4` ,
`SkillLevel_5` ,
`SkillLevel_6` ,
`SkillLevel_7` ,
`SkillLevel_8` ,
`SkillLevel_9` ,
`OwnsWeapon_1` ,
`OwnsWeapon_2` ,
`OwnsWeapon_3` ,
`OwnsWeapon_4` ,
`OwnsWeapon_5` ,
`OwnsWeapon_6` ,
`OwnsWeapon_7` ,
`OwnsWeapon_8` ,
`OwnsWeapon_9` ,
`OwnsWeapon_10` ,
`OwnsWeapon_11` ,
`OwnsWeapon_12` ,
`OwnsWeapon_13` ,
`OwnsWeapon_14` ,
`SteamID` ,
`Name` ,
`EquipedWep` ,
`EquipedWep_Side` ,
`Cash` ,
`Kills` ,
`Deaths` ,
`Headshots` ,
`TimeSpent` ,
`TimesPlayed` ,
`TimesWon` ,
`Points` ,
`OwnsIronsights` ,
`OwnsFrag` ,
`OwnsSmoke`,
`Rank`,
`CustTitle`
)
VALUES (
'0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', ']] .. user:SteamID() .. [[', ']] .. user:Nick() .. [[', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', ''
);]]);

	if not isok then VE.HandleError(error, VE.SaveUser, user) end
	
	Msg("Created account for " .. user:Nick() .. " successfully.
")
	
	VE.LoadUser ( user )
end

function VE.LoadUser ( user )
	test, isok, error = mysql.query(VE.db, [[SELECT * FROM `ve_profiles` WHERE `SteamID` = ']] .. user:SteamID() .. [[']]);
	
	Msg("Loading " .. user:Nick() .. "...
")
	
	if not isok then VE.HandleError(error, VE.LoadUser, user) return end
	
	local found = false
	for k, v in pairs(test) do found = true end
	
	if not found then VE.HandleError("No account found.", VE.NewUser, user); VE.NewUser(user) return end
	
	if table.getn(test) > 1 then Msg("----------
Multiple accounts found.
----------
") end
	
	for i = 1, 9 do
		user:SetNWInt("SkillLevel_" .. tostring(i), tonumber(test[1]*))
	end
	
	for i = 1, 14 do
		user:SetNWBool("OwnsWeapon_" .. tostring(i), util.tobool(test[1][i + 9]))
	end
		
	user:SetNWInt("EquipedWep", tonumber(test[1][26]))
	user:SetNWInt("EquipedWep_Side", tonumber(test[1][27]))
	
	user:SetNWInt("Cash", tonumber(test[1][28]))
	user:SetNWInt("Kills", tonumber(test[1][29]))
	user:SetNWInt("Deaths", tonumber(test[1][30]))
	user:SetNWInt("Headshots", tonumber(test[1][31]))
	user:SetNWInt("TimeSpent", tonumber(test[1][32]))
	user:SetNWInt("TimesPlayed", tonumber(test[1][33]) + 1)
	user:SetNWInt("TimesWon", tonumber(test[1][34]))
	user:SetNWInt("Points", tonumber(test[1][35]))
	
	user:SetNWBool('OwnsIronsights', util.tobool(test[1][36]))
	user:SetNWBool('OwnsFrag', util.tobool(test[1][37]))
	user:SetNWBool('OwnsSmoke', util.tobool(test[1][38]))
	
	user:SetNWInt("Rank", tonumber(test[1][39]))
	
	user:SetNWString("CustTitle", test[1][40])
	
	Msg("Loaded " .. user:Nick() .. " successfully.
")
end

for k, v in pairs(player.GetAll()) do
	GM:PlayerInitialSpawn(v)
end

Anyone who can convert this to save to flatfile(txt) will be rewarded with 10$

this is not in requests because it’s already done and needs editing.
Thank you.

[editline]08:35PM[/editline]

Contact me on steam if you are interested
<-

Bump


(User was banned for this post ("Bumpity bump" - mahalis))

Wait, so you someone to save it to a text file for you?

If thats true then its easy money right? :stuck_out_tongue:

i think he wants it to save data to a text file instead of using mysql

Hey Colt whats up, you realize that this does almost nothing in VE, there is plenty of other code that relies on the mysql module.

http://solidfiles.com/info_imgs/7GYv.jpg

Here you go.

Yes I’m being sarcastic.

[editline]10:46am[/editline]

I’m wondering why it got four downloads. Oh well.

Here’s a guideline:

Get everything it’s currently getting as an NWInt or whatever, store that in a table (a local one mind you) and glon.encode that table then use file.Write. For loading, use file.Read and glon.decode.

Why would you not want to use mysql?

Better question yet, why would you want to use flatfile based storage?
If the issue is that you do not have access to mysql, then for the love of all living use sqlite…