Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
And add some utility functions
%spark.pyspark
def parse_init_events(game_logs):
game_won_by = 0 # 0 for blue, 1 for gold
victory_type = 0 # 0 for economy, 1 for military, 2 for snail
blue_team_vector = team_data_vector()
gold_team_vector = team_data_vector()
for log in game_logs:
if log['event'] == 'victory':
if log['team'] == 'Gold':
game_won_by = 1
if log['type'] == 'military':
victory_type = 1
elif log['type'] == 'snail':
victory_type = 2
if log['event'] == 'spawn':
is_bot = log['isBot']
player = log['player']
player_team = player['team']
is_queen = player['queen']
if player_team == 'Blue':
current_team = blue_team_vector
else:
current_team = gold_team_vector
if is_bot:
current_team.num_minion_bots += 1
if is_queen:
current_team.is_queen_bot = 1
return game_won_by, victory_type, blue_team_vector, gold_team_vector
def parse_game_logs(game_logs):
game_data_vector_list = []
game_gate_list = []
# game details - need at the beginning to encode dependent vectors
game_won_by, victory_type, blue_team_vector, gold_team_vector = parse_init_events(game_logs)
# print(game_won_by, victory_type, blue_team_vector.to_data_vector(), gold_team_vector.to_data_vector())
current_game_data_vector = game_data_vector(blue_team_vector, gold_team_vector, game_won_by, victory_type)
# print(current_game_data_vector.to_data_vector())
game_data_vector_list.append(current_game_data_vector.to_data_vector())
# reverse the logs to actually arrange them in timestamped order
ordered_game_logs = list(reversed(game_logs))
# then process everything else till the game ends
gameset = set()
for log in ordered_game_logs:
gameset.add(str(log['event']))
event_name = str(log['event'])
if event_name == 'enriched_berryDeposit':
# berry deposit count
blue_team_vector, gold_team_vector = parse_berry_deposit(log, blue_team_vector, gold_team_vector)
elif event_name == 'enriched_playerKill':
# player kill - minion or queen
blue_team_vector, gold_team_vector = parse_player_kill(log, blue_team_vector, gold_team_vector)
elif event_name == 'enriched_useMaiden':
# create warrior or speed minion
blue_team_vector, gold_team_vector = parse_use_maiden(log, blue_team_vector, gold_team_vector)
elif event_name == 'blessMaiden':
# reserve gate
blue_team_vector, gold_team_vector = parse_reserve_maiden(log, blue_team_vector, gold_team_vector,
game_gate_list)
log_game_data_vector = game_data_vector(blue_team_vector, gold_team_vector, game_won_by, victory_type)
game_data_vector_list.append(log_game_data_vector.to_data_vector())
# print(gameset)
return game_data_vector_list
def parse_berry_deposit(log_line, blue_team_vector, gold_team_vector):
original_event = log_line['originalEvent']
player = original_event['player']
team = player['team']
if team == 'Blue':
blue_team_vector.num_berries_deposited += 1
else:
gold_team_vector.num_berries_deposited += 1
return blue_team_vector, gold_team_vector
def parse_reserve_maiden(log_line, blue_team_vector, gold_team_vector, game_gate_list):
new_team = log_line['team']
x = log_line['x']
y = log_line['y']
found = False
if new_team == 'Blue':
blue_team_vector.num_gates += 1
else:
gold_team_vector.num_gates += 1
if len(game_gate_list) == 0:
new_gate = gate_vector(x, y, new_team)
game_gate_list.append(new_gate)
else:
for gate in game_gate_list:
if gate.check(x, y):
found = True
old_team = gate.team
gate.flip_team(new_team)
if old_team == 'Blue':
if blue_team_vector.num_gates > 0:
blue_team_vector.num_gates -= 1
else:
if gold_team_vector.num_gates > 0:
gold_team_vector.num_gates -= 1
if not found:
new_gate = gate_vector(x, y, new_team)
game_gate_list.append(new_gate)
return blue_team_vector, gold_team_vector
def parse_use_maiden(log_line, blue_team_vector, gold_team_vector):
original_event = log_line['originalEvent']
player = original_event['player']
team = player['team']
current_state = log_line['currentState']
if team == 'Blue':
if current_state['isFast']:
blue_team_vector.num_speed_players += 1
if current_state['isWarrior']:
blue_team_vector.num_warriors += 1
else:
if current_state['isFast']:
gold_team_vector.num_speed_players += 1
if current_state['isWarrior']:
gold_team_vector.num_warriors += 1
return blue_team_vector, gold_team_vector
def parse_player_kill(log_line, blue_team_vector, gold_team_vector):
original_event = log_line['originalEvent']
victim_type = original_event['victimType']
player1 = original_event['player1']
team1 = player1['team']
player2 = original_event['player2']
team2 = player2['team']
if team1 == 'Blue':
if victim_type == 'Worker' or victim_type == 'Soldier':
# blue team kills gold team -> increment kills for gold (as gold team got killed)
gold_team_vector.num_kills += 1
else:
# queen was killed, decrement gold queen life
gold_team_vector.num_queen_lives_left -= 1
else:
if victim_type == 'Worker'or victim_type == 'Soldier':
# gold team kills gold team -> increment kills for gold (as gold team got killed)
blue_team_vector.num_kills += 1
else:
# queen was killed, decrement blue queen life
blue_team_vector.num_queen_lives_left -= 1
return blue_team_vector, gold_team_vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.