def convert_replay(replay_path: str, out_folder: str) -> Dict: ''' Parses the replay file intro proto and pandas. Returns a dictionary with keys ['proto', 'pandas'] with the paths to each file. ''' id = os.path.splitext(os.path.basename(replay_path))[0] proto_path = os.path.join(out_folder, '{}.pts'.format(id)) pandas_path = os.path.join(out_folder, '{}.gzip'.format(id)) temp_path = tempfile.mktemp(suffix='.json') _json = carball.decompile_replay(replay_path, temp_path) game = Game() game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis() with open(proto_path, 'wb') as f: analysis.write_proto_out_to_file(f) with open(pandas_path, 'wb') as f: analysis.write_pandas_out_to_file(f) return {'proto': proto_path, 'pandas': pandas_path}
def analyze_replay_file(replay_path: str, output_path: str, overwrite=True, controls: ControlsCreator = None, sanity_check: SanityChecker = None): """ Decompile and analyze a replay file. :param replay_path: Path to replay file :param output_path: Path to write JSON :param overwrite: If to overwrite JSON (suggest True if speed is not an issue) :param controls: Generate controls from the replay using our best guesses (ALPHA) :param sanity_check: Run sanity check to make sure we analyzed correctly (BETA) :return: AnalysisManager of game with analysis. """ _json = decompile_replay(replay_path, output_path, overwrite=overwrite) game = Game(loaded_json=_json) # get_controls(game) # TODO: enable and optimise. if controls is not None: controls.get_controls(game) if sanity_check is not None: sanity_check.check_game(game) analysis = AnalysisManager(game) analysis.create_analysis() return analysis
def processReplays(self, replay, outputfile): self.outputfile = outputfile _json = carball.decompile_replay(replay) game = Game() player = Player() # Initialize and perform default analysis game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis() # Get protobuf data to analyze proto_game = analysis.get_protobuf_data() # Readability self.meta = proto_game.game_metadata self.team = proto_game.teams self.players = proto_game.players self.getMetaInfo() self.getPlayerInfo() self.getTeamInfo() self.outputJSON()
def parse_files(abs_path, spell_check, playlist_filter, single_player): temp_output_dir = abs_path + "/TempJSON/" replay_dir = abs_path print("Engine: verifying temp directory") sys.stdout.flush() print("Engine: parsing folder: " + replay_dir) for replay_file in get_files(replay_dir): print("Engine: Analyzing replay: " + replay_file) sys.stdout.flush() _json = carball.decompile_replay(replay_dir + "/" + replay_file, output_path=temp_output_dir + 'decompiled_replay.json', overwrite=True) game = Game() game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis() raw_json = MessageToJson(analysis.protobuf_game) data = json.loads(raw_json) f = open(temp_output_dir + "lastfile.json", "w+") f.write(raw_json) f.close() print("Engine: sending data from " + replay_file + " to Builder") sys.stdout.flush() Builder(data, spell_check, playlist_filter, single_player)
def convert_json_to_game_frames(filename): with open(filename, encoding='utf-8', errors='ignore') as json_file: _json = json.load(json_file) game = Game() game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis() x = ControlsCreator() x.get_controls(game) frames = [] total = 0 duplicates = 0 previous_frame = None for col, row in analysis.data_frame.iterrows(): frame = {} frame["GameState"] = {} frame["GameState"]["time"] = NaN_fixer(row["game"]["time"]) frame["GameState"]["seconds_remaining"] = NaN_fixer( row["game"]["seconds_remaining"]) frame["GameState"]["deltatime"] = NaN_fixer(row["game"]["delta"]) frame["GameState"]["ball"] = {} frame["GameState"]["ball"]["position"] = [ NaN_fixer(row["ball"]["pos_x"]), NaN_fixer(row["ball"]["pos_y"]), NaN_fixer(row["ball"]["pos_z"]) ] frame["GameState"]["ball"]["velocity"] = [ velocity_scaler(NaN_fixer(row["ball"]["vel_x"])), velocity_scaler(NaN_fixer(row["ball"]["vel_y"])), velocity_scaler(NaN_fixer(row["ball"]["vel_z"])) ] frame["GameState"]["ball"]["rotation"] = [ NaN_fixer(row["ball"]["rot_x"]), NaN_fixer(row["ball"]["rot_y"]), NaN_fixer(row["ball"]["rot_z"]) ] frame["PlayerData"] = [] for i in range(len(game.players)): frame["PlayerData"].append( getPlayerFrame(game.players[i], i, col, row)) if previous_frame != None: if duplicateFrameCheck(frame, previous_frame): total += 1 duplicates += 1 continue previous_frame = frame frames.append(frame) total += 1 return frames
import carball from carball.json_parser.game import Game from carball.analysis.analysis_manager import AnalysisManager import sys import os import json _json_name = output_path = sys.argv[2] + '.tmp' _json = carball.decompile_replay(sys.argv[1], _json_name, overwrite=True) game = Game() game.initialize(loaded_json=_json) analysis = AnalysisManager(game) analysis.create_analysis(calculate_intensive_events=False) _output_tmp_name = sys.argv[2] + "-" + 'tmp' + ".json" with open(_output_tmp_name, 'w') as out: analysis.write_json_out_to_file(out) with open(_output_tmp_name) as tmp: data = json.load(tmp) time = data['gameMetadata']['time'] _output_name = sys.argv[2] + "-" + time + ".json" os.rename(_output_tmp_name, _output_name) if os.path.exists(_json_name): os.remove(_json_name) else:
def main(): parser = argparse.ArgumentParser( description='Rocket League replay parsing and analysis.') parser.add_argument( '-i', '--input', type=str, required=True, help= 'Path to replay file that will be analyzed. Carball expects a raw replay file unless ' '--skip-decompile is provided.') parser.add_argument( '--proto', type=str, required=False, help= 'The result of the analysis will be saved to this file in protocol buffers format.' ) parser.add_argument( '--json', type=str, required=False, help= 'The result of the analysis will be saved to this file in json file format. This is not ' 'the decompiled replay json from rattletrap.') parser.add_argument( '--gzip', type=str, required=False, help= 'The pandas data frame containing the replay frames will be saved to this file in a ' 'compressed gzip format.') parser.add_argument( '-sd', '--skip-decompile', action='store_true', default=False, help= 'If set, carball will treat the input file as a json file that Rattletrap outputs.' ) parser.add_argument( '-v', '--verbose', action='count', default=0, help= 'Set the logging level to INFO. To set the logging level to DEBUG use -vv.' ) parser.add_argument('-s', '--silent', action='store_true', default=False, help='Disable logging altogether.') args = parser.parse_args() if not args.proto and not args.json and not args.gzip: parser.error( 'at least one of the following arguments are required: --proto, --json, --gzip' ) log_level = logging.WARNING if args.verbose == 1: log_level = logging.INFO elif args.verbose >= 2: log_level = logging.DEBUG if args.silent: logging.basicConfig(handlers=[logging.NullHandler()]) else: logging.basicConfig(handlers=[logging.StreamHandler()], level=log_level) if args.skip_decompile: game = Game() game.initialize(loaded_json=args.input) manager = AnalysisManager(game) manager.create_analysis() else: manager = carball.analyze_replay_file(args.input) if args.proto: with open(args.proto, 'wb') as f: manager.write_proto_out_to_file(f) if args.json: proto_game = manager.get_protobuf_data() with open(args.json, 'w') as f: f.write(MessageToJson(proto_game)) if args.gzip: with gzip.open(args.gzip, 'wb') as f: manager.write_pandas_out_to_file(f)