-- Get Player Rank from userstats.dat by MikuAuahDark
-- This function translates next 4-byte sequence to 32bit unsigned integer. Bit hard to explain because used internally
local function Int(f)
	return f:read(1):byte()+f:read(1):byte()*256+f:read(1):byte()*65536+f:read(1):byte()*16777216
end

-- This function does everything for you
function GetUserStatsRank(usgn)
	-- Open file
	local f=io.open("sys/stats/userstats.dat","rb")
	-- Determine file size
	local size=f:seek("end")
	-- Seek to data block directly
	f:seek("set",17)
	-- Declare our table information
	local temp={
		--name="",	-- Uncomment it if you want to get first used player name on the server
		isexists=false,
		score=0,
		frags=0,
		deaths=0,
		time=0,
	}
	-- Loop
	repeat
		-- Get player name on the server
		local name=f:read("*l")
		-- Read USGN
		local temp_usgn=Int(f)
		-- If the usgn that want we retrieve it's information is this
		if usgn==temp_usgn then
			-- Yes, this is what we want
			temp.isexists=true
			-- temp.name=name	-- Uncomment it if you want to get first used player name on the server. This too
			-- Set data information
			temp.score=Int(f)
			temp.frags=Int(f)
			temp.deaths=Int(f)
			temp.time=Int(f)
			-- Get out from loop
			break
		-- Otherwise
		else
			-- Skip this data information
			f:seek("cur",16)
		end
	-- Break loop on EOF
	until f:seek("cur")==size
	-- Close file
	f:close()
	-- Return result. Check isexists field first before retrieving information
	return temp
end