-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecryptions.py
204 lines (189 loc) · 8 KB
/
decryptions.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
from flask import Flask, redirect, render_template, request, session, jsonify
from flask_session import Session
from datetime import datetime, timedelta
from pytz import timezone
import socket, struct
import sqlite3
app = Flask(__name__)
app.config.from_pyfile('instance/config.py')
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=9999)
app.config['SESSION_REFRESH_EACH_REQUEST'] = True
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
app.permanent_session_lifetime = timedelta(days=9999)
def onvisit(fetch=True):
est = timezone('America/New_York')
dateobj = datetime.now(est)
today = dateobj.strftime("%Y-%m-%d")
session["curDate"] = str(dateobj.strftime("%B %d, %Y"))
session["slashedDate"] = str(dateobj.strftime("%m/%d"))
if not session.get("visited"):
session["visited"] = today
if not session.get("switch2"):
session["switch2"] = False
if session["visited"] != today or not session["switch2"]:
session["visited"] = today
session["switch2"] = True
session["lives"] = 4
session["finished"] = False
session["stats"] = False
session["failed"] = []
session["replaced"] = []
if fetch:
getpuzzle()
def getpuzzle():
sqliteConnection = sqlite3.connect('static/cryptograms.db')
cursor = sqliteConnection.cursor()
cursor.execute("SELECT problem, solution, published_id FROM puzzles JOIN published ON puzzles.id = published.cryptogram_id WHERE date = (?);", (str(session["visited"]) + " 00:00:00",))
data = cursor.fetchone()
sqliteConnection.close()
try:
session["solution"] = data[0]
session["cryptogram"] = data[1]
session["number"] = data[2]
except:
session["solution"] = "TWO ASTRONAUTS WENT TO A BAR ON THE MOON. THEY LEFT AFTER A FEW MINUTES BECAUSE IT HAD NO ATMOSPHERE!"
session["cryptogram"] = "GKD JWGVDUJXGW KSUG GD J PJV DU GAS ODDU. GAST CSIG JIGSV J ISK ORUXGSW PSEJXWS RG AJZ UD JGODWNASVS!"
session["number"] = 139
# session["solution"] = 'AND'
# session["cryptogram"] = 'XSN'
# session["number"] = 62
count = 0
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for letter in alpha:
if letter not in session["solution"]:
count += 1
count = 26 - count
session['count'] = count
@app.route("/debug1923409123.html")
def debug():
return render_template("debug1923409123.html", metric=[session['lives'], session['finished'], len(session["replaced"]), session['count']])
@app.route("/", methods=["GET", "POST"])
def index():
onvisit()
session.permenant = True
app.permanent_session_lifetime = timedelta(days=9999)
if not session.get("returning"):
return redirect("/welcome")
if not session.get("replaced"):
session["replaced"] = []
if len(session["replaced"]) == session['count']:
session["finished"] = True
if not session.get("finished"):
session["finished"] = False
if session["finished"]:
return redirect("/complete")
if not session.get("lives"):
session["lives"] = 4
if not session.get("failed"):
session["failed"] = []
if not session.get("history"):
session["history"] = {
"games": 0,
"solved": 0,
"streak": 0,
"maxStreak": 0,
"avgLives": 0,
"closeCalls": 0,
"lives": [0, 0, 0, 0, 0]
}
return render_template("index.html", date=session["curDate"], number=session["number"],
lives=session["lives"], cryptogramText=session["cryptogram"],
replaced=session["replaced"], failed=session["failed"])
@app.route("/welcome", methods=["GET", "POST"])
def welcome():
onvisit(fetch=False)
if request.method == "POST":
session["returning"] = request.form.get("newUser")
if session["returning"] == "no":
return redirect("/instructions")
else:
return redirect("/")
return render_template("welcome.html", date=session["curDate"], number=session["number"])
@app.route("/complete")
def complete():
onvisit(fetch=False)
if not session.get("finished"):
return redirect("/")
if not session.get("stats"):
session["stats"] = False
if not session["stats"]:
session["stats"] = True
if session["finished"]:
if session["lives"] < 0:
session["lives"] = 0
session["history"]["games"] += 1
session["history"]["lives"][session["lives"]] += 1
total = session["history"]["lives"][1] + 2 * session["history"]["lives"][2] \
+ 3 * session["history"]["lives"][3] + 4 * session["history"]["lives"][4]
session["history"]["avgLives"] = total / (session["history"]["games"])
if session["lives"] == 0:
session["history"]["streak"] = 0
else:
session["history"]["solved"] += 1
session["history"]["streak"] += 1
if session["history"]["streak"] > session["history"]["maxStreak"]:
session["history"]["maxStreak"] = session["history"]["streak"]
if session["lives"] == 1:
session["history"]["closeCalls"] += 1
if session["lives"] == 0:
win = False
else:
win = True
return render_template("complete.html", date=session["curDate"], number=session["number"], solvedCryptogram=session["solution"],
dateDashed=session["slashedDate"], attempts=4 - session["lives"], state=session["lives"])
@app.route("/stats")
def stats():
onvisit(fetch=False)
if not session.get("history"):
session["history"] = {
"games": 0,
"solved": 0,
"streak": 0,
"maxStreak": 0,
"avgLives": 0,
"closeCalls": 0,
"lives": [0, 0, 0, 0, 0]
}
return render_template("stats.html", date=session["curDate"], number=session["number"], data=session["history"])
@app.route("/instructions")
def instructions():
onvisit(fetch=False)
return render_template("instructions.html", date=session["curDate"], number=session["number"])
@app.route("/give")
def give():
onvisit(fetch=False)
return render_template("give.html", date=session["curDate"], number=session["number"])
@app.errorhandler(404)
def page_not_found(e):
onvisit(fetch=False)
return render_template("404.html", date=session["curDate"], number=session["number"]), 404
@app.route("/api", methods=["POST"])
def api():
est = timezone('America/New_York')
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
data = request.json
if session["cryptogram"].find(data["old"]) == session["solution"].find(data["new"]):
session["replaced"].append(data["old"] + data["new"])
if len(session["replaced"]) == session['count']:
session["finished"] = True
# sqliteConnection = sqlite3.connect('static/data.db')
# cursor = sqliteConnection.cursor()
# cursor.execute('INSERT INTO solved (IP, Date, Lives, Solved) VALUES (?, ?, ?, ?);', (ip, datetime.now(est), session["lives"], 1))
# sqliteConnection.commit()
# sqliteConnection.close()
return jsonify({'message': 'correct', 'lives': session["lives"], 'complete': session["finished"]}), 200
else:
session["lives"] -= 1
session["failed"].append(data["old"] + data["new"])
if (int(session["lives"]) == 0):
session["finished"] = True
# sqliteConnection = sqlite3.connect('static/data.db')
# cursor = sqliteConnection.cursor()
# cursor.execute('INSERT INTO solved (IP, Date, Lives, Solved) VALUES (?, ?, ?, ?);', (ip, datetime.now(est), session["lives"], 0))
# sqliteConnection.commit()
# sqliteConnection.close()
return jsonify({'message': 'wrong', 'lives': session["lives"], 'complete': session["finished"]}), 200
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0')