这个程序将要求用户插入召唤师和基础,从最后的20场比赛,它将提供avg的统计,看看是否好,在过去的20场比赛中获胜(一个简单的评分系统)。
import requests
from getId import idcollect
from games import GAME
from wins import win_calc
#Key for riot API
Key = '**********************'
#ASKING USER FOR SUMMONER NAME
summonerName = input('Enter summoner name:')
#Objects
ids=idcollect()
game=GAME()
wins=win_calc()
#Collecting the acc id of summoner name
accId=ids.ID_collected(summonerName,Key)
#Collecting game id lists
game_list=[]
game_list=game.find_game_ids(accId,Key)
#Collecting wins list
win_list=[]
win_list=game.game_data(game_list,Key,summonerName)
#Calcuate whether the summoner is good or not
wins.is_dis_mane_good(win_list)
import requests
class GAME:
def find_game_ids(self,accId,key):
i=0
GAMEID = []
Idgame=20
url_match_list=('https://na1.api.riotgames.com/lol/match/v4/matchlists/by-account/'+(accId)+'?queue=420&endIndex=20&api_key='+(key))
response2=requests.get(url_match_list)
#Adding 20 games into the list
while Idgame>0:
GAMEID.append('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(response2.json()['matches'][i]['gameId'])+'?api_key='+(key))
i=i+1
Idgame=Idgame-1
return GAMEID
def game_data(self,game_list,key,sumName):
wins=[]
deaths=[]
deaths= []
kills=[]
assists=[]
visions=[]
csTotal=[]
#Finding the data of said summoner in each game id
for urls in game_list:
response=requests.get(urls)
Loop=0
index=0
while Loop<=10:
if response.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
Loop= Loop+1
index=index+1
elif response.json()['participantIdentities'][index]['player']['summonerName']==sumName:
deaths.append(response.json()['participants'][index]['stats']['deaths'])
kills.append(response.json()['participants'][index]['stats']['kills'])
assists.append(response.json()['participants'][index]['stats']['assists'])
visions.append(response.json()['participants'][index]['stats']['visionScore'])
csTotal.append(response.json()['participants'][index]['stats']['totalMinionsKilled'])
wins.append(response.json()['participants'][index]['stats']['win'])
break
#Finding avg of each stat
deaths=sum(deaths)/20
kills=sum(kills)/20
assists=sum(assists)/20
visions=sum(visions)/20
csTotal=sum(csTotal)/20
print('The avg kills is '+str(kills)+'\nThe avg deaths is '+str(deaths)+'\nThe avg assists is '+str(assists)+'\nThe avg visions is '+str(visions)+'\nThe avg cs total is '+str(csTotal))
return wins
import requests
class idcollect:
def ID_collected(self,sumName,key):
#COLLECTING DATA TO BE INSERTING FOR MATCHLIST DATABASE
url=('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/'+(sumName)+'?api_key='+(key))
response=requests.get(url)
accId=(response.json()['accountId'])
return accId
import random
class win_calc:
def is_dis_mane_good(self,winlist):
winlist=sum(winlist)/20
if (winlist<.33):
trash=['DIS MANE STINKS','run while you can','I repeat, YOU ARE NOT WINNING THIS','I predict a fat L','Have fun trying to carry this person','He is a walking trash can','He needs to find a new game','BAD LUCK!!!']
print (random.choice(trash))
elif (winlist>.33 and winlist<=.5):
notgood=['Losing a bit','Not very good','He needs lots of help','Your back might hurt a little','Does not win much']
print (random.choice(notgood))
elif (winlist>.5 and winlist<=.65):
ight=['He is ight','He can win a lil','You guys have a decent chance to win','Serviceable','Should be a dub']
print (random.choice(ight))
elif (winlist>.65):
good=['DUB!','You getting carried','His back gonna hurt a bit','winner winner chicken dinner','Dude wins TOO MUCH','You aint even gotta try','GODLIKE']
print (random.choice(good))
发布于 2020-08-11 16:38:05
find_game_ids
比需要的要复杂得多。您实际上有两个“计数器”,Idgame
和i
。一种是用来放置在字符串中,另一种是限制循环发生的次数,但如果你想一想,它们的值是相同的;正好相反。您不需要Idgame
,因为您只需检查是否i < 20
。您也不需要手动管理i
。range
是用于用例的,具体如下所示:
def find_game_ids(self, accId, key):
game_id = []
url_match_list = f"https://na1.api.riotgames.com/lol/match/v4/matchlists/by-account/{accId}?queue=420&endIndex=20&api_key={key}"
response2 = requests.get(url_match_list)
for i in range(20):
game_id.append(f"https://na1.api.riotgames.com/lol/match/v4/matches/{response2.json()['matches'][i]['gameId']}?api_key={key}"
return game_id
i
这里将是从0
到19
的所有数字。我还建议在其他地方创建一个变量来保存20
并调用N_GAMES
或其他什么东西。您似乎在多个点使用该20
。如果你在一个地方改变它,而忘了在其他地方改变它,你可能会有一个讨厌的错误。
我还改变了其他事情:
+
将字符串相加在一起,而是将其更改为使用f- string (请注意引号之前的f
)。它允许您使用{variable_name}
语法将变量直接放入字符串中。不过,这一点还可以进一步改进。如果您在这里迭代创建一个列表,列表理解有时会更清晰:
def find_game_ids(self, accId, key):
url_match_list = f"https://na1.api.riotgames.com/lol/match/v4/matchlists/by-account/{accId}?queue=420&endIndex=20&api_key={key}"
response2 = requests.get(url_match_list)
return [f"https://na1.api.riotgames.com/lol/match/v4/matches/{response2.json()['matches'][i]['gameId']}?api_key={key}"
for i in range(20)]
在每种情况下,主要的可读性问题都来自于字符串的长度。您可能希望将它拆分成多行,或者使用另一个函数在函数之外生成它。
在game_data
中,您要反复调用response.json()
。纵观这种方法的来源,它似乎不会进行任何缓存。这意味着每次对.json
的调用都将对数据进行解析,这是浪费CPU时间。将其保存到一个变量中一次,并根据需要使用它:
def game_data(self, game_list, key, sumName):
. . .
for urls in game_list:
response = requests.get(urls)
resp_json = response.json() # Save it to use it again later
Loop = 0
index = 0
while Loop <= 10:
if resp_json['participantIdentities'][index]['player']['summonerName'] != sumName:
Loop = Loop + 1
index = index + 1
elif resp_json['participantIdentities'][index]['player']['summonerName'] == sumName:
deaths.append(resp_json['participants'][index]['stats']['deaths'])
kills.append(resp_json['participants'][index]['stats']['kills'])
assists.append(resp_json['participants'][index]['stats']['assists'])
visions.append(resp_json['participants'][index]['stats']['visionScore'])
csTotal.append(resp_json['participants'][index]['stats']['totalMinionsKilled'])
wins.append(resp_json['participants'][index]['stats']['win'])
. . .
这不仅缩短了时间,还使以后更容易将某些预处理添加到数据中,而且还具有更快的潜力,因为您不会一次又一次地进行相同的处理。
#Finding avg of each stat
deaths=sum(deaths)/20
kills=sum(kills)/20
assists=sum(assists)/20
visions=sum(visions)/20
csTotal=sum(csTotal)/20
就像我说的,您在多个地方使用20
。如果你以后想改变这个号码呢?找出每一个相关的20
并将其更新到新的值是不好玩的。
将该数字存储一次,并使用该变量:
# Top of file by imports
N_GAMES = 20
. . .
# The for-loop in the updated find_game_ids
for i in range(N_GAMES):
. . .
# At the bottom of game_data
deaths=sum(deaths)/N_GAMES
kills=sum(kills)/N_GAMES
assists=sum(assists)/N_GAMES
visions=sum(visions)/N_GAMES
csTotal=sum(csTotal)/N_GAMES
对于win_calc
和id_collect
类,有一些值得注意的事情。
首先,它们不应该是课堂。不应该使用类的一个很好的指示是,您从未在任何方法中使用self
。通过在本例中使用类,只需构造一个空对象就可以调用该对象上的方法,在这里您可以这样做:
wins=win_calc()
只为了稍后调用一个方法:
wins.is_dis_mane_good(win_list)
只需使这些类成为简单的函数:
import random
def is_dis_mane_good(winlist):
winlist = sum(winlist) / 20
if (winlist < .33):
trash = ['DIS MANE STINKS', 'run while you can', 'I repeat, YOU ARE NOT WINNING THIS', 'I predict a fat L',
'Have fun trying to carry this person', 'He is a walking trash can', 'He needs to find a new game',
'BAD LUCK!!!']
print(random.choice(trash))
. . .
然后将它们作为普通函数使用:
is_dis_mane_good(win_list)
其次,如果将它们作为类来使用,则名称应该是CapitalCase:WinCalc
和IDCollect
(或者可能是IdCollect
)。
另外,我会重命名is_dis_mane_good
。在程序的输出中使用俚语是一回事,但是将方法命名为模糊的名称并不会给自己或其他代码读者带来任何好处。
在这个函数中,我还要做一些更多的修改:
0
。0.33
比.33
更具可读性。winlist > 0.33 and winlist <= 0.5
可以成为0.33 < winlist <= 0.5
。但是,正如注释中所指出的,实际上可以消除每个检查的一半,因为例如,如果winlist < 0.33
是假的,那么您知道winlist
必须大于0.33
,所以winlist > 0.33
检查是多余的。20
;)你拥有的地方越多,你就越有可能忘记至少更新其中的一个。我会用N_GAMES
代替。print(random.choice(. . .))
调用,然后在底部有一个print
。在这些变化之后,我就剩下这个了:
def competency_message(winlist):
winlist = sum(winlist) / N_GAMES
message_set = []
if winlist < 0.33: # Should be winlist <= 0.33 maybe?
message_set = ['DIS MANE STINKS', 'run while you can', 'I repeat, YOU ARE NOT WINNING THIS', 'I predict a fat L',
'Have fun trying to carry this person', 'He is a walking trash can', 'He needs to find a new game',
'BAD LUCK!!!']
elif winlist <= 0.5:
message_set = ['Losing a bit', 'Not very good', 'He needs lots of help', 'Your back might hurt a little',
'Does not win much']
elif winlist <= 0.65:
message_set = ['He is ight', 'He can win a lil', 'You guys have a decent chance to win', 'Serviceable',
'Should be a dub']
else:
message_set = ['DUB!', 'You getting carried', 'His back gonna hurt a bit', 'winner winner chicken dinner',
'Dude wins TOO MUCH', 'You aint even gotta try', 'GODLIKE']
print(random.choice(message_set))
https://codereview.stackexchange.com/questions/247788
复制