from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, current_app from .models import Game from . import db from .igdb_helpers import IGDBHelper import os main_bp = Blueprint('main', __name__) igdb = IGDBHelper() @main_bp.route('/') def index(): """Home page showing currently tracked games""" games = Game.query.all() return render_template('index.html', games=games) @main_bp.route('/search', methods=['GET', 'POST']) def search_games(): """Search for games using IGDB API""" results = [] query = '' if request.method == 'POST': query = request.form.get('query', '') if query: try: results = igdb.search_games(query) except Exception as e: flash(f"Oopsie! Couldn't search IGDB: {str(e)}", 'error') return render_template('search.html', results=results, query=query) @main_bp.route('/add_game/', methods=['GET', 'POST']) def add_game(igdb_id): """Add a game to tracking list""" # Check if we already have this game existing_game = Game.query.filter_by(igdb_id=igdb_id).first() if existing_game: flash(f"You'we alweady twacking {existing_game.name}!", 'info') return redirect(url_for('main.index')) # Check if we're at the 5 game limit if Game.query.count() >= 5: flash("You can onwy twack up to 5 games at once! Pwease wemove a game fiwst~", 'error') return redirect(url_for('main.index')) # Get game details from IGDB try: game_data = igdb.get_game_by_id(igdb_id) # Create new Game object new_game = Game( igdb_id=game_data['id'], name=game_data['name'], cover_url=game_data['cover_url'], description=game_data['description'], release_date=game_data['release_date'], platform=', '.join(game_data['platforms']) if game_data['platforms'] else None, developer=game_data['developer'], publisher=game_data['publisher'], status="Playing", progress=0 ) db.session.add(new_game) db.session.commit() flash(f"Added {new_game.name} to youw twacking wist! ^w^", 'success') return redirect(url_for('main.index')) except Exception as e: flash(f"Faiwed to add game: {str(e)}", 'error') return redirect(url_for('main.search_games')) @main_bp.route('/update_game/', methods=['POST']) def update_game(game_id): """Update game progress and status""" game = Game.query.get_or_404(game_id) if request.method == 'POST': # Update status if provided if 'status' in request.form: game.status = request.form['status'] # Update progress if provided if 'progress' in request.form: try: progress = int(request.form['progress']) if 0 <= progress <= 100: game.progress = progress except ValueError: flash("Pwogwess must be a numbew between 0 and 100!", 'error') # Update notes if provided if 'notes' in request.form: game.notes = request.form['notes'] db.session.commit() flash(f"Updated {game.name}! (。・ω・。)", 'success') return redirect(url_for('main.index')) @main_bp.route('/remove_game/', methods=['POST']) def remove_game(game_id): """Remove a game from tracking list""" game = Game.query.get_or_404(game_id) name = game.name db.session.delete(game) db.session.commit() flash(f"Removed {name} from youw twacking wist! OwO", 'success') return redirect(url_for('main.index'))