-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
145 lines (135 loc) · 5.58 KB
/
app.py
File metadata and controls
145 lines (135 loc) · 5.58 KB
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
from flask import Flask, request
from sqlalchemy import create_engine
from sqlalchemy import text as prepare
from twilio import twiml
from twilio.rest import Client
import jsonpickle
import random
from apscheduler.schedulers.background import BackgroundScheduler
app = Flask(__name__)
sql_alchemy_engine = create_engine('mysql://sms_game:sms_pw@localhost:3306/sms_game', pool_recycle=3600, echo=False)
account_sid=""
account_token=""
our_number=""
client=Client(account_sid,account_token)
messages = {}
with open('questions.json','r') as file:
messages = jsonpickle.decode(file.read())
@app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
@app.route("/sms",methods=['POST'])
def sms():
conn = sql_alchemy_engine.connect()
number = request.form['From']
message = request.form['Body']
sql = """INSERT INTO messages ('phone','message')
VALUES (:phone, :message)"""
conn.execute(prepare(sql),{'phone':number,'message':message})
sql = """SELECT * FROM conclave2020.registration"""
sql = """SELECT * FROM game WHERE phone = :phone"""
result = conn.execute(prepare(sql),{'phone':number})
found = False
for row in result:
found = True
current_round = row['current_round']
answer = message.lower()
answer = re.sub('[^a-z0-9]', '', answer)
correct = False
for a in messages[int(current_round)]['answers']:
if a in answer:
correct = True
if correct:
sql = """UPDATE `game` SET `q{}` = 1
WHERE phone=:phone""".format(current_round)
conn.execute(prepare(sql),{'phone':number})
current_answers = [0]*8
current_answers[int(current_round)] = 1
for answer in current_answers:
if row['q'+str(int(answer)+1)] == 1:
answer = 1
unanswered_questions = []
for a in range(0,8):
if current_answers[a]==0:
unanswered_questions.append(a)
if len(unanswered_questions)==0:
sql = """UPDATE `game` SET `current_round` = 0,
`current_hint` = 0
WHERE phone=:phone"""
conn.execute(prepare(sql),{'phone':number})
sql = """SELECT COUNT(1) AS c FROM game
WHERE q1=1 AND q2=1
AND q3=1 AND q4=1
AND q5=1 AND q6=1
AND q7=1 AND q8=1
"""
completed = conn.execute(prepare(sql)).fetchone()['c']
sql = """UPDATE `game` SET `rank` = :completed
WHERE phone=:phone"""
conn.execute(prepare(sql),{'completed':completed,'phone':number})
if int(c)==0:
message = "Congratulations! You are the first to complete the challenge! Your code is xxxxx"
else:
message = "Congratulations on completing the challenge. Unfortunately, you were number {}.".format(c)
client.messages.create(
body=message,
from_=our_number,
to=number
)
else:
new_qustion = random.choice(unanswered_questions)
sql = """UPDATE `game` SET `current_round` = :current_round,
`current_hint` = 0
WHERE phone=:phone"""
conn.execute(prepare(sql),{'phone':number,'current_round':new_qustion})
client.messages.create(
body=messages[new_question-1][0],
from_=our_number,
to=number
)
else:
client.messages.create(
body='wrong answer, try again',
from_=our_number,
to=number
)
if not found:
message = "Welcome to the C-4 Conclave Game Competition! There are 8 questions, one for each Lodge. You will receive your first question next. Each question will have five clues. Each clue will be sent to you after 5 minutes. Once you correcrly guess the answer, you will receive your next question. The first one to compete it will win an Amazon gift card."
client.messages.create(
body=message,
from_=our_number,
to=number
)
n = random.randint(1,8)
sql = """INSERT INTO `game`(`phone`, `current_round`, `current_hint`) VALUES
(:phone,:current_round,0)"""
conn.execute(prepare(sql),{'phone':number,'current_round':n})
client.messages.create(
body=messages[n-1][0],
from_=our_number,
to=number
)
conn.close()
return ''
if __name__ == "__main__":
app.run(host='0.0.0.0')
def scheduled_task():
conn = sql_alchemy_engine.connect()
sql = """SELECT * from game WHERE ts<NOW()-INTERVAL 5 MINUTE
AND current_round!=0 """
result = conn.execute(prepare(sql))
for row in result:
nexthint = int(row['current_hint'])
if len(messages[row['current_round']-1])>nexthint:
sql = """UPDATE game SET current_hint = :nexthint
WHERE phone=:phone"""
conn.execute(prepare(sql),{'phone':row['phone'],'nexthint':nexthint})
client.messages.create(
body=messages[row['current_round']-1][nexthint],
from_=our_number,
to=row['phone']
)
conn.close()
scheduler = BackgroundScheduler(timezone='utc')
scheduler.add_job(func=scheduled_task, trigger="interval", second=15)
scheduler.start()