-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
134 lines (106 loc) · 3.87 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from flask import Flask, render_template, redirect, request, session
import json
from datetime import datetime
app = Flask(__name__)
start_date = datetime(2024, 11, 5)
current_day = (datetime.now() - start_date).days + 1
max_guesses = 6 # Number of guesses allowed per day
base_url = 'http://namesoundaliker.it3.iktim.no'
#base_url = 'http://localhost:5000'
app.secret_key = 'super secret key that no one will ever guess haha'
import os
def get_day_data(day):
try:
day = int(day) # Ensure day is an integer
if day < 1:
return None
file_path = os.path.join('static', 'days', str(day), 'data.json')
with open(file_path) as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return None
except Exception as e:
print(e)
return None
@app.route('/')
def home():
return redirect(f'{base_url}/{current_day}')
@app.route('/<int:day>')
def day(day):
current_day = (datetime.now() - start_date).days + 1
if day > current_day:
return redirect(f'/{current_day}')
day_data = get_day_data(day)
if day_data is None:
return redirect(f'{base_url}/') # Redirect if data is missing or corrupted
if 'attempts' not in session:
session['attempts'] = {}
# Ensure that day attempts are initialized as a list
if not isinstance(session['attempts'].get(str(day)), list):
session['attempts'][str(day)] = [0, False]
attempts = session['attempts'][str(day)][0]
correct = session['attempts'][str(day)][1]
session.modified = True
hints = [
('1 - Type', f"1 - {day_data['hint1']}"),
('2 - Known for', f"2 - {day_data['hint2']}"),
('3 - Popularity', f"3 - {day_data['hint3']}"),
('4 - Origin/Source', f"4 - {day_data['hint4']}"),
('5 - Reveal Image', f"5 - {day_data['hint5']}"),
]
images = [
f"days/{day}/images/{day_data['img1']}",
f"days/{day}/images/{day_data['img2']}",
f"days/{day}/images/{day_data['img3']}",
]
return render_template(
'index.html',
day=day,
soundalike=day_data['soundalike'],
hints=hints,
guess=attempts,
correct=correct,
max_guesses=max_guesses,
current_day=current_day,
images=images,
)
@app.route('/<int:day>/guess', methods=['POST'])
def guess(day):
if type(day) != int:
return redirect(f'{base_url}/')
day_data = get_day_data(day)
if day_data is None:
return redirect(f'{base_url}/{day}')
correct_answers = day_data['answers']
user_guess = request.form.get('guess', '').lower()
if 'attempts' not in session:
session['attempts'] = {}
if not isinstance(session['attempts'].get(str(day)), list):
session['attempts'][str(day)] = [0, False]
if session['attempts'][str(day)][0] >= max_guesses or session['attempts'][str(day)][1]:
return redirect(f'{base_url}/{day}')
if user_guess in correct_answers:
session['attempts'][str(day)] = [session['attempts'][str(day)][0], True]
session.modified = True
return redirect(f'{base_url}/{day}')
else:
session['attempts'][str(day)][0] += 1
session.modified = True
return redirect(f'{base_url}/{day}')
@app.route('/<int:day>/skip', methods=['POST'])
def skip(day):
if 'attempts' not in session:
session['attempts'] = {}
if not isinstance(session['attempts'].get(str(day)), list):
session['attempts'][str(day)] = [0, False]
session['attempts'][str(day)][0] += 1
session.modified = True
return redirect(f'{base_url}/{day}')
@app.route('/<int:day>/next', methods=['POST'])
def next_day(day):
return redirect(f'{base_url}/{day + 1}')
@app.route('/<int:day>/prev', methods=['POST'])
def prev_day(day):
return redirect(f'{base_url}/{day - 1}')
if __name__ == '__main__':
app.run()