예제 #1
0
    def create_region_tables(self):
        from classes import DATABASE_DETAILS, Database, REGIONS

        database = Database(DATABASE_DETAILS)

        tables = ["averages", "champ_stats", "final_stats", "game_stats", "games", "games_checked", "players_checked",
                  "summoners"]

        for region in REGIONS:

            all_champs = list(database.get_all_items("Base_champ_list", "id"))
            for table in tables:
                database.clone_table(region + "_" + table, "Base_" + table)


            for champ in all_champs:
                database.clone_table(region + "_" + str(champ), "Base_champ")

            for i in range(3):
                database.insert_items(region + "_final_stats", "game_id", i)

            for champ in all_champs:
                database.insert_items(region + "_averages", "champ_id", champ)
                database.insert_items(region + "_champ_stats", "id, name", str(champ) +
                                      " , " + database.get_database_item("Base_champ_list", "id", champ, "name") )
        database.close_db()
예제 #2
0
class Get_summoners(threading.Thread):
    def __init__(self, region, database_details):
        threading.Thread.__init__(self)
        self.player_region = region
        self.database = Database(database_details)

    def run(self):
        #player = Player(player_id, player_region, API_KEY)
        if self.player_region == 'EUW1':
            player_list = [28809850]
        elif self.player_region == 'KR':
            player_list = [
                209305531, 5977905, 2620061, 6476762, 7835424, 200579114
            ]
        elif self.player_region == 'NA1':
            player_list = [
                31273666, 209000695, 206087402, 36584704, 232202464, 42732981
            ]
        elif self.player_region == 'OC1':
            player_list = [
                200615925, 200059234, 200286310, 200008599, 201844220,
                200236338
            ]

        start_time = time.time()
        misc = Misc()
        misc.logging(self.player_region,
                     "Running the summoner retreivel algorithm", "log")
        #The purpose of the function below is to find as many players that play mainly ARAM games as possible.
        #This is done by recursively going through players' games and extracting the 10 participants' ID's

        self.get_players(player_list, self.player_region, self.database,
                         API_KEY, 4, 0, 20000)

        misc.logging(self.player_region,
                     "Summoner retreival algorithm completed", "log")
        print("Time taken: %s seconds ---" % (time.time() - start_time))

        self.database.close_db()

    def get_players(self, player_list, player_region, database, api_key, i, j,
                    total_num_players):

        #i determines the depth of the execution
        i = i - 1
        if not player_list or i == 0:
            print "returning"
            return
        for player in player_list:
            print player_list
            player_o = Player(player_region, api_key, account_id=player)
            #player_o = Player(player, player_region, api_key)
            recent_games = player_o.get_games()
            if recent_games == -1 or recent_games == -2 or recent_games.__len__(
            ) < 10:
                continue
            print list(recent_games)[:20]
            for game in list(recent_games)[:20]:
                #time.sleep(0.61)
                if not database.insert_items(player_region + "_games_checked",
                                             "id", str(game)):
                    database.update_numberof_games(
                        player_region + "_games_checked", "id", str(game),
                        "times_hit", 1)
                else:
                    player_list1 = []
                    game_details = Game(game, player_region,
                                        api_key).game_details()
                    game_participants = Json_ops(
                        game_details).participants_id()

                    for current_player in game_participants:
                        if database.get_row_count(
                                player_region +
                                "_players_checked") >= total_num_players:
                            print "ads"
                            return
                        if not current_player == player:
                            player_list1.append(current_player)
                            if not database.insert_items(
                                    player_region + "_players_checked", "id",
                                    str(current_player)):
                                database.update_numberof_games(
                                    player_region + "_players_checked", "id",
                                    str(current_player), "times_hit", 1)

                    self.get_players(player_list1, player_region, database,
                                     api_key, i, j, total_num_players)
