Add ctf_stats.get_html() and ctf_stats.html_to_file()

This commit is contained in:
rubenwardy 2015-12-07 23:49:58 +00:00
parent 69952a969b
commit f93c6e416d

View file

@ -15,7 +15,7 @@ function ctf_stats.get_formspec_match_summary(stats)
return ret return ret
end end
function ctf_stats.get_formspec(title, players) local function calc_scores(players)
for i = 1, #players do for i = 1, #players do
local pstat = players[i] local pstat = players[i]
pstat.kills = pstat.kills or 0 pstat.kills = pstat.kills or 0
@ -28,6 +28,10 @@ function ctf_stats.get_formspec(title, players)
table.sort(players, function(one, two) table.sort(players, function(one, two)
return one.score > two.score return one.score > two.score
end) end)
end
function ctf_stats.get_formspec(title, players)
calc_scores(players)
local ret = "size[12,6.5]" local ret = "size[12,6.5]"
ret = ret .. "vertlabel[0,0;" .. title .. "]" ret = ret .. "vertlabel[0,0;" .. title .. "]"
@ -59,6 +63,60 @@ function ctf_stats.get_formspec(title, players)
return ret return ret
end end
function ctf_stats.get_html(title, players)
calc_scores(players)
local ret = "<h1>" .. title .. "</h1>"
ret = ret .. "<table>" ..
"<tr><th></th>" ..
"<th>username</th>" ..
"<th>kills</th>" ..
"<th>deaths</th>" ..
"<th>K/D ratio</th>" ..
"<th>captures</th>" ..
"<th>attempts</th>" ..
"<th>score</th></tr>"
for i = 1, #players do
local pstat = players[i]
local color = pstat.color or "#ffffff"
ret = ret ..
"<tr><td>" .. i ..
"</td><td>" .. pstat.name ..
"</td><td>" .. pstat.kills ..
"</td><td>" .. pstat.deaths ..
"</td><td>" .. math.floor(pstat.kills / (pstat.deaths + 1)*10)/10 ..
"</td><td>" .. pstat.captures ..
"</td><td>" .. pstat.attempts ..
"</td><td>" .. math.floor(pstat.score*10)/10 .. "</td></tr>"
if i > 40 then
break
end
end
ret = ret .. "</table>\n"
return ret
end
function ctf_stats.html_to_file(filepath)
local players = {}
for name, pstat in pairs(ctf_stats.players) do
pstat.name = name
pstat.color = nil
table.insert(players, pstat)
end
local html = ctf_stats.get_html("Player Rankings", players)
local f = io.open(filepath, "w")
f:write("<!doctype html>\n")
f:write("<html><head>\n")
f:write("<meta charset=\"utf-8\">\n")
f:write("<title>Player Rankings</title>\n")
f:write("<link rel=\"stylesheet\" href=\"score_style.css\">\n")
f:write("</head><body>\n")
f:write(html)
f:write("</body></html>\n")
f:close()
end
minetest.register_chatcommand("rankings", { minetest.register_chatcommand("rankings", {
func = function(name) func = function(name)