Skip to content

Commit c6b3fbd

Browse files
ClobberXDrubenwardy
authored andcommitted
Improve rankings formspec
- Renamed `render_per_team_stats` to `render_team_stats`, for the immaculate intention of shortening lines without adding a line-break. - Adds fixed minimum width to columns to space them out. - Displays a player's rankings at the bottom of the formspec without having to scroll down. (if the player isn't in the top 50) - This is achieved using a very hacky method: Another table is required, with the same column widths as the main table. This is the reason why I added fixed width to columns in the main table. :)
1 parent 5bd09b4 commit c6b3fbd

File tree

1 file changed

+69
-37
lines changed

1 file changed

+69
-37
lines changed

mods/ctf_stats/gui.lua

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
local storage = minetest.get_mod_storage()
22
local prev_match_summary = storage:get_string("prev_match_summary")
33

4-
local function render_per_team_stats(red, blue, stat, round)
4+
-- Formspec element that governs table columns and their attributes
5+
local tablecolumns = {
6+
"tablecolumns[color;",
7+
"text;",
8+
"text,width=20;",
9+
"text,width=4;",
10+
"text,width=4;",
11+
"text,width=4;",
12+
"text,width=6;",
13+
"text,width=6;",
14+
"text,width=6;",
15+
"text,width=6]"
16+
}
17+
tablecolumns = table.concat(tablecolumns, "")
18+
19+
local function render_team_stats(red, blue, stat, round)
520
local red_stat, blue_stat = red[stat], blue[stat]
621
if round then
7-
red_stat = math.floor(red_stat*10)/10
8-
blue_stat = math.floor(blue_stat*10)/10
22+
red_stat = math.floor(red_stat * 10) / 10
23+
blue_stat = math.floor(blue_stat * 10) / 10
924
end
10-
return red_stat+blue_stat .. " (" .. minetest.colorize(red.color, tostring(red_stat)) .. " - " .. minetest.colorize(blue.color, tostring(blue_stat)) .. ")"
25+
return red_stat + blue_stat .. " (" ..
26+
minetest.colorize(red.color, tostring(red_stat)) .. " - " ..
27+
minetest.colorize(blue.color, tostring(blue_stat)) .. ")"
1128
end
1229

1330
function ctf_stats.get_formspec_match_summary(stats, winner_team, winner_player, time)
@@ -42,30 +59,32 @@ function ctf_stats.get_formspec_match_summary(stats, winner_team, winner_player,
4259
end
4360

4461
local match_length = string.format("%02d:%02d:%02d",
45-
math.floor(time / 3600), -- hours
46-
math.floor((time % 3600) / 60), -- minutes
47-
math.floor(time % 60)) -- seconds
62+
math.floor(time / 3600), -- hours
63+
math.floor((time % 3600) / 60), -- minutes
64+
math.floor(time % 60)) -- seconds
4865

4966
local ret = ctf_stats.get_formspec("Match Summary", players, 1)
5067

5168
if stats[winner_team] then
5269
local winner_color = ctf.flag_colors[winner_team]:gsub("0x", "#")
5370
ret = ret .. "item_image[0,0;1,1;ctf_flag:flag_top_"..winner_team.."]"
54-
ret = ret .. "label[1,0;" .. minetest.colorize(winner_color, "TEAM " .. winner_team:upper() .. " WON!") .. "]"
55-
ret = ret .. "label[1,0.5;Flag captured by " .. winner_player .. "]"
71+
ret = ret .. "label[1,0;" .. minetest.colorize(winner_color,
72+
"TEAM " .. winner_team:upper() .. " WON!") .. "]"
73+
ret = ret .. "label[1,0.5;Flag captured by " ..
74+
minetest.colorize(winner_color, winner_player) .. "]"
5675
else
5776
ret = ret .. "label[1,0;NO WINNER]"
5877
end
5978

6079
ret = ret .. "label[6.5,0;Kills]"
61-
ret = ret .. "label[8,0;" .. render_per_team_stats(red, blue, "kills") .. "]"
80+
ret = ret .. "label[8,0;" .. render_team_stats(red, blue, "kills") .. "]"
6281
ret = ret .. "label[6.5,0.5;Attempts]"
63-
ret = ret .. "label[8,0.5;" .. render_per_team_stats(red, blue, "attempts") .. "]"
64-
ret = ret .. "label[9.5,0;Duration]"
65-
ret = ret .. "label[11,0;" .. match_length .. "]"
66-
ret = ret .. "label[9.5,0.5;Total score]"
67-
ret = ret .. "label[11,0.5;" .. render_per_team_stats(red, blue, "score", true) .. "]"
68-
ret = ret .. "label[3.5,7.2;Tip: type /rankings for league tables]"
82+
ret = ret .. "label[8,0.5;" .. render_team_stats(red, blue, "attempts") .. "]"
83+
ret = ret .. "label[10.5,0;Duration]"
84+
ret = ret .. "label[12,0;" .. match_length .. "]"
85+
ret = ret .. "label[10.5,0.5;Total score]"
86+
ret = ret .. "label[12,0.5;" .. render_team_stats(red, blue, "score", true) .. "]"
87+
ret = ret .. "label[2,7.75;Tip: type /rankings for league tables]"
6988

