//Custom time and score save
//Made by DarknessStylus

local timet = 0
local score = 0

addHook("PreThinkFrame", do
	for p in players.iterate do
		if p.savetimetable == nil or (type(p.savetimetable) ~= "table")
			p.savetimetable = {
				timet = 0,
				score = 0,
				savescore = false,
				savetime = false
			}
		end
	end
end)

addHook("IntermissionThinker", do
	for p in players.iterate do
		--if not (p.mo and p.mo.valid) then return end
		local savet = p.savetimetable
		//Check if the new score and time is lower than the saved score and time
		local file = io.openlocal("client/TimeScores/"..G_BuildMapTitle(p.mapselected).."_saved_score.dat")
		if file
			local string = file:read("*a")
			local oldscore = tonumber(string)
			if p.score > oldscore
				savet.savescore = true
				--print(p.score)
			end
			file:close()
		else
			savet.savescore = true
		end
	end
end)

addHook("ThinkFrame", do
	for p in players.iterate do
		if not (p.mo and p.mo.valid) then return end
		local savet = p.savetimetable
		if p.exiting
			if not savet.dontcalltwice
				//Check if the new score and time is lower than the saved score and time
				local file = io.openlocal("client/TimeScores/"..G_BuildMapTitle(p.mapselected).."_saved_score.dat")
				if file
					local string = file:read("*a")
					local oldscore = tonumber(string)
					if p.score > oldscore
						savet.savescore = true
					end
					file:close()
				else
					savet.savescore = true
				end
				local file2 = io.openlocal("client/TimeScores/"..G_BuildMapTitle(p.mapselected).."_saved_time.dat")
				if file2
					local string = file2:read("*a")
					local oldtime = tonumber(string)
					if oldtime > p.realtime
						savet.savetime = true
					end
					file2:close()
				else
					savet.savetime = true
				end
				savet.dontcalltwice = true
			end
		else
			if savet.dontcalltwice
				savet.dontcalltwice = nil
			end
		end
		//Save your records
		if savet.savescore
			if io and p == consoleplayer
				local file = io.openlocal("client/TimeScores/"..G_BuildMapTitle(p.mapselected).."_saved_score.dat", "w+")
				file:write(tostring(p.score))
				file:close()
				savet.savescore = false
				--CONS_Printf(p, "Saved score")
			end
		end
		if savet.savetime
			if io and p == consoleplayer
				local file2 = io.openlocal("client/TimeScores/"..G_BuildMapTitle(p.mapselected).."_saved_time.dat", "w+")
				file2:write(tostring(p.realtime))
				file2:close()
				savet.savetime = false
				--CONS_Printf(p, "Saved time")
			end
		end
		//If selecting a stage, use those files
		if p.stagegatepos.changeact
		or p.selectingstage
			if io and p == consoleplayer
				local file = io.openlocal("client/TimeScores/"..G_BuildMapTitle(p.mapselected).."_saved_score.dat")
				if file
					local string = file:read("*a")
					savet.score = tonumber(string)
					file:close()
				else
					savet.score = 0
				end
				local file2 = io.openlocal("client/TimeScores/"..G_BuildMapTitle(p.mapselected).."_saved_time.dat")
				if file2
					local string = file2:read("*a")
					savet.timet = tonumber(string)
					file2:close()
				else
					savet.timet = 0
				end
			end
		end
	end
end)