Last active
August 10, 2020 20:44
-
-
Save Kyrremann/b29397159e939cff2896ed53f1e7c10f to your computer and use it in GitHub Desktop.
SICK: Simple Indicative of Competitive sKill - https://love2d.org/wiki/SICK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- SICK: Simple Indicative of Competitive sKill | |
-- aka libhighscore | |
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 do | |
h.scores[i] = {score, name} | |
end | |
end | |
end | |
function h.load() | |
local file = love.filesystem.newFile(h.filename) | |
if not love.filesystem.getInfo(h.filename) or 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 file = love.filesystem.newFile(h.filename) | |
if not file:open("w") then return end | |
for i = 1, #h.scores do | |
item = h.scores[i] | |
file:write(item[1] .. "\t" .. item[2] .. "\n") | |
end | |
return file:close() | |
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}) | |
local highscore = h | |
return h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment