class "Highscores"

function Highscores : Highscores (scorefile)
	scorefile = system.projectpath().."/"..scorefile
	local fn,err = loadfile(scorefile)
	self.scorefile = scorefile
	if fn then
		self.scores = fn()
	end
	self.scores = self.scores or {}
end

function Highscores : enter (name, points)
	local time = os.date("%d.%m.%Y")
	local record = {name = name, points = points, date = time}
	table.insert(self.scores,record)
	for i,v in ipairs(self.scores) do v.id = i end
	table.sort(self.scores, function(a,b) if a.points == b.points then return a.id<b.id else return tonumber(a.points)>tonumber(b.points) end end)
	for i,v in ipairs(self.scores) do v.id = nil end
	while #self.scores>10 do table.remove(self.scores) end
	self:savescores()
end

function Highscores : savescores (file)
	file = file or self.scorefile
	local out = UtilFunctions.serialize(self.scores)
	local f = assert(io.open(file,"w"))
	f:write(out)
	f:close()
end

function Highscores : place(points)
	for i,v in ipairs(self.scores) do
		if v.points<points then return i end
	end
	return #self.scores + 1
end

highscores = new (Highscores,"highscores.cfg")