async def on_war_attack(attack, war): if attack.is_opponent: print(get_time() + ' - {0.attacker}(Opp) did {0.stars} stars on {0.defender}.'. format(attack, war)) else: print(get_time() + ' - {0.attacker} did {0.stars} stars on {0.defender}(Opp).'. format(attack, war))
def update_metrics(sheet): header = sheet.getRow(3) # row needed to look up the columns try: # use header to look up column numbers and return COLUMN number ( index + 1 ) # column_indicies = {'AVG Stars': header.index('AVG Stars') + 1, # 'War Efficiency': header.index('War Efficiency') + 1, # 'Star Efficiency': header.index('Star Efficiency') + 1, # 'AVG Attack Pos': header.index('AVG Attack Pos') + 1 # } columns = { 'AVG Stars': {}, 'War Efficiency': {}, 'Star Efficiency': {}, 'AVG Attack Pos': {} } for k, v in columns.items(): v['number'] = header.index(k) + 1 v['column'] = sheet.getColumn(v['number']) start_war = header.index('Attack 1') except ValueError: print(get_time() + ' - ERROR: Column not in sheet row 2!') return for i in range(3, sheet.rowCount): player_row = sheet.getRow(i + 1) # get player row from column # ATTRIBUTES WE ARE STORING # note: we only use one api request for n attributes player_metrics = { 'AVG Stars': get_avg_stars(player_row, start_war), 'War Efficiency': get_war_efficiency(player_row, start_war), 'Star Efficiency': get_star_efficiency(player_row, start_war), 'AVG Attack Pos': get_avg_attack_pos(player_row, start_war) } for k, v in player_metrics.items(): columns[k]['column'][i] = v for k, v in columns.items(): sheet.updateColumn(v['number'], v['column']) # all metrics up to date print(get_time() + ' - Metrics Updated')
def update_league_clans(league_group): # this creates CWL sheet for each clan # adds roster members and info, updates attacks, and predicts upcoming lineup # should only update members if needed (even if deleted on sheet) for clan in league_group['clans']: sheet_title = 'CWL - ' + clan['name'] if sheet_title not in ss.sheetTitles: print(get_time() + ' - Adding ' + clan['name'] + ' member data to spreadsheet') ss.createSheet(sheet_title, len(ss.sheetTitles)) sheet = ss[len(ss.sheetTitles) - 1] columns = ['Name', 'Tags', 'Pos', 'Town Hall', 'Stars For'] sheet.columnCount = len(columns) sheet.rowCount = 3 sheet.updateRow(1, [clan['name'], '']) sheet.updateRow(2, ['Players']) sheet.updateRow(3, columns) for sheet in ss: if sheet.title == sheet_title: header_row = sheet.getRow(3) column_list = {'name': {'column': sheet.getColumn(search(header_row, 'Name') + 1), 'number': search(header_row, 'Name') + 1, 'changed': False }, 'tag': {'column': sheet.getColumn(search(header_row, 'Tags') + 1), 'number': search(header_row, 'Tags') + 1, 'changed': False }, 'townHallLevel': {'column': sheet.getColumn(search(header_row, 'Town Hall') + 1), 'number': search(header_row, 'Town Hall') + 1, 'changed': False } } for member in clan['members']: member_index = search(column_list['tag']['column'], member['tag']) if member_index < 0: for k, v in column_list.items(): v['column'].append(member[k]) v['changed'] = True else: for k, v in column_list.items(): if v['column'][member_index] != member[k]: v['changed'] = True v['column'][member_index] = member[k] for k, v in column_list.items(): if v['changed']: sheet.updateColumn(v['number'], v['column'])
def update_league_metrics(): for sheet in ss: if sheet.title.find('CWL') >= 0: column_names = sheet.getRow(3) info = { 'header': {'row': sheet.getRow(1), 'number': 1, 'changed': False }, 'tags': {'column': sheet.getColumn(2), 'number': 2, 'changed': False } } column_list = { 'stars': {'column': sheet.getColumn(search(column_names, 'Stars For') + 1), 'number': search(column_names, 'Stars For') + 1, 'changed': False } } for i, event in enumerate(reversed(info['header']['row'])): if event.find('League') >= 0: # print(info['header']['row'][0], event) for row, player_tag in enumerate(info['tags']['column']): if player_tag.find('#') >= 0: player_row = sheet.getRow(row + 1) start_index = len(info['header']['row']) - i - 1 initial_value = str(column_list['stars']['column'][row]) if str(get_sum_stars(player_row, start_index)) != initial_value: column_list['stars']['column'][row] = get_sum_stars(player_row, start_index) column_list['stars']['changed'] = True for k, v in column_list.items(): if v['changed']: sheet.updateColumn(v['number'], v['column']) print(get_time() + ' Updating ' + info['header']['row'][0] + ' ' + k + ' metrics.') break
async def on_clan_member_trophies_change(old_trophies, new_trophies, player): update_trophies(player, new_trophies) print(get_time() + ' - {0.name} ({0.tag}) went from {1} to {2} trophies.'.format( player, old_trophies, new_trophies))
async def on_clan_member_leave(member, clan): print(get_time() + ' - {0.name} ({0.tag}) just left clan {1.name} ({1.tag}).'.format( member, clan))
async def on_clan_member_join(member, clan): print(get_time() + ' - {0.name} ({0.tag}) just joined clan {1.name} ({1.tag}).'.format( member, clan))
async def on_clan_member_donation(old_donation, new_donations, player): difference = new_donations - old_donation print(get_time() + ' - {0.name} ({0.tag}) just donated {1} troop space'.format( player, difference))
update_trophies(player, new_trophies) print(get_time() + ' - {0.name} ({0.tag}) went from {1} to {2} trophies.'.format( player, old_trophies, new_trophies)) @client.event async def on_war_attack(attack, war): if attack.is_opponent: print(get_time() + ' - {0.attacker}(Opp) did {0.stars} stars on {0.defender}.'. format(attack, war)) else: print(get_time() + ' - {0.attacker} did {0.stars} stars on {0.defender}(Opp).'. format(attack, war)) async def get_players(): the_clan = await client.get_clan(tag=CLAN_TAG) print(type(the_clan)) print(get_time() + " - Start:") get_players() client.add_clan_update(CLAN_TAG, retry_interval=30) # check every 30 seconds client.run_forever() # for player in the_clan.get_detailed_members(cache = True) # print(player.name)
def update_league_stars(league_war): # league war has attack information for 2 clans for k, clan in league_war.items(): if k == 'clan' or k == 'opponent': for num, title in enumerate(ss.sheetTitles): if title == 'CWL - ' + clan['name']: sheet = ss[num] match_column_start = search(sheet.getRow(2), league_war['match_tag']) + 1 if match_column_start < 0: sheet.columnCount += 1 match_column_start = sheet.columnCount pos_column_start = search(sheet.getRow(3), 'Pos') + 1 info = {'event': {'row': sheet.getRow(1), 'number': 1, 'changed': False }, 'tag': {'column': sheet.getColumn(2), 'number': 2, 'changed': False }, } column_list = {'position': {'column': sheet.getColumn(pos_column_start), 'number': pos_column_start, 'changed': False }, 'attack': {'column': sheet.getColumn(match_column_start), 'number': match_column_start, 'changed': False } } if league_war['state'] == 'preparation': column_list['attack']['column'] = [''] * sheet.rowCount column_list['position']['column'] = [''] * sheet.rowCount column_list['position']['column'][2] = 'Pos' column_list['attack']['changed'] = True column_list['position']['changed'] = True leagueDate = 'League ' + league_war['startTime'][4:6] + '-' + league_war['startTime'][0:4] if search(info['event']['row'], leagueDate) < 0: column_list['attack']['column'][0] = leagueDate column_list['attack']['column'][1] = league_war['match_tag'] column_list['attack']['column'][2] = 'Day ' + str(league_war['day']) for member in clan['members']: player_index = search(info['tag']['column'], member['tag']) column_list['position']['column'][player_index] = member['mapPosition'] if 'attacks' in member: for attack in member['attacks']: if player_index >= 0: if column_list['attack']['column'][player_index] != str(attack['stars']): column_list['attack']['column'][player_index] = attack['stars'] column_list['attack']['changed'] = True else: if league_war['state'] == 'warEnded': symbol = '-' else: symbol = '--' if column_list['attack']['column'][player_index] != symbol: column_list['attack']['column'][player_index] = symbol column_list['attack']['changed'] = True for k, v in column_list.items(): if v['changed']: sheet.updateColumn(v['number'], v['column']) if k == 'attack': print(get_time() + ' - Updating {0} Day {1}'.format(clan['name'], league_war['day']))
def update_members(member_list): print(get_time() + ' - Members Start Updating') dataHeaderRow = data.getRow(3) player_info = { 'tag': { 'column': data.getColumn(search(dataHeaderRow, 'Tags') + 1), 'number': search(dataHeaderRow, 'Tags') + 1, 'changed': False } } player_data = { 'name': { 'column': data.getColumn(search(dataHeaderRow, 'Name') + 1), 'number': search(dataHeaderRow, 'Name') + 1, 'changed': False }, 'donations': { 'column': data.getColumn(search(dataHeaderRow, 'Given') + 1), 'number': search(dataHeaderRow, 'Given') + 1, 'changed': False }, 'donationsReceived': { 'column': data.getColumn(search(dataHeaderRow, 'Received') + 1), 'number': search(dataHeaderRow, 'Received') + 1, 'changed': False }, 'trophies': { 'column': ['', '', 'Trophies'] + ([0] * (data.rowCount - 3)), 'number': search(dataHeaderRow, 'Trophies') + 1, 'changed': True }, } names_added = [] for member in member_list: member['name'] = member['name'].replace('==', '::') player_index = search(player_info['tag']['column'], member['tag']) if player_index < 0: # not found player_info['tag']['column'].append(member['tag']) names_added.append(member['name']) player_index = data.rowCount data.rowCount += 1 for k, v in player_data.items(): if player_index > len(v['column']) - 1: v['column'].append(member[k]) else: v['column'][player_index] = member[k] if len(names_added) != 0: print(get_time() + ' - Member(s) added to sheet: ' + str(names_added)) player_data['name']['column'][1] = 'Updated ' + simple_time() for k, v in {**player_data, **player_info}.items(): data.updateColumn(v['number'], v['column'])
def update_league(): print(get_time() + ' - Start update_league()') league_group = requests.get('https://api.clashofclans.com/v1/clans/' + CLAN_TAG + '/currentwar/leaguegroup', headers=headers).json() if 'reason' in league_group: # no api information return update_league_clans(league_group) war_list = queue.Queue() for day_num in range(1, 8): day = league_group['rounds'][day_num - 1] for match_up in day['warTags']: if match_up != '#0': match_tag = match_up.replace('#', '%23') league_war = requests.get( 'https://api.clashofclans.com/v1/clanwarleagues/wars/' + match_tag, headers=headers ).json() league_war['match_tag'] = match_tag league_war['day'] = day_num update_league_stars(league_war) for (k, clan) in league_war.items(): if k == 'clan' or k == 'opponent': if clan['tag'].replace('#', '%23') == CLAN_TAG: if league_war['state'] != 'preparation': clan['battleTag'] = match_tag clan['state'] = league_war['state'] war_list.put(clan) if war_list.qsize() > 3: war_list.get() player_tags = data.getColumn(2) header = data.getRow(2) count = 0 while not war_list.empty(): war = war_list.get() try: current_war_column = header.index(war['battleTag']) + 1 attack_list = data.getColumn(current_war_column) except ValueError: print('War being added') data.columnCount += 1 current_war_column = data.columnCount attack_list = data.getColumn(current_war_column) if count == 0: attack_list[0] = 'League ' + league_group['season'] attack_list[1] = war['battleTag'] attack_list[2] = 'Day {0}'.format(count + 1) for member in war['members']: try: player_index = player_tags.index(member['tag']) if 'attacks' in member: for attack in member['attacks']: attack_list[player_index] = attack['stars'] else: if war['state'] == 'warEnded': attack_list[player_index] = '-' else: attack_list[player_index] = '--' except ValueError: pass data.updateColumn(current_war_column, attack_list) count += 1 print(get_time() + ' - League update complete')
import ezsheets import queue from copy import deepcopy from funcs import get_time import requests from funcs import * from metrics import * from credentials import * print(get_time() + ' - Program Start.') CLAN_TAG = '%23P02CUUUU' ss = get_spreadsheet() # variable assignment to sheets data = ss[0] analytics = ss[1] print(get_time() + ' - ' + data.title + " opened.") def search(list, key): try: return list.index(key) except ValueError: print('\n*********\n' + str(key) + ' not found' + '\n*********\n') return -2