|
| 1 | +from app.utils.elo_rating import calculate_new_rating |
| 2 | +from app.utils.firebase import get_firebase |
| 3 | + |
| 4 | + |
| 5 | +def check_player_statistics(game_slug): |
| 6 | + db = get_firebase().database() |
| 7 | + |
| 8 | + print('Grabbing players from Firebase') |
| 9 | + players = db.child('players').get().val() |
| 10 | + print('Grabbing player statistics from Firebase') |
| 11 | + player_statistics = db.child('games').child(game_slug).child('player_statistics').get().val() |
| 12 | + print('Grabbing game sessions from Firebase') |
| 13 | + game_sessions = db.child('games').child(game_slug).child('sessions').get().val() |
| 14 | + |
| 15 | + expected_player_statistics = {} |
| 16 | + for slack_user_id, player in players.items(): |
| 17 | + expected_player_statistics[slack_user_id] = { |
| 18 | + 'total_games': 0, |
| 19 | + 'games_won': 0, |
| 20 | + 'games_lost': 0, |
| 21 | + 'elo_rating': 1200 |
| 22 | + } |
| 23 | + |
| 24 | + player_keys = ['loser', 'winner'] |
| 25 | + |
| 26 | + for session_id, game_session in game_sessions.items(): |
| 27 | + print(f'Checking expected ratings for session: {session_id}') |
| 28 | + loser_slack_user_id = game_session['loser']['slack_user_id'] |
| 29 | + loser_session_player = game_session['loser'] |
| 30 | + |
| 31 | + winner_slack_user_id = game_session['winner']['slack_user_id'] |
| 32 | + winner_session_player = game_session['winner'] |
| 33 | + |
| 34 | + if loser_slack_user_id in expected_player_statistics: |
| 35 | + opponent_rating = expected_player_statistics[winner_slack_user_id]['elo_rating'] |
| 36 | + |
| 37 | + expected_loser_elo_rating = calculate_new_rating( |
| 38 | + player_rating=expected_player_statistics[loser_slack_user_id]['elo_rating'], |
| 39 | + opponent_rating=opponent_rating, |
| 40 | + player_won=False |
| 41 | + ) |
| 42 | + |
| 43 | + if expected_loser_elo_rating != loser_session_player['elo_rating']['after']: |
| 44 | + print(f'Expected loser rating {expected_loser_elo_rating} but was ' |
| 45 | + f'{loser_session_player["elo_rating"]["after"]}') |
| 46 | + exit(1) |
| 47 | + |
| 48 | + if winner_slack_user_id in expected_player_statistics: |
| 49 | + opponent_rating = expected_player_statistics[loser_slack_user_id]['elo_rating'] |
| 50 | + |
| 51 | + expected_winner_elo_rating = calculate_new_rating( |
| 52 | + player_rating=expected_player_statistics[winner_slack_user_id]['elo_rating'], |
| 53 | + opponent_rating=opponent_rating, |
| 54 | + player_won=True |
| 55 | + ) |
| 56 | + |
| 57 | + if expected_winner_elo_rating != winner_session_player['elo_rating']['after']: |
| 58 | + print(f'Expected winner rating {expected_winner_elo_rating} but was ' |
| 59 | + f'{winner_session_player["elo_rating"]["after"]}') |
| 60 | + exit(1) |
| 61 | + |
| 62 | + for player_key in player_keys: |
| 63 | + is_winner = player_key == 'winner' |
| 64 | + slack_user_id = game_session[player_key]['slack_user_id'] |
| 65 | + session_player = game_session[player_key] |
| 66 | + # Update the local actual player statistics |
| 67 | + expected_player_statistics[slack_user_id]['total_games'] += 1 |
| 68 | + expected_player_statistics[slack_user_id]['elo_rating'] = session_player['elo_rating']['after'] |
| 69 | + if is_winner: |
| 70 | + expected_player_statistics[slack_user_id]['games_won'] += 1 |
| 71 | + else: |
| 72 | + expected_player_statistics[slack_user_id]['games_lost'] += 1 |
| 73 | + |
| 74 | + for slack_user_id, expected_player_statistic in expected_player_statistics.items(): |
| 75 | + if expected_player_statistic['elo_rating'] != player_statistics[slack_user_id]['elo_rating']: |
| 76 | + print(f'elo_rating for slack_user_id {slack_user_id} does not equal the expected value.\n' |
| 77 | + f'Expected: {expected_player_statistic["elo_rating"]}\n' |
| 78 | + f'Actual: {player_statistics[slack_user_id]["elo_rating"]}\n') |
| 79 | + if expected_player_statistic['total_games'] != player_statistics[slack_user_id]['total_games']: |
| 80 | + print(f'total_games for slack_user_id {slack_user_id} does not equal the expected value.\n' |
| 81 | + f'Expected: {expected_player_statistic["total_games"]}\n' |
| 82 | + f'Actual: {player_statistics[slack_user_id]["total_games"]}\n') |
| 83 | + if expected_player_statistic['games_won'] != player_statistics[slack_user_id]['games_won']: |
| 84 | + print(f'games_won for slack_user_id {slack_user_id} does not equal the expected value.\n' |
| 85 | + f'Expected: {expected_player_statistic["games_won"]}\n' |
| 86 | + f'Actual: {player_statistics[slack_user_id]["games_won"]}\n') |
| 87 | + if expected_player_statistic['games_lost'] != player_statistics[slack_user_id]['games_lost']: |
| 88 | + print(f'games_lost for slack_user_id {slack_user_id} does not equal the expected value.\n' |
| 89 | + f'Expected: {expected_player_statistic["games_lost"]}\n' |
| 90 | + f'Actual: {player_statistics[slack_user_id]["games_lost"]}\n') |
| 91 | + |
| 92 | + """ |
| 93 | + for card_uid, card in cards.items(): |
| 94 | + if 'sessions' in card: |
| 95 | + #print(card) |
| 96 | + print(f'Removing sessions from card: {card_uid}') |
| 97 | + db.child('cards').child(card_uid).child('sessions').remove() |
| 98 | + """ |
| 99 | + |
| 100 | + """ |
| 101 | + for session_id, game_session in game_sessions.items(): |
| 102 | + if 'elo_rating' not in game_session['loser'] or 'elo_rating' not in game_session['winner']: |
| 103 | + for player_key in player_keys: |
| 104 | + if 'slack_user_id' in game_session[player_key]: |
| 105 | + slack_user_id = game_session[player_key]['slack_user_id'] |
| 106 | + elif 'card_uid' not in game_session[player_key]: |
| 107 | + print(f'Could not find card_uid in session #{session_id} for "{player_key}"') |
| 108 | + sys.exit(1) |
| 109 | + else: |
| 110 | + card_uid = game_session[player_key]['card_uid'] |
| 111 | + card_info = cards[card_uid] |
| 112 | + slack_user_id = card_info['slack_user_id'] |
| 113 | + player = players[slack_user_id] |
| 114 | + session_player = game_session[player_key] |
| 115 | +
|
| 116 | + print(f'Updating session #{session_id} for player key "{player_key}" ({slack_user_id})') |
| 117 | + db.child('games').child(game_slug).child('sessions').child(session_id).child(player_key).set({ |
| 118 | + 'slack_user_id': slack_user_id, |
| 119 | + 'elo_rating': { |
| 120 | + 'before': session_player['rating_before'], |
| 121 | + 'after': session_player['rating_after'], |
| 122 | + 'delta': session_player['rating_delta'] |
| 123 | + } |
| 124 | + }) |
| 125 | +
|
| 126 | + print(f'Adding session #{session_id} to player ({slack_user_id})') |
| 127 | + db.child('players').child(slack_user_id).child('sessions').child(game_slug).child(session_id).set({ |
| 128 | + 'card_uid': card_uid, |
| 129 | + 'new_elo_rating': session_player['rating_after'], |
| 130 | + 'elo_rating_delta': session_player['rating_delta'], |
| 131 | + 'winner': player_key == 'winner' |
| 132 | + }) |
| 133 | +
|
| 134 | + # print(f'Removing session #{session_id} from card_uid ({card_uid})') |
| 135 | + # db.child('cards').child(card_uid).child('sessions').remove() |
| 136 | + #return |
| 137 | + """ |
| 138 | + return |
| 139 | + |
0 commit comments