Beispiel #1
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")
Beispiel #2
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")