From c3b6b1794da75ded9377cb8cba9f0375cc33ad32 Mon Sep 17 00:00:00 2001 From: Kaan Date: Tue, 9 Aug 2022 01:57:52 +0200 Subject: [PATCH] Refactored everything, also Fixed list bugs where it would try to show 700 entries --- __pycache__/api.cpython-310.pyc | Bin 834 -> 848 bytes .../get_members_2v2_elo.cpython-310.pyc | Bin 0 -> 1824 bytes __pycache__/order_teamname.cpython-310.pyc | Bin 876 -> 890 bytes __pycache__/send_embeds.cpython-310.pyc | Bin 0 -> 860 bytes __pycache__/sort_players_elo.cpython-310.pyc | Bin 0 -> 1246 bytes api.py | 4 +- get_members_2v2_elo.py | 92 +++++ main.py | 385 +----------------- order_teamname.py | 53 +-- send_embeds.py | 36 ++ sort_players_elo.py | 43 ++ test_data.json | 23 ++ 12 files changed, 243 insertions(+), 393 deletions(-) create mode 100644 __pycache__/get_members_2v2_elo.cpython-310.pyc create mode 100644 __pycache__/send_embeds.cpython-310.pyc create mode 100644 __pycache__/sort_players_elo.cpython-310.pyc create mode 100644 get_members_2v2_elo.py create mode 100644 send_embeds.py create mode 100644 sort_players_elo.py create mode 100644 test_data.json diff --git a/__pycache__/api.cpython-310.pyc b/__pycache__/api.cpython-310.pyc index 907beed04251fd224c9b5ab4c7cba5a2c95b366e..dd6bb6b7cd38ad5ff61d7f2a2d2965dab09a9b04 100644 GIT binary patch delta 80 zcmX@ac7csIpO=@50SKylKPKJT$lJ=u!IhR;lAIBrJb60f0mi7w5lnJok`SSSoW#o1 dqWGf3yzJDJ_~OZpOmd8TlNT^014$NUI{*)V8Fl~w delta 66 zcmcb>c8HBPpO=@50SG$2ZA&`0k++qRl|4PR#Ch^8#siGECPy>Laq|KB0Xd14sYSt) S`e23He&~N-R663S;*6DlooC>id3d4v7*u+r6x{In-`M8_?u}T@{d37 z?sR|6Vx4u*q9}dR%|;X5kGnb^kFxHI3v)6`yDt%AI?&zY*z|_+@DSR)XK$bN;$*Zv zz9@W>lg$c+S_cul$))4ncwBN#sDF}~gn)e8%u+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%e5L$g zGG|=LMz7QAG`r58({$RsUZ>|Y^HA?mzti7yT28m!Yz$}1H@={|t$M=8kqPBX2WJmI zKbS?CGf2$muXgT6fd!eXWy24g0(}9I-$1>`EAt#Fe1YFuD{E>{==}qJ1}AxnFj`sX zM41XQmir9KOauHsg#nca(9)HLjR*WX-+K zIGE{wb+=NEqG2Yr(&1%#VKuC+D4d7t#FjAAd#yCU=EscY0p}6}mOJg-c5>UT6?J*c z)F_zvoU=%8FJ#EaYu8E^V$8G&TT@+?e=lMb&iyePMIoPQD-j`AnnaB2GG#IlVWPsA z*KBQPT{yXI=eCpEt|rH^(uKV6CnRn`o4@)`_Pv*q3F*yzpYM7yUWkBsf+ex^j+SZ? zbMJ-EXFL>M+SDj3BJ0UgmpA+Z>?QAmGTxO)mE0snTm`v7s(4dZu=!iu0qn45i7l{h tnMLu{>D`KAay=T6KB)}OI^9sWA)$+dc}y1(dz?;NrZ;0E6Q^H=`~k=D+AaV9 literal 0 HcmV?d00001 diff --git a/__pycache__/sort_players_elo.cpython-310.pyc b/__pycache__/sort_players_elo.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d20ae21033562b06aa65f0de381487247ce7e1d6 GIT binary patch literal 1246 zcmZ`&OK%e~5VpPEM;?6ut)NheJs{B>T0uDg6{T}j@-luDKtg}%Hi=9RAAr)I00?}w}4aNRAFW-qO2T` zVc$k$NV10t8;%3V1>7+* z^5f)d1HwPM?xJ~&Gk;C>L+M{QJ@JFC%7Q350MCXiOxoH`VR)3bV2HF8@mO1yC2D5 zniQKg6KHlr&rK_z)%^>JyYSKeZ0~HfK1k?eYY+tSdP^pm=<=4}X(C(i4pcvhTYEt~ zh(iIGQ0-aU&HuQO9_Yn?jpm<}AAvZ;!_&P)D502Q&#D8RZh{G(Kk+QD)X8^1F+HW= zLr)h;{n%Tiq>3G|l$2KSbW bestCurrent: + bestCurrent = rating + bestPeak = peak + bestCurrentTeam = team["teamname"] + + # ADD ALL VALUES TO ARRAYS + if bestCurrentTeam.startswith("bestCurrentTeam is undefined"): + bestCurrent = -1 + bestPeak = -1 + bestCurrentTeam = player["name"] + print(str(num) + ': ' + bestCurrentTeam) + print("current: " + str(bestCurrent)) + print("peak: " + str(bestPeak)) + print(' ') + clan_2v2_teamnames.append(bestCurrentTeam) + clan_current_2v2_ratings.append(bestCurrent) + clan_peak_2v2_ratings.append(bestPeak) + + except: + currentResult = "**" + \ + str(num) + ". " + player["name"] + \ + "**: **current:**" + " -1" + " **peak:**" + " -1" + + clan_2v2_teamnames.append(player["name"]) + clan_current_2v2_ratings.append(-1) + clan_peak_2v2_ratings.append(-1) + + print(currentResult) + return_values = [] + return_values.append(clan_2v2_teamnames) + return_values.append(clan_current_2v2_ratings) + return_values.append(clan_peak_2v2_ratings) + return_values.append(clan) + return return_values diff --git a/main.py b/main.py index 56095af1..405db2f0 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,19 @@ import os from pydoc import classname +import re from secrets import get_keys import discord from discord.ext import commands from discord.ext import tasks import time import json -from api import getClan -from api import getPlayerStats +from api import fetch_clan +from api import fetch_player_ranked_stats from keep_alive import keep_alive from order_teamname import ArrangeTeamName - -skyward_image_link = 'https://cdn.discordapp.com/attachments/841405262023884820/841405879496212530/Skyward-1.png' -elo_channel = 976552050953437194 -test_channel = 973594560368373820 -using_channel = elo_channel -first_time = True -clan = [] -clan_members = [] -clan_current_2v2_ratings = [] -clan_peak_2v2_ratings = [] -clan_2v2_teamnames = [] -clan_current_2v2_ratings_sorted = [] -clan_peak_2v2_ratings_sorted = [] -clan_2v2_teamnames_sorted = [] +from send_embeds import send_embeds +from get_members_2v2_elo import get_members_2v2_elo +from sort_players_elo import sort_players_elo bot = commands.Bot(command_prefix=['r', 'R']) @@ -34,336 +24,21 @@ async def on_ready(): log_channel = bot.get_channel(973594560368373820) await log_channel.send("I'm back online!") while True: - await poop() + await main() print('start waiting') time.sleep(2500) print('done waiting') -def get_clan(): - global clan - try: - clan = json.loads(getClan().content) # request - except: - clan = [] - - -def get_clan_members(): - global clan_members - try: - clan_members = clan['clan'] - # print(clan_members) - clan_members_test_2members = [{ - "brawlhalla_id": 6433263, - "name": "Zwergennest", - "rank": "Member", - "join_date": 1618333318, - "xp": 232764 - }, { - "brawlhalla_id": 56793941, - "name": "Gød-sñîpêr", - "rank": "Member", - "join_date": 1659878282, - "xp": 228 - }] - clan_members_test_3members = [ - { - "brawlhalla_id": 3764805, - "name": "Kyouno Natsumi <3", - "rank": "Officer", - "join_date": 1618497345, - "xp": 126456 - }, - { - "brawlhalla_id": 49936771, - "name": "Hunter", - "rank": "Officer", - "join_date": 1618949986, - "xp": 559758 - }, - { - "brawlhalla_id": 7364605, - "name": "100 ping", - "rank": "Officer", - "join_date": 1629141768, - "xp": 148940 - }, { - "brawlhalla_id": 56793941, - "name": "Gød-sñîpêr", - "rank": "Member", - "join_date": 1659878282, - "xp": 228 - }] - clan_members_test_26members = [ - { - "brawlhalla_id": 6433263, - "name": "Zwergennest", - "rank": "Member", - "join_date": 1618333318, - "xp": 232764 - }, - { - "brawlhalla_id": 3764805, - "name": "Kyouno Natsumi <3", - "rank": "Officer", - "join_date": 1618497345, - "xp": 126456 - }, - { - "brawlhalla_id": 49936771, - "name": "Hunter", - "rank": "Officer", - "join_date": 1618949986, - "xp": 559758 - }, - { - "brawlhalla_id": 7364605, - "name": "100 ping", - "rank": "Officer", - "join_date": 1629141768, - "xp": 148940 - }, - { - "brawlhalla_id": 9316238, - "name": "Nymphe", - "rank": "Member", - "join_date": 1630749984, - "xp": 196595 - }, - { - "brawlhalla_id": 71428893, - "name": "zDisigma", - "rank": "Member", - "join_date": 1631063079, - "xp": 246692 - }, - { - "brawlhalla_id": 6202261, - "name": "Linox", - "rank": "Member", - "join_date": 1633609381, - "xp": 221639 - }, - { - "brawlhalla_id": 5708299, - "name": "55p", - "rank": "Member", - "join_date": 1638807175, - "xp": 96895 - }, - { - "brawlhalla_id": 79588993, - "name": "Helsem", - "rank": "Member", - "join_date": 1642035178, - "xp": 96609 - }, - { - "brawlhalla_id": 8615459, - "name": "Hidetsu?!", - "rank": "Member", - "join_date": 1644348435, - "xp": 46956 - }, - { - "brawlhalla_id": 3794093, - "name": "MrEmpanadilla19", - "rank": "Member", - "join_date": 1644749675, - "xp": 186442 - }, - { - "brawlhalla_id": 35913608, - "name": "rufus", - "rank": "Member", - "join_date": 1645191275, - "xp": 86445 - }, - { - "brawlhalla_id": 7940781, - "name": "Solarson", - "rank": "Officer", - "join_date": 1645883930, - "xp": 110952 - }, - { - "brawlhalla_id": 43227947, - "name": "Mogzy", - "rank": "Member", - "join_date": 1646239755, - "xp": 56374 - }, - { - "brawlhalla_id": 26358569, - "name": "Uncle Timmy", - "rank": "Member", - "join_date": 1646239982, - "xp": 64960 - }, - { - "brawlhalla_id": 5111230, - "name": "WE CAN'T PROMISE ANYTHING", - "rank": "Member", - "join_date": 1646913914, - "xp": 65003 - }, - { - "brawlhalla_id": 74662063, - "name": "Roddd", - "rank": "Leader", - "join_date": 1647024356, - "xp": 27000 - }, - { - "brawlhalla_id": 58366728, - "name": "DanHasCorona", - "rank": "Member", - "join_date": 1648231212, - "xp": 124323 - }, - { - "brawlhalla_id": 70022787, - "name": "WE WILL THINK ABOUT IT", - "rank": "Officer", - "join_date": 1648927059, - "xp": 137165 - }, - { - "brawlhalla_id": 84499165, - "name": "Kaizaar", - "rank": "Member", - "join_date": 1649621274, - "xp": 62369 - }, - { - "brawlhalla_id": 2658232, - "name": "gonconist", - "rank": "Member", - "join_date": 1650631486, - "xp": 14671 - }, - { - "brawlhalla_id": 61355963, - "name": "abzyo", - "rank": "Member", - "join_date": 1651247127, - "xp": 12692 - }, - { - "brawlhalla_id": 9201392, - "name": "Lowkey", - "rank": "Member", - "join_date": 1652175951, - "xp": 118006 - }, - { - "brawlhalla_id": 40660695, - "name": "twitch.tv/friendlyherobrn", - "rank": "Member", - "join_date": 1653504781, - "xp": 56326 - }, - { - "brawlhalla_id": 3221385, - "name": "El Eldad", - "rank": "Member", - "join_date": 1653506496, - "xp": 31008 - }, - { - "brawlhalla_id": 47231784, - "name": "Fochino", - "rank": "Member", - "join_date": 1653659995, - "xp": 25528 - }, - ] - clan_members = clan_members_test_3members - except: - clan_members = [] - - -def Save2v2Elo(): - num = 0 - for player in clan_members: - num += 1 - try: - all_my_2v2_teams = json.loads( - getPlayerStats( - player["brawlhalla_id"]).content)["2v2"] # request - - # FIND BEST TEAM CURRENT ELO - bestCurrentTeam = "bestCurrentTeam is undefined" - bestCurrent = -1 - bestPeak = -1 - - for team in all_my_2v2_teams: - rating = team["rating"] - peak = team["peak_rating"] - if rating > bestCurrent: - bestCurrent = rating - bestPeak = peak - bestCurrentTeam = team["teamname"] - - # ADD ALL VALUES TO ARRAYS - if bestCurrentTeam.startswith("bestCurrentTeam is undefined"): - bestCurrent = -1 - bestPeak = -1 - bestCurrentTeam = player["name"] - print(str(num) + ': ' + bestCurrentTeam) - print("current: " + str(bestCurrent)) - print("peak: " + str(bestPeak)) - print(' ') - clan_2v2_teamnames.append(bestCurrentTeam) - clan_current_2v2_ratings.append(bestCurrent) - clan_peak_2v2_ratings.append(bestPeak) - - except: - currentResult = "**" + \ - str(num) + ". " + player["name"] + \ - "**: **current:**" + " -1" + " **peak:**" + " -1" - - clan_2v2_teamnames.append(player["name"]) - clan_current_2v2_ratings.append(-1) - clan_peak_2v2_ratings.append(-1) +async def main(): + # get teams, current and peak elo's sorted + return_values = sort_players_elo() + clan_2v2_teamnames_sorted = return_values[0] + clan_current_2v2_ratings_sorted = return_values[1] + clan_peak_2v2_ratings_sorted = return_values[2] + clan = return_values[3] - print(currentResult) - time.sleep(1) - - -def SortPlayersElo(): - print('start sorting players elo...') - while len(clan_current_2v2_ratings) > 0: - index = -1 - bestIndex = 0 - highestCurrentRating = -2 - for (current, peak, teamCurrent) in zip(clan_current_2v2_ratings, - clan_peak_2v2_ratings, - clan_2v2_teamnames): - index += 1 - if current > highestCurrentRating: - highestCurrentRating = current - bestIndex = index - clan_current_2v2_ratings_sorted.append( - clan_current_2v2_ratings.pop(bestIndex)) - clan_peak_2v2_ratings_sorted.append( - clan_peak_2v2_ratings.pop(bestIndex)) - clan_2v2_teamnames_sorted.append(clan_2v2_teamnames.pop(bestIndex)) - print('done sorting') - - -async def poop(): - get_clan() - global clan - get_clan_members() - global clan_members - Save2v2Elo() - SortPlayersElo() - global first_time - global msg1 - global msg2 - global msg3 - global msg4 + # prepare embeds embed2 = discord.Embed(title="Skyward", description="Total Exp: " + str(clan['clan_xp']), color=0x289fb4) @@ -372,9 +47,6 @@ async def poop(): embed5 = discord.Embed(description="", color=0x289fb4) global num num = 1 - print("current: " + str(len(clan_current_2v2_ratings_sorted))) - print("peak: " + str(len(clan_peak_2v2_ratings_sorted))) - print("names: " + str(len(clan_2v2_teamnames_sorted))) for (current, peak, currentTeam) in zip(clan_current_2v2_ratings_sorted, clan_peak_2v2_ratings_sorted, clan_2v2_teamnames_sorted): @@ -394,29 +66,12 @@ async def poop(): await send_embeds(embed2=embed2, embed3=embed3, embed4=embed4, - embed5=embed5) - - -async def send_embeds(embed2, embed3, embed4, embed5): - channel = bot.get_channel(using_channel) - await channel.purge(limit=10) - - await channel.send(skyward_image_link) # send 1 - print("sent 1") - - await channel.send(embed=embed2) # send 2 - print("sent 2") - - await channel.send(embed=embed3) # send 3 - print('sent 3') - - if len(embed4.description) > 1: - await channel.send(embed=embed4) # send 4 - print('sent 4') + embed5=embed5, bot=bot) - if len(embed5.description) > 1: - await channel.send(embed=embed5) # send 5 - print('sent 5') + # clear embeds for some reason + clan_current_2v2_ratings_sorted.clear() + clan_peak_2v2_ratings_sorted.clear() + clan_2v2_teamnames_sorted.clear() keep_alive() diff --git a/order_teamname.py b/order_teamname.py index 160b4dda..47fffec2 100644 --- a/order_teamname.py +++ b/order_teamname.py @@ -1,31 +1,32 @@ -from api import getPlayerStats -from api import getClan +from api import fetch_player_ranked_stats +from api import fetch_clan import json + def ArrangeTeamName(teamName): - player1_ID = teamName["brawlhalla_id_one"] - player2_ID = teamName["brawlhalla_id_two"] - try: - player1 = json.loads(getPlayerStats(player1_ID).content) - player2 = json.loads(getPlayerStats(player2_ID).content) - print('p1: ' + player1['name']) - print('p2: ' + player2['name']) + player1_ID = teamName["brawlhalla_id_one"] + player2_ID = teamName["brawlhalla_id_two"] + try: + player1 = json.loads(fetch_player_ranked_stats(player1_ID).content) + player2 = json.loads(fetch_player_ranked_stats(player2_ID).content) + print('p1: ' + player1['name']) + print('p2: ' + player2['name']) - inSkyward = False - json_object = json.loads(getClan().content) - members = json_object['clan'] - for member in members: - if player1['name'] == member['name']: - inSkyward = True + inSkyward = False + json_object = json.loads(fetch_clan().content) + members = json_object['clan'] + for member in members: + if player1['name'] == member['name']: + inSkyward = True - print('ordered') - if inSkyward == True: - res = player1['name'] + "+" + player2['name'] - else: - res = player2['name'] + "+" + player1['name'] - print(res) - return res - except: - print('not ordered') - print(teamName['teamname']) - return teamName['teamname'] + print('ordered') + if inSkyward == True: + res = player1['name'] + "+" + player2['name'] + else: + res = player2['name'] + "+" + player1['name'] + print(res) + return res + except: + print('not ordered') + print(teamName['teamname']) + return teamName['teamname'] diff --git a/send_embeds.py b/send_embeds.py new file mode 100644 index 00000000..f2a5cd27 --- /dev/null +++ b/send_embeds.py @@ -0,0 +1,36 @@ +skyward_image_link = 'https://cdn.discordapp.com/attachments/841405262023884820/841405879496212530/Skyward-1.png' +elo_channel = 976552050953437194 +test_channel = 973594560368373820 +using_channel = elo_channel + + +async def send_embeds(embed2, embed3, embed4, embed5, bot): + + # Get channel + channel = bot.get_channel(using_channel) + + # Remove last 10 messages in channel + await channel.purge(limit=10) + + # Send Embed 1 + await channel.send(skyward_image_link) + print("sent 1") + + # Send Embed 2 + await channel.send(embed=embed2) + print("sent 2") + + # Send Embed 3 (if possible) + if len(embed3.description) > 1: + await channel.send(embed=embed3) + print('sent 3') + + # Send Embed 4 (if possible) + if len(embed4.description) > 1: + await channel.send(embed=embed4) + print('sent 4') + + # Send Embed 5 (if possible) + if len(embed5.description) > 1: + await channel.send(embed=embed5) + print('sent 5') diff --git a/sort_players_elo.py b/sort_players_elo.py new file mode 100644 index 00000000..5d4a9a3e --- /dev/null +++ b/sort_players_elo.py @@ -0,0 +1,43 @@ +from pickletools import read_unicodestring1 +from get_members_2v2_elo import get_members_2v2_elo + + +def sort_players_elo(): + # get 2v2 teams, current and peak elo's + returned_values = get_members_2v2_elo() + clan_2v2_teamnames = returned_values[0] + clan_current_2v2_ratings = returned_values[1] + clan_peak_2v2_ratings = returned_values[2] + clan = returned_values[3] + + clan_current_2v2_ratings_sorted = [] + clan_peak_2v2_ratings_sorted = [] + clan_2v2_teamnames_sorted = [] + + # sort players elo + print('start sorting players elo...') + while len(clan_current_2v2_ratings) > 0: + index = -1 + bestIndex = 0 + highestCurrentRating = -2 + for (current, peak, teamCurrent) in zip(clan_current_2v2_ratings, + clan_peak_2v2_ratings, + clan_2v2_teamnames): + index += 1 + if current > highestCurrentRating: + highestCurrentRating = current + bestIndex = index + clan_current_2v2_ratings_sorted.append( + clan_current_2v2_ratings.pop(bestIndex)) + clan_peak_2v2_ratings_sorted.append( + clan_peak_2v2_ratings.pop(bestIndex)) + clan_2v2_teamnames_sorted.append(clan_2v2_teamnames.pop(bestIndex)) + print('done sorting') + + # Return all values + return_values = [] + return_values.append(clan_2v2_teamnames_sorted) + return_values.append(clan_current_2v2_ratings_sorted) + return_values.append(clan_peak_2v2_ratings_sorted) + return_values.append(clan) + return return_values diff --git a/test_data.json b/test_data.json new file mode 100644 index 00000000..72b6768e --- /dev/null +++ b/test_data.json @@ -0,0 +1,23 @@ +[ + { + "brawlhalla_id": 6433263, + "name": "Zwergennest", + "rank": "Member", + "join_date": 1618333318, + "xp": 232800 + }, + { + "brawlhalla_id": 3764805, + "name": "Kyouno Natsumi <3", + "rank": "Officer", + "join_date": 1618497345, + "xp": 127110 + }, + { + "brawlhalla_id": 49936771, + "name": "Hunter", + "rank": "Officer", + "join_date": 1618949986, + "xp": 562464 + } +] \ No newline at end of file