Stats: Compute played and won games

This commit is contained in:
Maximilian Keßler 2023-11-23 22:05:46 +01:00
parent 16105d1965
commit 729dbb9085
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -80,33 +80,59 @@ def update_user_statistics():
# Update total number of moves # Update total number of moves
for clue_starved in [True, False]: for clue_starved in [True, False]:
rating_type = utils.get_rating_type(clue_starved)
# We insert 0 here to ensure that we have an entry for each player # We insert 0 here to ensure that we have an entry for each player
# Note that this will immediately be changed by the next query in case it is nonzero, # Note that this will immediately be changed by the next query in case it is nonzero,
# so the zero value never shows up in the database if it was nonzero before. # so the zero value never shows up in the database if it was nonzero before.
cur.execute( cur.execute(
"INSERT INTO user_statistics (user_id, variant_type, total_game_moves)" "INSERT INTO user_statistics (user_id, variant_type, total_game_moves, games_played, games_won)"
" (" " ("
" SELECT id, %s, %s FROM users" " SELECT id, %s, 0, 0, 0 FROM users"
" )" " )"
"ON CONFLICT (user_id, variant_type) DO UPDATE " "ON CONFLICT (user_id, variant_type) DO UPDATE "
"SET total_game_moves = EXCLUDED.total_game_moves", "SET"
(utils.get_rating_type(clue_starved), 0) " (total_game_moves, games_played, games_won)"
" ="
" (EXCLUDED.total_game_moves, EXCLUDED.games_played, EXCLUDED.games_won)",
(rating_type,)
) )
cur.execute( cur.execute(
"INSERT INTO user_statistics (user_id, variant_type, total_game_moves)" "INSERT INTO user_statistics (user_id, variant_type, total_game_moves)"
" (" " ("
" SELECT users.id, %s, SUM(games.num_turns) FROM users " " SELECT users.id, %s, SUM(games.num_turns) FROM users "
" LEFT OUTER JOIN game_participants " " INNER JOIN game_participants "
" ON game_participants.user_id = users.id " " ON game_participants.user_id = users.id "
" LEFT OUTER JOIN games " " INNER JOIN games "
" ON game_participants.game_id = games.id " " ON game_participants.game_id = games.id "
" LEFT OUTER JOIN variants" " INNER JOIN variants"
" ON variants.id = games.variant_id " " ON variants.id = games.variant_id "
" WHERE variants.clue_starved = %s OR variants.clue_starved IS NULL" " WHERE variants.clue_starved = %s OR variants.clue_starved IS NULL"
" GROUP BY users.id " " GROUP BY users.id "
" ) " " ) "
"ON CONFLICT (user_id, variant_type) DO UPDATE " "ON CONFLICT (user_id, variant_type) DO UPDATE "
"SET total_game_moves = EXCLUDED.total_game_moves", "SET total_game_moves = EXCLUDED.total_game_moves",
(utils.get_rating_type(clue_starved), clue_starved) (rating_type, clue_starved)
)
cur.execute(
"INSERT INTO user_statistics (user_id, variant_type, games_played, games_won)"
" ("
" SELECT"
" users.id,"
" %s,"
" COUNT(games.id),"
" COUNT(games.id) FILTER ( WHERE variants.num_suits * 5 = games.score )"
" FROM users "
" INNER JOIN game_participants "
" ON game_participants.user_id = users.id "
" INNER JOIN games "
" ON game_participants.game_id = games.id "
" INNER JOIN variants "
" ON variants.id = games.variant_id "
" WHERE variants.clue_starved = %s OR variants.clue_starved IS NULL "
" GROUP BY users.id"
" )"
"ON CONFLICT (user_id, variant_type) DO UPDATE "
"SET (games_played, games_won) = (EXCLUDED.games_played, EXCLUDED.games_won)",
(rating_type, clue_starved)
) )
conn_manager.get_connection().commit() conn_manager.get_connection().commit()