-
Notifications
You must be signed in to change notification settings - Fork 0
/
thinkingFlask.py
119 lines (98 loc) · 2.54 KB
/
thinkingFlask.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
from flask import Flask, request
from twilio.rest import Client
import requests
import time
import random
import api
import math
import changetext
import operator
app = Flask(__name__)
f = open('twilioapi', 'r')
acc = f.read().split()
ACCOUNT_SID = acc[0]
AUTH_TOKEN = acc[1]
f.close()
client = Client(ACCOUNT_SID, AUTH_TOKEN)
# client.messages.create(
# to = '',
# from_ = '',
# body = 'yay?',
# media_url = 'https://raw.githubusercontent.com/pooneetthaper/ThinkingGame/master/images/0.jpg?token=AHlePwtuOuIbczP3cS06i2m1i6J55ijhks5Y80yBwA%3D%3D'
# )
maxRound = 5
currentRound = -1
record = {}
randomPhotos = []
currentTags = []
@app.route('/')
def index():
setUpGame()
app.logger.info("HI")
return render_template("index.html")
@app.route('/increment')
def increment():
endRound()
nextRound()
@app.route('/sms', methods=['POST']) # UPDATE FUNCTION
def sms():
global currentRound
global maxRound
global record
global currentWaits
global began
num = request.form['From']
inp = request.form['Body']
if(currentRound < 0):
addUser(num, inp)
else:
makeGuess(num,inp)
def setUpGame():
global randomPhotos
allPhotos = api.getAllPaths()
randomIndices = []
while(len(randomIndices) < maxRound):
index = random.randrange(len(allPhotos))
if index not in randomIndices:
randomIndices.append(index)
randomPhotos.append(allPhotos[index])
def addUser(number,username):
global record
record[number] = {}
record[number]["lastSuccessfulRound"] = 0
record[number]["score"] = 0
record[number]["username"] = username
def nextRound():
#send time request to flask server
global randomPhotos
global currentRound
global currentTags
currentRound +=1
currentTags = api.getAllTags(randomPhotos[currentRound])
# print(randomPhotos[currentRound],currentTags)
def endRound():
changetext.changeText(getTops())
def makeGuess(number,guess):
global currentRound
global record
if currentRound > record.get(number).get("lastSuccessfulRound"):
index = isGood(guess)
if (index >= 0):
record[number]["score"] += getScore(index)
record[number]["lastSuccessfulRound"] = currentRound
def getTops():
global record
newDict = {}
for user in record:
newDict[record[user]["username"]] = record[user]["score"]
sortedRanking = sorted(newDict.items(), key=operator.itemgetter(1))
return sortedRanking[:10]
def isGood(guess):
global currentTags
for i in range(5):
if guess.lower() == currentTags[i].lower(): return i
return -1
def getScore(index):
return math.floor(3-(index/2.0))
if __name__ == "__main__":
app.run(debug = True)