-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
102 lines (72 loc) · 2.53 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from flask import (
Flask,
render_template,
request,
redirect,
session,
flash,
)
import random
import pandas as pd
from dotenv import find_dotenv, load_dotenv
import os
import logging
ENV_FILE = find_dotenv(".env")
if ENV_FILE:
load_dotenv(ENV_FILE)
app = Flask(__name__)
app.secret_key = "your_secret_key"
app.config["fish_csv"] = os.getenv("FISH_CSV")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/game")
def game():
if "score" not in session:
session["score"] = 0
if "rounds_counter" not in session:
session["rounds_counter"] = 0
fish_df = pd.read_csv(app.config["fish_csv"], index_col=0)
real_fish = fish_df[fish_df["real"] == "True"]["name"].to_list()
made_up_fish = fish_df[fish_df["real"] == "False"]["name"].to_list()
real_options = random.sample(real_fish, k=3)
finte = random.sample(made_up_fish, k=1)[0]
options = real_options + [finte]
random.shuffle(options)
correct_index = options.index(finte)
correct_option = options[correct_index]
session["correct_option"] = correct_option
return render_template("game.html", options=options, score=session["score"])
@app.route("/submit", methods=["POST"])
def submit():
session["rounds_counter"] += 1
selected_option = request.form.get("option")
if not selected_option:
flash("Bitte wähle eine Option aus.")
return redirect("/game")
if selected_option == session.get("correct_option"):
session["score"] += 1
flash(f"Korrekt! {random.choice(['🐟', '🐠', '🐡', '🦈'])}")
else:
flash(f"Leider falsch! {random.choice(['🎣', '🍣', '🍤'])}")
print(f"Selected option: {selected_option}, Score: {session['score']}")
return redirect("/game")
@app.route("/end_game", methods=["GET", "POST"])
def end_game():
try:
percentage_correct = (session["score"] / session["rounds_counter"]) * 100
except ZeroDivisionError:
percentage_correct = 0
text_to_display = f"Du hast {session['score']} von {session['rounds_counter']} Finten enttarnt! ({percentage_correct:.2f}%)"
return render_template("game_over.html", text=text_to_display)
@app.route("/reset")
def reset():
session["score"] = 0
session["rounds_counter"] = 0
return redirect("/")
@app.route("/about")
def about():
from_page = request.args.get("from_page", "game")
return render_template("about.html", from_page=from_page)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)