balloon_bop/leaderboard.lua

180 lines
5.1 KiB
Lua

local storage = minetest.get_mod_storage()
function balloon_bop.store_scores(data)
storage:set_string("scores", minetest.serialize(data))
end
function balloon_bop.get_scores()
return minetest.deserialize(storage:get_string("scores"))
end
-- _bop.scores is a table with the forllowing structure:
-- {
-- [arena_name] ={
-- [p_name]=score,
-- [p_name]=score,
-- ..
-- },
-- [arena_name] ={
-- [p_name]=score,
-- [p_name]=score,
-- ..
-- },
-- ..
-- }
balloon_bop.scores = balloon_bop.get_scores() or {}
function balloon_bop.get_leader_form(arena_name)
local p_names = "<no data>"
local scores = "<no data>"
if balloon_bop.scores[arena_name] then
local ordered_names = {}
local data = balloon_bop.scores[arena_name]
-- sort the scores
for p_name,score in pairs(data) do
if ordered_names == {} then
table.insert(ordered_names,p_name)
else
for idx,o_name in ipairs(ordered_names) do
if score >= data[o_name] then
table.insert(ordered_names,idx,p_name)
end
end
end
end
if #ordered_names >=1 then
p_names = ""
scores = ""
for idx,u_name in ipairs(ordered_names) do
p_names = p_names .. u_name
scores = scores .. tostring(data[u_name])
if idx ~= #ordered_names then
p_names = p_names .. ","
scores = scores .. ","
end
end
end
end
return "formspec_version[5]"..
"size[10.5,10.5]"..
"button[0.6,0.6;9.3,0.8;hs_title;Balloon Bop Leaderboard]"..
"button[0.6,1.4;9.3,0.8;arena_name;<arena>]"..
"textlist[0.6,2.5;7.6,7.4;names;"..p_names..";1;false]"..
"textlist[8.3,2.5;1.6,7.4;scores;"..scores..";1;false]"
end
function balloon_bop.get_leader_form_endgame(arena_name,l_data)
local p_names = "<no data>"
local scores = "<no data>"
local lp_names = "<no data>"
local lscores = "<no data>"
if balloon_bop.scores[arena_name] then
local ordered_names = {}
local data = balloon_bop.scores[arena_name]
-- sort the scores
for p_name,score in pairs(data) do
if ordered_names == {} then
table.insert(ordered_names,p_name)
else
for idx,o_name in ipairs(ordered_names) do
if score >= data[o_name] then
table.insert(ordered_names,idx,p_name)
end
end
end
end
if #ordered_names >=1 then
p_names = ""
scores = ""
for idx,u_name in ipairs(ordered_names) do
p_names = p_names .. u_name
scores = scores .. tostring(data[u_name])
if idx ~= #ordered_names then
p_names = p_names .. ","
scores = scores .. ","
end
end
end
end
if l_data then
local ordered_names = {}
local data = l_data
-- sort the scores
for p_name,score in pairs(data) do
if ordered_names == {} then
table.insert(ordered_names,p_name)
else
for idx,o_name in ipairs(ordered_names) do
if score >= data[o_name] then
table.insert(ordered_names,idx,p_name)
end
end
end
end
if #ordered_names >=1 then
lp_names = ""
lscores = ""
for idx,u_name in ipairs(ordered_names) do
lp_names = lp_names .. u_name
scores = scores .. tostring(data[u_name])
if idx ~= #ordered_names then
lp_names = lp_names .. ","
lscores = lscores .. ","
end
end
end
end
return "formspec_version[5]"..
"size[10.5,10.5]"..
"button[0.6,0.6;9.3,0.8;hs_title;Balloon Bop Leaderboard]"..
"button[0.6,1.4;9.3,0.8;arena_name;<arena>]"..
"button[0.6,1.4;9.3,0.8;arena_name;<arena>]"..
"textlist[0.6,6.1;7.3,3.8;g_names;"..p_names..";1;false]"..
"textlist[8,6.1;1.9,3.8;g_scores;"..scores..";1;false]"..
"textlist[0.6,3.3;7.3,2;l_names;"..lp_names..";1;false]"..
"textlist[8,3.3;1.9,2;l_scores;"..lscores..";1;false]"..
"button[0.6,2.7;9.3,0.6;this;This Game]"..
"button[0.6,5.5;9.3,0.6;high;LeaderBoard]"
end
minetest.register_chatcommand("balloonbopscores", {
params = "<arena name>", -- Short parameter description
description = "Show leaderboard", -- Full description
func = function(name, param)
if param then
if balloon_bop.scores[param] then
minetest.show_formspec(name, "bb_scores", balloon_bop.get_leader_form(balloon_bop.scores[param]))
else
return false, "[!] No data for that arena or that arena does not exist!"
end
end
end,
})