This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
forked from joeyHXD/dota2_watcher_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOTA2.py
301 lines (253 loc) · 11.2 KB
/
DOTA2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import requests
import json
from .DOTA2_dicts import *
from .player import player
import random
import time
# 这里替换成你自己的API
# http://steamcommunity.com/dev/apikey
api_key = ""
#使用代理服务器发起请求
#proxies={'代理类型':'ip:port'}
proxies = { "http": "", "https": ""}
# 异常处理
class DOTA2HTTPError(Exception):
pass
# 根据slot判断队伍, 返回1为天辉, 2为夜魇
def get_team_by_slot(slot):
if slot < 100:
return 1
else:
return 2
def get_last_match_id_by_short_steamID(short_steamID):
# get match_id
url = 'https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/v001/?key={}' \
'&account_id={}&matches_requested=1'.format(api_key, short_steamID)
response = requests.get(url,proxies=proxies)
if response.status_code >= 400:
if response.status_code == 401:
raise DOTA2HTTPError("Unauthorized request 401. Verify API key.")
if response.status_code == 503:
raise DOTA2HTTPError("The server is busy or you exceeded limits. Please wait 30s and try again.")
raise DOTA2HTTPError("Failed to retrieve data: %s. URL: %s" % (response.status_code, url))
match = response.json()
try:
match_id = match["result"]["matches"][0]["match_id"]
except KeyError:
raise DOTA2HTTPError("Response Error: Key Error")
except IndexError:
raise DOTA2HTTPError("Response Error: Index Error")
return match_id
def get_match_detail_info(match_id):
# get match detail
url = 'https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/' \
'?key={}&match_id={}'.format(api_key, match_id)
response = requests.get(url,proxies=proxies)
if response.status_code >= 400:
if response.status_code == 401:
raise DOTA2HTTPError("Unauthorized request 401. Verify API key.")
if response.status_code == 503:
raise DOTA2HTTPError("The server is busy or you exceeded limits. Please wait 30s and try again.")
raise DOTA2HTTPError("Failed to retrieve data: %s. URL: %s" % (response.status_code, url))
match = response.json()
try:
match_info = match["result"]
except KeyError:
raise DOTA2HTTPError("Response Error: Key Error")
except IndexError:
raise DOTA2HTTPError("Response Error: Index Error")
return match_info
# 接收某局比赛的玩家列表, 生成开黑战报
# 参数为玩家对象列表和比赛ID
def generate_party_message(match_id, player_list: [player]):
try:
match = get_match_detail_info(match_id=match_id)
except DOTA2HTTPError:
return("DOTA2开黑战报生成失败")
# 比赛模式
mode_id = match["game_mode"]
if mode_id in (15, 19): # 各种活动模式不通报
return 0
mode = GAME_MODE[mode_id] if mode_id in GAME_MODE else '未知'
lobby_id = match['lobby_type']
lobby = LOBBY[lobby_id] if lobby_id in LOBBY else '未知'
player_num = len(player_list)
# 更新玩家对象的比赛信息
for i in player_list:
for j in match['players']:
if i.short_steamID == j['account_id']:
i.dota2_kill = j['kills']
i.dota2_death = j['deaths']
i.dota2_assist = j['assists']
i.kda = ((1. * i.dota2_kill + i.dota2_assist) / i.dota2_death) \
if i.dota2_death != 0 else (1. * i.dota2_kill + i.dota2_assist)
i.dota2_team = get_team_by_slot(j['player_slot'])
i.hero = j['hero_id']
i.last_hit = j['last_hits']
i.damage = j['hero_damage']
i.gpm = j['gold_per_min']
i.xpm = j['xp_per_min']
break
# 队伍信息
team = player_list[0].dota2_team
team_damage = 0
team_kills = 0
team_deaths = 0
for i in match['players']:
if get_team_by_slot(i['player_slot']) == team:
team_damage += i['hero_damage']
team_kills += i['kills']
team_deaths += i['deaths']
win = False
if match['radiant_win'] and team == 1:
win = True
elif not match['radiant_win'] and team == 2:
win = True
elif match['radiant_win'] and team == 2:
win = False
elif not match['radiant_win'] and team == 1:
win = False
nicknames = ''
if player_num >= 3:
for i in range(player_num - 1):
nicknames += player_list[i].nickname
nicknames += ', '
else:
nicknames += player_list[0].nickname
nicknames += '和'
nicknames += player_list[player_num - 1].nickname
top_kda = 0
for i in player_list:
if i.kda > top_kda:
top_kda = i.kda
if (win and top_kda > 10) or (not win and top_kda > 6):
postive = True
elif (win and top_kda < 4) or (not win and top_kda < 1):
postive = False
else:
if random.randint(0, 1) == 0:
postive = True
else:
postive = False
print_str = ''
if win and postive:
print_str += random.choice(WIN_POSTIVE_PARTY).format(nicknames) + '\n'
elif win and not postive:
print_str += random.choice(WIN_NEGATIVE_PARTY).format(nicknames) + '\n'
elif not win and postive:
print_str += random.choice(LOSE_POSTIVE_PARTY).format(nicknames) + '\n'
else:
print_str += random.choice(LOSE_NEGATIVE_PARTY).format(nicknames) + '\n'
start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(match['start_time']))
duration = match['duration']
print_str += "开始时间: {}\n".format(start_time)
print_str += "持续时间: {:.0f}分{:.0f}秒\n".format(duration / 60, duration % 60)
print_str += '游戏模式: [{}/{}]\n'.format(mode, lobby)
for i in player_list:
nickname = i.nickname
#以后出新英雄就把编号名字加进dict里,这序号貌似和官网的不一样
hero = HEROES_LIST_CHINESE[i.hero] if i.hero in HEROES_LIST_CHINESE else f"{i.hero}"
#hero = HEROES_LIST_CHINESE[i.hero] if i.hero in HEROES_LIST_CHINESE else '不知道什么鬼'
kda = i.kda
last_hits = i.last_hit
damage = i.damage
kills, deaths, assists = i.dota2_kill, i.dota2_death, i.dota2_assist
gpm, xpm = i.gpm, i.xpm
damage_rate = 0 if team_damage == 0 else (100 * (float(damage) / team_damage))
participation = 0 if team_kills == 0 else (100 * float(kills + assists) / team_kills)
deaths_rate = 0 if team_deaths == 0 else (100 * float(deaths) / team_deaths)
print_str += "{}使用{}, \nKDA: {:.2f}[{}/{}/{}], \nGPM/XPM: {}/{}, " \
"\n补刀数: {}, \n总伤害: {}({:.2f}%), \n参战率: {:.2f}%, \n参葬率: {:.2f}%\n" \
.format(nickname, hero, kda, kills, deaths, assists, gpm, xpm, last_hits,
damage, damage_rate, participation, deaths_rate)
print_str += "战绩详情: https://cn.dotabuff.com/matches/{}".format(match_id)
return(print_str)
# 接收某局比赛的玩家信息, 生成单排战报
# 参数为玩家对象
def generate_solo_message(match_id, player_obj: player):
try:
match = get_match_detail_info(match_id=match_id)
except DOTA2HTTPError:
return("DOTA2单排战报生成失败")
# 比赛模式
mode_id = match["game_mode"]
if mode_id in (15, 19): # 各种活动模式不通报
return 0
mode = GAME_MODE[mode_id] if mode_id in GAME_MODE else '未知'
lobby_id = match['lobby_type']
lobby = LOBBY[lobby_id] if lobby_id in LOBBY else '未知'
# 更新玩家对象的比赛信息
for j in match['players']:
if player_obj.short_steamID == j['account_id']:
player_obj.dota2_kill = j['kills']
player_obj.dota2_death = j['deaths']
player_obj.dota2_assist = j['assists']
player_obj.kda = ((1. * player_obj.dota2_kill + player_obj.dota2_assist) / player_obj.dota2_death) \
if player_obj.dota2_death != 0 else (1. * player_obj.dota2_kill + player_obj.dota2_assist)
player_obj.dota2_team = get_team_by_slot(j['player_slot'])
player_obj.hero = j['hero_id']
player_obj.last_hit = j['last_hits']
player_obj.damage = j['hero_damage']
player_obj.gpm = j['gold_per_min']
player_obj.xpm = j['xp_per_min']
break
# 队伍信息
team = player_obj.dota2_team
team_damage = 0
team_kills = 0
team_deaths = 0
for i in match['players']:
if get_team_by_slot(i['player_slot']) == team:
team_damage += i['hero_damage']
team_kills += i['kills']
team_deaths += i['deaths']
win = False
if match['radiant_win'] and team == 1:
win = True
elif not match['radiant_win'] and team == 2:
win = True
elif match['radiant_win'] and team == 2:
win = False
elif not match['radiant_win'] and team == 1:
win = False
if (win and player_obj.kda > 10) or (not win and player_obj.kda > 6):
postive = True
elif (win and player_obj.kda < 4) or (not win and player_obj.kda < 1):
postive = False
else:
if random.randint(0, 1) == 0:
postive = True
else:
postive = False
print_str = ''
if win and postive:
print_str += random.choice(WIN_POSTIVE_PARTY).format(player_obj.nickname) + '\n'
elif win and not postive:
print_str += random.choice(WIN_NEGATIVE_PARTY).format(player_obj.nickname) + '\n'
elif not win and postive:
print_str += random.choice(LOSE_POSTIVE_PARTY).format(player_obj.nickname) + '\n'
else:
print_str += random.choice(LOSE_NEGATIVE_PARTY).format(player_obj.nickname) + '\n'
start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(match['start_time']))
duration = match['duration']
print_str += "开始时间: {}\n".format(start_time)
print_str += "持续时间: {:.0f}分{:.0f}秒\n".format(duration // 60, duration % 60)
print_str += '游戏模式: [{}/{}]\n'.format(mode, lobby)
nickname = player_obj.nickname
hero = HEROES_LIST_CHINESE[player_obj.hero] if player_obj.hero in HEROES_LIST_CHINESE else f'{player_obj.hero}'
#hero = HEROES_LIST_CHINESE[player_obj.hero] if player_obj.hero in HEROES_LIST_CHINESE else '不知道什么鬼'
kda = player_obj.kda
last_hits = player_obj.last_hit
damage = player_obj.damage
kills, deaths, assists = player_obj.dota2_kill, player_obj.dota2_death, player_obj.dota2_assist
gpm, xpm = player_obj.gpm, player_obj.xpm
damage_rate = 0 if team_damage == 0 else (100 * (float(damage) / team_damage))
participation = 0 if team_kills == 0 else (100 * float(kills + assists) / team_kills)
deaths_rate = 0 if team_deaths == 0 else (100 * float(deaths) / team_deaths)
print_str += "{}使用{}, \nKDA: {:.2f}[{}/{}/{}], \nGPM/XPM: {}/{}, " \
"\n补刀数: {}, \n总伤害: {}({:.2f}%), \n参战率: {:.2f}%, \n参葬率: {:.2f}%\n" \
.format(nickname, hero, kda, kills, deaths, assists, gpm, xpm, last_hits,
damage, damage_rate, participation, deaths_rate)
print_str += "战绩详情: https://cn.dotabuff.com/matches/{}".format(match_id)
# print(print_str)
return(print_str)