From ded2fe875030a5d3469232efa0f1b3e3db3d2bea Mon Sep 17 00:00:00 2001 From: MisterE123 Date: Tue, 19 Apr 2022 14:52:37 -0400 Subject: [PATCH] wip score system and bug fix --- init.lua | 5 +- leaderboard.lua | 179 ++++++++++++++++++++++++++++++ minigame_manager/on_load.lua | 2 +- minigame_manager/on_start.lua | 2 +- minigame_manager/on_time_tick.lua | 24 +++- mod.conf | 2 +- 6 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 leaderboard.lua diff --git a/init.lua b/init.lua index 19d94b3..102cf09 100644 --- a/init.lua +++ b/init.lua @@ -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") diff --git a/leaderboard.lua b/leaderboard.lua new file mode 100644 index 0000000..cd67e61 --- /dev/null +++ b/leaderboard.lua @@ -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 = "" + local scores = "" + + 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;]".. + "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 = "" + local scores = "" + local lp_names = "" + local lscores = "" + + 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;]".. + "button[0.6,1.4;9.3,0.8;arena_name;]".. + "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 = "", -- 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, +}) diff --git a/minigame_manager/on_load.lua b/minigame_manager/on_load.lua index 8fa6ea8..a52632c 100644 --- a/minigame_manager/on_load.lua +++ b/minigame_manager/on_load.lua @@ -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) \ No newline at end of file diff --git a/minigame_manager/on_start.lua b/minigame_manager/on_start.lua index 940ab99..2575ed4 100644 --- a/minigame_manager/on_start.lua +++ b/minigame_manager/on_start.lua @@ -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 }, diff --git a/minigame_manager/on_time_tick.lua b/minigame_manager/on_time_tick.lua index 7470ff7..8b7b7a8 100644 --- a/minigame_manager/on_time_tick.lua +++ b/minigame_manager/on_time_tick.lua @@ -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 diff --git a/mod.conf b/mod.conf index b904b6e..c159d56 100644 --- a/mod.conf +++ b/mod.conf @@ -1,2 +1,2 @@ name = balloon_bop -depends = arena_lib, default \ No newline at end of file +depends = arena_lib, default