Example #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()
Example #2
0
class Third_step(threading.Thread):
    def __init__(self, threadID, lock, region):
        threading.Thread.__init__(self)
        self.player_region = region
        self.threadID = threadID
        self.lock = lock
        self.database = Database(DATABASE_DETAILS)
        self.m = Misc()

    def run(self):

        time_check = time.time()
        checking_period = 3600
        self.m.logging(
            self.player_region,
            "Running the Regular updates thread for the first time", "log")
        while True:
            self.lock[self.threadID].acquire()
            if time.time() - time_check >= checking_period:
                print self.player_region, " ", self.threadID, " ", time.time()
                time_check = time.time()
                with open(STATIC_DATA_PATH + "End_Exec", "r") as end_check:
                    status = list(end_check.readlines())[0].strip()
                    if status == "True":
                        self.m.logging(
                            self.player_region,
                            "Regular updates thread: End of execution was requested. This thread will exit now",
                            "log")
                        print self.player_region, "Regular updates thread: End of execution was requested. This thread will now exit."
                        break

                self.m.logging(
                    self.player_region,
                    "Running the Regular updates thread (hourly runs)", "log")
                if self.update_averages() == 1:
                    print self.player_region + " checkpoint 1"
                    self.update_final_stats()
                    self.update_champ_stats()
                    self.update_game_stats()
                    self.lock[self.threadID].release()
                else:
                    print self.player_region + " checkpoint 2"
                    self.lock[self.threadID].release()
                    time.sleep(checking_period)
            else:
                print self.player_region + " checkpoint 3"
                self.lock[self.threadID].release()
                time.sleep(checking_period)

    def update_averages(self):

        self.m.logging(
            self.player_region,
            "Updating the averages for region " + self.player_region, "log")
        one_off_values_avg = dict()

        columns = list(self.database.get_column_names("Base_champ"))

        all_champs = list(self.database.get_all_items("Base_champ_list", "id"))

        for champ in all_champs:

            games_analysed = self.database.get_database_item(
                self.player_region + "_champ_stats", "id", champ,
                "games_analysed")
            magic_dmg = self.database.get_sum(
                self.player_region + "_" + str(champ), "magical_damage")
            deaths = self.database.get_sum(
                self.player_region + "_" + str(champ), "deaths")
            if games_analysed == 0 or magic_dmg == 0 or deaths == 0:
                self.m.logging(
                    self.player_region,
                    "Unable to update the averages for region " +
                    self.player_region +
                    " due to insufficient games. This thread will sleep until the next scheduled execution ",
                    "error")
                return -1

        for champ in all_champs:
            self.database.update_column_values(
                self.player_region + "_" + str(champ), "games_analysed",
                games_analysed)
            for i in range(columns.__len__()):
                one_off_values_avg[columns[i]] = self.database.get_sum(
                    self.player_region + "_" + str(champ),
                    columns[i]) / games_analysed
            deaths = self.database.get_sum(
                self.player_region + "_" + str(champ), "deaths")
            one_off_values_avg["total_tankiness"] = (
                self.database.get_sum(self.player_region + "_" + str(champ),
                                      "damage_taken") +
                self.database.get_sum(self.player_region + "_" + str(champ),
                                      "damage_mitigated")) / games_analysed
            one_off_values_avg["kill_ratio"] = self.database.get_sum(
                self.player_region + "_" + str(champ), "kills") / deaths
            one_off_values_avg["assist_ratio"] = self.database.get_sum(
                self.player_region + "_" + str(champ), "assists") / deaths
            one_off_values_avg["dmg_ratio"] = self.database.get_sum(
                self.player_region + "_" + str(champ),
                "physical_damage") / self.database.get_sum(
                    self.player_region + "_" + str(champ), "magical_damage")
            if one_off_values_avg["dmg_ratio"] >= 100:
                one_off_values_avg["dmg_ratio"] = 99
            one_off_values_avg["game_id"] = 99
            one_off_values_avg["result"] = 99
            one_off_values_avg["rank"] = 99
            one_off_values_avg["games_analysed"] = games_analysed
            self.database.update_fields(self.player_region + "_averages",
                                        "champ_id", champ, one_off_values_avg)
        return 1

    def update_final_stats(self):

        self.m.logging(self.player_region, "Updating Final_stats tables",
                       "log")

        self.m.logging(
            self.player_region,
            "Updating the final stats table for region " + self.player_region,
            "log")
        one_off_values_avg = dict()

        columns = list(self.database.get_column_names("Base_final_stats"))

        all_champs = list(self.database.get_all_items("Base_champ_list", "id"))
        for i in range(columns.__len__()):
            one_off_values_avg[columns[i]] = 0

        for champ in all_champs:

            for i in range(columns.__len__()):
                one_off_values_avg[columns[i]] = one_off_values_avg[
                    columns[i]] + self.database.get_database_item(
                        self.player_region + "_averages", "champ_id", champ,
                        columns[i])
        for i in range(columns.__len__()):
            one_off_values_avg[columns[i]] = one_off_values_avg[
                columns[i]] / all_champs.__len__()

        one_off_values_avg["game_id"] = 1
        self.database.update_fields(self.player_region + "_final_stats",
                                    "game_id", 1, one_off_values_avg)

        # max's

        one_off_values_max = dict()

        columns = list(
            self.database.get_column_names(self.player_region +
                                           "_final_stats"))

        for i in range(columns.__len__()):
            one_off_values_max[columns[i]] = 0

        for i in range(columns.__len__()):
            one_off_values_max[columns[i]] = self.database.get_max(
                self.player_region + "_averages", columns[i])

        one_off_values_max["game_id"] = 2
        one_off_values_max["result"] = 99

        self.database.update_fields(self.player_region + "_final_stats",
                                    "game_id", 2, one_off_values_max)

        # min's

        one_off_values_min = dict()

        columns = list(
            self.database.get_column_names(self.player_region +
                                           "_final_stats"))

        for i in range(columns.__len__()):
            one_off_values_min[columns[i]] = 0

        for i in range(columns.__len__()):
            one_off_values_min[columns[i]] = self.database.get_min(
                self.player_region + "_averages", columns[i])

        one_off_values_min["game_id"] = 0
        one_off_values_min["result"] = 99

        self.database.update_fields(self.player_region + "_final_stats",
                                    "game_id", 0, one_off_values_min)

        return

    def update_champ_stats(self):

        s = Stat_rating()

        self.m.logging(
            self.player_region,
            "Updating the champ stats table for region " + self.player_region,
            "log")
        all_champs = self.database.get_all_items("Base_champ_list", "id")
        for champ in all_champs:

            damage_ratios = s.damage_type_rating(
                self.database.get_sum(self.player_region + "_" + str(champ),
                                      "physical_damage"),
                self.database.get_sum(self.player_region + "_" + str(champ),
                                      "magical_damage"),
                self.database.get_sum(self.player_region + "_" + str(champ),
                                      "true_damage"),
                self.database.get_sum(self.player_region + "_" + str(champ),
                                      "damage_to_champs"))

            values = dict()

            games_played = self.database.get_database_item(
                self.player_region + "_champ_stats", "id", champ,
                "games_analysed")

            values["damage_dealt"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "damage_dealt"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "damage_dealt"))

            values["win_rate"] = s.stat_percent(
                self.database.get_sum(self.player_region + "_" + str(champ),
                                      "result"), games_played)

            values["damage_to_turrets"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "damage_to_turrets"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "damage_to_turrets"))

            values["waveclear"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "minions_killed"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "minions_killed"))

            values["tankiness"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "total_tankiness"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "total_tankiness"))

            values["cc_score"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "cc_score"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "cc_score"))

            values["p_type"] = damage_ratios[0]
            values["m_type"] = damage_ratios[1]
            values["t_type"] = damage_ratios[2]

            values["p_dmg"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "physical_damage"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "physical_damage"))
            values["m_dmg"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "magical_damage"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "magical_damage"))
            values["t_dmg"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "true_damage"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "true_damage"))

            values["total_dmg"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "damage_to_champs"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "damage_to_champs"))

            values["dmg_ratio"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "dmg_ratio"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "dmg_ratio"))

            values["assist_ratio"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "assist_ratio"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "assist_ratio"))

            values["kill_ratio"] = s.stat_percent(
                self.database.get_database_item(
                    self.player_region + "_averages", "champ_id", champ,
                    "kill_ratio"),
                self.database.get_database_item(
                    self.player_region + "_final_stats", "game_id", 2,
                    "kill_ratio"))
            if values["dmg_ratio"] > 100:
                print "champ stats dmg ratio", values["dmg_ratio"], champ
            self.database.update_fields(self.player_region + "_champ_stats",
                                        "id", champ, values)

    def update_game_stats(self):

        self.m.logging(
            self.player_region,
            "Updating the game stats table for region " + self.player_region,
            "log")

        games = self.database.get_all_items(self.player_region + "_games",
                                            "id")

        stat_names = list(
            self.database.get_column_names("Base_champ_stats"))[2:]
        for game_id in games:
            champs = list(
                self.database.get_row(self.player_region + "_games", "id",
                                      game_id))[4:]
            if not champs:
                continue
            if champs[1] == 0:
                continue
            winning_team = champs.pop(0)

            values1 = dict()
            values2 = dict()

            for stat_name in stat_names:
                values1[stat_name + "_1"] = values2[stat_name + "_2"] = 0

            counter = 0
            for champ in champs:
                details = list(
                    self.database.get_row(self.player_region + "_champ_stats",
                                          "id", champ))[2:]
                if counter < 5:
                    for i in range(stat_names.__len__()):
                        values1[stat_names[i] + "_1"] += details[i]
                    counter += 1
                else:
                    for i in range(stat_names.__len__()):
                        values2[stat_names[i] + "_2"] += details[i]

            for a in values1:
                values1[a] = values1[a] / 5
            for b in values2:
                values2[b] = values2[b] / 5
            values = values2.copy()
            values.update(values1)
            values["winning_team"] = winning_team

            self.database.insert_items(self.player_region + "_game_stats",
                                       "id", game_id)
            self.database.update_fields(self.player_region + "_game_stats",
                                        "id", game_id, values)
Example #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")
from __future__ import division
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()