From c722199763cf52f27f23a578cee34f68e09a1a6c Mon Sep 17 00:00:00 2001 From: cadenforrest Date: Wed, 25 Nov 2020 02:49:32 -0600 Subject: [PATCH 1/4] fixing things --- find_hero_test.py | 19 ------------------- opendota.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 19 deletions(-) delete mode 100644 find_hero_test.py diff --git a/find_hero_test.py b/find_hero_test.py deleted file mode 100644 index b1357bb..0000000 --- a/find_hero_test.py +++ /dev/null @@ -1,19 +0,0 @@ -import opendota - -herolist = opendota.get_hero_list() - -i = 1 - - -def find_hero(heroname): - i = 1 - for hero in herolist: - i += 1 - if herolist[i]["localized_name"] == heroname: - return herolist[i] - - print("Didn't find the hero") - return - - -print(find_hero("Juggernaut")) diff --git a/opendota.py b/opendota.py index 0d4b97f..ab0b023 100644 --- a/opendota.py +++ b/opendota.py @@ -11,6 +11,31 @@ # def find_matches_by_hero(): +def make_query(hero1, hero2): + query = + f"""SELECT player_matches.{get_hero_id(hero1)} AS hero1, + player_matches1.{get_hero_id(hero2)} AS hero2, + player_matches.player_slot AS ps1, + player_matches1.player_slot AS ps2, + matches.match_id, + leagues.name leaguename + FROM player_matches + JOIN player_matches AS player_matches1 + ON player_matches.match_id = player_matches1.match_id + JOIN matches + ON player_matches.match_id = matches.match_id + JOIN leagues using(leagueid) + WHERE player_matches.hero_id = 4 + AND player_matches1.hero_id = 3 + AND matches.start_time >= extract(epoch + FROM timestamp '2020-10-23T06:53:44.537Z') + AND abs(player_matches.player_slot - player_matches1.player_slot) > 123 + ORDER BY matches.match_id NULLS LAST LIMIT 200""" + + + + + def find_hero(heroname): for hero in load_hero_list(): @@ -46,3 +71,6 @@ def load_hero_list(): if __name__ == "__main__": main() + + + From 608f8f7e2ea9ec17d6223c9d77be6e51812b2719 Mon Sep 17 00:00:00 2001 From: cadenforrest Date: Fri, 27 Nov 2020 04:00:11 -0600 Subject: [PATCH 2/4] added a 1v1 query generator. get_matches returns list of pro matches between two heroes --- opendota.py | 23 +++++++++++++++++------ tests/test_find_hero.py | 7 +++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/opendota.py b/opendota.py index ab0b023..eba687d 100644 --- a/opendota.py +++ b/opendota.py @@ -12,9 +12,8 @@ # def find_matches_by_hero(): def make_query(hero1, hero2): - query = - f"""SELECT player_matches.{get_hero_id(hero1)} AS hero1, - player_matches1.{get_hero_id(hero2)} AS hero2, + query = f"""SELECT player_matches.hero_id AS hero1, + player_matches1.hero_id AS hero2, player_matches.player_slot AS ps1, player_matches1.player_slot AS ps2, matches.match_id, @@ -25,16 +24,28 @@ def make_query(hero1, hero2): JOIN matches ON player_matches.match_id = matches.match_id JOIN leagues using(leagueid) - WHERE player_matches.hero_id = 4 - AND player_matches1.hero_id = 3 + WHERE player_matches.hero_id = {get_hero_id(hero1)} + AND player_matches1.hero_id = {get_hero_id(hero2)} AND matches.start_time >= extract(epoch FROM timestamp '2020-10-23T06:53:44.537Z') AND abs(player_matches.player_slot - player_matches1.player_slot) > 123 ORDER BY matches.match_id NULLS LAST LIMIT 200""" - + response = requests.get( + f"{API_ROOT}/explorer", params=dict(api_key=secret.OPENDOTA_API_KEY, sql = query) + ) + return response.json() +##returns list of pro match ids played between two opposing heroes +def get_matches(hero1, hero2): + query_response = make_query(hero1, hero2) + match_list = query_response['rows'] ##list of dictionaries for some reason + result = [] + + for match in match_list: + result.append(match['match_id']) + return result def find_hero(heroname): diff --git a/tests/test_find_hero.py b/tests/test_find_hero.py index 5f335ec..8f49869 100644 --- a/tests/test_find_hero.py +++ b/tests/test_find_hero.py @@ -10,3 +10,10 @@ def test_find_juggernaut(): def test_hero_id(): juggernaut_id = opendota.get_hero_id("Juggernaut") assert juggernaut_id == 8 + +def test_query(): + print(opendota.make_query("Bloodseeker", "Crystal Maiden")) + + +def test_get_matches(): + print(opendota.get_matches("Bloodseeker", "Crystal Maiden")) \ No newline at end of file From 769766b12201d8fdfe38e5168932416f9a93641a Mon Sep 17 00:00:00 2001 From: cadenforrest Date: Fri, 27 Nov 2020 04:00:41 -0600 Subject: [PATCH 3/4] gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9216640..8da8369 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ secret.py *__pycache__/* -.idea/* \ No newline at end of file +.idea/* +Pipfile +Pipfile.lock +.vscode/settings.json From 1c2e36f739d6822d168fab8a3341c4582e0fcb1f Mon Sep 17 00:00:00 2001 From: cadenforrest Date: Fri, 27 Nov 2020 19:31:17 -0600 Subject: [PATCH 4/4] first pass at making a 4 hero query generator, no real test yet sadge --- opendota.py | 116 ++++++++++++++++++++++++++++++++++------ tests/test_find_hero.py | 14 +++-- 2 files changed, 111 insertions(+), 19 deletions(-) diff --git a/opendota.py b/opendota.py index eba687d..a91c600 100644 --- a/opendota.py +++ b/opendota.py @@ -8,10 +8,96 @@ # get/find matches - -# def find_matches_by_hero(): - -def make_query(hero1, hero2): +def generate_query(*heroes): + """generates an SQL query where hero1+hero2 is vs hero3+hero4""" + # won't support 2v3's or 1v3's in the case of trilanes + + def select(*heroes): + """generates select statement for certain num of heroes""" + select = f"SELECT" + + + i = 0 #leave me be sir + for data in heroes: + for hero in data: + if i == 0: + select = select + f" player_matches.hero_id AS hero1, \n" + else: + select = select + f"player_matches{i}.hero_id AS hero{i+1}, \n" + i = i+1 + i = 0 + for hero in data: + if i == 0: + select = select + f"player_matches.player_slot AS ps1, \n" + else: + select = select + f"player_matches{i}.player_slot AS ps{i+1}, \n" + i = i+1 + + select = select + f"matches.match_id,\n" + select = select + f"leagues.name leaguename\n" + return select + + def join(*heroes): + """generates join statements for certain num of heroes""" + join = f"FROM player_matches \n" + i = 0 + for data in heroes: + for hero in data: + if i == 0: + i = i+1 + continue + else: + join = join + f"JOIN player_matches AS player_matches{i}\n" + join = join + f"ON player_matches.match_id = player_matches{i}.match_id\n" + i = i+1 + join = join + f"JOIN matches\n" + join = join + f"ON player_matches.match_id = matches.match_id\n" + join = join + f"JOIN leagues using(leagueid)\n" + return join + + def where(*heroes): + """generates where statement for certain num of heroes""" + i = 0 + where = f"WHERE" + for data in heroes: + for hero in data: + if i == 0: + where = where + f" player_matches.hero_id = {get_hero_id(hero)}\n" + i = i+1 + else: + where = where + f"AND player_matches{i}.hero_id = {get_hero_id(hero)}\n" + i = i+1 + + #probably need to pass in a timestamp or something at some point for the following + where = where + f"AND matches.start_time >= extract(epoch FROM timestamp '2018-10-01T06:53:44.537Z')\n" + return where + + def teams(*heroes): + """adds onto where statement and specifies which team each hero is on, hero1+hero2 =teamA, hero3+hero4 = teamB""" + i = 0 + teams = "" + for data in heroes: + for hero in data: + if i == 0: + i = i+1 + continue + elif i == 1: + teams = teams + f"AND abs(player_matches.player_slot - player_matches1.player_slot) < 6\n" + i = i+1 + elif i == 2: + teams = teams + f"AND abs(player_matches.player_slot - player_matches2.player_slot) > 123\n" + i = i+1 + elif i == 3: + teams = teams + f"AND abs(player_matches2.player_slot - player_matches3.player_slot) < 6\n" + teams = teams + f"AND abs(player_matches1.player_slot - player_matches3.player_slot) > 123\n" + i = i+1 + teams = teams + "ORDER BY matches.match_id NULLS LAST LIMIT 200" + return teams + + return select(heroes) + join(heroes) + where(heroes) + teams(heroes) + + +def make_query(hero1, hero2): query = f"""SELECT player_matches.hero_id AS hero1, player_matches1.hero_id AS hero2, player_matches.player_slot AS ps1, @@ -32,18 +118,21 @@ def make_query(hero1, hero2): ORDER BY matches.match_id NULLS LAST LIMIT 200""" response = requests.get( - f"{API_ROOT}/explorer", params=dict(api_key=secret.OPENDOTA_API_KEY, sql = query) + f"{API_ROOT}/explorer", params=dict(api_key=secret.OPENDOTA_API_KEY, sql=query) ) return response.json() - -##returns list of pro match ids played between two opposing heroes -def get_matches(hero1, hero2): + + +def get_matches(hero1, hero2): + + """returns list of pro match ids played between two heroes""" + query_response = make_query(hero1, hero2) - match_list = query_response['rows'] ##list of dictionaries for some reason + match_list = query_response["rows"] #list of dictionaries for some reason result = [] - for match in match_list: - result.append(match['match_id']) + for match in match_list: + return [match['match_id'] for match in match_list] return result @@ -81,7 +170,4 @@ def load_hero_list(): if __name__ == "__main__": - main() - - - + main() \ No newline at end of file diff --git a/tests/test_find_hero.py b/tests/test_find_hero.py index 8f49869..b17ef11 100644 --- a/tests/test_find_hero.py +++ b/tests/test_find_hero.py @@ -11,9 +11,15 @@ def test_hero_id(): juggernaut_id = opendota.get_hero_id("Juggernaut") assert juggernaut_id == 8 -def test_query(): + +def test_query(): print(opendota.make_query("Bloodseeker", "Crystal Maiden")) - + assert type(opendota.make_query("Bloodseeker", "Crystal Maiden")) is dict + + +def test_get_matches(): + assert type(opendota.get_matches("Bloodseeker", "Crystal Maiden")) is list + -def test_get_matches(): - print(opendota.get_matches("Bloodseeker", "Crystal Maiden")) \ No newline at end of file +def test_generate_query(): + print(opendota.generate_query("Bloodseeker", "Crystal Maiden", "Juggernaut", "Oracle")) \ No newline at end of file