diff --git a/install/database_schema.sql b/install/database_schema.sql index ef8a9fd..f022b57 100644 --- a/install/database_schema.sql +++ b/install/database_schema.sql @@ -269,6 +269,11 @@ CREATE TABLE variant_ratings ( change REAL NOT NULL, value_after REAL NOT NULL, + /** + * Since multiple variants change rating for a single game, we mark the primary variant that changed here. + * This enables us to know what rows to display on statistic pages + */ + primary_change BOOLEAN NOT NULL, PRIMARY KEY (league_id, variant_id, num_players) ); diff --git a/src/ratings.py b/src/ratings.py index 49e6c42..28397c1 100644 --- a/src/ratings.py +++ b/src/ratings.py @@ -197,9 +197,9 @@ def process_rating_of_next_game() -> bool: user_changes, variant_change = compute_rating_changes(user_ratings, games_played, variant_rating, score == 5 * num_suits) # Update database for the played variant - cur.execute("INSERT INTO variant_ratings (league_id, variant_id, num_players, change, value_after) " - "VALUES (%s, %s, %s, %s, %s)", - (league_id, variant_id, num_players, variant_change, variant_rating + variant_change) + cur.execute("INSERT INTO variant_ratings (league_id, variant_id, num_players, change, value_after, primary_change) " + "VALUES (%s, %s, %s, %s, %s, %s)", + (league_id, variant_id, num_players, variant_change, variant_rating + variant_change, True) ) # Note: We do not commit here, only after players have been processed as well @@ -232,12 +232,12 @@ def process_rating_of_next_game() -> bool: other_variant_change = -variant_change / len(variants_of_same_rating_type) other_variant_vals = [] for (other_variant_id, other_num_players, current_rating) in variants_of_same_rating_type: - other_variant_vals.append((league_id, other_variant_id, other_num_players, other_variant_change, current_rating + other_variant_change)) + other_variant_vals.append((league_id, other_variant_id, other_num_players, other_variant_change, current_rating + other_variant_change, False)) # This updates the variants now psycopg2.extras.execute_values( cur, - "INSERT INTO variant_ratings (league_id, variant_id, num_players, change, value_after) " + "INSERT INTO variant_ratings (league_id, variant_id, num_players, change, value_after, primary_change) " "VALUES %s", other_variant_vals ) diff --git a/src/render_site.py b/src/render_site.py index 6385830..481dd36 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -182,12 +182,13 @@ def get_games(): "LEFT OUTER JOIN variants" " ON variants.id = game_data.variant_id " "LEFT OUTER JOIN variant_ratings" - " ON variant_ratings.league_id = game_data.league_id " + " ON variant_ratings.league_id = game_data.league_id AND variant_ratings.primary_change = true " "GROUP BY (" " game_data.game_id, game_data.num_players, users, user_ids, game_data.variant_id, variants.name, seed, score, num_turns," " game_data.league_id, num_bottom_deck_risks, num_crits_lost, change, value_after" " ) " - "ORDER BY league_id DESC" + "ORDER BY league_id DESC", + (True,) ) res = [] for row in cur.fetchall():