wip score system and bug fix

master
MisterE123 2022-04-19 14:52:37 -04:00
parent d50d9a81ba
commit ded2fe8750
6 changed files with 209 additions and 5 deletions

View File

@ -2,8 +2,11 @@
local modname = "balloon_bop"
balloon_bop = {}
local round_time_default = 20
--====================================================
--====================================================
-- Minigame registration
@ -82,7 +85,7 @@ end
local manager_path = path .. "/minigame_manager/"
dofile(path .. "/leaderboard.lua")
dofile(path .. "/nodes.lua")
dofile(path .. "/items.lua")
dofile(path .. "/blocks.lua")

179
leaderboard.lua Normal file
View File

@ -0,0 +1,179 @@
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,
})

View File

@ -2,5 +2,5 @@
arena_lib.on_load("balloon_bop", function(arena)
arena_lib.HUD_send_msg_all("title", arena, "Pop all the balloons, don't let them touch anything else!", 3, nil, "0xE6482E")
balloon_bop.scores[arena.name] = balloon_bop.scores[arena.name] or {}
end)

View File

@ -63,7 +63,7 @@ arena_lib.on_join("balloon_bop", function(p_name, arena, as_spectator)
number = 0xE6482E,
position = { x = .97, y = .03},
offset = {x = 0, y = 0},
text = arena.players[pl_name].score,
text = arena.players[p_name].score,
alignment = {x = -1, y = 1},
scale = {x = 100, y = 100},
size = {x = 2 },

View File

@ -27,6 +27,12 @@ arena_lib.on_time_tick("balloon_bop", function(arena)
if balloon_bop.infohuds[pl_name] then
player:hud_change(balloon_bop.infohuds[pl_name],"text",arena.players[pl_name].score)
end
-- set each players' nametag with their score
player:set_nametag_attributes({
text = tostring(arena.players[pl_name].score),
color = "#5A5353",
bgcolor = "#CFC6B8" or false,
})
end
end
@ -65,7 +71,7 @@ arena_lib.on_time_tick("balloon_bop", function(arena)
if arena.arena_lives == 0 then
for pl_name,stats in pairs(arena.players) do -- it is a good convention to use "pl_name" in for loops and "p_name" elsewhere
if balloon_bop.infohuds[pl_name] then
player = minetest.get_player_by_name(pl_name)
local player = minetest.get_player_by_name(pl_name)
if player then
-- clear HUDs
player:hud_remove(balloon_bop.infohuds[pl_name])
@ -97,6 +103,22 @@ arena_lib.on_time_tick("balloon_bop", function(arena)
count = count + 1
end
end
-- show leaderboards
local l_data = {}
for pl_name,stats in pairs(arena.players) do
l_data[pl_name]=stats.score
if balloon_bop.scores[arena.name][pl_name] then
balloon_bop.scores[arena.name][pl_name] = balloon_bop.scores[arena.name][pl_name] + stats.score
else
balloon_bop.scores[arena.name][pl_name] = stats.score
end
end
balloon_bop.store_scores(balloon_bop.scores)
for pl_name,stats in pairs(arena.players) do
minetest.show_formspec(pl_name, "bb_scores", balloon_bop.get_leader_form_endgame(balloon_bop.scores[arena.name],l_data))
end
if count > 1 then -- we cannot have more than one winner
arena_lib.force_arena_ending('balloon_bop', arena,'Game')
else

View File

@ -1,2 +1,2 @@
name = balloon_bop
depends = arena_lib, default
depends = arena_lib, default