7089
-- Set prev_match_summary and write to mod_storage
7190
prev_match_summary = ret
@@ -79,15 +98,15 @@ function ctf_stats.get_formspec(title, players, header, hlt_name)
7998
return one.score > two.score
8099
end)
81100

82-
local ret = "size[13,"..6.5+header.."]"
101+
local ret = "size[14," .. 7 + header .. "]"
83102
ret = ret .. default.gui_bg .. default.gui_bg_img
84103
ret = ret .. "container[0," .. header .. "]"
85104

86-
ret = ret .. "vertlabel[0,0;" .. title .. "]"
87-
ret = ret .. "tablecolumns[color;text;text;text;text;text;text;text;text;text]"
105+
ret = ret .. "vertlabel[0,1;" .. title .. "]"
106+
ret = ret .. tablecolumns
88107
ret = ret .. "tableoptions[highlight=#00000000]"
89-
ret = ret .. "table[0.5,0;12.25,6;scores;"
90-
ret = ret .. "#ffffff,,Player,Kills,Deaths,K/D ratio,Bounty kills,Captures,Attempts,Score"
108+
ret = ret .. "table[0.5,0;13.25,6.1;scores;"
109+
ret = ret .. "#ffffff,,Player,Kills,Deaths,K/D,Bounty Kills,Captures,Attempts,Score"
91110

92111
local player_in_top_50 = false
93112

@@ -116,7 +135,11 @@ function ctf_stats.get_formspec(title, players, header, hlt_name)
116135
"," .. pstat.attempts ..
117136
"," .. math.floor(pstat.score * 10) / 10
118137
end
138+
ret = ret .. ";-1]"
119139

140+
-- If hlt_name not in top 50, add a separate table
141+
-- This would result in the player's score displayed at the bottom
142+
-- of the list but yet be visible without having to scroll
120143
if hlt_name and not player_in_top_50 then
121144
local hlt_player, hlt_rank, hlt_kd
122145

@@ -128,25 +151,34 @@ function ctf_stats.get_formspec(title, players, header, hlt_name)
128151
end
129152
end
130153

131-
hlt_kd = hlt_player.kills
132-
if hlt_player.deaths > 1 then
133-
hlt_kd = hlt_kd / hlt_player.deaths
154+
if hlt_player then
155+
hlt_kd = hlt_player.kills
156+
if hlt_player.deaths > 1 then
157+
hlt_kd = hlt_kd / hlt_player.deaths
158+
end
159+
160+
ret = ret .. tablecolumns
161+
ret = ret .. "tableoptions[highlight=#00000000]"
162+
ret = ret .. "table[0.5,6.1;13.25,0.4;hlt_score;"
163+
ret = ret .. "#ffff00" ..
164+
"," .. hlt_rank ..
165+
"," .. hlt_player.name ..
166+
"," .. hlt_player.kills ..
167+
"," .. hlt_player.deaths ..
168+
"," .. math.floor(hlt_kd * 10) / 10 ..
169+
"," .. hlt_player.bounty_kills ..
170+
"," .. hlt_player.captures ..
171+
"," .. hlt_player.attempts ..
172+
"," .. math.floor(hlt_player.score * 10) / 10 .. ";-1]"
134173
end
135-
ret = ret ..
136-
"," .. "#ffff00" ..
137-
"," .. hlt_rank ..
138-
"," .. hlt_player.name ..
139-
"," .. hlt_player.kills ..
140-
"," .. hlt_player.deaths ..
141-
"," .. math.floor(hlt_kd * 10) / 10 ..
142-
"," .. hlt_player.bounty_kills ..
143-
"," .. hlt_player.captures ..
144-
"," .. hlt_player.attempts ..
145-
"," .. math.floor(hlt_player.score * 10) / 10
174+
-- else
175+
-- ret = ret .. "box[0.5,6.1;13.25,0.4;#101010]"
176+
-- Adds a box where the extra table should be, in order to make it
177+
-- appear as an extension of the main table, but the color can't be
178+
-- matched, and looks slightly brighter or slightly darker than the table
146179
end
147180

148-
ret = ret .. ";-1]"
149-
ret = ret .. "button_exit[0.5,6;3,1;close;Close]"
181+
ret = ret .. "button_exit[10,6.5;3,1;close;Close]"
150182
ret = ret .. "container_end[]"
151183
return ret
152184
end

0 commit comments

Comments
 (0)