-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
223 lines (189 loc) · 7.01 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from functools import wraps
from data import Songs
import random
import time
app = Flask(__name__)
app.config['SECRET_KEY'] = 'addyoursecretkeyhere' #this method for adding session secret key in pythonanywhere
songs_data = Songs()
def is_registered(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'registered' in session:
return f(*args, **kwargs)
else:
return redirect(url_for('register'))
return wrap
@app.route('/')
def index():
return render_template('home.html')
@app.route('/about/')
def about():
return render_template('about.html')
@app.route('/highscore/')
def highscore():
return render_template('highscore.html')
##################Core game logic and random number generation
@app.route('/random_security/')
@is_registered
def random_security():
if True:
#session['rand_num'] = random.randint(0, len(songs_data)-1)
session['rand_num'] = random.choice(session['unvisited'])
session['unvisited'].remove(session['rand_num'])
if len(session['unvisited']) < 1:
session['gamecomplete'] = True
return redirect(url_for('gamecomplete'))
return redirect(url_for('game'))
return render_template('security.html')
class SongForm(Form):
name = StringField('Identify the song<br>(NOTE: select from the drop-down as you type)', [validators.DataRequired()])
@app.route('/game/', methods = ['GET', 'POST'])
@is_registered
def game():
#rand_num = random.randint(0, 4)
form = SongForm(request.form)
if request.method == 'POST' and form.validate():
session['name_candidate'] = form.name.data
return redirect(url_for('score'))
return render_template('game.html', i = session['rand_num'], songs = songs_data, form = form, score = session['score'])
################# giving clues
@app.route('/gameclue', methods = ['GET', 'POST'])
@is_registered
def clue():
session['clue'] = True
form = SongForm(request.form)
if request.method == 'POST' and form.validate():
session['name_candidate'] = form.name.data
return redirect(url_for('score'))
return render_template('gameclue.html', i = session['rand_num'], songs = songs_data, form = form, score = session['score'])
################# music scoring engine
#this function is useed as udayip spell checker, not at all efficient
def LongestCommonSubsequence(X, Y):
# find the length of the strings
m = len(X)
n = len(Y)
# declaring the array for storing the dp values
L = [[None]*(n+1) for i in xrange(m+1)]
"""Following steps build L[m+1][n+1] in bottom up fashion
Note: L[i][j] contains length of LCS of X[0..i-1]
and Y[0..j-1]"""
for i in range(m+1):
for j in range(n+1):
if i == 0 or j == 0 :
L[i][j] = 0
elif X[i-1] == Y[j-1]:
L[i][j] = L[i-1][j-1]+1
else:
L[i][j] = max(L[i-1][j] , L[i][j-1])
# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1]
return L[m][n]
@app.route('/muscorengine')
@is_registered
def score():
song_answer = songs_data[session['rand_num']]
closeness = LongestCommonSubsequence(session['name_candidate'].lower(), song_answer['name'].lower())
closenesspercent = (closeness*100.0)/len(song_answer['name'])
print "\n\n\n\n\n\n\n\n closeness percent -->", closenesspercent
if closenesspercent >= 50.0:
#if session['name_candidate'].lower() == song_answer['name'].lower():
if session['clue'] == True:
session['clue'] = False
flash('You don\'t get any points since you used clues', 'danger')
return redirect(url_for('random_security'))
else:
session['score'] = session['score'] + 1
return redirect(url_for('random_security'))
else:
return redirect(url_for('gameover'))
return ('Checking answer...')
################# registration and authorisation
class RegisterForm(Form):
name = StringField('Your name', [validators.Length(min = 3, max = 50), validators.DataRequired()])
@app.route('/register/', methods = ['GET', 'POST'])
def register():
session.clear()
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
session['registered'] = True
session['score'] = 0
session['clue'] = False
session['gamecomplete'] = False
session['unvisited'] = []
for i in range(0, len(songs_data)):
print "/n/n/n/n/n",i
session['unvisited'].append(i)
session['username'] = name #succesfully entered the name, now you can play
flash("Let's start, {}".format(session['username']), 'success')
return redirect(url_for('random_security'))
return render_template('register.html', form = form)
@app.route('/thaan_oru_potanaa')
@is_registered
def redhand():
return('Njan oru pottan annenu karithiyo ?')
def palindrome(n):
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
return 1
else:
return 0
################# quiting the game or Getting game-over-ed
@app.route('/gameover')
@is_registered
def gameover():
final_score = session['score']
final_rand_num = session['rand_num']
song_name = songs_data[session['rand_num']]['name']
film_name = songs_data[session['rand_num']]['film']
release_year = songs_data[session['rand_num']]['year']
yturl = songs_data[session['rand_num']]['yturl']
flag = 0
flag = palindrome(final_score)
if flag == 1:
category = 'palindrome'
reaction = 1
if (final_score)*(100)/(len(songs_data)) <= 10 and flag == 1:
category = 2
reaction = random.randint(1, 13)
if (final_score)*(100)/(len(songs_data)) > 10 and final_score <= 20 and flag == 0:
category = 4
reaction = random.randint(1, 9)
if (final_score)*(100)/(len(songs_data)) > 20 and final_score <= 30 and flag == 0:
category = 8
reaction = random.randint(1, 14)
if (final_score)*(100)/(len(songs_data)) > 30 and final_score <= 40 and flag == 0:
category = 16
reaction = random.randint(1, 10)
if (final_score)*(100)/(len(songs_data)) > 40 and final_score <= 50 and flag == 0:
category = 32
reaction = random.randint(1, 5)
if (final_score)*(100)/(len(songs_data)) > 50 and final_score <= 60 and flag == 0:
category = 64
reaction = random.randint(1, 8)
if (final_score)*(100)/(len(songs_data)) > 60 and final_score < 70 and flag == 0:
category = 128
reaction = random.randint(1, 6)
if (final_score)*(100)/(len(songs_data)) >= 70 and flag == 0:
category = 256
reaction = random.randint(1, 7)
session.clear()
return render_template('gameover.html', song_name = song_name, film_name = film_name, release_year = release_year, yturl = yturl, final_score = final_score, category = category, reaction = reaction)
################# When the user is lit af and completes the effing game. Shit
@app.route('/gamecomplete')
@is_registered
def gamecomplete():
if session['gamecomplete'] == True:
session.clear()
return render_template('gamecomplete.html')
else:
return redirect(url_for('redhand'))
if __name__ == "__main__":
#app.secret_key = "addyoursecretkeyhere" #how to add secret key in python anywhere
app.run(debug = True)