def opentomongo(): db_eng_2 = DBA.DbInfoGenerator('vpgame').info # 连接到本地库 Newclient = pymongo.MongoClient(db_eng_2['host'], 7974) New_db = Newclient['admin'] # 'admin'的账号密码 New_db.authenticate(db_eng_2['user'], db_eng_2['password']) # 连接database'damin' # 'admin'的账号密码 print('success connet the database') # 查询对应的比赛id并且建立list pro_match_info=dota2_api.get_api_json('https://api.opendota.com/api/proMatches?ccdc7024-3890-44dd-b602-ec193dee6f23') dota_match_old = list(New_db.dota_basic_data_2020.find({}, {'_id': 0, 'match_id': 1})) dota_match_new = list(New_db.dota_basic_data_2021.find({}, {'_id': 0, 'match_id': 1})) dota_match = dota_match_old + dota_match_new dota_match = pd.DataFrame(dota_match) # 如果数据库内已经有数据,不插入这部分数据 match_list_haven = [] if len(dota_match ) != 0: match_list_haven = list(dota_match['match_id']) # 查看还需要插入的比赛场次 # 对于以及在数据库的比赛进行筛选 m = 0 for mi in pro_match_info: # print(mi['match_id']) if mi['match_id'] not in match_list_haven: match_info = dota2_api.get_api_json( 'https://api.opendota.com/api/matches/{}?ccdc7024-3890-44dd-b602-ec193dee6f23'.format(mi['match_id'])) print(mi) New_db.dota_basic_data_2021.insert_one(match_info) m = m + 1 print(m) # 将战队的历史数据中每个队员的历史数据插入到对应的数据库中 data1 = list(New_db.dota_basic_data_2021.find( {'match_id': mi['match_id']}, {'_id': 0, 'players': 1})) print('ready to insert') for i in range(len(data1)): if 'players' in data1[i].keys(): for j in range(len(data1[i]['players'])): player_info = data1[i]['players'][j] New_db.players_2021.insert_one(player_info) print('success insert data ready to calculate the elo')
def win_or_defeat(match_id): match_info = get_api_json( 'https://api.opendota.com/api/matches/{}'.format(match_id)) if match_info['radiant_win'] == True: return True else: return False
def team_win_or_defeat(match_id): match_info = get_api_json( 'https://api.opendota.com/api/matches/{}'.format(match_id)) if match_info['radiant_team_id'] == 1838315: #判断秘密战队是否为天辉方, #注意千万不要带上引号啊!!! return True else: return False
def normal_match_insert(ids): for i in ids: match_info = dota2_api.get_api_json( 'https://api.opendota.com/api/matches/{}?ccdc7024-3890-44dd-b602-ec193dee6f23' .format(i)) if len(match_info) < 10: # i+=1 #print("当前线程:", threading.currentThread().name, "----", i, 'this match not found') continue else: #db.Normal_matches_total_info.insert_one(match_info) # i+=1 MatchIdValidList.append(i)
def opentomongo(): db_eng_1 = DBA.DbInfoGenerator('vpgame').info db_eng_2 = DBA.DbInfoGenerator('model_builder').info # 连接到本地库 client = pymongo.MongoClient(db_eng_1['host'], 7974) # Newclient = pymongo.MongoClient(db_eng_2['host'], 27017) # 连接database'damin' db = client['admin'] # 'admin'的账号密码 db.authenticate(db_eng_1['user'], db_eng_1['password']) print('success connet the database') # sql语句建立查询 sql = """ SELECT matches.match_id, matches.start_time FROM matches JOIN match_patch using(match_id) WHERE TRUE AND matches.start_time between extract(epoch from timestamp '2020-01-01T00:00:00.000Z') and extract(epoch from timestamp '2021-01-01T00:00:00.000Z') """ # 查询对应的比赛id并且建立list match_ids = dota2_api.get_api_json( 'https://api.opendota.com/api/explorer?sql={}'.format(sql))['rows'] print(len(match_ids)) dota_match = list( db.dota_basic_data_2020.find({}, { '_id': 0, 'match_id': 1 })) dota_match = pd.DataFrame(dota_match) # 如果数据库内已经有数据,不插入这部分数据 match_list_haven = [] if len(dota_match) != 0: match_list_haven = list(dota_match['match_id']) # 查看还需要插入的比赛场次 print(len(match_ids) - len(dota_match) + 1) # 对于以及在数据库的比赛进行筛选 m = 0 for mi in match_ids: # print(mi['match_id']) if mi['match_id'] not in match_list_haven: match_info = dota2_api.get_api_json( 'https://api.opendota.com/api/matches/{}?ccdc7024-3890-44dd-b602-ec193dee6f23' .format(mi['match_id'])) print(mi) db.dota_basic_data_2020.insert_one(match_info) m = m + 1 print(m) # 将战队的历史数据中每个队员的历史数据插入到对应的数据库中 data1 = list( db.dota_basic_data_2020.find({'match_id': mi['match_id']}, { '_id': 0, 'players': 1 })) print('ready to insert') for i in range(len(data1)): if 'players' in data1[i].keys(): for j in range(len(data1[i]['players'])): player_info = data1[i]['players'][j] db.players_2020.insert_one(player_info) print('success insert data ready to calculate the elo')
import dota2_api import pandas as pd import logging # loading the elo_rank info team_rank=dota2_api.get_api_json('https://api.opendota.com/api/teams') logger = logging.getLogger(__name__) logger.setLevel(level=logging.INFO) handler = logging.FileHandler('prediction_output.log') formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) team_rank=pd.DataFrame(team_rank) team_rank=team_rank.drop_duplicates(['name']) team_rank=team_rank[['name','tag','rating','wins','losses']] def elo_team(team1,team2): x1=team_rank.loc[team_rank['tag']==team1,['rating']] x2=team_rank.loc[team_rank['tag']==team2,['rating']] x1=x1.iloc[0][0] x2=x2.iloc[0][0] p1=1/(1+10**((x2-x1)/400)) p2=1/(1+10**((x1-x2)/400)) if p1 > p2: return('the winner team is %s'%(team1)) else: return('the winner team is %s'%(team2)) team1=['Liquid','Secret','PSG.LGD'] team2=['Gambit','OG','EG'] for i in range(len(team1)): logger.info(elo_team(team1[i],team2[i]))
AND (player_matches.account_id = 87278757) AND (matches.leagueid = 10946) ORDER BY match_id LIMIT 60""" # sql = """SELECT # matches.match_id # FROM matches # JOIN match_patch using(match_id) # JOIN leagues using(leagueid) # where patch >= '7.24' # ORDER BY RANDOM() # LIMIT 100 # """ match_ids = get_api_json( 'https://api.opendota.com/api/explorer?sql={}'.format(sql))['rows'] matchCount = 0 winCount = 0 secret_winCount = 0 secret_rad = 0 for mi in match_ids: matchCount = matchCount + 1 id = mi['match_id'] if win_or_defeat(id) == True: winCount = winCount + 1 if team_win_or_defeat(id) == True: print('天辉', id) secret_rad = secret_rad + 1 secret_winCount = secret_winCount + 1 else: