add path to GameStorage and add option to enable store game state (default its disabled)
This commit is contained in:
+19
-6
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user