Update app.py#43
Open
tuananh1807 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
from flask import Flask, render_template, request, redirect, url_for, jsonify
import sqlite3
app = Flask(name)
Kết nối SQLite
def get_db_connection():
conn = sqlite3.connect('db.sqlite3')
conn.row_factory = sqlite3.Row
return conn
Tạo bảng người chơi nếu chưa có
def init_db():
conn = get_db_connection()
conn.execute('''
CREATE TABLE IF NOT EXISTS players (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
score INTEGER NOT NULL DEFAULT 0
)
''')
conn.commit()
conn.close()
init_db()
Trang chính
@app.route('/')
def index():
return render_template('index.html')
Chế độ đấu 2 người
@app.route('/player-vs-player')
def player_vs_player():
return render_template('player_vs_player.html')
Chế độ đấu với máy
@app.route('/player-vs-ai', methods=['GET', 'POST'])
def player_vs_ai():
if request.method == 'POST':
name = request.form.get('name')
if not name:
return redirect(url_for('player_vs_ai'))
Cập nhật điểm người chơi sau khi thắng
@app.route('/update_score', methods=['POST'])
def update_score():
name = request.json.get('name')
conn = get_db_connection()
conn.execute('UPDATE players SET score = score + 1 WHERE name = ?', (name,))
conn.commit()
conn.close()
return jsonify(success=True)
Bảng xếp hạng
@app.route('/leaderboard')
def leaderboard():
conn = get_db_connection()
players = conn.execute('SELECT * FROM players ORDER BY score DESC LIMIT 10').fetchall()
conn.close()
return render_template('leaderboard.html', players=players)
if name == 'main':
app.run(debug=True)