add path to GameStorage and add option to enable store game state (default its disabled)

This commit is contained in:
2024-04-13 01:09:26 +02:00
parent 91e0d8fd1b
commit b3d98dbdeb
2 changed files with 23 additions and 8 deletions
+4 -2
View File
@@ -1,8 +1,10 @@
from server.Files import save_file from server.Files import save_file
import os
class GameStorage: class GameStorage:
def __init__(self, snake:str): def __init__(self, snake:str, path:str):
self.snake_type = snake self.snake_type = snake
self.folder = path
def start_new_game(self, game_type:dict, game_board:dict, snake:dict): def start_new_game(self, game_type:dict, game_board:dict, snake:dict):
self.game_type = game_type self.game_type = game_type
@@ -19,7 +21,7 @@ class GameStorage:
self.snake_history = snake_history_state self.snake_history = snake_history_state
def save(self, path:str, callback=None, **kwargs): def save(self, path:str, callback=None, **kwargs):
save_file(path, { save_file(os.path.join(self.folder, path), {
"snake": { "snake": {
"type": self.snake_type, "type": self.snake_type,
"choices": self.snake_history, "choices": self.snake_history,
+16 -3
View File
@@ -15,10 +15,10 @@ class Server:
self.port = port self.port = port
self.debug = debug self.debug = debug
self.snake = snake self.snake = snake
self.game_state_storage = GameStorage(snake.__class__.__name__)
self.config_file = os.path.join(data_path, 'data', 'snake-config.json') 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") 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) save_file(self.config_file, snake_config, callback=json.dump, indent=2, ensure_ascii=False)
return snake_config 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 # info is called when you create your Battlesnake on play.battlesnake.com
# and controls your Battlesnake's appearance # and controls your Battlesnake's appearance
# TIP: If you open your Battlesnake URL in a browser you should see this data # 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 # start is called when your Battlesnake begins a game
def _start(self, game_state:dict): def _start(self, game_state:dict):
if self.store_game_state:
self.game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"]) self.game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"])
self.snake.clear_history() self.snake.clear_history()
print("GAME START:", game_state["game"]) print("GAME START:", game_state["game"])
# move is called when your Battlesnake game is running game # move is called when your Battlesnake game is running game
def _move(self, game_state:dict) -> dict: def _move(self, game_state:dict) -> dict:
next_move = self.snake.choose_move(game_state) next_move = self.snake.choose_move(game_state)
if self.store_game_state:
self.game_state_storage.add_moves(game_state["board"], next_move) 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"]}) print("MOVE:", f"{next_move:5},", "Me:", {"head": game_state["you"]["head"], "length": game_state["you"]["length"]})
return {"move": next_move} return {"move": next_move}
# end is called when your Battlesnake finishes a game # end is called when your Battlesnake finishes a game
def _end(self, game_state:dict): def _end(self, game_state:dict):
if self.store_game_state:
self.game_state_storage.add_end_state(game_state["board"], self.snake.get_history()) 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) 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") print("GAME OVER\n")