예제 #3
0
    def run(self):
        database = Database(DATABASE_DETAILS)
        aram_files_path = GAMES_FOLDERS_PATH + self.player_region
        num_games_to_get = 99

        time_check = time.time()
        checking_period = 3600

        keep_running = True
        m = Misc()
        player_count = 0
        checked_players_games = dict()
        second_step = Second_step(self.player_region)
        player_population = database.get_row_count(self.player_region +
                                                   "_players_checked")
        processing_unit = player_population / MAX_THREAD_NUM
        start_position = int(
            round(processing_unit * (self.threadID % MAX_THREAD_NUM)))
        end_position = int(round(start_position + processing_unit - 1))
        if self.threadID % MAX_THREAD_NUM == MAX_THREAD_NUM - 1:
            end_position = player_population
        while True:
            for one_player in list(
                    database.get_all_items(
                        self.player_region + "_players_checked",
                        "id"))[start_position:end_position]:
                self.lock[self.threadID].acquire()
                if keep_running == True:
                    patch_date = Static(API_KEY).get_patch_date()
                    player_id = str(one_player)
                    #print player_id
                    player = Player(self.player_region,
                                    API_KEY,
                                    account_id=player_id)

                    # Check if player already exists in players table. If not, add them.
                    ##        m.logging(self.player_region, "Checking player: " + str(player_id), "log")
                    database.insert_items(self.player_region + "_summoners",
                                          "id", player_id)

                    # Get the timestamp of the last checked game
                    current_last_game_epoch = database.get_database_item(
                        self.player_region + "_summoners", "id", player_id,
                        "last_game_epoch")
                    if current_last_game_epoch < patch_date:
                        current_last_game_epoch = patch_date

                    recent_games = player.get_games(current_last_game_epoch,
                                                    count=num_games_to_get)

                    if player.total_games == 0:
                        self.lock[self.threadID].release()
                        continue

                    if recent_games == -2:
                        m.logging(
                            self.player_region, "Removing " + str(player_id) +
                            " from " + self.player_region + "_summoners and " +
                            self.player_region + "_players_checked", "log")
                        database.delete_line(self.player_region + "_summoners",
                                             "id", player_id)
                        database.delete_line(
                            self.player_region + "_players_checked", "id",
                            player_id)
                        self.lock[self.threadID].release()
                        continue

                    database.update_numberof_games(
                        self.player_region + "_summoners", "id", player_id,
                        "total_games", player.total_games)

                    # If the player has played new games since the last check, update the overall number of games and the time of the last played game.
                    if not current_last_game_epoch == player.date_last_game:
                        database.update_fields(
                            self.player_region + "_summoners", "id", player_id,
                            {"last_game_epoch": player.date_last_game})

            ##            m.logging(self.player_region, "Player " + str(player_id) + " played new games since last check", "log" )

            ##        else:
            ##            m.logging(self.player_region, "Player " + str(player_id) + " Didn't play any new games since last check", "log")

            # If the games are ARAM games, update the table with new number of aram games and the new percentage of played aram games.
                    if player.aram_games is not 0:
                        database.update_numberof_games(
                            self.player_region + "_summoners", "id", player_id,
                            "aram_games", player.aram_games)

                    #if not database.get_database_item(self.player_region + "_summoners", "id", player_id, "total_games") == 0:
                    aram_percentage = (float(
                        database.get_database_item(
                            self.player_region + "_summoners", "id", player_id,
                            "aram_games")) / float(
                                database.get_database_item(
                                    self.player_region + "_summoners", "id",
                                    player_id, "total_games"))) * 100
                    database.update_fields(
                        self.player_region + "_summoners", "id", player_id,
                        {"aram_games_percentage": aram_percentage})
                    # If new ARAM games were played since last check, update the games file for this player with the new games data
                    games = []
                    if not player.aram_games == 0:

                        ##            m.logging(self.player_region, "Updating " + player_id + "'s file with recent ARAM games", "log" )
                        player_file = os.path.join(aram_files_path, player_id)
                        with open(player_file, 'a') as player_history:
                            for game_id in recent_games:
                                if database.insert_items(
                                        self.player_region + "_games",
                                        "id, summoner, date",
                                        game_id + " , " + player_id + " , " +
                                        recent_games[game_id]) == 1:
                                    games.append(game_id)
                                    game = Game(game_id, self.player_region,
                                                API_KEY)
                                    try:
                                        json.dump(game.game_details(),
                                                  player_history)
                                        player_history.write("\n")
                                    except ValueError:
                                        m.logging(
                                            self.player_region,
                                            "Error while saving games " +
                                            game_id + " for " + player_id,
                                            "error")
                                        self.lock[self.threadID].release()
                                        continue

            ##        else:
            ##            m.logging(self.player_region, player_id + " did not play any ARAM games recently", "log")

                    m.logging(
                        self.player_region, self.player_region + "- Thread: " +
                        str(self.threadID) + ", Player: " + str(player_id) +
                        ", All: " + str(player.total_games) + ", ARAM: " +
                        str(player.aram_games), "log")

                    if games:
                        checked_players_games[player_id] = games
                    player_count += 1
                    self.lock[self.threadID].release()

                    if player_count % 2 == 0:
                        if checked_players_games:
                            self.lock[self.threadID].acquire()
                            second_step.update_tables(checked_players_games)
                            self.lock[self.threadID].release()
                            checked_players_games = dict()
                            if time.time() - time_check >= checking_period:
                                time_check = time.time()
                                with open(STATIC_DATA_PATH + "End_Exec",
                                          "r") as end_check:
                                    for line in end_check.readlines():
                                        if line.strip() == "True":
                                            m.logging(
                                                self.player_region,
                                                str(self.threadID) +
                                                ": End of execution was requested. This thread will exit now",
                                                "log")
                                            print str(
                                                self.threadID
                                            ) + ": End of execution was requested. This thread will now exit."
                                            keep_running = False

                else:
                    database.close_db()
                    return

            m.logging(self.player_region, "End of loop reached. Restarting..",
                      "log")
예제 #4
0
from classes import Database, DATABASE_DETAILS, REGIONS

database = Database(DATABASE_DETAILS)

tables = [
    "averages", "champ_stats", "final_stats", "game_stats", "games",
    "games_checked", "players_checked", "summoners"
]

for region in REGIONS:

    all_champs = list(database.get_all_items("Base_champ_list", "id"))
    for table in tables:
        database.clone_table(region + "_" + table, "Base_" + table)

    for champ in all_champs:
        database.clone_table(region + "_" + str(champ), "Base_" + str(champ))

    #Populate initial values
    for i in range(3):
        database.insert_items(region + "_final_stats", "game_id", i)

    for champ in all_champs:
        database.insert_items(region + "_averages", "champ_id", champ)
        database.insert_items(
            region + "_champ_stats", "id, name",
            str(champ) + " , " + "\"" + database.get_database_item(
                "Base_champ_list", "id", champ, "name") + "\"")

database.close_db()