-
Notifications
You must be signed in to change notification settings - Fork 0
/
sick.lua
64 lines (51 loc) · 1.26 KB
/
sick.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
55
56
57
58
59
60
61
62
63
64
-- SICK: Simple Indicative of Competitive sKill
-- Version 2.0
local h = {}
h.scores = {}
function h.set(filename, places, name, score)
h.filename = filename
h.places = places
if not h.load() then
h.scores = {}
for i = 1, places, 1 do
h.scores[i] = {score, name}
end
end
end
function h.load()
if not love.filesystem.getInfo(h.filename) then return end
local file = love.filesystem.newFile(h.filename)
if not file:open("r") then return end
h.scores = {}
for line in file:lines() do
local i = line:find('\t', 1, true)
h.scores[#h.scores+1] = {tonumber(line:sub(1, i-1)), line:sub(i+1)}
end
return file:close()
end
local function sortScore(a, b)
return a[1] > b[1]
end
function h.add(name, score)
h.scores[#h.scores+1] = {score, name}
table.sort(h.scores, sortScore)
end
function h.save()
local data = ""
for i = 1, #h.scores do
item = h.scores[i]
data = data .. item[1] .. "\t" .. item[2] .. "\n"
end
return love.filesystem.write(h.filename, data)
end
setmetatable(h, {__call = function(self)
local i = 0
return function()
i = i + 1
if i <= h.places and h.scores[i] then
return i, unpack(h.scores[i])
end
end
end})
highscore = h
return h