Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved highscores from worldpath to modstorage #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions highscore.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@


-- ordered list: { name: '', xp: 0 }
xp_redo.highscore = {}

local fname = minetest.get_worldpath().."/highscore.txt"
local storage = minetest.get_mod_storage()


local write_file = function()
local f = io.open(fname, "w")
local data_string = minetest.serialize(xp_redo.highscore)
f:write(data_string)
io.close(f)
local function write_file(highscore)
if type(highscore) == "table" then
storage:set_string("highscore", minetest.serialize(highscore))
end
end

local f = io.open(fname, "r")
if f then -- file exists
local data_string = f:read("*all")
xp_redo.highscore = minetest.deserialize(data_string)
io.close(f)
else
write_file()

--Get general modstorage
local function load_file()
local highscore_table = storage:get_string("highscore")
if highscore_table then
highscore_table = minetest.deserialize(highscore_table)
--extra check needed
if type(highscore_table) ~= "table" then
highscore_table = {}
end
else
highscore_table = {}
end
return highscore_table
end

xp_redo.highscore = load_file()

local update_highscore = function()
local players = minetest.get_connected_players()
Expand Down Expand Up @@ -54,11 +55,22 @@ local update_highscore = function()
end

local timer = 0
local count = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime;
if timer >= 60 then
update_highscore()
write_file()
timer = 0
end
timer = timer + dtime;
if timer >= 60 then
count = count + 1
update_highscore()
--write_file()
timer = 0
end
if count >= 5 then
write_file(xp_redo.highscore)
end
Comment on lines +67 to +69
Copy link
Member

@S-S-X S-S-X Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count doesn't do anything useful after first write, you probably meant to reset it here to write every 5 minutes?

Suggested change
if count >= 5 then
write_file(xp_redo.highscore)
end
if count >= 5 then
write_file(xp_redo.highscore)
count = 0
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ups emberassing that I forgot this but yeah I meant that

end)

minetest.register_on_shutdown(function()
write_file(xp_redo.highscore)
end)