Ejemplo n.º 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()
Ejemplo n.º 2
0
def reset_database():
    tables = ["summoners", "averages", "champ_stats", "final_stats", "game_stats", "games"]
    database = Database(DATABASE_DETAILS)
    all_champs = list(database.get_all_items("Base_champ_list", "id"))
    misc = Misc()
    for region in REGIONS:

        for table in tables:
            database.delete_table(region + "_" + table)
            database.replicate_table("Base_" + table, region + "_" + table)

        for champ in all_champs:
            database.delete_table(region + "_" + str(champ))
            database.replicate_table("Base_champ", region + "_" + str(champ))
Ejemplo n.º 3
0
class Second_step:
    def __init__(self, region):
        self.region = region
        self.database = Database(DATABASE_DETAILS)
        self.games_folder = GAMES_FOLDERS_PATH + region
        self.s = Stat_rating()
        self.misc = Misc()

    def update_tables(self, checked_games_players):

        #database = Database(DATABASE_DETAILS)

        # misc = Misc()
        for player_id in checked_games_players:

            current_file = os.path.join(self.games_folder, player_id)

            with open(current_file, "r") as games_file:
                wins = 0

                #player_id = os.path.basename(games_file.name)

                if os.stat(current_file).st_size == 0:
                    self.misc.logging(
                        self.region,
                        player_id + "' games file is empty. Skipping ..",
                        "error")
                    continue
                #print player_id

                all_games = games_file.readlines()
                existing_champs = list(
                    self.database.get_all_items("Base_champ_list", "id"))
                for game in all_games:
                    champ_names = dict()
                    try:
                        json_game = json.loads(game.strip())
                    except ValueError:
                        self.misc.logging(
                            self.region, "Json error in file: " +
                            str(player_id) + ".. Ignoring", "error")
                        continue
                    if json_game == -1:
                        continue
                    output = Json_ops(json_game)
                    game_id = output.game_id()

                    if game_id == -1:
                        self.misc.logging(
                            self.region, "Found a non Json object in file: " +
                            str(player_id) + ".. Ignoring", "error")
                        print "Found a non Json object .. Ignoring"
                        continue

                    if str(game_id) in checked_games_players[player_id]:
                        all_champs = output.get_all_champs()
                        new_champ_check = False
                        for champ in all_champs:
                            if not champ in existing_champs:
                                new_champ_check = True
                                break
                        if new_champ_check == True:
                            self.misc.logging(
                                self.region, "New champ found in game " +
                                str(game_id) + ". Ignoring this game..",
                                "error")
                            self.database.delete_line(self.region + "_games",
                                                      "id", str(game_id))
                            self.database.update_numberof_games(
                                self.region + "_summoners", "id", player_id,
                                "aram_games", -1)
                            self.database.update_numberof_games(
                                self.region + "_summoners", "id", player_id,
                                "total_games", -1)
                            continue
                        for i in range(10):
                            champ_names["champ_" + str(i + 1)] = all_champs[i]
                            if i == output.player_position(player_id):
                                player = all_champs[i]

                        champ_names["winning_team"] = output.winning_team()
                        champ_names["date"] = output.game_date()
                        self.database.update_fields(self.region + "_games",
                                                    "id", game_id, champ_names)

                        for champ in all_champs:

                            self.database.insert_items(
                                self.region + "_" + str(champ), "game_id",
                                game_id)
                            result = output.did_win(champ)
                            if champ == player:
                                wins += result
                            if not result == 2:
                                stats = output.get_champ_stat(
                                    champ, "damage_dealt", "damage_to_champs",
                                    "damage_to_turrets", "damage_taken",
                                    "physical_damage", "magical_damage",
                                    "true_damage", "time_alive", "cc_score",
                                    "minions_killed", "kills", "deaths",
                                    "assists", "rank", "damage_mitigated")

                                stats["game_duration"] = output.game_duration()
                                stats["result"] = result
                                # stats = output.get_champ_stat(champ)

                                rank = output.get_rank(champ)
                                rank_number = self.s.rank_mapping(rank)

                                stats["rank"] = rank_number

                                #print stats
                                self.database.update_fields(
                                    self.region + "_" + str(champ), "game_id",
                                    game_id, stats)
                                self.database.update_numberof_games(
                                    self.region + "_champ_stats", "id", champ,
                                    "games_analysed", 1)
                        self.database.update_fields(
                            self.region + "_games", "id", game_id,
                            {"duration": stats["game_duration"]})

                [aram_games, total_games,
                 won_games] = self.database.get_database_row_items(
                     self.region + "_summoners", {"id": player_id},
                     "aram_games, total_games, won_games")
                if total_games == 0 or aram_games == 0:
                    continue
                self.database.update_fields(
                    self.region + "_summoners", "id", player_id, {
                        "aram_games_percentage":
                        100 * aram_games / total_games,
                        "won_games": won_games + wins,
                        "win_rate": 100 * (won_games + wins) / aram_games
                    })
            try:
                os.remove(os.path.join(self.games_folder, player_id))
            except OSError as e:
                self.misc.logging(
                    self.region,
                    "Error while deleting game file: " + e.message, "error")
