change console output and better way to store the game history
This commit is contained in:
@@ -11,13 +11,18 @@
|
|||||||
# For more info see docs.battlesnake.com
|
# For more info see docs.battlesnake.com
|
||||||
|
|
||||||
from server.Files import read_file, save_file
|
from server.Files import read_file, save_file
|
||||||
|
from server.GameStorage import GameStorage
|
||||||
from config import SNAKE
|
from config import SNAKE
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
import typing
|
import typing
|
||||||
import json, os
|
import json, os
|
||||||
|
|
||||||
# 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
|
||||||
|
game_state_storage = GameStorage(SNAKE.__class__.__name__)
|
||||||
|
|
||||||
def info() -> typing.Dict:
|
def info() -> typing.Dict:
|
||||||
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'data', 'snake-config.json')
|
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'data', 'snake-config.json')
|
||||||
snake = read_file(CONFIG_PATH, json.load)
|
snake = read_file(CONFIG_PATH, json.load)
|
||||||
@@ -27,28 +32,31 @@ def info() -> typing.Dict:
|
|||||||
print("INFO", snake)
|
print("INFO", snake)
|
||||||
return snake
|
return snake
|
||||||
|
|
||||||
print("INFO Snake: ", snake)
|
print("INFO Snake:", snake)
|
||||||
return snake
|
return snake
|
||||||
|
|
||||||
# start is called when your Battlesnake begins a game
|
# start is called when your Battlesnake begins a game
|
||||||
def start(game_state: typing.Dict):
|
def start(game_state: typing.Dict):
|
||||||
|
game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"])
|
||||||
SNAKE.clear_history()
|
SNAKE.clear_history()
|
||||||
print("GAME START")
|
print("GAME START:", game_state["game"])
|
||||||
|
|
||||||
|
# move is called when your Battlesnake game is running game
|
||||||
|
def move(game_state: typing.Dict) -> typing.Dict:
|
||||||
|
next_move = SNAKE.choose_move(game_state)
|
||||||
|
game_state_storage.add_moves(game_state["board"], next_move)
|
||||||
|
print("MOVE:", f"{next_move},", "Me:", {"body": game_state["you"]["body"], "head": game_state["you"]["head"], "length": game_state["you"]["length"]})
|
||||||
|
return {"move": next_move}
|
||||||
|
|
||||||
# end is called when your Battlesnake finishes a game
|
# end is called when your Battlesnake finishes a game
|
||||||
def end(game_state: typing.Dict):
|
def end(game_state: typing.Dict):
|
||||||
HISTORY_PATH = os.path.join(os.path.dirname(__file__), 'dataa', 'history')
|
HISTORY_PATH = os.path.join(os.path.dirname(__file__), 'data', 'history')
|
||||||
|
|
||||||
save_file(os.path.join(HISTORY_PATH, f"{SNAKE.__class__.__name__}-{game_state['game']['id']}.json"), SNAKE.get_history(), callback=json.dump, ensure_ascii=False)
|
game_state_storage.add_end_state(game_state["board"], SNAKE.get_history())
|
||||||
|
game_state_storage.save(os.path.join(HISTORY_PATH, f"{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")
|
||||||
|
|
||||||
def move(game_state: typing.Dict) -> typing.Dict:
|
|
||||||
next_move = SNAKE.choose_move(game_state)
|
|
||||||
print(f"MOVE {game_state['turn']}: {next_move}")
|
|
||||||
|
|
||||||
return {"move": next_move}
|
|
||||||
|
|
||||||
# Start server when `python main.py` is run
|
# Start server when `python main.py` is run
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from server.server import run_server
|
from server.server import run_server
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
from server.Files import save_file
|
||||||
|
|
||||||
|
class GameStorage:
|
||||||
|
def __init__(self, snake:str):
|
||||||
|
self.snake_type = snake
|
||||||
|
|
||||||
|
def start_new_game(self, game_type:dict, game_board:dict, snake:dict):
|
||||||
|
self.game_type = game_type
|
||||||
|
self.start_position = snake
|
||||||
|
self.game_board = [game_board]
|
||||||
|
self.moves = []
|
||||||
|
|
||||||
|
def add_moves(self, game_board:dict, my_move:str):
|
||||||
|
self.game_board.append(game_board)
|
||||||
|
self.moves.append(my_move)
|
||||||
|
|
||||||
|
def add_end_state(self, game_board:dict, snake_history_state:list[dict]):
|
||||||
|
self.game_board.append(game_board)
|
||||||
|
self.snake_history = snake_history_state
|
||||||
|
|
||||||
|
def save(self, path:str, callback=None, **kwargs):
|
||||||
|
save_file(path, {
|
||||||
|
"snake": {
|
||||||
|
"type": self.snake_type,
|
||||||
|
"choices": self.snake_history,
|
||||||
|
},
|
||||||
|
"game": {
|
||||||
|
"type": self.game_type,
|
||||||
|
"snake_start": self.start_position,
|
||||||
|
"gameboard": self.game_board,
|
||||||
|
"my_moves": self.moves,
|
||||||
|
}
|
||||||
|
}, callback=callback, **kwargs)
|
||||||
Reference in New Issue
Block a user