1
- #!/usr/bin/env python
2
- # coding: utf-8
3
-
4
- # In[92]:
1
+ # %%
5
2
from pymongo import MongoClient
6
3
import json
7
4
import dota2api
11
8
# %%
12
9
api = dota2api .Initialise (api_key )
13
10
14
- # In[97]:
15
- # ip = '20.40.184.204'
16
- ip = '104.209.80.106'
17
- client = MongoClient (ip , 27017 )
11
+ # %%
12
+ ip = "10.0.1.4"
13
+ port = 27017
14
+ username = os .environ .get ('MONGO_USER' )
15
+ pwd = os .environ .get ('MONGO_PWD' )
16
+ client = MongoClient (
17
+ ip ,
18
+ port ,
19
+ username = username ,
20
+ password = pwd ,
21
+ authMechanism = 'SCRAM-SHA-1'
22
+ )
18
23
19
- # In[6]:
20
- # query = ("CREATE DATABASE dota7")
21
- # cursor.execute(query)
24
+ # %%
22
25
db = client .dota
23
26
24
- # In[ ]:
25
- # def process_heroes():
26
- # query = ("SELECT * FROM heroes")
27
- # cursor.execute(query)
28
- # global hero_win_rates
29
- # hero_win_rates = []
30
- # h_columns = cursor.column_names
31
- # heroes = cursor.fetchall()
32
- # for h in heroes:
33
- # h = dict(zip(h_columns, h))
34
- # hero_win_rates.append({'id': h['id'], 'name': h['localized_name'], 'matches_won': 0.0, 'matches_played': 0.0})
35
- # match_hero_stmt = ("SELECT * FROM match_hero WHERE hero_id=%s")
36
- # for h in hero_win_rates:
37
- # h['hero_matchup'] = []
38
- # for he in heroes:
39
- # he = dict(zip(h_columns, he))
40
- # h['hero_matchup'].append({'id': he['id'], 'name': he['localized_name'], 'matches_won_against': 0.0, 'matches_played_against': 0.0})
41
- # data = (h['id'],)
42
- # cursor.execute(match_hero_stmt,data)
43
- # mh_columns = cursor.column_names
44
- # for mh in cursor.fetchall():
45
- # mh = dict(zip(mh_columns, mh))
46
-
47
- # matches_stmt = ("SELECT * FROM matches WHERE match_id=%s")
48
- # data = (mh['match_id'],)
49
- # cursor.execute(matches_stmt,data)
50
-
51
- # mat = dict(zip(cursor.column_names, cursor.fetchone()))
52
- # if ((mh['player_slot'] < 128 and mat['radiant_win']) or (mh['player_slot'] >= 128 and not mat['radiant_win'])):
53
- # h['matches_won'] = h['matches_won'] + 1
54
- # h['matches_played'] = h['matches_played'] + 1
55
-
56
- # match_hero_stmt2 = ("SELECT * FROM match_hero WHERE match_id=%s AND NOT hero_id=%s")
57
- # data = (mh['match_id'],mh['hero_id'])
58
- # cursor.execute(match_hero_stmt2,data)
59
-
60
- # for mh2 in cursor.fetchall():
61
- # mh2 = dict(zip(mh_columns, mh2))
62
- # pl = next((pl for pl in h['hero_matchup'] if pl['id']==mh2['hero_id']), None)
63
- # if(pl):
64
- # if(mh['player_slot']<128 and mh2['player_slot']>=128 or mh['player_slot']>=128 and mh2['player_slot']<128):
65
- # pl['matches_played_against'] += 1
66
- # if(mh['player_slot']<128 and mat['radiant_win'] or mh['player_slot']>=128 and not mat['radiant_win']):
67
- # pl['matches_won_against'] += 1
68
- # for h in hero_win_rates:
69
- # h['win_rate'] = None if (h['matches_played']==0) else h['matches_won']/h['matches_played']
70
- # save_wr_stmt = ("""UPDATE heroes
71
- # SET win_rate = %s,
72
- # matches_played = %s
73
- # WHERE id = %s""")
74
- # data = (None if (h['win_rate']==None) else h['win_rate'], h['matches_played'],h['id'])
75
- # cursor.execute(save_wr_stmt,data)
76
- # for pl in h['hero_matchup']:
77
- # pl['win_rate'] = None if (pl['matches_played_against']==0) else pl['matches_won_against']/pl['matches_played_against']
78
- # save_mwr_stmt = ("""UPDATE hero_matchups
79
- # SET win_rate = %s,
80
- # matches_played = %s
81
- # WHERE hero_id = %s AND opponent_id = %s""")
82
- # data = (None if not pl['win_rate'] else pl['win_rate'], pl['matches_played_against'],h['id'],pl['id'])
83
- # cursor.execute(save_mwr_stmt,data)
84
- # print("id: {}, name: {}, wins: {}, total: {}, win rate: {}".format(h['id'],
85
- # h['name'],
86
- # h['matches_won'],
27
+ # %%
87
28
def process_heroes ():
88
29
heroes_table = db .heroes
89
30
match_hero_table = db .match_hero
@@ -192,7 +133,7 @@ def match_hero_query2(match_id, hero_id):
192
133
print ("id: {}, name: {}, wins: {}, total: {}, win rate: {}" .format (h ['_id' ], h ['name' ], h ['matches_won' ], h ['matches_played' ], "-" if (h ['win_rate' ]== None ) else '%.2f' % (100 * h ['win_rate' ])+ "%" ))
193
134
194
135
195
- # In[ ]:
136
+ # %%
196
137
# def generate_hero_matchups():
197
138
# query = ("DELETE FROM hero_matchups")
198
139
# cursor.execute(query)
@@ -209,30 +150,10 @@ def match_hero_query2(match_id, hero_id):
209
150
# data = (hid1, hid2)
210
151
# cursor.execute(create_mwr_stmt, data)
211
152
212
- # In[ ]:
153
+ # %%
213
154
process_heroes ()
214
155
215
- # In[ ]:
216
- # def suggest_1():
217
- # global hero_win_rates
218
- # top_heroes = []
219
- # max_top = 5
220
- # sorted_hero_win_rates = sorted(hero_win_rates, key=lambda k: (-1,-1) if (k['win_rate']==None) else (k['win_rate'],k['matches_played']), reverse=True)
221
- # temp_wr = sorted_hero_win_rates[0]['win_rate']
222
- # i = 0
223
- # while i < max_top or sorted_hero_win_rates[i]['win_rate'] == temp_wr:
224
- # temp_wr = sorted_hero_win_rates[i]['win_rate']
225
- # add_hero = {'id': sorted_hero_win_rates[i]['id'],
226
- # 'matches_played': sorted_hero_win_rates[i]['matches_played'],
227
- # 'matches_won': sorted_hero_win_rates[i]['matches_won'],
228
- # 'name': sorted_hero_win_rates[i]['name'],
229
- # 'win_rate': sorted_hero_win_rates[i]['win_rate']}
230
- # top_heroes.append(add_hero)
231
- # print(add_hero)
232
- # i = i + 1
233
- # return top_heroes
234
-
235
-
156
+ # %%
236
157
def suggest_1 ():
237
158
global hero_win_rates
238
159
top_heroes = []
@@ -252,7 +173,7 @@ def suggest_1():
252
173
i = i + 1
253
174
return top_heroes
254
175
255
- # In[ ]:
176
+ # %%
256
177
def suggest_2 (hero_id1 ):
257
178
global hero_win_rates
258
179
top_heroes = []
@@ -283,5 +204,3 @@ def suggest_2(hero_id1):
283
204
# print(sorted_hero_matchups[i])
284
205
i = i + 1
285
206
return top_heroes
286
-
287
- #%%
0 commit comments