implemented new model for database, new routes for save. Fixed logout not allowing post requests
This commit is contained in:
@@ -20,6 +20,7 @@ def create_app():
|
||||
login_manager.login_view = "login.login"
|
||||
|
||||
from .models.user import User
|
||||
from .models.gamestate import GameState
|
||||
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,11 @@
|
||||
from .. import db
|
||||
from flask_login import UserMixin
|
||||
|
||||
class GameState(UserMixin, db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_id = db.Column(db.Integer, nullable=False)
|
||||
|
||||
board = db.Column(db.Text, nullable=False) # JSON String
|
||||
mode = db.Column(db.String(10), nullable=False) # queens / rooks
|
||||
|
||||
created_at = db.Column(db.DateTime, default=db.func.now())
|
||||
Binary file not shown.
Binary file not shown.
@@ -46,7 +46,7 @@ def login():
|
||||
|
||||
return render_template("login.html")
|
||||
|
||||
@login_bp.route('/logout')
|
||||
@login_bp.route('/logout', methods=['POST'])
|
||||
@login_required
|
||||
def logout():
|
||||
logout_user()
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from flask import Blueprint, jsonify, render_template, request
|
||||
from flask_login import login_required, current_user
|
||||
import random
|
||||
import random, json
|
||||
from ..models.gamestate import GameState
|
||||
from ..models.user import User
|
||||
from .. import db
|
||||
|
||||
main_bp = Blueprint("main", __name__)
|
||||
|
||||
@@ -73,3 +76,33 @@ def solve_rooks(n=8):
|
||||
random.shuffle(solution)
|
||||
return solution
|
||||
|
||||
@main_bp.route("/save", methods=["POST"])
|
||||
def save_game():
|
||||
data = request.json
|
||||
|
||||
board = data["board"]
|
||||
mode = data["mode"]
|
||||
|
||||
game = GameState(
|
||||
user_id=current_user.id,
|
||||
board=json.dumps(board),
|
||||
mode=mode
|
||||
)
|
||||
|
||||
db.session.add(game)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"status": "saved"})
|
||||
|
||||
@main_bp.route("/load")
|
||||
def load_game():
|
||||
game = GameState.query.filter_by(user_id=current_user.id)\
|
||||
.order_by(GameState.id.desc()).first()
|
||||
|
||||
if not game:
|
||||
return jsonify({"board": [-1]*8, "mode": "queens"})
|
||||
|
||||
return jsonify({
|
||||
"board": json.loads(game.board),
|
||||
"mode": game.mode
|
||||
})
|
||||
|
||||
@@ -197,3 +197,38 @@ function updateQueenList() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function saveGame() {
|
||||
await fetch("/save", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
board: board,
|
||||
mode: mode
|
||||
})
|
||||
});
|
||||
|
||||
alert("💾 Spiel gespeichert!");
|
||||
}
|
||||
|
||||
async function loadGame() {
|
||||
const res = await fetch("/load");
|
||||
const data = await res.json();
|
||||
|
||||
board = data.board;
|
||||
mode = data.mode;
|
||||
|
||||
// Buttons richtig setzen
|
||||
document.getElementById("btnQueens").classList.remove("active");
|
||||
document.getElementById("btnRooks").classList.remove("active");
|
||||
|
||||
if (mode === "queens") {
|
||||
document.getElementById("btnQueens").classList.add("active");
|
||||
} else {
|
||||
document.getElementById("btnRooks").classList.add("active");
|
||||
}
|
||||
|
||||
drawBoard();
|
||||
}
|
||||
|
||||
@@ -62,6 +62,9 @@
|
||||
<button class="btn" onclick="solve()">Solve</button>
|
||||
<button class="btn" onclick="checkSolution()">Check</button>
|
||||
<button class="btn secondary" onclick="resetBoard()">Reset</button>
|
||||
|
||||
<button class="btn" onclick="saveGame()">💾 Save</button>
|
||||
<button class="btn" onclick="loadGame()">📂 Load</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -5,3 +5,11 @@ CREATE TABLE users (
|
||||
email VARCHAR(120) NOT NULL UNIQUE,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE game_state (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
board TEXT NOT NULL, -- JSON String (z.B. "[0,4,7,5,2,6,1,3]")
|
||||
mode TEXT NOT NULL, -- "queens" oder "rooks"
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
Reference in New Issue
Block a user