def ranking(): db = PostgresDB(db_connection_str) ranking = db.get_ranking() for player_ranking in ranking: player_ranking['avatar_path'] = get_avatar_path(player_ranking['name']) db.commit() return json.dumps(ranking, ensure_ascii=False)
def run(self): [db_connection_str, player_id, game_id] = self.validate_arguments() print('Running {}...'.format(self.command_name())) db = PostgresDB(db_connection_str) db.delete_player_from_game(player_id, game_id) recompute_ranking_command = RecomputeRankingCommand([db_connection_str]) recompute_ranking_command.run()
def game(): db = PostgresDB(db_connection_str) game_id = int(request.args.get('game_id')) game_res = db.get_game(game_id) for player_data in game_res['players_data']: player_data['avatar_path'] = get_avatar_path(player_data['name']) db.commit() return json.dumps(game_res, ensure_ascii=False)
def create_game(): db = PostgresDB(db_connection_str) game_id = None try: data_json = request.get_json(force=True) if not data_json.get('player_stats', []): return Response(status=400) game_ts = data_json.get('game_ts', None) players_stats = data_json['player_stats'] game_id = db.create_game(game_ts) stats = list(map(lambda player_stats: PlayerStats.from_json(db, player_stats), players_stats)) ranker = GameRankingComputer(db, game_id, stats) updates = ranker.get_score_ranking_updates() for player_stats, score, ranking_delta in updates: player_stats.save(game_id) db.insert_player_game_ranking(player_stats.id, game_id, score, ranking_delta) except Exception as e: db.rollback() raise e db.commit() return { 'game_id': game_id, }
def games(): db = PostgresDB(db_connection_str) page_size, page = int(request.args.get('page_size', 5)), int(request.args.get('page', 0)) num_pages = ceil(db.get_num_games()/page_size) games_res = db.get_games(page_size, page) for players_data in games_res.values(): for player in players_data: player['avatar_path'] = get_avatar_path(player['name']) db.commit() return json.dumps({'num_pages': num_pages, 'games': games_res}, ensure_ascii=False)
def run(self): [db_connection_str] = self.validate_arguments() print('Running {}...'.format(self.command_name())) db = PostgresDB(db_connection_str) db.truncate_player_game_ranking() players_stats_by_game_id = db.get_players_stats() # Get games with lower id first for game_id in sorted(players_stats_by_game_id): players_stats_raw = players_stats_by_game_id[game_id] players_stats = list(map( lambda player_stats: PlayerStats(db, player_stats['name'], player_stats['rounds_played'], player_stats['kills'], player_stats['damage'], player_stats['self_damage'], player_stats['player_id']), players_stats_raw)) ranker = GameRankingComputer(db, game_id, players_stats) updates = ranker.get_score_ranking_updates() for player_stats, score, ranking_delta in updates: db.insert_player_game_ranking(player_stats.id, game_id, score, ranking_delta) db.commit()
def player_ranking_history(): db = PostgresDB(db_connection_str) player_name = request.args['player_name'] db.commit() return json.dumps(db.get_player_ranking_history(player_name),ensure_ascii=False)
def run(self): [db_connection_str, game_id] = self.validate_arguments() print('Running {}...'.format(self.command_name())) db = PostgresDB(db_connection_str) db.delete_game(game_id)