Another local score framework

No replies
MikeHart
User offline. Last seen 1 week 2 days ago. Offline
Joined: 22 Mar 2010

Hi folks,

here is my local score framework. Suggestions and critics are welcome. To use this in your game, you have 7 easy functions that will interface with a list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- Create a new scorelist. This has to be done ALWAYS before you load one
scList = score.newList(numberOfEntries)
 
-- Load the scorelist from a given filename
scList:Load(scoreFileName)
 
-- Save the scorelist to a given filename
scList:Save(scoreFileName)
 
-- Check if a points value is inside the highscore list, and if yes return the index. 
-- It will return 0 if it isn't inside the list
index = scList:CheckScore(points)
 
-- Store a new score at the given index with the point value and a name
scList:StoreEntry(index,points,name)
 
-- Return the name at a given index
scoreName = scList:GetName(index)
 
-- Return the point value at a given index
scorePoints = scList:GetPoints(index)

Here is the score.lua file. It uses the JSON lib from this place:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-- score.lua 
-- Author Michael Hartlef
-- Version 1.0
-- License: MIT
 
module(..., package.seeall)
Json = require("Json")
 
 
--****************************************************************
function newList(hsnum)
--****************************************************************
        scoreList = {}
        scoreList.entrycount = hsnum
        scoreList.list = {}
        local i = 0
        for i = hsnum,1,-1 do
                local ts = {}
                ts.name = "---"
                ts.points = 0
                table.insert(scoreList.list, ts)
        end
        
        
        --****************************************************************
        function scoreList:Load(fn)
        --****************************************************************
                local path = system.pathForFile( fn, system.DocumentsDirectory )
                -- io.open opens a file at path. returns nil if no file found
                local file = io.open( path, "r" )
                if file then
                        -- read all contents of file into a string
                        local contents = file:read( "*a" )
                        scoreList.list = Json.Decode(contents);
                        scoreList.entrycount = #scoreList.list
                        io.close( file )
                end
        end
        
        --****************************************************************
        function scoreList:Save(fn)
        --****************************************************************
                local path = system.pathForFile( fn, system.DocumentsDirectory )
                local file = io.open( path, "w" )
                if file then
                        local contents = Json.Encode(scoreList.list)
                        file:write( contents )
                        io.close( file )
                end
        end
        
        
        --****************************************************************
        function scoreList:CheckScore(p)
        --****************************************************************
                print ("#scoreList.list:"..#scoreList.list)
                local i = 0
                for i = 1,#scoreList.list,1 do
                        local ts = scoreList.list[i]
                        if ts.points < p then
                                return i
                        end
                end
                return 0
        end
        
        --****************************************************************
        function scoreList:StoreEntry(index,points,name)
        --****************************************************************
                local ts = {}
                ts.name = name
                ts.points = points
                table.insert(scoreList.list, index, ts)
                table.remove(scoreList.list,(scoreList.entrycount+1))
        end
        
        --****************************************************************
        function scoreList:Count()
        --****************************************************************
                return scoreList.entrycount
        end
        
        --****************************************************************
        function scoreList:GetName(i)
        --****************************************************************
                local ts = scoreList.list[i]
                return ts.name
        end
        
        --****************************************************************
        function scoreList:GetPoints(i)
        --****************************************************************
                local ts = scoreList.list[i]
                return ts.points
        end
        
        return scoreList
end

Ok, that's it. I hope you like it.

Cheers
Michael