diff --git a/bots/tracker.py b/bots/tracker.py index 14bb0a0..e652493 100644 --- a/bots/tracker.py +++ b/bots/tracker.py @@ -825,7 +825,10 @@ async def rescan_existing_tournaments(context): continue # tournament deleted or unreachable new_fids = set(new_data['players'].keys()) - # Correct player_sno for subscriptions whose SNo drifted from the cache + # Correct player_sno for subscriptions whose SNo drifted from the cache. + # Also reset last_results_count so the bot re-learns points for the + # correct player (stale counts from the old wrong player could be higher + # than the real player's count, suppressing result notifications forever). tnr_url_frag = f'tnr{tnr}.aspx' for fid in new_fids: if fid not in fide_to_users: @@ -833,7 +836,7 @@ async def rescan_existing_tournaments(context): correct_sno = new_data['players'][fid]['sno'] conn = _get_conn() conn.execute( - 'UPDATE subscriptions SET player_sno = ? ' + 'UPDATE subscriptions SET player_sno = ?, last_results_count = 0 ' 'WHERE fide_id = ? AND tournament_url LIKE ? ' 'AND player_sno != ? AND active = 1', (correct_sno, fid, f'%{tnr_url_frag}%', correct_sno)) @@ -922,24 +925,29 @@ async def check_all_subscriptions(context): pass update_result(sub['id'], results_count, points) - # Round fully completed → calculate next round pairings + # Round fully completed → calculate next round pairings (if not the last round) current_rd = tournament.get('current_round', 0) - if current_rd > sub['last_round_done'] and current_rd < tournament.get('num_rounds', 0): - try: - result = calculate_next_round(tournament) - fmt = format_pairings(result, tournament.get('name', ''), sub['lang'], - sub['player_name'], sub['player_sno']) - chunks = _render_chunks(fmt) - for chunk in chunks: - try: - await context.bot.send_message( - sub['user_id'], chunk, - parse_mode=ParseMode.MARKDOWN_V2, - disable_web_page_preview=True) - except Exception: - break - except Exception: - pass + if current_rd > sub['last_round_done']: + if current_rd < tournament.get('num_rounds', 0): + try: + result = calculate_next_round(tournament) + fmt = format_pairings(result, tournament.get('name', ''), sub['lang'], + sub['player_name'], sub['player_sno']) + chunks = _render_chunks(fmt) + for chunk in chunks: + try: + await context.bot.send_message( + sub['user_id'], chunk, + parse_mode=ParseMode.MARKDOWN_V2, + disable_web_page_preview=True) + except Exception: + break + except Exception: + pass + # Always advance last_round_done — even for the final round. + # Without this, the quick-check condition (quick_rd > last_round_done) + # stays True forever on finished tournaments, causing a full fetch + # (~22 HTTP requests) every 5 minutes. update_round(sub['id'], current_rd)