-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighscoreManager.lua
54 lines (45 loc) · 1.52 KB
/
HighscoreManager.lua
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
require("class")
HighscoreEntry = class(function(self, name, score)
self.name = name
self.score = score
end)
HighscoreManager = {}
function HighscoreManager.init()
HighscoreManager.highscores = {}
love.filesystem.setIdentity("Snakes")
HighscoreManager.loadFromDisk()
end
function HighscoreManager.addEntry(newEntry)
local score = newEntry.score
-- no need for complicated sorting here just find the score location and insert it
for i,entry in ipairs(HighscoreManager.highscores) do
if entry.score < score then
table.insert(HighscoreManager.highscores, i, newEntry)
HighscoreManager.saveToDisk()
return
end
end
-- incase where the biggest score
HighscoreManager.highscores[#HighscoreManager.highscores+1] = newEntry
HighscoreManager.saveToDisk()
end
function HighscoreManager.loadFromDisk()
local data, size = love.filesystem.read("highscore.sav")
local entrys = {}
local sep = ","
-- some string slipt functionIi found on lua-users.org
data:gsub("([^"..sep.."]*)"..sep, function(c) table.insert(entrys, c) end)
-- takes every to elemets and add them togeter as an highscore entry
for i=0,(#entrys/2)-1 do
local j = (i*2)+1
HighscoreManager.highscores[#HighscoreManager.highscores+1] = HighscoreEntry(entrys[j], tonumber(entrys[j+1]))
end
end
function HighscoreManager.saveToDisk()
local data = ""
-- build comma seprated list of the highscores
for _,entry in ipairs(HighscoreManager.highscores) do
data = data..entry.name..","..entry.score..","
end
love.filesystem.write( "highscore.sav", data )
end