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 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
   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
   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 or 0
      if avg_players > max then
         avg_players = max
      end
      local avg = string.format("%.2f", avg_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 join a session. The most ever recorded was '..max..'!\n'..
         '   The traitor has won '..traitor..' games, whereas '..team..' have been team wins.\n'..
         '   This level has been visited by single players '..solo..' time(s).]'..
         '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

function lobby.stats_overview(name, max)
   local formspec =
      'formspec_version[3]'..
      'size[14,10]'..
      'label[.5,.5;Viewing stats for your maps with auto assigned IDs.]'..
      'tablecolumns[text,width=16;text,align=right;text,align=right;text,align=right;text,align=right;text,align=right;text,align=right]'..
      'table[.5,1;13,8.5;stats;Map Name,Play Count,   Avg Players,   Traitor Wins,   Team Wins,   Max Players,   Solo Visits,'
   for i = 1, max do
      local map_id = name..'_'..i
      local data = lobby.savedata.data[map_id]
      if data then
         local map_name = data['map_name'] or '[unnamed]'
         local stats = lobby.savedata.stats[map_id]
         if stats then
            local solo = stats.solo_play or 0
            local player = stats.multi_play or 0
            local traitor = stats.winner_traitor or 0
            local team = stats.winner_team or 0
            local avg_players = stats.player_count or 0
            local max = stats.max_players or 0
            formspec = formspec ..
            map_name..','..player..','..avg_players..','..traitor..','..team..','..max..','..solo..','
         end
      end
   end
   formspec = formspec ..']'
   return formspec
end

minetest.register_chatcommand('stats', {
   description = 'View stats on a level. Provide no name to see all your stats.',
   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
      else
         local i = 1
         local map_id = name..'_'..i
         while lobby.savedata.IDs[map_id] do
            i = i + 1
            map_id = name..'_'..i
         end
         minetest.show_formspec(name, 'lobby:stats_overview', lobby.stats_overview(name, i))
      end
   end
})
