-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
185 lines (145 loc) · 5.84 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
from flask import Flask, render_template, url_for, redirect, session, flash
from flask import request
from SudokuModel import SudokuModel
import passwords
from utils import string_to_grid
app = Flask(__name__)
app.secret_key = "super secret key"
# stores the model generated
data = {}
# difficulty mapping
difficulties = {1: "easy", 2: "medium", 3: "hard"}
# supported themes
index_themes = {"grey": 'index.html', "donda": 'donda_index.html', "dianna": 'dianna_index.html'}
play_themes = {"grey": 'playGame.html', "donda": 'donda_playGame.html', "dianna": 'dianna_playGame.html'}
protected_themes = {"dianna": True}
# the home page
@app.route("/")
@app.route("/<chosen_theme>")
def index(chosen_theme=None):
if 'theme' not in session:
session['theme'] = 'grey'
if chosen_theme is not None and chosen_theme in index_themes and 'theme' in session:
session['theme'] = chosen_theme
return render_template(index_themes.get(session['theme'], 'index.html'))
# creates a random board according to difficulty submitted by gen form
@app.route("/makeBoard", methods=['POST', 'GET'])
def make_board():
if request.method == 'POST':
diff = request.form.get('difficulty')
data['model'] = SudokuModel(difficulty=diff)
return redirect(url_for('play_game'))
elif request.method == 'GET':
data['model'] = SudokuModel()
return redirect(url_for('play_game'))
return redirect(url_for('index'))
@app.route("/togglingIncorrect")
def toggle_incorrect():
if "toggled" not in session:
session["toggled"] = True
else:
session["toggled"] = not session["toggled"]
return redirect(url_for('play_game'))
# resets board to original
@app.route("/resetBoard")
def reset():
model = data.get('model', None)
if model is None:
return redirect(url_for('index'))
else:
model.reset()
return redirect(url_for('play_game'))
# resets board to solved state
@app.route("/solveBoard")
def solve():
model = data.get('model', None)
if model is None:
return redirect(url_for('index'))
else:
model.solve()
return redirect(url_for('play_game'))
@app.route("/uploadBoard", methods=['POST'])
def upload_board():
if request.method == 'POST':
input_board = string_to_grid(request.form.get('input_board'))
if len(input_board) == 0:
flash("Invalid board input, try again")
return redirect(url_for('index'))
else:
try:
data['model'] = SudokuModel(input_board)
return redirect(url_for('play_game'))
except TypeError:
flash("Invalid board input, try again")
return redirect(url_for('index'))
#
# main page for game play
@app.route("/play_game<row><col>", methods=['GET', 'POST'])
@app.route("/play_game")
def play_game(row=-1, col=-1):
model = data.get('model', None)
theme = session['theme']
if model is None or (protected_themes.get(theme, False) and session.get("login", "") != "yes"):
return redirect(url_for('index'))
# initial call from index page
if row == -1 or col == -1:
return render_template(play_themes.get(theme, "playGame.html"), board=model.get_board(),
original=model.original,
flag=False,
model=model, incorrects=model.get_incorrect())
else:
row_num = int(row)
col_num = int(col)
# for when selecting a cell
if request.method == 'GET':
return render_template(play_themes.get(theme, "playGame.html"), flag=True, row=row_num,
col=col_num,
board=model.get_board(),
original=model.original, testPage=True, incorrects=model.get_incorrect())
# for when filling a cell
elif request.method == 'POST':
new_val = int(request.form.get('val'))
model.fill(row_num, col_num, new_val)
return redirect(url_for('play_game'))
@app.route("/ajax_playGame", methods=['POST', 'GET'])
def ajax_playGame():
model = data.get('model', None)
if model is None:
return redirect(url_for('index'))
if request.method == 'POST':
value = int(request.form.get('lol', "HOLÁ"))
row = int(request.form.get('row'))
col = int(request.form.get('col'))
model.fill(row, col, value)
return render_template('board.html', flag=False, board=model.get_board(), original=model.original,
incorrects=model.get_incorrect())
loc = request.args.get('jsdata')
row = int(loc[1])
col = int(loc[4])
return render_template("board.html", row=row, col=col, flag=True, board=model.get_board(),
original=model.original,
incorrects=model.get_incorrect())
@app.route("/escape")
def escape():
model = data.get('model', None)
if model is None:
return redirect(url_for('index'))
return render_template("board.html", flag=True, board=model.get_board(),
original=model.original,
incorrects=model.get_incorrect())
@app.route("/verify<name>", methods=['GET', 'POST'])
def verify(name=""):
# require passwords file, not included on github for privacy reasons
logins = passwords.logins
password = request.form.get('input_password')
if logins.get(name) == password:
session["login"] = "yes"
data['model'] = SudokuModel()
# return render_template('dianna_playGame.html')
return redirect(url_for('play_game'))
else:
session["login"] = "no"
return redirect(url_for('index'))
# if __name__ == "__main__":
# # app.run(host="0.0.0.0", port=8080, debug=True)
# serve(app, host="0.0.0.0", port=80)