All pastes #744579 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-10-21 16:51 UTC
rendered paste body
function SaveAchievements(user)
	Cleared = true
	
	for k,v in pairs(Achieve) do
		local unique = k
		print(tostring(unique))
		if not user.Achieves[unique] then
			user.Achieves[unique] = 0
		end
	end
	
	-- Optimize the ID so we can save as little data as possible, yet have enough to make sure there are no duplicates.
	local sid = user:SteamID()
	local steamid_seperated = string.Explode(":",sid)
	local motherfile = string.sub(steamid_seperated[1], 7, 7) .. steamid_seperated[2]  -- Thanks to Termy58 for helping me figure this'n out!
	sid = steamid_seperated[3]
	-- Players will be seperated into two files, 00 and 01.
	
	if !(file.Exists("TheChillZone/Achievements/players"..motherfile..".txt")) then
		file.Write("TheChillZone/Achievements/players"..motherfile..".txt","")
	end
	
	str = file.Read("TheChillZone/Achievements/players"..motherfile..".txt")
	print("str = "..str)
	local pbl = string.Explode("\n",str)
	
	for key,v in pairs(pbl) do
	
		print("Checking pbl["..key.."] ("..pbl[key]..")")
		
		for ach,amt in pairs(user.Achieves) do
		
			print("Crosschecking achievement "..ach.." with amount of "..amt.." for pbl["..key.."]")
			local pba = string.Explode("@",pbl[key])
			
			if pba[2] then
			
				print("pba[2] exists ("..pba[2]..")")
				
				if pba[1] == sid then -- If the ID matches...
				
					print("pba[1] == sid ("..pba[1].." == "..sid..")")
					
					if pba[2] == ach then -- If the achievement matches...
					
						print("pba[2] == ach ("..pba[2].." == "..ach..")")
						print("prior to change, pba[3] == "..pba[3])
						str = string.gsub(str,pba[3],amt) -- Replace the old amount with the new amount.
						print("after the change, pba[3] == "..pba[3]..", should be "..amt)
						Cleared = false
						
					end
				end
			end
		end
	end
	
	print("\n\n\nSTRING IS: "..str.."\n\n")
	
	if Cleared then
		for k,v in pairs(user.Achieves) do
			print(tostring(k))
			str = str..sid.."@"..k.."@"..v.."\n"
		end
	end
	
	file.Write("TheChillZone/Achievements/players"..motherfile..".txt",str)
end