Ejemplo n.º 4
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)
Ejemplo n.º 5
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")
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
class Daily_check(threading.Thread):
    def __init__(self, threadID, lock, database_details, api_key):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.lock = lock
        self.api_key = api_key
        self.database_details = database_details
        self.database = Database(database_details)
        self.static = Static(api_key)
        self.misc = Misc()

    def run(self):

        time_check = time.time()
        backup_time = time.time()

        while True:
            if time.time() - time_check >= checking_period:
                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.misc.logging(
                            DEFAULT_REGION,
                            "Regular checks thread: End of execution was requested. This thread will exit now",
                            "log")
                        print DEFAULT_REGION, "Regular checks thread: End of execution was requested. This thread will now exit."
                        break
                self.misc.logging(DEFAULT_REGION, "Running the daily checks",
                                  "log")
                self.misc.logging(DEFAULT_REGION,
                                  "Checking for a version change", "log")
                self.check_version()
                self.misc.logging(DEFAULT_REGION, "Checking for a new champ",
                                  "log")
                self.check_for_new_champ()
                if time.time() - backup_time >= backup_period:
                    backup_time = time.time()
                    self.misc.logging(DEFAULT_REGION,
                                      "Uploading a copy of the database",
                                      "log")
                    self.sftp_database("regular_database_backup")
            else:
                time.sleep(checking_period)

    def check_version(self):
        online_version = self.static.check_current_version()
        current_version = self.static.get_current_version()
        if online_version == current_version:
            self.misc.logging(
                DEFAULT_REGION,
                "Current version unchanged (" + online_version + ")", "log")
        else:
            for l in self.lock:
                l.acquire()
                print "acquired ", l
            self.update_version(online_version)
            self.misc.logging(
                DEFAULT_REGION,
                "A new patch has been deployed! the new version is " +
                online_version, "log")
            self.sftp_database(current_version)
            #Mysql_operations(self.database_details).export_database(current_version)

            self.reset_database()
            for l in self.lock:
                l.release()

    def update_version(self, new_version):
        return self.static.update_current_version(new_version)

    def check_for_new_champ(self):

        champ_check = self.static.champs_list()
        all_champs = list(self.database.get_all_items("Base_champ_list", "id"))
        new_champ = False
        for id in champ_check:
            if not id in all_champs:
                new_champ = True
                champ_details = Champ(id, self.api_key).getChampDetails()
                self.misc.logging(
                    DEFAULT_REGION, "New champ found! adding " +
                    str(champ_details['name']) + " to the appropriate tables",
                    "log")
                self.add_new_champ(champ_details)

        if new_champ == False:
            self.misc.logging(DEFAULT_REGION,
                              "No new champ has been added since last check",
                              "log")

    def add_new_champ(self, champ_details):

        new_champ_id = str(champ_details['id'])
        new_champ_name = champ_details['name']
        new_champ_class_1 = champ_details['class1']
        if "class2" in champ_details:
            new_champ_class_2 = champ_details['class2']
            self.database.insert_items(
                "Base_champ_list", "name, id, class1, class2",
                new_champ_name + "," + new_champ_id + "," + new_champ_class_1 +
                "," + new_champ_class_2)
        else:
            self.database.insert_items(
                "Base_champ_list", "name, id, class1",
                new_champ_name + "," + new_champ_id + "," + new_champ_class_1)
        for region in REGIONS + ["Base"]:
            self.database.insert_items(region + "_averages", "champ_id",
                                       new_champ_id)
            self.database.insert_items(region + "_champ_stats", "name, id",
                                       new_champ_name + "," + new_champ_id)
            self.misc.logging(
                region, new_champ_name + " has been added to " + region +
                "_averages" + " and " + region + "_champ_stats", "log")
        for region in REGIONS:
            self.database.clone_table(region + "_" + new_champ_id,
                                      "Base_champ")
            self.misc.logging(
                region, region + "_" + new_champ_id + " has been created",
                "log")

    def reset_database(self):

        tables = ["averages", "champ_stats", "final_stats", "game_stats"]

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

        for region in REGIONS:

            for table in tables:
                self.database.delete_table(region + "_" + table)
                self.database.replicate_table("Base_" + table,
                                              region + "_" + table)
                self.misc.logging(
                    region,
                    "table " + region + "_" + table + " has been reset", "log")

            for champ in all_champs:
                self.database.delete_table(region + "_" + str(champ))
                self.database.replicate_table("Base_champ",
                                              region + "_" + str(champ))
                self.misc.logging(
                    region,
                    "table " + region + "_" + str(champ) + " has been reset",
                    "log")

    def sftp_database(self, file_name):
        local_file_name = Mysql_operations(
            self.database_details).export_database(file_name)
        remote_file_name = local_file_name.split("/")[-1]
        transfer_error = False
        try:
            transport = paramiko.Transport((SFTP_HOST, SFTP_PORT))
            transport.connect(username=SFTP_USERNAME, password=SFTP_PASSWORD)
            connection = paramiko.SFTPClient.from_transport(transport)

            connection.put(local_file_name,
                           SFTP_REMOTE_PATH + remote_file_name)

            self.misc.logging(
                DEFAULT_REGION,
                "Database upload successful (" + remote_file_name + ")", "log")
        except Exception as e:
            self.misc.logging(
                DEFAULT_REGION,
                "Error while transferring database " + remote_file_name +
                " to the remote SFTP server. Error message: " + str(e),
                "error")
            transfer_error = True
        try:
            os.remove(local_file_name)
            self.misc.logging(
                DEFAULT_REGION, "Local copy of backup file (" +
                remote_file_name + ") has been deleted.", "log")
        except OSError as e:
            self.misc.logging(
                DEFAULT_REGION, "Error while deleting local backup file (" +
                remote_file_name + "): " + e.message, "error")
        if not transfer_error:
            connection.close()
            transport.close()
        return