diff --git a/server/GameStorage.py b/server/GameStorage.py index 11c8818..7bed363 100644 --- a/server/GameStorage.py +++ b/server/GameStorage.py @@ -1,8 +1,10 @@ from server.Files import save_file +import os class GameStorage: - def __init__(self, snake:str): + def __init__(self, snake:str, path:str): self.snake_type = snake + self.folder = path def start_new_game(self, game_type:dict, game_board:dict, snake:dict): self.game_type = game_type @@ -19,7 +21,7 @@ class GameStorage: self.snake_history = snake_history_state def save(self, path:str, callback=None, **kwargs): - save_file(path, { + save_file(os.path.join(self.folder, path), { "snake": { "type": self.snake_type, "choices": self.snake_history, diff --git a/server/Server.py b/server/Server.py index bb41a35..034da24 100644 --- a/server/Server.py +++ b/server/Server.py @@ -15,10 +15,10 @@ class Server: self.port = port self.debug = debug self.snake = snake - self.game_state_storage = GameStorage(snake.__class__.__name__) self.config_file = os.path.join(data_path, 'data', 'snake-config.json') - self.history_path = os.path.join(data_path, 'data', 'history') + self.game_state_storage = GameStorage(snake.__class__.__name__, path=os.path.join(data_path, 'data', 'history')) + self.store_game_state = False self.app = Flask("Battlesnake") @@ -63,6 +63,9 @@ class Server: save_file(self.config_file, snake_config, callback=json.dump, indent=2, ensure_ascii=False) return snake_config + def enable_store_game_state(self): + self.store_game_state = True + # info is called when you create your Battlesnake on play.battlesnake.com # and controls your Battlesnake's appearance # TIP: If you open your Battlesnake URL in a browser you should see this data @@ -73,19 +76,29 @@ class Server: # start is called when your Battlesnake begins a game def _start(self, game_state:dict): - self.game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"]) + if self.store_game_state: + self.game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"]) + self.snake.clear_history() print("GAME START:", game_state["game"]) # move is called when your Battlesnake game is running game def _move(self, game_state:dict) -> dict: next_move = self.snake.choose_move(game_state) - self.game_state_storage.add_moves(game_state["board"], next_move) + + if self.store_game_state: + self.game_state_storage.add_moves(game_state["board"], next_move) + print("MOVE:", f"{next_move:5},", "Me:", {"head": game_state["you"]["head"], "length": game_state["you"]["length"]}) return {"move": next_move} # end is called when your Battlesnake finishes a game def _end(self, game_state:dict): - self.game_state_storage.add_end_state(game_state["board"], self.snake.get_history()) - self.game_state_storage.save(os.path.join(self.history_path, f"{self.snake.__class__.__name__}_{datetime.now().strftime('%d.%m.%Y_%H%M%S')}_{game_state['game']['id']}.json"), callback=json.dump, indent=2, ensure_ascii=False) + if self.store_game_state: + self.game_state_storage.add_end_state(game_state["board"], self.snake.get_history()) + self.game_state_storage.save( + f"{self.snake.__class__.__name__}_{datetime.now().strftime('%d.%m.%Y_%H%M%S')}_{game_state['game']['id']}.json", + callback=json.dump, indent=2, ensure_ascii=False + ) + print("GAME OVER\n")