Step 3: Stats and Cheaters چالش 6 از جلسهی 20، سخت
(سختتر) قدم ۳: دو مسیر اضافه کن.
- GET /players/<name>: با یک db.one و COUNT(*) AS plays و MAX(points) AS best (و WHERE player = ?) جواب بده {"player": ..., "plays": ..., "best": ...}. اگر plays صفر بود، 404 با {"error": "no scores for Parisa"} (با همان اسم). (COUNT همیشه یک ردیف میدهد، حتی برای کسی که امتیازی ندارد، پس plays را نگاه کن، نه None.)
- DELETE /scores/<int:score_id>: پاک کن؛ اگر .changed صفر بود 404 با {"error": "no score with id 7"}، وگرنه 204.
امتیاز ۷ (Sara، Snake، ۱۵۰) تقلبی بود. این درخواستها را بعد از همهی درخواستهای قبلی اضافه کن:
```
ask("GET /players/Sara")
ask("DELETE /scores/7")
ask("GET /players/Sara")
ask("GET /top/Snake")
ask("DELETE /scores/7")
ask("GET /players/Parisa")`
خروجی قدمهای ۱ و ۲ همان میماند و بعد از آن این میآید:``
GET /players/Sara → 200 OK
{"player": "Sara", "plays": 3, "best": 150}
DELETE /scores/7 → 204 No Content
GET /players/Sara → 200 OK
{"player": "Sara", "plays": 2, "best": 145}
GET /top/Snake → 200 OK
[
{"player": "Sara", "best": 145},
{"player": "Kian", "best": 140},
{"player": "Mina", "best": 140}
]
DELETE /scores/7 → 404 Not Found
{"error": "no score with id 7"}
GET /players/Parisa → 404 Not Found
{"error": "no scores for Parisa"}
from server import app, ask, db, request, abort
db.script('''
CREATE TABLE scores (id INTEGER PRIMARY KEY, player TEXT, game TEXT, points INTEGER);
INSERT INTO scores (player, game, points) VALUES ('Sara', 'Snake', 145), ('Ali', 'Snake', 95), ('Mina', 'Snake', 140);
INSERT INTO scores (player, game, points) VALUES ('Sara', 'Pong', 30), ('Reza', 'Pong', 160), ('Kian', 'Snake', 140);
INSERT INTO scores (player, game, points) VALUES ('Sara', 'Snake', 150), ('Omid', 'Pong', 38);
''')
GAMES = ["Snake", "Pong"]
@app.post("/scores")
def add_score():
data = request.json or {}
player = data.get("player")
game = data.get("game")
points = data.get("points")
problems = []
if not isinstance(player, str) or player == "":
problems.append("player is required")
if game not in GAMES:
problems.append("game must be Snake or Pong")
if not isinstance(points, int) or points < 0 or points > 1000:
problems.append("points must be a whole number from 0 to 1000")
if problems:
return {"errors": problems}, 400
done = db.run("INSERT INTO scores (player, game, points) VALUES (?, ?, ?)", (player, game, points))
return db.one("SELECT * FROM scores WHERE id = ?", (done.id,)), 201
@app.get("/top/<game>")
def top_players(game):
if game not in GAMES:
abort(404, "no game called " + game)
return db.all("SELECT player, MAX(points) AS best FROM scores WHERE game = ? GROUP BY player ORDER BY best DESC, player LIMIT 3", (game,))
ask("POST /scores", {"player": "Leila", "game": "Pong", "points": 45})
ask("POST /scores", {"player": "", "game": "Chess", "points": 5000})
ask("POST /scores", {"player": "Nima", "game": "Snake", "points": "lots"})
ask("GET /top/Snake")
ask("GET /top/Pong")
ask("GET /top/Chess")
راهنما و جواب این چالش · برگشت به درس جلسهی 20: پروژه: سرور جدول امتیاز
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال