Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __pycache__/app.cpython-313.pyc
Binary file not shown.
30 changes: 10 additions & 20 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'supersecretkey'


# Initialize the database object only once
migrate = Migrate(app, db)
db.init_app(app)
Expand All @@ -31,13 +30,11 @@
def load_user(user_id):
return User.query.get(int(user_id))


# Home Page (Landing Page)
@app.route('/')
def home():
return render_template('index.html')


# Register Page
@app.route('/register', methods=['GET', 'POST'])
def register():
Expand All @@ -57,12 +54,11 @@ def register():
db.session.add(new_user)
db.session.commit()

flash("Registration successful! Please log in.", "success")
flash("Registration Successful!", "success")
return redirect(url_for('login'))

return render_template('register.html')


# Login Page
@app.route('/login', methods=['GET', 'POST'])
def login():
Expand All @@ -75,42 +71,40 @@ def login():
if user and user.check_password(password):
# Log the user in using Flask-Login
login_user(user) # This will manage the session automatically

flash("Login successful!", "success")
flash(f"Welcome back, {user.username}!", "success")
return redirect(url_for('dashboard')) # Redirect to the dashboard
else:
flash("Invalid credentials! Try again.", "danger")

return render_template('login.html')



@app.route('/config-check')
def config_check():
return f"Testing Mode: {app.config['TESTING']}"


@app.route('/user-check')
def user_check():
if current_user.is_authenticated:
return f"Logged in as: {current_user.username}"
else:
return "No user is currently logged in."


# Dashboard Page
@app.route('/dashboard')
@login_required
def dashboard():
print(f"[DEBUG] Logged in user: {current_user.username}")
return render_template('dashboard.html', username=current_user.username)



@app.route('/guess', methods=['POST'])
@login_required
def guess():
guess = int(request.form['guess'])
try:
guess = int(request.form['guess'])
except ValueError:
flash("Invalid guess! Please enter a valid number.", "danger")
return redirect(url_for('play_game', level=session.get('level', 'easy')))

number_to_guess = session.get('number_to_guess')

if guess == number_to_guess:
Expand All @@ -122,7 +116,6 @@ def guess():
else:
return jsonify({'result': 'Too high!'})


# Game Page (Handles Easy, Medium, Hard)
@app.route('/play/<level>', methods=['GET', 'POST'])
@login_required # This ensures the user is logged in
Expand All @@ -141,6 +134,7 @@ def play_game(level):
session['random_number'] = random.randint(1, levels[level])
session['attempts'] = 5 # Total number of attempts
session['game_status'] = None # Track the game status (win/loss)
session['level'] = level # Store the current level

# Game variables
correct_number = session['random_number']
Expand Down Expand Up @@ -194,19 +188,16 @@ def play_game(level):
correct_number=correct_number,
game_status=game_status)


# Leaderboard
@app.route('/leaderboard')
def leaderboard():

leaderboard_data = User.query.order_by(User.score.desc()).all()
leaderboard = [{"username": user.username, "score": user.score} for user in leaderboard_data]
return render_template('leaderboard.html', leaderboard=leaderboard)
#return render_template('leaderboard.html', leaderboard=leaderboard)

@app.route('/reset')
def reset_game():
session.pop('number', None)
session.pop('random_number', None)
session.pop('attempts', None)
level = session.get('level', 'easy') # fallback level
return redirect(url_for('play_game', level=level))
Expand All @@ -219,7 +210,6 @@ def logout():
flash("You have been logged out successfully.", "info")
return redirect(url_for('home'))


# Run the application
if __name__ == "__main__":
with app.app_context():
Expand Down
35 changes: 24 additions & 11 deletions login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,37 @@
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<div class="login shadow-sm rounded p-4">
<h2 class="p-3" >Login</h2>
<div class="container">
<h2 class="text-center">Login</h2>

<form method="POST">
<input type="text" class="form-control" name="username" placeholder="Username" required>
<input type="password" class="form-control" name="password" placeholder="Password" required>
<button type="submit" class="">Login</button>
</form>
<p>New here? <a href="{{ url_for('register') }}">Register</a></p>
<!-- Display Flash Messages -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="alert alert-dismissible fade show" role="alert">
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}

<form method="POST">
<label class="form-label">Username</label>
<input type="text" class="form-control" name="username" required>
<label class="form-label">Password</label>
<input type="password" class="form-control" name="password" required>
<button type="submit" class="btn btn-outline-primary">Login</button>
</form>

<p>New here? <a href="{{ url_for('register') }}">Register</a></p>
</div>

<script src="{{ url_for('static', filename='script.js') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
45 changes: 26 additions & 19 deletions register.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,39 @@
<title>Register</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<div class="register mt-4 p-4 bg-white shadow-sm rounded text-center">

<h2 class="text-secondary p-2">Register</h2>
<form method="POST">
<label class="form-label text-muted">Username</label>
<input type="text" class ="form-control border-light bg-light" name="username" required>
<div class="container">
<h2 class="text-center">Register</h2>

<label class="form-label text-muted">Password</label>
<input type="password" class ="form-control border-light bg-light" name="password" required>
<!-- Display Flash Messages -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="alert alert-dismissible fade show" role="alert">
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}

<label class="form-label text-muted">Comfirm Password</label>
<input type="password" class ="form-control border-light bg-light" name="confirm_password" required>

<button type="submit" class="btn btn-outline-secondary">Register</button>
</form>

<p class="text-muted">Already have an account? <a href="{{ url_for('login') }}">Login</a></p>
<form method="POST">
<label class="form-label">Username</label>
<input type="text" class="form-control" name="username" required>
<label class="form-label">Password</label>
<input type="password" class="form-control" name="password" required>
<label class="form-label">Confirm Password</label>
<input type="password" class="form-control" name="confirm_password" required>
<button type="submit" class="btn btn-outline-primary">Register</button>
</form>

<p>Already have an account? <a href="{{ url_for('login') }}">Login</a></p>
</div>


<script src="{{ url_for('static', filename='script.js') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
Binary file modified test/__pycache__/test_example.cpython-313-pytest-8.3.5.pyc
Binary file not shown.
Loading