def parsed_replay_processing(protobuf_game, query_params: Dict[str, any] = None, preserve_upload_date=True) -> bool: logger.debug("Successfully parsed replay adding data to DB") # Process if query_params is None or len(query_params) == 0: match_exists = add_objects(protobuf_game, preserve_upload_date=preserve_upload_date) return match_exists query_params = parse_query_params(upload_file_query_params, query_params, add_secondary=True) if 'player_id' in query_params: protobuf_game.game_metadata.primary_player.id = query_params[ 'player_id'] match_exists = add_objects(protobuf_game, preserve_upload_date=preserve_upload_date) logger.debug("SUCCESS: Added base data to db adding query params") if query_params is None or len(query_params) == 0: return match_exists try: game_id = protobuf_game.game_metadata.match_guid if game_id == "": game_id = protobuf_game.game_metadata.id except: game_id = None error_counter = [] # Add game visibility option try: apply_game_visibility(query_params=query_params, game_id=game_id, game_exists=match_exists, proto_game=protobuf_game) except CalculatedError as e: error_counter.append('visibility') ErrorLogger.log_error(e, message='Error changing visibility', logger=logger) # Add game visibility option try: apply_tags_to_game(query_params=query_params, game_id=game_id) except CalculatedError as e: error_counter.append('tags') ErrorLogger.log_error(e, message='Error adding tags', logger=logger) if len(error_counter) == 0: logger.debug("SUCCESS: Processed all query params") else: logger.warning('Found ' + str(len(error_counter)) + ' errors while processing query params: ' + str(error_counter)) return match_exists
def initialize_db_with_parsed_replays(parsed_replays, session=None): if session is None: session = initialize_db() add_initial_player(session) for proto in parsed_replays: add_objects(proto, session=session) return session
def test_same_ranks(self): match: PlayerGame = self.session.query(PlayerGame).filter( PlayerGame.game == self.guid).first() assert (match is not None) rank = match.rank add_objects(self.proto, session=self.session) match: PlayerGame = self.session.query(PlayerGame).filter( PlayerGame.game == self.guid).first() assert (rank == match.rank)
def test_same_upload_date(self): match: Game = self.session.query(Game).filter( Game.hash == self.guid).first() assert (match is not None) upload_date = match.upload_date add_objects(self.proto, session=self.session) match: Game = self.session.query(Game).filter( Game.hash == self.guid).first() assert (upload_date == match.upload_date)
def test_same_ranks(self): add_objects(self.proto, session=self.session) match: PlayerGame = self.session.query(PlayerGame).filter(PlayerGame.game == self.guid).first() self.assertIsNotNone(match) rank = match.rank time.sleep(1) add_objects(self.proto, session=self.session) match: PlayerGame = self.session.query(PlayerGame).filter(PlayerGame.game == self.guid).first() self.assertEqual(rank, match.rank)
def test_same_upload_date(self): add_objects(self.proto, session=self.session) match: Game = self.session.query(Game).filter(Game.hash == self.guid).first() self.assertIsNotNone(match) upload_date = match.upload_date time.sleep(1) add_objects(self.proto, session=self.session) match: Game = self.session.query(Game).filter(Game.hash == self.guid).first() self.assertEqual(upload_date, match.upload_date)
def initialize_db_with_replays(replay_list, session=None): if session is None: session = initialize_db() guids = [] protos = [] for replay in replay_list: replay, proto, guid = parse_file(replay) add_objects(proto, session=session) guids.append(guid) protos.append(proto) return session, protos, guids
def api_upload_proto(): print('Proto uploaded') # Convert to byte files from base64 response = request.get_json() proto_in_memory = io.BytesIO( base64.b64decode(gzip.decompress(response['proto']))) pandas_in_memory = io.BytesIO( base64.b64decode(gzip.decompress(response['pandas']))) protobuf_game = ProtobufManager.read_proto_out_from_file(proto_in_memory) # Path creation filename = protobuf_game.game_metadata.match_guid if filename == '': filename = protobuf_game.game_metadata.id filename += '.replay' parsed_path = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'data', 'parsed', filename) id_replay_path = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'data', 'rlreplays', protobuf_game.game_metadata.id + '.replay') guid_replay_path = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'data', 'rlreplays', filename) # Process add_objects(protobuf_game) # Write to disk proto_in_memory.seek(0) pandas_in_memory.seek(0) with open(parsed_path + '.pts', 'wb') as f: f.write(proto_in_memory.read()) with open(parsed_path + '.gzip', 'wb') as f: f.write(pandas_in_memory.read()) # Cleanup if os.path.isfile(id_replay_path): shutil.move(id_replay_path, guid_replay_path) return jsonify({'Success': True})
def test_add(self): add_objects(self.proto, session=self.session) match = self.session.query(PlayerGame).filter(PlayerGame.game == self.guid).first() self.assertIsNotNone(match)