def gametier_calc(game_data, region): old_debug_all = db.Connection.debug_all db.Connection.debug_all = False player_keys = {} sum = 0 total = 10 masters = 0 challengers = 0 for each in game_data['participantIdentities']: conn = db.Connection() player_id = each['player']['summonerId'] player = conn.player_get(player_id, region) conn.close() try: last_updated = datetime.datetime.now() - player[0]['lastUpdated'] last_updated = last_updated.days except IndexError: last_updated = 999 if len(player) == 0 or last_updated >= LAST_UPDATED_THRESHOLD: found = players.getRanksFromLeague(region, player_id) if not found: total -= 1 continue else: conn = db.Connection() player = conn.player_get(player_id, region) conn.close() if len(player) == 0: total -= 1 continue player = player[0] player_keys[player['summonerID']] = player['playerKey'] tier = player['tier'] div = player['division'] if ((tier == 6) or (tier == 7)): div = 5 sum += tier sum += 0.2 * (5 - div) mean = sum / total if challengers >= 5: tier_score = 7 if masters >= 5: tier_score = 6 else: if mean > 6.25: tier_score = 7 elif mean > 6: tier_score = 6 else: tier_score = math.floor(mean) db.Connection.debug_all = old_debug_all return tier_score, player_keys
def game_data_get(gameID, region): conn = db.Connection() game_info = conn.game_info_get(gameID, region) conn.close() if len(game_info) == 0 or game_info[0]['gd.gameKey'] is None: print("Match data for " + str(gameID) + " does not exist. " "Adding to database...") url = ("https://" + region.lower() + ".api.riotgames.com/lol/match/v4/matches/" + str(gameID)) try: response = core.api_get(url).json() game_data = json.dumps(response) tier, players = gametier.gametier_calc(response, region) version = response['gameVersion'].split('.') patch_major = int(version[0]) patch_minor = int(version[1]) conn = db.Connection() if len(game_info) == 0: key = None else: key = game_info[0]['gameKey'] key = conn.game_save(gameID, tier, patch_major, patch_minor, region, game_data, key) conn.close() if (patch_major < core.PATCH_MAJOR or patch_minor < core.PATCH_MINOR): print("Game is not on current patch...") return -2, -2, -2, -2 return response, tier, key, players except AttributeError as e: print("AttributeError") print(e) return -1, -1, -1, -1 else: tier = game_info[0]['rank'] key = game_info[0]['gameKey'] print("Match data for " + str(key) + " already exists. Loading...") conn = db.Connection() if not conn.champ_stats_full(key): game_data = conn.game_data_get(key) player_keys = {} for each in game_data['participantIdentities']: player_id = each['player']['summonerId'] player = conn.player_get(player_id, region)[0] player_keys[player['summonerID']] = player['playerKey'] conn.close() return game_data, tier, key, player_keys else: print("Fully added data, no need for pull matchData.") game_data = tier = key = players = -1 conn.close() return game_data, tier, key, players
def parse_stats_worker(games_master, region): conn = db.Connection() games_incomplete = [] for game in games_master: full = conn.champ_stats_full(game['gameKey']) if not full: games_incomplete.append(game['gameID']) conn.close() game_stats_add(games_incomplete, region)
def games_collect(player, region, new_games=[], time_begin=None, time_end=None, streak=0): if time_end is None: time_epoch = datetime.datetime.utcfromtimestamp(0) time_now = datetime.datetime.now() #time_now = time_now.replace(hour=0, minute=0, second=0, microsecond=0) time_end = int((time_now - time_epoch).total_seconds() * 1000) time_begin = time_end - 604800000 account_id = player['accountID'] url = ("https://" + region.lower() + ".api.riotgames.com/lol/match/v4/matchlists/by-account/" + str(account_id) + "?queue=420&beginTime=" + str(time_begin) + "&endTime=" + str(time_end)) try: game_history = core.api_get(url).json() total_games = game_history['totalGames'] if total_games == 0: return new_games conn = db.Connection() for i in range(len(game_history['matches'])): if streak == 10: print("20 recorded in a row. Assuming done...") return new_games game = game_history['matches'][i] if (game['queue'] != 420 or game['season'] != core.CURRENT_SEASON): continue else: '''print("Checking if game " + str(game['gameId']) + " already recorded.")''' patch_major, patch_minor = conn.game_exists( str(game['gameId']), region) if patch_major is not -1: print("Data already recorded.") if ((patch_major < core.PATCH_MAJOR) or (patch_minor < core.PATCH_MINOR)): print("No longer on current patch...") return new_games streak += 1 else: '''print("Adding game " + str(game['gameId']) + " to list of new game.")''' new_games.append(game['gameId']) conn.close() new_games = games_collect(player, region, new_games, time_begin - 604800000, time_begin, streak) return new_games except KeyError: # Encountered if no games found (404) return new_games
def champ_stats_add(stats): print("Adding stats...") #print(stats) key = stats['game_key'] lane = stats['lane'] duration = stats['duration'] win = stats['win'] player_key = stats['player_key'] champ_id = stats['champ_id'] champ_stats = stats['champ_stats'] conn = db.Connection() conn.champ_stats_save(key, champ_id, player_key, lane, duration, win, champ_stats) conn.close()
def parse_stats(region): conn = db.Connection() games = conn.games_elite_get(region) conn.close() games_split = core.splitter(6, games) threads = [] for game_group in games_split: thread = threading.Thread(target=parse_stats_worker, args=(game_group, region)) thread.daemon = True threads.append(thread) thread.start() for thread in threads: thread.join()
def main(): started = core.time.time() conn = db.Connection() group = 'COMPETITIVE' arrs = conn.execute("SELECT * FROM ChampHash") arrs = core.splitter(10, arrs) threads = [] for each in arrs: t = threading.Thread(target=createCharacterCharts, args=(each, group)) t.daemon = True threads.append(t) t.start() for t in threads: t.join() print("Done.") print(core.time.time() - started)
def count(): conn = db.Connection() results = conn.execute('''SELECT COUNT(*) AS 'count' FROM Player''') results = str(results[0]['count']) print("Number of players being tracked: " + results) results = conn.execute('''SELECT COUNT(*) AS 'count' FROM Game''') results = str(results[0]['count']) print("Number of matches stored (total): " + results) results = conn.execute( '''SELECT COUNT(*) AS 'count' FROM Game WHERE patchMajor = %s AND patchMinor = %s''', (core.PATCH_MAJOR, core.PATCH_MINOR)) results = str(results[0]['count']) print("Number of matches stored (current patch): " + results) conn.close()
def update(): url = ("https://na1.api.riotgames.com/lol/static-data/v4/" + "champions?locale=en_US&dataById=false") response = core.api_get(url).json() path = str(pathlib.Path(core.os.getcwd())) + "\\Data" nameToId = {} idToName = {} conn = db.Connection() conn.execute("DELETE FROM ChampHash", ) query = "INSERT INTO ChampHash (champID, champName) VALUES (%s, %s)" for each in response['data'].keys(): data = response['data'][each] id = data['id'] name = data['name'] conn.execute(query, (id, name)) nameToId[response['data'][each]['name']] = response['data'][each]['id'] idToName[response['data'][each]['id']] = response['data'][each]['name'] core.saveFile(path, "champNameToId", "json", nameToId) core.saveFile(path, "champIdToName", "json", idToName)
def updatePlayerRank(players, target_player, target_found, queue, tier, region_base, region, league_id, verbose=False): updated = 0 added = 0 for player in players: div = db.division_from_api[player['rank']] player_id = int(player['playerOrTeamId']) if player_id == target_player: target_found = True queue.put(target_found) name = player['playerOrTeamName'] if verbose: print("Adding/Updating: " + str(player_id)) # Champion mastery stats, not currently in use, but was functional '''url = "https://" + region_base + ".api.riotgames.com/championmastery/location/" + region + "/player/" + player_id + "/champions?api_key=" + KEY masteryData = api_get(url).json() keys = ['chestGranted','championPoints','playerId','championPointsUntilNextLevel','championPointsSinceLastLevel','lastPlayTime','tokensEarned'] for i in range(len(masteryData)): for key in keys: if key in masteryData[i]: del masteryData[i][key] #masteryData = json.dumps(masteryData)''' conn = db.Connection(0) action = conn.player_save(player_id, name, tier, div, region_base) conn.close() if action == 0: added += 1 elif action == 1: updated += 1 if verbose: print("Updated: " + str(updated)) print("Added: " + str(added)) return target_found
def collect_region(region, is_multiprocessing=True): conn = db.Connection() players = conn.players_elite_get(region) conn.close() if is_multiprocessing: processes = [] players_split = core.splitter(6, players) for player_group in players_split: process = multiprocessing.Process(target=collect_region_worker, args=( region, player_group, )) processes.append(process) process.start() try: for process in processes: process.join() except KeyboardInterrupt: for process in processes: process.terminate() else: collect_region_worker(region, players)
def createCharacterCharts(arr, group): #champDict = getIDChampDict() wantedStatKeys = [ 'kills', 'deaths', 'assists', 'totalDamageDealtToChampions', 'largestMultiKill', 'largestKillingSpree', 'turretKills', 'totalTimeCrowdControlDealt', 'goldEarned', 'totalMinionsKilled', 'neutralMinionsKilledTeamJungle', 'neutralMinionsKilledEnemyJungle', 'totalHeal', 'totalDamageTaken' ] for region in ['NA1', 'KR']: for entry in arr: conn = db.Connection() champID = entry['champID'] matches = conn.champ_stats_get(core.PATCH_MAJOR, core.PATCH_MINOR, region, champID) timeTotal = {} winTotal = {} statSummary = {} winCount = 0.0 print("Matches this patch for " + entry['champName'] + " in " + region + ": " + str(len(matches))) if (len(matches)) != 0: for match in matches: matchKey = match['gameKey'] createStatSummary(statSummary, match, wantedStatKeys) winCount = createNewCharacterCharts( match, group, timeTotal, winTotal, winCount) #print(str(champID) + ": " + str(timeTotal) + ", " + str(winTotal)) numMatches = len(statSummary['kills']) for key in statSummary.keys(): total = 0.0 for datum in statSummary[key]: total += datum statSummary[key] = total / numMatches #print(key + ": " + str(statSummary[key])) totalRatio = winCount / len(winTotal[champID]) #print(str(champID) + ": " + str(len(winTotal[champID])) + " matches, " + str(winCount) + " wins. Ratio: " + str(totalRatio)) data, length = setupGraph(group, True, timeTotal, winTotal) query = '''SELECT * FROM ChampCache WHERE champ = %s AND patchMajor = %s AND patchMinor = %s AND region = %s''' conn = db.Connection() results = conn.execute( query, (champID, core.PATCH_MAJOR, core.PATCH_MINOR, region)) statSummary = core.json.dumps(statSummary) if len(results) == 0: query = '''INSERT INTO ChampCache (champ, patchMajor, patchMinor, durWRSize, durWRStats, totalWRStats, statSummary, region) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)''' conn.execute( query, (champID, core.PATCH_MAJOR, core.PATCH_MINOR, length, str(data), totalRatio, statSummary, region)) else: query = '''UPDATE ChampCache SET durWRSize=%s, durWRStats=%s, totalWRStats=%s, statSummary=%s, lastUpdated=CURRENT_TIMESTAMP WHERE champ=%s AND patchMajor = %s AND patchMinor = %s AND region = %s''' conn.execute( query, (length, str(data), totalRatio, statSummary, champID, core.PATCH_MAJOR, core.PATCH_MINOR, region)) else: query = '''SELECT COUNT(*) FROM ChampCache WHERE champ = %s AND patchMajor = %s AND patchMinor = %s AND region = %s''' results = conn.execute( query, (champID, core.PATCH_MAJOR, core.PATCH_MINOR, region))[0] if results == 0: query = '''INSERT INTO ChampCache(champ, patchMajor, patchMinor, totalWRStats, region) VALUES (%s, %s, %s, %s, %s)''' conn.execute(query, (champID, core.PATCH_MAJOR, core.PATCH_MINOR, 0, region)) conn.close()