-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
111 lines (76 loc) · 3.06 KB
/
run.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
103
104
105
106
107
108
109
110
111
import os
import json
from flask import Flask, render_template, request, redirect
from operator import itemgetter
app = Flask(__name__)
with open("data/highscores.json", "r") as json_data:
highscores = json.load(json_data)
player_score = 0
def write_to_file(filename, data):
with open(filename, "a") as file:
file.writelines(data)
def add_users(username):
write_to_file("data/users.txt", username.title() + "\n")
riddle_index = 0
riddles = ""
@app.route('/')
def index():
return render_template("index.html", title="HOME | SPACE RIDDLE")
@app.route('/leaderboard')
def leaderboard():
with open("data/highscores.json", "r") as json_data:
highscores = json.load(json_data)
return render_template("leaderboard.html", title="RANK | SPACE RIDDLE", highscores=highscores)
@app.route('/enter_name', methods=["GET", "POST"])
def enter_name():
if request.method == "POST":
username = request.form["username"]
add_users(username)
return redirect(username)
return render_template("enter_name.html", title="REGISTER | SPACE RIDDLE")
@app.route('/<username>', methods=["GET", "POST"])
def play(username):
with open("data/riddles.json", "r") as json_data:
riddles = json.load(json_data)
riddle_index = 0
player_score = 0
incorrect = ''
global user_try
user_try = 0
if request.method == "POST":
riddle_index = int(request.form["riddle_index"])
player_score = int(request.form["score"])
user_answer = request.form["answer"].lower()
user_try = int(request.form["userTry"])
print("user try: " + str(user_try))
if user_answer == riddles[riddle_index]["answer"]:
riddle_index += 1
player_score += 1
incorrect = ''
else:
print("user try == else: " + str(user_try))
print(type(user_try))
if(user_try == 1):
user_try -= 1
print("user try == other: " + str(user_try))
riddle_index += 1
elif(user_try == 0):
incorrect = user_answer
user_try += 1
if riddle_index > 5:
result = {
"name": username,
"score": player_score
}
with open("data/highscores.json", "r") as json_data:
highscores = json.load(json_data)
highscores.append(result)
highscores = sorted(
highscores, key=itemgetter('score'), reverse=True)
with open("data/highscores.json", "w") as file:
json.dump(highscores, file)
return render_template("leaderboard.html", title="GAME OVER | SPACE RIDDLE", score=player_score, highscores=highscores)
return render_template("answer_riddle.html", title="PLAY | SPACE RIDDLE", username=username, riddles=riddles, riddle_index=riddle_index, score=player_score, incorrect=incorrect, userTry=user_try)
if __name__ == '__main__':
app.run(host=os.environ.get('IP', '0.0.0.0'),
port=os.environ.get('PORT', '5000'))