From fd44ca7128642337bbd2fed9098a9f0a75dd41d3 Mon Sep 17 00:00:00 2001 From: bl3kunja-FW Date: Tue, 11 Nov 2025 17:34:42 +0100 Subject: [PATCH] saving issue fixed --- app/storage.py | 14 ++++++------ templates/results_calculator.html | 20 +++++++++++++++-- templates/tournament.html | 12 +++++++++-- tv_app.py | 36 +++++++++++++++++++------------ 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/app/storage.py b/app/storage.py index 0755a8d..f129250 100644 --- a/app/storage.py +++ b/app/storage.py @@ -171,7 +171,7 @@ class ArchiveStorage(FileStorage): @staticmethod def archive_tournament(tournament_data, results_data): - """Archive completed tournament data""" + """Archive completed tournament data. Returns (success, filename) tuple""" try: FileStorage._ensure_directory(ARCHIVE_DIR) @@ -188,14 +188,15 @@ class ArchiveStorage(FileStorage): success = FileStorage._write_json(archive_path, archive_data) if success: print(f"Tournament archived to: {archive_path}") - return success + return (True, archive_filename) + return (False, None) except Exception as e: print(f"Error archiving tournament: {e}") - return False + return (False, None) @staticmethod def archive_league(league_data): - """Archive completed league data""" + """Archive completed league data. Returns (success, filename) tuple""" try: FileStorage._ensure_directory(LEAGUE_ARCHIVE_DIR) @@ -211,10 +212,11 @@ class ArchiveStorage(FileStorage): success = FileStorage._write_json(archive_path, archive_data) if success: print(f"League archived to: {archive_path}") - return success + return (True, archive_filename) + return (False, None) except Exception as e: print(f"Error archiving league: {e}") - return False + return (False, None) @staticmethod def get_archived_tournaments(): diff --git a/templates/results_calculator.html b/templates/results_calculator.html index 147ad79..e69c238 100644 --- a/templates/results_calculator.html +++ b/templates/results_calculator.html @@ -1305,10 +1305,26 @@ if (response.ok) { const responseData = await response.json(); - // Always show results first when tournament finishes + // Determine where to redirect based on tournament type and archive + let redirectUrl = '/results'; + + // Priority 1: If league finished, redirect to league archive + if (responseData.league_archived && responseData.league_archive_filename) { + redirectUrl = `/archive/league/${responseData.league_archive_filename}`; + } + // Priority 2: If standalone tournament, redirect to tournament archive + else if (responseData.archived && responseData.archive_filename) { + redirectUrl = `/archive/tournament/${responseData.archive_filename}`; + } + // Priority 3: If league tournament (not final), show league standings + else if (responseData.league && !responseData.league_finished) { + redirectUrl = '/results'; // This will show league standings with current tournament + } + + // Always show results after tournament finishes alert('Tournament finished successfully! Showing results...'); setTimeout(() => { - window.location.href = '/results'; + window.location.href = redirectUrl; }, 1000); } else { const error = await response.json(); diff --git a/templates/tournament.html b/templates/tournament.html index c386dba..bb37e76 100644 --- a/templates/tournament.html +++ b/templates/tournament.html @@ -1505,7 +1505,7 @@

πŸ† Liga (Aktivna)

- Liga je Aktivna - {{ league_state.completed_tournaments|length }}/{{ league_state.total_tournaments }} Zaključeno + Liga je Aktivna
@@ -1533,7 +1533,7 @@
- πŸ“Š Turnirji - {{ league_state.completed_tournaments|length }}/{{ league_state.total_tournaments }} + πŸ“Š Turnirji
{% for i in range(1, league_state.total_tournaments + 1) %} @@ -2650,6 +2650,14 @@ console.log('πŸ† Tournament Management loaded'); console.log('League active:', leagueActive); console.log('Tournament active:', tournamentActive); + + {% if league_state %} + console.log('πŸ“Š League State Debug:'); + console.log(' - Completed tournaments:', {{ league_state.completed_tournaments|length }}); + console.log(' - Total tournaments:', {{ league_state.total_tournaments }}); + console.log(' - Current tournament:', {{ league_state.current_tournament }}); + console.log(' - Completed list:', {{ league_state.completed_tournaments|tojson }}); + {% endif %} }, { once: true }); }); diff --git a/tv_app.py b/tv_app.py index e67ba95..b71edeb 100644 --- a/tv_app.py +++ b/tv_app.py @@ -1286,13 +1286,13 @@ def reset_league(): # Archive current league if it exists league_state = load_league_state() if league_state: - archive_league(league_state) - + _, _ = archive_league(league_state) # Unpack tuple but ignore values + # Remove league, tournament, and results files for file_path in [LEAGUE_FILE, TOURNAMENT_FILE, RESULTS_FILE]: if os.path.exists(file_path): os.remove(file_path) - + return jsonify({'status': 'success'}) except Exception as e: return jsonify({'status': 'error', 'message': str(e)}), 400 @@ -1526,20 +1526,22 @@ def finish_tournament(): # Archive the tournament (only if it's NOT part of a league) archive_success = False + archive_filename = None if not league_state: # Only archive standalone tournaments - archive_success = archive_tournament(tournament_state, results) + archive_success, archive_filename = archive_tournament(tournament_state, results) tournament_type = results.get('tournament_type', '20_targets') print(f"Standalone {tournament_type} tournament archived: {archive_success}") else: tournament_type = results.get('tournament_type', '20_targets') print(f"League {tournament_type} tournament - not archiving individual tournament") - + # Archive the league if it just finished league_archive_success = False + league_archive_filename = None if league_finished and league_state: - league_archive_success = archive_league(league_state) + league_archive_success, league_archive_filename = archive_league(league_state) print(f"League archived: {league_archive_success}") - + # Save final results if save_results(results): # End the tournament by removing tournament state @@ -1550,15 +1552,21 @@ def finish_tournament(): if league_finished and os.path.exists(LEAGUE_FILE): os.remove(LEAGUE_FILE) - # Delete results file after archiving - if os.path.exists(RESULTS_FILE): - os.remove(RESULTS_FILE) + # Delete results file only if tournament was archived + # For non-final league tournaments, keep results file so /results route can show both league standings and tournament results + if archive_success or league_archive_success: + # Tournament or league was archived, safe to delete results + if os.path.exists(RESULTS_FILE): + os.remove(RESULTS_FILE) + # If not archived (non-final league tournament), keep results file for display response_data = { 'status': 'success', 'results': results, 'archived': archive_success, + 'archive_filename': archive_filename, 'league_archived': league_archive_success if league_finished else None, + 'league_archive_filename': league_archive_filename if league_finished else None, 'tournament_type': results.get('tournament_type', '20_targets'), 'tournament_format': get_tournament_format_description(results.get('tournament_type', '20_targets')) } @@ -1683,12 +1691,12 @@ def emergency_reset(): tournament_state = load_tournament_state() league_state = load_league_state() results = load_results() - + if tournament_state and results: - archive_tournament(tournament_state, results) - + _, _ = archive_tournament(tournament_state, results) # Unpack tuple but ignore values + if league_state: - archive_league(league_state) + _, _ = archive_league(league_state) # Unpack tuple but ignore values # Remove all state files for file_path in [LEAGUE_FILE, TOURNAMENT_FILE, RESULTS_FILE]: