-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
103 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,57 @@ | ||
return function(prefix, top, sorting_finished) | ||
return function(top) | ||
local modstorage = assert(core.get_mod_storage(), "Can only init rankings at runtime!") | ||
|
||
local modstorage = assert(minetest.get_mod_storage(), "Can only init rankings at runtime!") | ||
|
||
-- If callback isn't passed then coroutine will never yield | ||
local function op_all(operation, callback) | ||
if not callback then | ||
minetest.log("warning", "op_all() called without callback, it will block the server step until it finishes") | ||
end | ||
|
||
local TARGET_INTERVAL = 0.1 | ||
local interval = 0.05 | ||
local time = minetest.get_us_time() | ||
local times = 0 | ||
local keys = modstorage:to_table()["fields"] | ||
local c = coroutine.wrap(function() | ||
for k, v in pairs(keys) do | ||
times = times + 1 | ||
operation(k, v) | ||
|
||
if callback and ((minetest.get_us_time()-time) / 1e6) >= interval then | ||
coroutine.yield() | ||
end | ||
end | ||
|
||
return "done" | ||
end) | ||
|
||
local function rep() | ||
if ((minetest.get_us_time()-time) / 1e6) > TARGET_INTERVAL then | ||
interval = interval - 0.01 | ||
else | ||
interval = interval + 0.01 | ||
end | ||
time = minetest.get_us_time() | ||
|
||
if c() ~= "done" then | ||
minetest.after(0, rep) | ||
elseif callback then | ||
callback() | ||
for k, v in pairs(modstorage:to_table()["fields"]) do | ||
local rank = core.parse_json(v) | ||
if rank ~= nil and rank.score then | ||
top:set(k, rank.score) | ||
end | ||
end | ||
|
||
rep() | ||
end | ||
|
||
local timer = minetest.get_us_time() | ||
op_all(function(noprefix_key, value) | ||
local rank = minetest.parse_json(value) | ||
return { | ||
backend = "default", | ||
top = top, | ||
modstorage = modstorage, | ||
|
||
if rank ~= nil and rank.score then | ||
top:set(noprefix_key, rank.score) | ||
end | ||
end, | ||
function() | ||
minetest.log( | ||
"action", | ||
"Sorted rankings by score '"..prefix:sub(1, -2).."'. Took "..((minetest.get_us_time()-timer) / 1e6) | ||
) | ||
sorting_finished() | ||
end) | ||
get = function(self, pname) | ||
pname = PlayerName(pname) | ||
|
||
return { | ||
backend = "default", | ||
top = top, | ||
modstorage = modstorage, | ||
local rank_str = self.modstorage:get_string(pname) | ||
|
||
prefix = "", | ||
|
||
op_all = op_all, | ||
|
||
get = function(self, pname) | ||
pname = PlayerName(pname) | ||
|
||
local rank_str = self.modstorage:get_string(pname) | ||
if not rank_str or rank_str == "" then | ||
return false | ||
end | ||
|
||
if not rank_str or rank_str == "" then | ||
return false | ||
end | ||
return core.parse_json(rank_str) | ||
end, | ||
set = function(self, pname, newrankings, erase_unset) | ||
pname = PlayerName(pname) | ||
|
||
return minetest.parse_json(rank_str) | ||
end, | ||
set = function(self, pname, newrankings, erase_unset) | ||
pname = PlayerName(pname) | ||
if not erase_unset then | ||
local rank = self:get(pname) | ||
if rank then | ||
for k, v in pairs(newrankings) do | ||
rank[k] = v | ||
end | ||
|
||
if not erase_unset then | ||
local rank = self:get(pname) | ||
if rank then | ||
for k, v in pairs(newrankings) do | ||
rank[k] = v | ||
newrankings = rank | ||
end | ||
|
||
newrankings = rank | ||
end | ||
end | ||
|
||
self.top:set(pname, newrankings.score or 0) | ||
self.modstorage:set_string(pname, minetest.write_json(newrankings)) | ||
end, | ||
add = function(self, pname, amounts) | ||
pname = PlayerName(pname) | ||
|
||
local newrankings = self:get(pname) or {} | ||
|
||
for k, v in pairs(amounts) do | ||
newrankings[k] = (newrankings[k] or 0) + v | ||
end | ||
self.top:set(pname, newrankings.score or 0) | ||
self.modstorage:set_string(pname, core.write_json(newrankings)) | ||
end, | ||
add = function(self, pname, amounts) | ||
pname = PlayerName(pname) | ||
|
||
self.top:set(pname, newrankings.score or 0) | ||
self.modstorage:set_string(pname, minetest.write_json(newrankings)) | ||
end, | ||
del = function(self, pname) | ||
pname = PlayerName(pname) | ||
local newrankings = self:get(pname) or {} | ||
|
||
self.top:set(pname, 0) | ||
self.modstorage:set_string(pname) | ||
end, | ||
} | ||
for k, v in pairs(amounts) do | ||
newrankings[k] = (newrankings[k] or 0) + v | ||
end | ||
|
||
self.top:set(pname, newrankings.score or 0) | ||
self.modstorage:set_string(pname, core.write_json(newrankings)) | ||
end | ||
} | ||
end |