Beispiel #1
0
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,
    }
Beispiel #2
0
    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()