-
Notifications
You must be signed in to change notification settings - Fork 0
/
cooldown.py
334 lines (290 loc) · 12.5 KB
/
cooldown.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import logging
import json
import csv
import requests
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session, convert_errors
SESSION_STATE = "state"
SESSION_CHAMPION = "champion"
SESSION_ABILITY = "ability"
SESSION_RANK = "rank"
SESSION_CDR = "cdr"
app = Flask(__name__)
ask = Ask(app, "/")
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
version_url = 'http://ddragon.leagueoflegends.com/api/versions.json'
version_headers = {'Accept-Charset' : 'utf-8'}
version_response = requests.get(version_url, headers=version_headers)
version_json = json.loads(version_response.text)
version = version_json[0]
logging.debug(version)
pronunciation = {}
with open('pronunciation.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
pronunciation[row['alexa_pronunciation']] = row['champion_name']
@ask.launch
def launched():
session.attributes[SESSION_STATE] = 'launch'
welcome_text = render_template('welcome')
help_text = render_template('help')
return question(welcome_text).reprompt(help_text)
@ask.intent('SupportedChampionsIntent')
def supported_champions():
with open('LIST_OF_CHAMPIONS.txt') as championfile:
champions = ', '.join([line.rstrip('\n') for line in championfile])
list_champions_text = render_template('list_champions', champions=champions)
list_champions_reprompt_text = render_template('list_champions_reprompt')
return question(list_champions_text).reprompt(list_champions_reprompt_text)
@ask.intent('ChampionIntent', mapping = {'champion' : 'Champion'})
def get_champion(champion):
state = session.attributes.get(SESSION_STATE)
if state == 'champion' or state == 'launch':
session.attributes[SESSION_CHAMPION] = champion
ability = session.attributes.get(SESSION_ABILITY)
rank = session.attributes.get(SESSION_RANK)
cdr = session.attributes.get(SESSION_CDR)
if ability is None:
return _dialog_ability()
if rank is None:
return _dialog_rank()
if cdr is None:
return _dialog_cdr()
if all(values is not None for values in session.attributes.values()):
return _get_cooldown(champion, ability, rank, cdr)
@ask.intent('AbilityIntent', mapping = {'ability' : 'Ability'})
def get_ability(ability):
state = session.attributes.get(SESSION_STATE)
if state == 'ability':
session.attributes[SESSION_ABILITY] = ability
champion = session.attributes.get(SESSION_CHAMPION)
rank = session.attributes.get(SESSION_RANK)
cdr = session.attributes.get(SESSION_CDR)
if champion is None:
return _dialog_champion()
if rank is None:
return _dialog_rank()
if cdr is None:
return _dialog_cdr()
if all(values is not None for values in session.attributes.values()):
return _get_cooldown(champion, ability, rank, cdr)
@ask.intent('RankIntent', mapping = {'rank' : 'Rank'})
def get_rank(rank):
# SINCE THERE IS Amazon.NUMBER ALREADY MAPPED HERE, WE CAN'T GET TO CDR WITHOUT THIS CODE
state = session.attributes.get(SESSION_STATE)
if state == 'cdr':
return get_cdr(rank)
elif state == 'rank':
session.attributes[SESSION_RANK] = rank
champion = session.attributes.get(SESSION_CHAMPION)
ability = session.attributes.get(SESSION_ABILITY)
cdr = session.attributes.get(SESSION_CDR)
if champion is None:
return _dialog_champion()
if ability is None:
return _dialog_ability()
if cdr is None:
return _dialog_cdr()
if all(values is not None for values in session.attributes.values()):
return _get_cooldown(champion, ability, rank, cdr)
@ask.intent('CooldownReductionIntent', mapping = {'cdr' : 'CooldownReduction'})
def get_cdr(cdr):
state = session.attributes.get(SESSION_STATE)
if state == 'cdr':
if cdr == '?':
# IF USER GIVES BAD MAPPING ex. {furniture} percent, THEN HANDLE IT
cdr_dialog_bad_text = render_template('cdr_dialog_bad')
cdr_dialog_bad_text_reprompt = render_template('cdr_dialog_bad_reprompt')
return question(cdr_dialog_bad_text).reprompt(cdr_dialog_bad_text_reprompt)
else:
session.attributes[SESSION_CDR] = cdr
champion = session.attributes.get(SESSION_CHAMPION)
ability = session.attributes.get(SESSION_ABILITY)
rank = session.attributes.get(SESSION_RANK)
if champion is None:
return _dialog_champion()
if ability is None:
return _dialog_ability()
if rank is None:
return _dialog_rank()
if all(values is not None for values in session.attributes.values()):
return _get_cooldown(champion, ability, rank, cdr)
@ask.intent('OneshotCooldownIntent', mapping = {'champion' : 'Champion',
'ability' : 'Ability',
'rank' : 'Rank',
'cdr' : 'CooldownReduction'})
def oneshot_cooldown(champion, ability, rank, cdr):
''' User can give a one statement query asking about champion cooldown'''
# ADD CONDITIONAL STATEMENTS IF ALEXA DOES NOT HEAR ONE OR MORE MAPPING
if cdr is None:
session.attributes[SESSION_CHAMPION] = champion
session.attributes[SESSION_ABILITY] = ability
session.attributes[SESSION_RANK] = rank
return _dialog_cdr()
if rank is None:
session.attributes[SESSION_CHAMPION] = champion
session.attributes[SESSION_ABILITY] = ability
session.attributes[SESSION_CDR] = cdr
return _dialog_rank()
if ability is None:
session.attributes[SESSION_CHAMPION] = champion
session.attributes[SESSION_RANK] = rank
session.attributes[SESSION_CDR] = cdr
return _dialog_ability()
if champion is None:
session.attributes[SESSION_ABILITY] = ability
session.attributes[SESSION_RANK] = rank
session.attributes[SESSION_CDR] = cdr
return _dialog_champion()
return _get_cooldown(champion, ability, rank, cdr)
def sanitize_name(champion_name):
''' Remove extraneous punctuation before look up in dictionary '''
sanitized_name = champion_name.title()
for char in [' ', '.', '\'']:
if char in sanitized_name:
sanitized_name = sanitized_name.replace(char, '')
return sanitized_name
def _get_cooldown(champion, ability, rank, cdr):
''' Create correct binding to ability and keyboard press, and calculate cooldown'''
session.attributes[SESSION_STATE] = "calculation"
# GRAB SESSION ATTRIBUTES IF USER WENT DIALOG ROUTE
if session.attributes.get(SESSION_CHAMPION) is not None:
champion = session.attributes.get(SESSION_CHAMPION)
if session.attributes.get(SESSION_ABILITY) is not None:
ability = session.attributes.get(SESSION_ABILITY)
if session.attributes.get(SESSION_RANK) is not None:
rank = session.attributes.get(SESSION_RANK)
if session.attributes.get(SESSION_CDR) is not None:
cdr = session.attributes.get(SESSION_CDR)
logging.debug(champion)
logging.debug(ability)
logging.debug(rank)
logging.debug(cdr)
try:
sanitized_champion_name = sanitize_name(champion) # CLEAN UP NAME BEFORE LOOK UP
champion_name = pronunciation[sanitized_champion_name] # RETURN CHAMPION NAME THAT JSONDATA RECOGNIZES
except KeyError:
champion_name = sanitized_champion_name
if champion_name == 'WuKong':
champion_name = 'MonkeyKing' # convert
logging.debug("Sanitized champion name is {}".format(champion_name))
url = "http://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion/{}.json".format(version, champion_name)
headers = {'Accept-Charset' : 'utf-8'}
response = requests.get(url, headers=headers)
champion_data = json.loads(response.text)['data'][champion_name]
# USE == because we are checking equality, not if they are same object (is)
keybinding = 0
if ability.lower() == 'q':
keybinding = 0
elif ability.lower() == 'w':
keybinding = 1
elif ability.lower() == 'e':
keybinding = 2
elif ability.lower() == 'r' or ability.lower() == 'ult' or ability.lower() == 'ultimate':
keybinding = 3
rank_index = int(rank) - 1
cooldown_reduction = float(cdr) / 100
spell = champion_data['spells'][keybinding]
spell_cooldown = spell['cooldown'][rank_index]
cooldown = round(spell_cooldown * (1 - cooldown_reduction), 2)
calculation_text = render_template('calculation', champion=champion_name, rank=rank, ability=ability, cdr=cdr, cooldown=cooldown)
return question(calculation_text)
def _dialog_champion():
session.attributes[SESSION_STATE] = "champion"
champion_dialog_text = render_template('champion_dialog')
champion_dialog_reprompt_text = render_template('champion_dialog_reprompt')
return question(champion_dialog_text).reprompt(champion_dialog_reprompt_text)
def _dialog_ability():
session.attributes[SESSION_STATE] = "ability"
champion = session.attributes.get(SESSION_CHAMPION)
if champion is None:
ability_dialog_text = render_template('ability_dialog_alternative')
else:
ability_dialog_text = render_template('ability_dialog', champion=champion)
ability_dialog_reprompt_text = render_template('ability_dialog_reprompt', champion=champion)
return question(ability_dialog_text).reprompt(ability_dialog_reprompt_text)
def _dialog_rank():
session.attributes[SESSION_STATE] = "rank"
ability = session.attributes.get(SESSION_ABILITY)
if ability is None:
rank_dialog_text = render_template('rank_dialog_alternative')
else:
rank_dialog_text = render_template('rank_dialog', ability=ability)
rank_dialog_reprompt_text = render_template('rank_dialog_reprompt', ability=ability)
return question(rank_dialog_text).reprompt(rank_dialog_reprompt_text)
def _dialog_cdr():
session.attributes[SESSION_STATE] = "cdr"
champion = session.attributes.get(SESSION_CHAMPION)
if champion is None:
cdr_dialog_text = render_template('cdr_dialog_alternative')
else:
cdr_dialog_text = render_template('cdr_dialog', champion=champion)
cdr_dialog_reprompt_text = render_template('cdr_dialog_reprompt', champion=champion)
return question(cdr_dialog_text).reprompt(cdr_dialog_reprompt_text)
def _dialog_no_slot():
overall_dialog_text = render_template('overall_dialog')
return question(overall_dialog_text).reprompt(overall_dialog_text)
@ask.session_ended
def session_ended():
logging.debug("Session ended")
return "{}", 200
@ask.intent('AMAZON.HelpIntent')
def help():
help_text = render_template('help')
return question(help_text)
@ask.intent("AMAZON.StopIntent")
def stop():
bye_text = render_template('bye')
return statement(bye_text)
@ask.intent("AMAZON.PreviousIntent")
def previous():
state = session.attributes.get(SESSION_STATE)
if state == 'ability':
return _dialog_champion()
elif state == 'rank':
return _dialog_ability(session.attributes.get(SESSION_CHAMPION))
elif state == 'cdr':
return _dialog_rank(session.attributes.get(SESSION_ABILITY))
else:
invalid_previous_text = render_template('invalid_previous')
return statement(invalid_previous_text)
@ask.intent("AMAZON.RepeatIntent")
def repeat():
state = session.attributes.get(SESSION_STATE)
if state == 'champion':
return _dialog_champion()
elif state == 'ability':
return _dialog_ability(session.attributes.get(SESSION_CHAMPION))
elif state == 'rank':
return _dialog_rank(session.attributes.get(SESSION_ABILITY))
elif state == 'cdr':
return _dialog_cdr(session.attributes.get(SESSION_CHAMPION))
else:
return launched()
@ask.intent("AMAZON.YesIntent")
def yes():
state = session.attributes.get(SESSION_STATE)
if state == 'calculation':
session.attributes[SESSION_CHAMPION] = None
session.attributes[SESSION_ABILITY] = None
session.attributes[SESSION_RANK] = None
session.attributes[SESSION_CDR] = None
return _dialog_champion()
@ask.intent("AMAZON.NoIntent")
def no():
state = session.attributes.get(SESSION_STATE)
if state == 'calculation':
return stop()
@ask.intent("AMAZON.CancelIntent")
def cancel():
bye_text = render_template('bye')
return statement(bye_text)
@ask.intent("AMAZON.StartOverIntent")
def startover():
session.attributes[SESSION_CHAMPION] = None
session.attributes[SESSION_ABILITY] = None
session.attributes[SESSION_RANK] = None
session.attributes[SESSION_CDR] = None
return launched()
if __name__ == "__main__":
app.run()