main #3

Merged
daniel156161 merged 5 commits from main into dev 2024-04-13 11:48:47 +02:00
Showing only changes of commit 615acfc75a - Show all commits
+11 -6
View File
@@ -15,8 +15,10 @@ class Server:
self.snake = snake self.snake = snake
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.game_state_storage = GameStorage(snake.__class__.__name__, path=os.path.join(data_path, 'data', 'history')) self.data_path = data_path
self.store_game_state = False self.store_game_state = False
self.running_games:dict[str, TemplateSnake] = {}
self.app = Flask("Battlesnake") self.app = Flask("Battlesnake")
@@ -75,7 +77,8 @@ 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: if self.store_game_state:
self.game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"]) self.running_games[game_state["game"]["id"]] = GameStorage(self.snake.__class__.__name__, path=os.path.join(self.data_path, 'data', 'history'))
self.running_games[game_state["game"]["id"]].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"])
@@ -85,7 +88,9 @@ class Server:
next_move = self.snake.choose_move(game_state) next_move = self.snake.choose_move(game_state)
if self.store_game_state: if self.store_game_state:
self.game_state_storage.add_moves(game_state["board"], next_move) self.running_games[game_state["game"]["id"]].add_moves(game_state["board"], next_move)
if self.debug:
print(self.running_games[game_state["game"]["id"]])
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}
@@ -93,11 +98,11 @@ class Server:
# 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: if self.store_game_state:
self.game_state_storage.add_end_state(game_state["board"], self.snake.get_history()) self.running_games[game_state["game"]["id"]].add_end_state(game_state["board"], self.snake.get_history(), game_state["turn"])
self.game_state_storage.set_winner_snake_name(game_state["board"]['snakes']) self.running_games[game_state["game"]["id"]].save(
self.game_state_storage.save(
f"{self.snake.__class__.__name__}_{datetime.now().strftime('%d.%m.%Y_%H%M%S')}_{game_state['game']['id']}.json", 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 callback=json.dump, indent=2, ensure_ascii=False
) )
del self.running_games[game_state["game"]["id"]]
print("GAME OVER:\n- Winner is", [ x["name"] for x in game_state["board"]['snakes']]) print("GAME OVER:\n- Winner is", [ x["name"] for x in game_state["board"]['snakes']])