728x90
젠킨스와 빅스토리지를 잘 활용하면 계속해서 수집할 수 있습니다.
var.txt로 저장된 매치아이디를 통해서 게임정보를 불러오고 csv로 저장함
from google.cloud import bigquery
from google.oauth2 import service_account
from google.cloud import storage
import pandas as pd
from datetime import datetime
import csv
from datetime import datetime
import os
# 인게임 데이터가져와서 팀조합, 포지션 모으는 함수
def combi_position(ingame):
df3 = pd.DataFrame({'Win': [],
'Lose': [],
'Win_Position': [],
'Lose_Position': []})
for i, j in enumerate(ingame["win_team"]):
if j == 100:
win_temp = []
lose_temp = []
result = []
win_position = []
lose_position = []
for k in range(5):
win_temp.append(ingame.loc[i][f"player_{k}_championId"])
if ingame.loc[i][f'player_{k}_individualPosition'] == 'TOP':
win_position.append(1)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'JUNGLE':
win_position.append(2)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'MIDDLE':
win_position.append(3)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'BOTTOM':
win_position.append(4)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'UTILITY':
win_position.append(5)
else:
win_position.append(0)
for k in range(5, 10):
lose_temp.append(ingame.loc[i][f"player_{k}_championId"])
if ingame.loc[i][f'player_{k}_individualPosition'] == 'TOP':
lose_position.append(1)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'JUNGLE':
lose_position.append(2)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'MIDDLE':
lose_position.append(3)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'BOTTOM':
lose_position.append(4)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'UTILITY':
lose_position.append(5)
else:
lose_position.append(0)
result.append(win_temp)
result.append(lose_temp)
result.append(win_position)
result.append(lose_position)
df3 = df3.append(pd.Series(result, index=df3.columns), ignore_index=True)
else:
win_temp = []
lose_temp = []
result = []
win_position = []
lose_position = []
for k in range(5, 10):
win_temp.append(ingame.loc[i][f"player_{k}_championId"])
if ingame.loc[i][f'player_{k}_individualPosition'] == 'TOP':
win_position.append(1)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'JUNGLE':
win_position.append(2)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'MIDDLE':
win_position.append(3)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'BOTTOM':
win_position.append(4)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'UTILITY':
win_position.append(5)
else:
win_position.append(0)
for k in range(5):
lose_temp.append(ingame.loc[i][f"player_{k}_championId"])
if ingame.loc[i][f'player_{k}_individualPosition'] == 'TOP':
lose_position.append(1)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'JUNGLE':
lose_position.append(2)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'MIDDLE':
lose_position.append(3)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'BOTTOM':
lose_position.append(4)
elif ingame.loc[i][f'player_{k}_individualPosition'] == 'UTILITY':
lose_position.append(5)
else:
lose_position.append(0)
result.append(win_temp)
result.append(lose_temp)
result.append(win_position)
result.append(lose_position)
df3 = df3.append(pd.Series(result, index=df3.columns), ignore_index=True)
return df3
# 포지션 소팅해서 조합 순서 정렬해주는 함수
def sorting_ingame(a):
df3 = pd.DataFrame({'Win': [],
'Lose': [],
'Win_Position': [],
'Lose_Position': []})
for i in range(len(a)):
result = []
b = a.loc[i]['Win']
c = a.loc[i]['Win_Position']
d = a.loc[i]['Lose']
e =a.loc[i]['Lose_Position']
c, b = zip(*sorted(zip(c, b)))
e, d = zip(*sorted(zip(e, d)))
result.append(list(b))
result.append(list(d))
result.append(list(c))
result.append(list(e))
df3 = df3.append(pd.Series(result, index=df3.columns), ignore_index=True)
df4 = df3[['Win','Lose']]
now = datetime.today().strftime("%y%m%d%H%M")
df4.to_csv(f'winlose_{now}.csv')
return df4
# 챔프를 태깅하는 작업을 하는 함수
def tagging(data, tag, duplicate_tag):
dupl_tag = duplicate_tag
top = tag[tag.Position == 1]
jug = tag[tag.Position == 2]
mid = tag[tag.Position == 3]
adc = tag[tag.Position == 4]
sup = tag[tag.Position == 5]
top_tag = pd.DataFrame(top, columns=['champ_num', 'Tank', 'Bruiser', 'AD_AP', 'CC', 'Initiate', 'Nuking', 'Care'])
jug_tag = pd.DataFrame(jug, columns=['champ_num', 'Tank', 'Bruiser', 'AD_AP', 'CC', 'Initiate', 'Nuking', 'Care'])
mid_tag = pd.DataFrame(mid, columns=['champ_num', 'Tank', 'Bruiser', 'AD_AP', 'CC', 'Initiate', 'Nuking', 'Care'])
adc_tag = pd.DataFrame(adc, columns=['champ_num', 'Tank', 'Bruiser', 'AD_AP', 'CC', 'Initiate', 'Nuking', 'Care'])
sup_tag = pd.DataFrame(sup, columns=['champ_num', 'Tank', 'Bruiser', 'AD_AP', 'CC', 'Initiate', 'Nuking', 'Care'])
etc_tag = pd.DataFrame(dupl_tag, columns=['champ_num', 'Tank', 'Bruiser', 'AD_AP', 'CC', 'Initiate', 'Nuking', 'Care'])
top_dic = top_tag.set_index('champ_num').T.to_dict('list')
jug_dic = jug_tag.set_index('champ_num').T.to_dict('list')
mid_dic = mid_tag.set_index('champ_num').T.to_dict('list')
adc_dic = adc_tag.set_index('champ_num').T.to_dict('list')
sup_dic = sup_tag.set_index('champ_num').T.to_dict('list')
etc_dic = etc_tag.set_index('champ_num').T.to_dict('list')
time = datetime.today().strftime("%Y%m%d%H%M")
f = open(f'{time}.csv', 'a', encoding='utf-8', newline='')
wr = csv.writer(f)
wr.writerow(['win_champ', 'lose_champ', 'win_tags', 'lose_tags'])
for k in range(len(data)):
win_data, lose_data = [], []
win_champ_list = data.loc[k]['Win']
lose_champ_list = data.loc[k]['Lose']
win_position_list = data.loc[k]['Win_Position']
lose_position_list = data.loc[k]['Lose_Position']
for combi in range(0, 5):
try:
if win_position_list[combi] == 1:
win_tags_data = top_dic[win_champ_list[combi]]
elif win_position_list[combi] == 2:
win_tags_data = jug_dic[win_champ_list[combi]]
elif win_position_list[combi] == 3:
win_tags_data = mid_dic[win_champ_list[combi]]
elif win_position_list[combi] == 4:
win_tags_data = adc_dic[win_champ_list[combi]]
elif win_position_list[combi] == 5:
win_tags_data = sup_dic[win_champ_list[combi]]
else:
win_tags_data = etc_dic[win_champ_list[combi]]
except:
try:
win_tags_data = etc_dic[win_champ_list[combi]]
except:
pass
win_data.append(win_tags_data)
for combi in range(0, 5):
try:
if lose_position_list[combi] == 1:
lose_tags_data = top_dic[lose_champ_list[combi]]
elif lose_position_list[combi] == 2:
lose_tags_data = jug_dic[lose_champ_list[combi]]
elif lose_position_list[combi] == 3:
lose_tags_data = mid_dic[lose_champ_list[combi]]
elif lose_position_list[combi] == 4:
lose_tags_data = adc_dic[lose_champ_list[combi]]
elif lose_position_list[combi] == 5:
lose_tags_data = sup_dic[lose_champ_list[combi]]
else:
lose_tags_data = etc_dic[lose_champ_list[combi]]
except:
try:
lose_tags_data = etc_dic[lose_champ_list[combi]]
except:
pass
lose_data.append(lose_tags_data)
csv_result = {'win': data.loc[k]['Win'], 'lose': data.loc[k]['Lose'], 'win_tags': win_data,
'lose_tags': lose_data}
wr.writerow([data.loc[k]['Win'], data.loc[k]['Lose'], win_data, lose_data])
return 'Done'
def str_to_array(one_tag):
total = []
team = []
for i in one_tag:
row = []
for k in i[0]:
if len(row) != 7:
try:
temp = int(k)
row.append(temp)
except:
continue
else:
team.append(row)
row = []
if len(team) == 5:
total.append(team)
team = []
total_array = np.array((total))
return total_array
def find_last_qry(last_id):
if last_id == None :
last_match_id = "select match_id from `cslee.target_game_data` order by match_id desc limit 10"
last_match_id = bigquery_client.query(last_match_id).to_dataframe()
return int(last_match_id[-1:]['match_id'])
else :
new_last_match_id = f"select match_id from `cslee.target_game_data` where match_id < {last_id} order by match_id desc limit 10"
last_match_id = bigquery_client.query(new_last_match_id).to_dataframe()
return int(last_match_id[-1:]['match_id'])
# str_to_array를 쓸때 아래와 같이 처리 필요함!
# win = df[['win_tags']]
# lose = df[['lose_tags']]
#
# win = win.values
# lose = lose.values
# 이후 str_to_array(win).sum(axis=0) 하면 전체 태깅합나옴
credentials = service_account.Credentials.from_service_account_file('admin.json')
bigquery_client = bigquery.Client(credentials=credentials, project='cslee-323908')
storage_client = storage.Client(credentials=credentials, project='cslee-323908')
if not os.path.exists('var.txt'):
pass
else:
with open('var.txt', 'r') as f:
var = f.readline()
try:
var = int(var)
ingame_query = f"select match_id,win_team,player_0_championId,player_1_championId,player_2_championId,player_3_championId,player_4_championId,player_5_championId,player_6_championId,player_7_championId,player_8_championId,player_9_championId,player_0_individualPosition,player_1_individualPosition,player_2_individualPosition,player_3_individualPosition,player_4_individualPosition,player_5_individualPosition,player_6_individualPosition,player_7_individualPosition,player_8_individualPosition,player_9_individualPosition from `cslee.target_game_data` where match_id < {var} and Duration>1500000 order by match_id desc limit 10000"
ingame = bigquery_client.query(ingame_query).to_dataframe()
var = find_last_qry(var)
except:
ingame_query = f"select match_id,win_team,player_0_championId,player_1_championId,player_2_championId,player_3_championId,player_4_championId,player_5_championId,player_6_championId,player_7_championId,player_8_championId,player_9_championId,player_0_individualPosition,player_1_individualPosition,player_2_individualPosition,player_3_individualPosition,player_4_individualPosition,player_5_individualPosition,player_6_individualPosition,player_7_individualPosition,player_8_individualPosition,player_9_individualPosition from `cslee.target_game_data` where Duration>1500000 order by match_id desc limit 10000"
ingame = bigquery_client.query(ingame_query).to_dataframe()
var = find_last_qry(None)
with open('var.txt', 'w+') as f:
var = str(var)
f.write(var)
# 텍스트파일에 var를 저장하고 불러와서 슥삭
# 함수에 select문 변경하기
tag_query = "select * from `cslee.tag`"
tag = bigquery_client.query(tag_query).to_dataframe()
dupltag_query = "select * from `cslee.duplicate_tag`"
dupltag = bigquery_client.query(dupltag_query).to_dataframe()
dp1 = combi_position(ingame)
dp2 = sorting_ingame(dp1)
var.txt를 통해서 어디서부터 수집할 지 결정해줍니다.
급하게 만든거라서 어색하거나 불필요한 부분이 있을 수 있습니다.
728x90
'프로젝트 > Riot LoL' 카테고리의 다른 글
챔피언 티어정보 만들기 (0) | 2021.10.19 |
---|---|
베스트 조합 찾아내는 코드 by CSV (0) | 2021.10.19 |
포지션정보넣으면 승률+밴픽률순서로 출력 (0) | 2021.10.19 |
챔피언 별 밴픽률을 구해보자 (0) | 2021.09.24 |
Modeling을 위한 전처리를 해보자 (0) | 2021.09.15 |