]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/batch/badges/playerdata.py
Allowing elos to be shown even for less than 32 games
[xonotic/xonstat.git] / xonstat / batch / badges / playerdata.py
1 import sqlalchemy as sa
2 import sqlalchemy.sql.functions as func
3 from xonstat.models import *
4 from xonstat.views.player import get_games_played, get_overall_stats, get_ranks, get_elos
5
6
7 class PlayerData:
8
9     # player data, will be filled by get_data()
10     data = {}
11
12     def __init__(self):
13         self.data = {}
14
15     def __getattr__(self, key):
16         if self.data.has_key(key):
17             return self.data[key]
18         return None
19
20     @classmethod
21     def get_data(self, player_id):
22         """Return player data as dict.
23
24         This function is similar to the function in player.py but more optimized
25         for this purpose.
26         """
27         # total games
28         # wins/losses
29         # kills/deaths
30         
31         # duel/dm/tdm/ctf elo + rank
32         player = DBSession.query(Player).filter_by(player_id=player_id).\
33                 filter(Player.active_ind == True).one()
34         games_played    = get_games_played(player_id)
35         overall_stats   = get_overall_stats(player_id)
36         ranks           = get_ranks(player_id)
37         elos            = get_elos(player_id)
38
39         games_played_dict = {}
40         for game in games_played:
41             games_played_dict[game.game_type_cd] = game
42
43         ranks_dict = {}
44         for gt,rank in ranks.items():
45             ranks_dict[gt] = (rank.rank, rank.max_rank)
46
47         elos_dict = {}
48         for gt,elo in elos.items():
49             if elo.games > 0:
50                 elos_dict[gt] = elo.elo
51
52         self.data = {
53                 'player':player,
54                 'games_played':games_played_dict,
55                 'overall_stats':overall_stats,
56                 'ranks':ranks_dict,
57                 'elos':elos_dict,
58             }
59          
60