Reviewed-on: #1
Co-authored-by: Daniel Dolezal <d.dolezal97@protonmail.com>
Co-committed-by: Daniel Dolezal <d.dolezal97@protonmail.com>
This commit was merged in pull request #1.
This commit is contained in:
2024-04-13 03:03:09 +02:00
committed by daniel156161
parent 5633c4c134
commit 7acc269df4
12 changed files with 478 additions and 114 deletions
Regular → Executable
+21 -47
View File
@@ -1,3 +1,5 @@
#!/usr/bin/env python3
# Welcome to
# __________ __ __ .__ __
# \______ \_____ _/ |__/ |_| | ____ ______ ____ _____ | | __ ____
@@ -10,54 +12,26 @@
# To get you started we've included code to prevent your Battlesnake from moving backwards.
# For more info see docs.battlesnake.com
from server.Files import read_file, save_file
from server.GameStorage import GameStorage
from config import SNAKE
from server.SnakeBuilder import SnakeBuilder
from server.Server import Server
from datetime import datetime
import typing
import json, os
# 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
game_state_storage = GameStorage(SNAKE.__class__.__name__)
def info() -> typing.Dict:
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'data', 'snake-config.json')
snake = read_file(CONFIG_PATH, json.load)
if not snake:
snake = {"apiversion":"1","author":"","color":"#888888","head":"default","tail":"default"}
save_file(CONFIG_PATH, snake, callback=json.dump, indent=2, ensure_ascii=False)
print("INFO", snake)
return snake
print("INFO Snake:", snake)
return snake
# start is called when your Battlesnake begins a game
def start(game_state: typing.Dict):
game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"])
SNAKE.clear_history()
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
def end(game_state: typing.Dict):
HISTORY_PATH = os.path.join(os.path.dirname(__file__), 'data', 'history')
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")
from dotenv import load_dotenv, find_dotenv
import os
# Start server when `python main.py` is run
if __name__ == "__main__":
from server.server import run_server
run_server({"info": info, "start": start, "move": move, "end": end})
load_dotenv(find_dotenv())
server = Server(
data_path=os.path.dirname(__file__),
snake=SnakeBuilder.build(os.environ.get("SNAKE", "DummSnake")),
)
if os.environ.get("STORE_GAME_HISTORY", None):
server.enable_store_game_state()
server.run(
host=os.environ.get("HOST", "0.0.0.0"),
port=int(os.environ.get("PORT", "8000")),
debug=bool(os.environ.get("DEBUG", False))
)