-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a command that modifies the saved replay #25
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,4 +112,5 @@ credentials.json | |
|
||
# League | ||
league/ | ||
settings.json | ||
settings.json | ||
/autoleagueplay/bots/ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ def on_tick(self, tick: TrainingTickPacket) -> Optional[Grade]: | |
time.sleep(1) # Give time for replay_monitor to register replay and for RL to load main menu | ||
if self.replay_monitor.replay_id or self.replay_monitor.replay_preference == ReplayPreference.IGNORE_REPLAY: | ||
self.replay_monitor.stop_monitoring() | ||
self.replay_monitor.anonymize_replay() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ReplayMonitor does not have this function, |
||
return Pass() | ||
|
||
# Check if game is over and replay recorded | ||
|
@@ -88,10 +89,12 @@ def on_tick(self, tick: TrainingTickPacket) -> Optional[Grade]: | |
self.match_result = fetch_match_score(tick.game_tick_packet) | ||
if self.replay_monitor.replay_id or self.replay_monitor.replay_preference == ReplayPreference.IGNORE_REPLAY: | ||
self.replay_monitor.stop_monitoring() | ||
self.replay_monitor.anonymize_replay() | ||
return Pass() | ||
seconds_since_game_end = game_info.seconds_elapsed - self.last_match_time | ||
if seconds_since_game_end > 15: | ||
self.replay_monitor.stop_monitoring() | ||
self.replay_monitor.anonymize_replay() | ||
return FailDueToNoReplay() | ||
else: | ||
if game_info.is_round_active and not game_info.is_match_ended: | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,24 +2,71 @@ | |||||
from enum import Enum | ||||||
from pathlib import Path | ||||||
from typing import Dict, Any | ||||||
import os | ||||||
import RattletrapPython.rattletrap as rat | ||||||
import json | ||||||
|
||||||
import requests | ||||||
from rlbottraining.history.metric import Metric | ||||||
from autoleagueplay.paths import WorkingDir | ||||||
from watchdog.events import LoggingEventHandler | ||||||
from watchdog.observers import Observer | ||||||
|
||||||
|
||||||
class ReplayPreference(Enum): | ||||||
SAVE = 'save' # save to the default replays directory | ||||||
CALCULATED_GG = 'calculated_gg' # save locally and upload to https://calculated.gg/ | ||||||
IGNORE_REPLAY = 'ignore' | ||||||
ANONYMIZE_REPLAY = 'anonym' | ||||||
|
||||||
|
||||||
def upload_to_calculated_gg(replay_path: Path): | ||||||
with open(replay_path, 'rb') as f: | ||||||
response = requests.post('https://calculated.gg/api/upload', files={'replays': f}) | ||||||
print(f'upload response to {replay_path.name}: {response}') | ||||||
|
||||||
def anonymize_replay(replay_path: Path, working_dir: WorkingDir): | ||||||
replay_name = str(os.path.basename(str(replay_path)).split('.')[0]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You already have the Path object, so you can just do
Suggested change
|
||||||
anonym_replay_path = working_dir.replays / str(replay_name + '.replay') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about remaning the replay file to something reasonable? Like "Beast from the East vs Skybot"? This means you will have to call the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to do that. i can even rename the in-game name, but i couldnt from replay.py |
||||||
anonym_json_path = working_dir.replays / str('replay' + '.json') | ||||||
rat.parse(str(replay_path), anonym_json_path) | ||||||
|
||||||
with open(anonym_json_path) as replay_file: | ||||||
replay = json.load(replay_file) | ||||||
try: | ||||||
replay['header']['body']['properties']['value']['Team0Score']['value']['int'] = 0 | ||||||
except: | ||||||
pass | ||||||
try: | ||||||
replay['header']['body']['properties']['value']['Team1Score']['value']['int'] = 0 | ||||||
except: | ||||||
pass | ||||||
try: | ||||||
replay['header']['body']['properties']['value']['HighLights']['value']['array'] = [] | ||||||
except: | ||||||
pass | ||||||
try: | ||||||
replay['header']['body']['properties']['value']['HighLights']['size'] = '0' | ||||||
except: | ||||||
pass | ||||||
try: | ||||||
replay['header']['body']['properties']['value']['Goals']['value']['array'] = [] | ||||||
except: | ||||||
pass | ||||||
try: | ||||||
replay['header']['body']['properties']['value']['Goals']['size'] = '0' | ||||||
except: | ||||||
pass | ||||||
try: | ||||||
replay['content']['body']['marks'] = [] | ||||||
except: | ||||||
pass | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, does all these try-excepts has to be done individually? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there may be a better way, but if one fails, i want it to keep going to the next ( for example, if team 0 doesn't score, that field doesn't exist) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then this is probably fine. |
||||||
with open(anonym_json_path, 'w') as replay_file: | ||||||
json.dump(replay, replay_file, indent=4) | ||||||
|
||||||
rat.generate(anonym_json_path, str(anonym_replay_path)) | ||||||
|
||||||
|
||||||
|
||||||
|
||||||
def parse_replay_id(replay_path: Path) -> str: | ||||||
replay_id, extension = replay_path.name.split('.') | ||||||
|
@@ -31,6 +78,7 @@ def parse_replay_id(replay_path: Path) -> str: | |||||
class ReplayMonitor(Metric): | ||||||
|
||||||
replay_preference: ReplayPreference | ||||||
working_dir: WorkingDir | ||||||
|
||||||
replay_id: str = None | ||||||
observer: Observer = None | ||||||
|
@@ -52,10 +100,21 @@ def on_modified(set_replay_id_self, event): | |||||
replay_path = Path(event.src_path) | ||||||
if replay_monitor.replay_preference == ReplayPreference.CALCULATED_GG: | ||||||
upload_to_calculated_gg(replay_path) | ||||||
if replay_monitor.replay_preference == ReplayPreference.ANONYMIZE_REPLAY: | ||||||
anonymize_replay(replay_path, replay_monitor.working_dir) | ||||||
replay_monitor.replay_id = parse_replay_id(replay_path) | ||||||
|
||||||
def on_created(set_replay_id_self, event): | ||||||
NicEastvillage marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if event.is_directory: return | ||||||
assert event.src_path.endswith('.replay') | ||||||
nonlocal replay_monitor | ||||||
replay_path = Path(event.src_path) | ||||||
if replay_monitor.replay_preference == ReplayPreference.CALCULATED_GG: | ||||||
upload_to_calculated_gg(replay_path) | ||||||
if replay_monitor.replay_preference == ReplayPreference.ANONYMIZE_REPLAY: | ||||||
anonymize_replay(replay_path, replay_monitor.working_dir) | ||||||
replay_monitor.replay_id = parse_replay_id(replay_path) | ||||||
|
||||||
def on_created(self, event): | ||||||
pass | ||||||
def on_deleted(self, event): | ||||||
pass | ||||||
def on_moved(self, event): | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not recommend using the autoleagueplay folder as your working directory for league play (I am assuming that is why you had to add this). That will get annoying since ALP creates a lot of files. Either use
league/
which is already ignored or a totally different folder.