From a9586d9c7f1db21db4b5d36d112f0d278a439151 Mon Sep 17 00:00:00 2001 From: gaithern Date: Mon, 5 Feb 2024 21:04:43 -0600 Subject: [PATCH] Thinking --- worlds/lol/Client.py | 216 ------------------------------------------- worlds/lol/Data.py | 53 +++-------- worlds/lol/Test.py | 68 ++++++++++++++ 3 files changed, 83 insertions(+), 254 deletions(-) create mode 100644 worlds/lol/Test.py diff --git a/worlds/lol/Client.py b/worlds/lol/Client.py index 3234844ce4d1..11402751c708 100644 --- a/worlds/lol/Client.py +++ b/worlds/lol/Client.py @@ -22,103 +22,6 @@ os.makedirs(game_communication_path) -###API FUNCTIONS### -def get_header(api_key): - return { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", - "Accept-Language": "en-US,en;q=0.9", - "Accept-Charset": "application/x-www-form-urlencoded; charset=UTF-8", - "Origin": "https://developer.riotgames.com", - "X-Riot-Token": api_key - } - -def get_puuid_by_riot_id(game_name, tag_line, api_key, region_long): - url = "https://" + region_long + ".api.riotgames.com/riot/account/v1/accounts/by-riot-id/" + game_name + "/" + tag_line - response = requests.get(url, headers=get_header(api_key)) - if str(response) == "": - return "Forbidden, check your API Key" - elif str(response) == "": - return "Data not found. Check your Riot ID, Tag Line, or configure your region." - else: - return json.loads(response.text)["puuid"] - -def get_last_match_id_by_puuid(puuid, api_key, region_long): - url = "https://" + region_long + ".api.riotgames.com/lol/match/v5/matches/by-puuid/" + str(puuid) + "/ids?start=0&count=1" - response = requests.get(url, headers=get_header(api_key)) - return json.loads(response.text)[0] - -def get_match_info_by_match_id(match_id, api_key, region_long): - url = "https://" + region_long + ".api.riotgames.com/lol/match/v5/matches/" + str(match_id) - response = requests.get(url, headers=get_header(api_key)) - return json.loads(response.text) - -def get_item_ids_purchased(puuid, match_info): - item_ids = [] - item_slots = ["item0", "item1", "item2", "item3", "item4", "item5", "item6"] - for participant in match_info["info"]["participants"]: - if participant["puuid"] == puuid: - for item_slot in item_slots: - if item_slot in participant.keys(): - item_ids.append(participant[item_slot]) - return item_ids - -def won_match(puuid, match_info): - for participant in match_info["info"]["participants"]: - if participant["puuid"] == puuid: - return participant["win"] - return False - -def get_collected_item_ids(): - item_ids = [] - for root, dirs, files in os.walk(game_communication_path): - for file in files: - if str(file).startswith("AP"): - with open(os.path.join(game_communication_path, file), 'r') as f: - item_id = int(f.readline()) - item_ids.append(item_id) - f.close() - return item_ids - -def get_sent_location_ids(): - sent_location_ids = [] - for root, dirs, files in os.walk(game_communication_path): - for file in files: - if str(file).startswith("send"): - sent_location_ids.append(str(file).replace("send", "")) - return sent_location_ids - -def get_available_checks(): - item_ids = [int(str(item_id)[4:]) for item_id in get_collected_item_ids()] - sent_location_ids = [int(str(location_id)[4:]) for location_id in get_sent_location_ids()] - available_checks = list(set(item_ids)-set(sent_location_ids)) - return available_checks - -def get_game_mode_offset(game_mode): - if game_mode == "ARAM": - return 5652000000 - elif game_mode == "CHERRY": - return 5653000000 - else: - return 5651000000 - -def get_item_name(check_item_id): - versions_url = "https://ddragon.leagueoflegends.com/api/versions.json" - most_recent_version = requests.get(versions_url).json()[0] - items_url = "https://ddragon.leagueoflegends.com/cdn/" + str(most_recent_version) + "/data/en_US/item.json" - item_data = requests.get(items_url).json()["data"] - for item_id in list(item_data.keys()): - if int(check_item_id) == int(item_id): - return item_data[item_id]["name"] - return "Not Found" - -def send_check(item_id): - with open(os.path.join(game_communication_path, "send" + str(item_id)), 'w') as f: - f.close() - -def send_victory(): - with open(os.path.join(game_communication_path, "victory"), 'w') as f: - f.close() - ###Client### if __name__ == "__main__": Utils.init_logging("LOLClient", exception_logger="Client") @@ -141,125 +44,6 @@ class LOLClientCommandProcessor(ClientCommandProcessor): region_short_options = ["br1" , "la1" , "la2" , "na1" , "jp1" , "kr" , "tw2" , "eun1" , "euw1" , "ru" , "tr1" , "oc1", "ph2", "sg" , "th2", "vn2"] region_long_options = ["americas", "americas", "americas", "americas", "asia", "asia", "asia", "europe", "europe", "europe", "europe", "sea", "sea", "sea", "sea", "sea"] - def _cmd_set_api_key(self, api_key): - """Set the API Key for RIOT API""" - self.api_key = api_key - self.output(f"API Key Set") - - def _cmd_set_riot_id(self, riot_id_and_tag_line): - """Set the PUUID from Riot API using the passed Riot ID and Tag Line""" - if "#" not in riot_id_and_tag_line: - self.output(f"Please format your input like: RIOT_ID#TAG_LINE") - else: - riot_id = riot_id_and_tag_line.split("#")[0] - tag_line = riot_id_and_tag_line.split("#")[1] - if self.api_key != "": - self.player_puuid = get_puuid_by_riot_id(riot_id, tag_line, self.api_key, self.region_long) - if self.player_puuid == "Forbidden, check your API Key": - self.output("Forbidden, check your API Key") - self.player_puuid = "" - elif self.player_puuid == "Data not found. Check your Riot ID, Tag Line, or configure your region.": - self.output("Data not found. Check your Riot ID, Tag Line, or configure your region.") - self.player_puuid = "" - else: - self.output(f"PUUID Set") - else: - self.output(f"Please set your API Key") - - def _cmd_set_region(self, region_number): - """Sets the region number. Default is NA""" - if region_number.isnumeric(): - region_number = int(region_number) - if region_number >= 0 and region_number < len(self.region_short_options): - self.region_short = self.region_short_options[region_number] - self.region_long = self.region_long_options[region_number] - self.output(f"Region set: " + self.region_short + " - " + self.region_long) - else: - self.output(f"Invalid int. Please choose a valid option. View options by running /print_region_options") - else: - self.output(f"Invalid integer passed. Please pass a valid option. View options by running /print_region_options") - - def _cmd_check_last_match(self): - """Checks the last match for victory with unlocked items""" - new_locations = [] - if self.api_key != "" and self.player_puuid != "": - unlocked_item_ids = get_collected_item_ids() - if len(unlocked_item_ids) > 0: - last_match_id = get_last_match_id_by_puuid(self.player_puuid, self.api_key, self.region_long) - last_match_info = get_match_info_by_match_id(last_match_id, self.api_key, self.region_long) - game_mode_offset = get_game_mode_offset(last_match_info["info"]["gameMode"]) - if won_match(self.player_puuid, last_match_info): - item_ids_purchased = get_item_ids_purchased(self.player_puuid, last_match_info) - self.output("Item IDs Unlocked: " + str(unlocked_item_ids)) - for item_id in item_ids_purchased: - self.output("Item ID Purchased: " + str(int(item_id) + game_mode_offset + 10000000)) - if int(item_id) + game_mode_offset in unlocked_item_ids: - new_locations.append(int(item_id) + game_mode_offset + 10000000) - else: - self.output(f"Last Match Resulted in a Loss...") - else: - self.output(f"You have no items!") - else: - self.output(f"Please set your API Key and Summoner Name") - if len(new_locations) > 0: - for location in new_locations: - send_check(location) - else: - self.output(f"No new valid items") - - def _cmd_receive_starting_items(self): - """When you're ready to start your run, this receives your starting items""" - starting_location_ids = [5660_000001, 5660_000002, 5660_000003, 5660_000004, 5660_000005, 5660_000006] - for location_id in starting_location_ids: - with open(os.path.join(game_communication_path, "send" + str(location_id)), 'w') as f: - f.close() - self.output("Items Received") - - def _cmd_check_for_victory(self): - victory_item_ids = [5650000001, 5650000002, 5650000003, 5650000004, 5650000005, 5650000006] - victory_items_collected = 0 - item_ids = get_collected_item_ids() - for item_id in item_ids: - if int(item_id) in victory_item_ids: - victory_items_collected = victory_items_collected + 1 - if victory_items_collected >= len(victory_item_ids): - send_victory() - else: - self.output("You have " + str(victory_items_collected) + " out of " + str(len(victory_item_ids)) + " victory items collected.") - - def _cmd_print_item_ids(self): - """Prints currently collected item ids""" - item_ids = get_collected_item_ids() - for item_id in item_ids: - self.output(item_id) - - def _cmd_print_puuid(self): - """Prints the defined PUUID""" - self.output(self.player_puuid) - - def _cmd_print_api_key(self): - """Prints the defined API Key""" - self.output(self.api_key) - - def _cmd_print_region(self): - """Prints currently selected region.""" - self.output(f"Region: " + self.region_short + " - " + self.region_long) - - def _cmd_print_region_options(self): - """Prints all region options""" - i = 0 - while i < len(self.region_short_options): - self.output(f"Region " + str(i) + ": " + self.region_short_options[i] + " - " + self.region_long_options[i]) - i = i + 1 - - def _cmd_print_available_checks(self): - """Prints all available items for which you can receive checks.""" - available_checks = get_available_checks() - if len(available_checks) > 0: - for available_check in available_checks: - self.output(get_item_name(available_check)) - else: - self.output(f"No available items!") class LOLContext(CommonContext): command_processor: int = LOLClientCommandProcessor diff --git a/worlds/lol/Data.py b/worlds/lol/Data.py index 3b5e6bd85563..eba9afb56765 100644 --- a/worlds/lol/Data.py +++ b/worlds/lol/Data.py @@ -5,48 +5,25 @@ maps_url = "https://static.developer.riotgames.com/docs/lol/maps.json" most_recent_version = requests.get(versions_url).json()[0] items_url = "https://ddragon.leagueoflegends.com/cdn/" + str(most_recent_version) + "/data/en_US/item.json" -sr_items = {} -aram_items = {} -arena_items = {} +champions_url = "https://ddragon.leagueoflegends.com/cdn/" + str(most_recent_version) + "/data/en_US/champion.json" +items = {} +champions = {} for map in requests.get(maps_url).json(): - if map["mapName"] == "Summoner's Rift" and map["notes"] == "Current Version": - sr_map_id = map["mapId"] if map["mapName"] == "Howling Abyss": - aram_map_id = map["mapId"] - if map["mapName"] == "Rings of Wrath": - arena_map_id = map["mapId"] - + map_id = map["mapId"] item_data = requests.get(items_url).json()["data"] for item_id in item_data.keys(): if "into" not in item_data[item_id].keys() and item_data[item_id]["gold"]["purchasable"] and item_data[item_id]["gold"]["total"] > 1000 and "requiredAlly" not in item_data[item_id].keys(): - if item_data[item_id]["maps"][str(sr_map_id)]: - sr_items[item_id] = item_data[item_id]["name"] - if item_data[item_id]["maps"][str(aram_map_id)]: - aram_items[item_id] = item_data[item_id]["name"] - if item_data[item_id]["maps"][str(arena_map_id)]: - arena_items[item_id] = item_data[item_id]["name"] - - for item_id in item_data.keys(): - if "specialRecipe" in item_data[item_id].keys(): - if str(item_data[item_id]["specialRecipe"]) in sr_items.keys(): - del sr_items[str(item_data[item_id]["specialRecipe"])] - sr_items[item_id] = item_data[item_id]["name"] - if str(item_data[item_id]["specialRecipe"]) in aram_items.keys(): - del aram_items[str(item_data[item_id]["specialRecipe"])] - aram_items[item_id] = item_data[item_id]["name"] - if str(item_data[item_id]["specialRecipe"]) in arena_items.keys(): - del arena_items[str(item_data[item_id]["specialRecipe"])] - arena_items[item_id] = item_data[item_id]["name"] - - #Doing this twice for items that transform twice + if item_data[item_id]["maps"][str(map_id)]: + items[item_id] = item_data[item_id]["name"] +for i in range(3): #Doing this multiple times for items that transform multiple times for item_id in item_data.keys(): if "specialRecipe" in item_data[item_id].keys(): - if str(item_data[item_id]["specialRecipe"]) in sr_items.keys(): - del sr_items[str(item_data[item_id]["specialRecipe"])] - sr_items[item_id] = item_data[item_id]["name"] - if str(item_data[item_id]["specialRecipe"]) in aram_items.keys(): - del aram_items[str(item_data[item_id]["specialRecipe"])] - aram_items[item_id] = item_data[item_id]["name"] - if str(item_data[item_id]["specialRecipe"]) in arena_items.keys(): - del arena_items[str(item_data[item_id]["specialRecipe"])] - arena_items[item_id] = item_data[item_id]["name"] \ No newline at end of file + if str(item_data[item_id]["specialRecipe"]) in items.keys(): + del items[str(item_data[item_id]["specialRecipe"])] + items[item_id] = item_data[item_id]["name"] +champion_data = requests.get(champions_url).json()["data"] +for champion in list(champion_data.keys()): + champions[int(champion_data[champion]["key"])] = champion +print(json.dumps(champions, indent=2)) +print(json.dumps(items, indent=2)) diff --git a/worlds/lol/Test.py b/worlds/lol/Test.py new file mode 100644 index 000000000000..64e2df94bcc6 --- /dev/null +++ b/worlds/lol/Test.py @@ -0,0 +1,68 @@ +import requests +import json + +url = "https://127.0.0.1:2999/liveclientdata/allgamedata" + +def took_tower(game_data, player_name): + for event in game_data["events"]["Events"]: + if event["EventName"] == "TurretKilled" and event["KillerName"] == player_name: + return True + return False + +def assisted_tower(game_data, player_name): + for event in game_data["events"]["Events"]: + if event["EventName"] == "TurretKilled" and (event["KillerName"] == player_name or player_name in event["Assisters"]): + return True + return False + +def took_epic_monster(game_data, player_name, monster_name): + for event in game_data["events"]["Events"]: + if event["EventName"] == monster_name + "Kill" and event["KillerName"] == player_name: + return True + return False + +def assisted_epic_monster(game_data, player_name, monster_name): + for event in game_data["events"]["Events"]: + if event["EventName"] == monster_name + "Kill" and (event["KillerName"] == player_name or player_name in event["Assisters"]): + return True + return False + +def stole_epic_monster(game_data, player_name, monster_name): + for event in game_data["events"]["Events"]: + if event["EventName"] == monster_name + "Kill" and (event["KillerName"] == player_name or player_name in event["Assisters"]) and str(event["Stolen"]) == "True": + return True + return False + +def assisted_kill(game_data, player_name): + for event in game_data["events"]["Events"]: + if event["EventName"] == "ChampionKill" and (event["KillerName"] == player_name or player_name in event["Assisters"]): + return True + return False + +def player_vision_score(game_data, player_name): + for player in game_data["allPlayers"]: + if player["summonerName"] == player_name: + return player["wardScore"] + return 0 + +def player_creep_score(game_data, player_name): + for player in game_data["allPlayers"]: + if player["summonerName"] == player_name: + return player["creepScore"] + return 0 + +def vision_score_above(game_data, player_name, score_target): + return player_vision_score(game_data, player_name) >= score_target + +def creep_score_above(game_data, player_name, score_target): + return player_creep_score(game_data, player_name) >= score_target + + + + + +try: + x = requests.get(url, verify=False).json() + print(json.dumps(x, indent=2)) +except: + print("No connection to game") \ No newline at end of file