Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Class: Managing a highscore list — Gideros Forum

Class: Managing a highscore list

MikeHartMikeHart Guru
edited January 2012 in Code snippets
Hi folks,

here is the gideros verson of my high-score list manager. You need JSON for it. I got my version from here:

http://www.chipmunkav.com/downloads/otherdownloads.htm


Edit: Updated to version 1.11 which fixes a bug regarding scores with a value of zero.
-- score_gideros.lua 
-- Author Michael Hartlef
-- Version 1.11
-- License: MIT
 
cScoreList = gideros.class(EventDispatcher)
 
--****************************************************************
function cScoreList:init(entryCount)
--****************************************************************
	self.entrycount = entryCount
	self.list = {}
	local i = 0
	for i = entryCount,1,-1 do
		local ts = {}
		ts.name = "---"
		ts.points = 0
		table.insert(self.list, ts)
	end
end
 
--****************************************************************
function cScoreList:load(fileName)
--****************************************************************
	local hsFile = io.open( fileName, "r" )
	if hsFile then
		local hsContent = hsFile:read( "*a" )
		self.list = Json.Decode(hsContent)
		self.entrycount = #self.list
		io.close( hsFile )
	end
end
 
--****************************************************************
function cScoreList:save(fileName)
--****************************************************************
	local hsFile = io.open( fileName, "w" )
	if hsFile then
		local hsContent = Json.Encode(self.list)
		hsFile:write( hsContent )
		io.close( hsFile )
	end
end
 
--****************************************************************
function cScoreList:checkEntry(scorepoints)
--****************************************************************
	local i = 0
	if scorepoints == 0 then return (self.entrycount+1) end
	for i = 1,#self.list,1 do
		local ts = self.list[i]
		if ts.points < scorepoints then
			return i
		end
	end
	return 0
end
 
--****************************************************************
function cScoreList:storeEntry(index, scorepoints, name)
--****************************************************************
	if scorepoints == 0 or index< 1 or index > self.entrycount then return end
	local ts = {}
	ts.name = name
	ts.points = scorepoints
	table.insert(self.list, index, ts)
	table.remove(self.list,(self.entrycount+1))
end
 
--****************************************************************
function cScoreList:count()
--****************************************************************
	return self.entrycount
end
 
--****************************************************************
function cScoreList:getName(i)
--****************************************************************
	local ts = self.list[i]
	return ts.name
end
 
--****************************************************************
function cScoreList:getPoints(i)
--****************************************************************
	local ts = self.list[i]
	return ts.points
end
Here is a code sample where I check the score against the list and then then maybe store it:
HighScore = cScoreList.new(10)
--......
local scoreIndex = HighScore:checkEntry(gameScore)
if scoreIndex <= HighScore:count() then
	HighScore:storeEntry(scoreIndex, gameScore, "NameOfPlayer")
end
HighScore:save("scoreList.txt")

Likes: atilim, gorkem

+1 -1 (+2 / -0 )Share on Facebook

Comments

Sign In or Register to comment.