def get_user_id(username, location): """Gets the user id for a particular user. Args: username: URL encoded username for the summoner. location: Riot abbreviation for the region. """ try: LOGGING.push( "*'" + username + "'* from @'" + location + "'@ is requesting their user ID." ) # TODO(Save the ID lookup in the database.) session = RiotSession(API_KEY, location) response = session.get_ids([urllib.pathname2url(username)]) return response[urllib.pathname2url(username)]['id'] # TODO(Fix this to catch both 429 and 400 errors w/ Riot Exception.) except ValueError: LOGGING.push( "Tried to get *'" + username + "'* id. Response did not have user id." ) abort(404, {'message': "User ID was not found."})
def analyze_player(player_id, location): """Analyzes a player for recent game statistics. Args: player_id: The Riot player_id for the player in question. location: The abbreviated location for the particular player. """ LOGGING.push( "Player analysis for *'" + str(player_id) + "'* is being created." ) flags = {} session = RiotSession(API_KEY, location) # TODO(Maybe reduce requests by storing the updated time?) match_list = session.get_match_list(player_id) match_list = match_list[:min(15, len(match_list))] match_ids = [match['matchId'] for match in match_list] for match in match_ids: check_match = ( db.session.query(Match) .filter(Match.match_id == match) .count() ) if check_match == 0: match_data = session.get_match(match) try: store_match(match_data) except KeyError: LOGGING.push("Could not store match *'" + str(match) + "'*.") else: LOGGING.push( "*'" + str(match) + "'* already exists in the database." ) matches = ( db.session.query(Champion) .join(Match) .filter(Match.match_id.in_(match_ids)) ) player_data = ( db.session.query(PlayerData.champion_id) .filter_by(player_id=player_id, location=location) .order_by(PlayerData.won.desc()) .limit(5) .all() ) flags['best_champs'] = [champ[0] for champ in player_data] flags['best_champ'] = flags['best_champs'][0] flags['played_best_champ'] = False flags['wins'] = 0 flags['losses'] = 0 flags['durations'] = [] flags['lose_kdas'] = [] for match in matches.filter(Champion.player_id == player_id).all(): if match.won: flags['wins'] += 1 else: flags['losses'] += 1 flags['lose_kdas'].append(match.get_kda()) flags['durations'].append(match.match.match_duration / 60) if match.champion_id in flags['best_champs']: flags['played_best_champ'] = True flags['best_champ'] = match.champion_id flags['best_champ_kda'] = match.get_kda() flags['won'] = flags['wins'] > flags['losses'] best_champ = ( db.session.query(ChampionData) .filter_by(champion_id=flags['best_champ']) .first() ) if flags['best_champ'] is not None: # TODO(Kind of arbitrary number of kda. only ensures they're positive.) flags['best_champ_well'] = flags['best_champ_kda'] > 1 else: flags['best_champ_well'] = ( db.session.query(PlayerData) .filter_by(champion_id=flags['best_champ']) .first() .get_kda() > 1 ) flags['average_duration'] = ( reduce(lambda x, y: x + y, flags['durations']) / float(len(flags['durations'])) ) if flags['lose_kdas'] is not None: flags['average_lose_kda'] = ( reduce(lambda x, y: x + y, flags['lose_kdas']) / float(len(flags['lose_kdas'])) ) flags['long_games'] = flags['average_duration'] > 30 response = "" if flags['best_champs'] is not None: response += ( "You seem to play " + html_surround(best_champ.get_name()) + " a lot. " ) if flags['played_best_champ']: response += ( "That's good, because you seem " + "to be winning with that champion. " ) if flags['best_champ_well']: response += ( "Even better you end up with a great KDA at " + html_surround(round(flags['best_champ_kda'], 2), "i") + ". " ) else: response += "However you haven't been playing them recently. " if flags['best_champ_well']: response += ( "That kind of sucks because you seem " + "to do well with them with a KDA of " + html_surround(round(flags['best_champ_kda'], 2), "i") + ". " ) else: response += ( "Consider, though, that your KDA is " + "okay with them. " ) if flags['won']: response += ( "Looking at your most recent ranked matches, you " + "seem to be winning. You are going " + html_surround(str(flags['wins']) + "-" + str(flags['losses'])) + " right now. That's pretty good. " ) if flags['lose_kdas'] is not None and flags['average_lose_kda'] > 1: response += ( "Taking a look at your losses, you don't seem to be doing " + "too badly. Your average KDA in your lost games is " + html_surround(str(round(flags['average_lose_kda'], 2))) + " which isn't bad. Make sure you're capturing lots of " + "objectives in your games and working together " + "with your team. " ) if flags['long_games']: response += ( "You seem to be having " + html_surround("long games.") + " This can be a good thing and a bad thing. " + "If you are able to, make sure you close out your games " + "early. A mistake later in the game can be a lot of trouble! " ) else: response += ( "Your most recent ranked matches aren't " + "positive, sadly. You are going " + html_surround(str(flags['wins']) + "-" + str(flags['losses'])) + " right now. Consider trying out some new champions in these " + "lists that may spark for you! " ) if flags['lose_kdas'] is not None and flags['average_lose_kda'] > 1: response += ( "However, your average KDA is definitely not bad in " + "these losses. Make sure you're capturing lots of " + "objectives in your games and working " + "together with your team. " ) else: response += ( "Your average KDAs in these losing games " + "aren't too great. Make sure to play safe and always " + "ward up if you're getting ganked a lot! " ) if flags['long_games']: response += ( "You also seem to be having " + html_surround("long games. ") + "If you are ahead in lane, make sure you start " + "to close out the game if you can! If you wait too long, " + "you may be more susceptible to mistakes and " + "champions who scale really well late game, such as Nasus." ) return response
def setup(self): self.session = RiotSession(api="ASDF")
class TestRiotSession(object): def setup(self): self.session = RiotSession(api="ASDF") def teardown(self): pass def test_get_stats(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = "cats" stats = self.session.get_stats(1234) assert stats == "cats" get.assert_called_once_with( URLS['stats'].format( location=self.session.location, player=str(1234) ), params={} ) """ set_trace() """ def test_get_featured(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} featured = self.session.get_featured() assert featured == [] get.assert_called_once_with( URLS['featured'].format( location=self.session.location, ), params={} ) # TODO: What if the featured is actually not None? # Check if it actually made a request? # Then we would be testing Riot's servers instead. # Maybe check if there is actual request headers? # assert self.session.get_featured() is not None def test_get_matches(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} matches = self.session.get_matches(1234) assert matches == [] get.assert_called_once_with( URLS['matches'].format( location=self.session.location, player=str(1234) ), params={'rankedQueues': 'RANKED_SOLO_5x5', 'endIndex': 5} ) def test_get_match(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} match = self.session.get_match(1234) assert match == {} get.assert_called_once_with( URLS['match'].format( location=self.session.location, match=str(1234) ), params={} ) def test_get_match_list(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} l = self.session.get_match_list(1234) assert l == [] get.assert_called_once_with( URLS['match_list'].format( location=self.session.location, player=str(1234) ), params={'rankedQueues': 'RANKED_SOLO_5x5'} ) def test_get_ids(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} ids = self.session.get_ids([]) assert ids == {} get.assert_called_once_with( URLS['ids'].format( location=self.session.location, players='' ), params={} ) def test_get_champion(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} champion_data = self.session.get_champion(1234) assert champion_data == {} get.assert_called_once_with( URLS['champion'].format( location=self.session.location, champion=str(1234) ), params={'champData': 'all'} )
class TestRiotSession(object): def setup(self): self.session = RiotSession(api="ASDF") def teardown(self): pass def test_get_stats(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = "cats" stats = self.session.get_stats(1234) assert stats == "cats" get.assert_called_once_with(URLS['stats'].format( location=self.session.location, player=str(1234)), params={}) """ set_trace() """ def test_get_featured(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} featured = self.session.get_featured() assert featured == [] get.assert_called_once_with(URLS['featured'].format( location=self.session.location, ), params={}) # TODO: What if the featured is actually not None? # Check if it actually made a request? # Then we would be testing Riot's servers instead. # Maybe check if there is actual request headers? # assert self.session.get_featured() is not None def test_get_matches(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} matches = self.session.get_matches(1234) assert matches == [] get.assert_called_once_with(URLS['matches'].format( location=self.session.location, player=str(1234)), params={ 'rankedQueues': 'RANKED_SOLO_5x5', 'endIndex': 5 }) def test_get_match(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} match = self.session.get_match(1234) assert match == {} get.assert_called_once_with(URLS['match'].format( location=self.session.location, match=str(1234)), params={}) def test_get_match_list(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} l = self.session.get_match_list(1234) assert l == [] get.assert_called_once_with( URLS['match_list'].format(location=self.session.location, player=str(1234)), params={'rankedQueues': 'RANKED_SOLO_5x5'}) def test_get_ids(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} ids = self.session.get_ids([]) assert ids == {} get.assert_called_once_with(URLS['ids'].format( location=self.session.location, players=''), params={}) def test_get_champion(self): with mock.patch.object(self.session, "get") as get: get.return_value.json.return_value = {} champion_data = self.session.get_champion(1234) assert champion_data == {} get.assert_called_once_with(URLS['champion'].format( location=self.session.location, champion=str(1234)), params={'champData': 'all'})