82 lines
3.5 KiB
Lua
82 lines
3.5 KiB
Lua
function lobby.update_stats(map_id, mode, win, player_count)
|
|
local stats = lobby.savedata.stats[map_id] or {}
|
|
stats.solo_play = stats.solo_play or 0
|
|
stats.multi_play = stats.multi_play or 0
|
|
stats.winner_traitor = stats.winner_traitor or 0
|
|
stats.winner_team = stats.winner_team or 0
|
|
stats.player_count = stats.player_count or 0
|
|
stats.max_players = stats.max_players or 0
|
|
if mode == 'solo' then --Solo sessions played.
|
|
stats.solo_play = stats.solo_play + 1
|
|
elseif mode == 'player' then --Team sessions played.
|
|
stats.multi_play = stats.multi_play + 1
|
|
end
|
|
if win == 'traitor' then
|
|
stats.winner_traitor = stats.winner_traitor + 1
|
|
elseif win == 'team' then
|
|
stats.winner_team = stats.winner_team + 1
|
|
end
|
|
if player_count then --takes an integer as input
|
|
local count = stats.multi_play
|
|
local avg = stats.player_count
|
|
local total = count * avg
|
|
local player_count_avg = (total + player_count) / (count + 1)
|
|
stats.player_count = player_count_avg
|
|
if player_count > stats.max_players then
|
|
stats.max_players = player_count
|
|
end
|
|
end
|
|
lobby.savedata.stats[map_id] = stats
|
|
end
|
|
|
|
function lobby.retrieve_stats(map_id)
|
|
local formspec = ''
|
|
if lobby.savedata.stats[map_id] then
|
|
local data = lobby.savedata.data[map_id]
|
|
local map_name = data['map_name'] or map_id
|
|
local stats = lobby.savedata.stats[map_id]
|
|
local solo = stats.solo_play
|
|
local player = stats.multi_play
|
|
local traitor = stats.winner_traitor
|
|
local team = stats.winner_team
|
|
local avg_players = stats.player_count
|
|
--local max = stats.max_players
|
|
formspec =
|
|
'formspec_version[3]'..
|
|
'size[16,9]'..
|
|
'label[.5,.5;Viewing stats for '..map_name..', ('..map_id..')]'..
|
|
'textarea[.5,1;7.5,5;;; This level has been played '..player..' times.'..
|
|
' On average '..avg_players..' players join a session.\n'..
|
|
--The most ever recorded was '..max..'!
|
|
' The traitor has won '..traitor..' games, whereas '..team..' have been team wins.\n'..
|
|
' This level has been visited by single players '..solo..' times.]'..
|
|
'textarea[8.5,.5;7,5;;; What does this all mean and why should you care?\n'..
|
|
' If you find that your level\'s wins strongly favor one party you might want to tweak the number of tasks,'..
|
|
' the granted XP, or the required XP. People are more likely to play your level when the chances of winning are fairly even.'..
|
|
' You may find that the wins don\'t add up to the times played, that is due to crashes. :()]'..
|
|
'label[.5,5.5;This space reserved for eventual charts and tables.]'
|
|
else
|
|
formspec =
|
|
'formspec_version[3]'..
|
|
'size[16,9]'..
|
|
'label[.5,2;No data on this level yet! Get some friends and play a round.]'..
|
|
'label[.5,5.5;This space reserved for eventual charts and tables.]'
|
|
end
|
|
return formspec
|
|
end
|
|
|
|
minetest.register_chatcommand('stats', {
|
|
description = 'View stats on a level, valid map name required.',
|
|
func = function(name, map_name)
|
|
if lobby.savedata.name_2_id[map_name] then
|
|
local map_id = lobby.savedata.name_2_id[map_name]
|
|
if lobby.savedata.IDs[map_id] then
|
|
local data = lobby.savedata.data[map_id]
|
|
if data.owner_name == name or minetest.check_player_privs(name, {server = true}) then
|
|
minetest.show_formspec(name, 'lobby:stats', lobby.retrieve_stats(map_id))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
})
|