main #1

Merged
daniel156161 merged 15 commits from main into dev 2024-04-13 03:03:13 +02:00
3 changed files with 11 additions and 9 deletions
Showing only changes of commit 4173544d08 - Show all commits
+7 -5
View File
@@ -14,8 +14,8 @@ from server.Files import read_file, save_file
from server.GameStorage import GameStorage from server.GameStorage import GameStorage
from config import SNAKE from config import SNAKE
from dotenv import load_dotenv, find_dotenv
from datetime import datetime from datetime import datetime
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
@@ -23,7 +23,7 @@ import json, os
# 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__) game_state_storage = GameStorage(SNAKE.__class__.__name__)
def info() -> typing.Dict: def info() -> 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)
if not snake: if not snake:
@@ -36,20 +36,20 @@ def info() -> typing.Dict:
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:dict):
game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"]) game_state_storage.start_new_game(game_state["game"], game_state["board"], game_state["you"])
SNAKE.clear_history() 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(game_state: typing.Dict) -> typing.Dict: def move(game_state:dict) -> dict:
next_move = SNAKE.choose_move(game_state) next_move = SNAKE.choose_move(game_state)
game_state_storage.add_moves(game_state["board"], next_move) 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(game_state: typing.Dict): def end(game_state:dict):
HISTORY_PATH = os.path.join(os.path.dirname(__file__), 'data', 'history') 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.add_end_state(game_state["board"], SNAKE.get_history())
@@ -59,5 +59,7 @@ def end(game_state: typing.Dict):
# Start server when `python main.py` is run # Start server when `python main.py` is run
if __name__ == "__main__": if __name__ == "__main__":
load_dotenv(find_dotenv())
from server.server import run_server from server.server import run_server
run_server({"info": info, "start": start, "move": move, "end": end}) run_server({"info": info, "start": start, "move": move, "end": end})
+1
View File
@@ -5,5 +5,6 @@ itsdangerous==2.1.2
Jinja2==3.1.3 Jinja2==3.1.3
MarkupSafe==2.1.5 MarkupSafe==2.1.5
numpy==1.26.4 numpy==1.26.4
python-dotenv==1.0.1
scipy==1.12.0 scipy==1.12.0
Werkzeug==3.0.1 Werkzeug==3.0.1
+3 -4
View File
@@ -1,12 +1,10 @@
import logging import logging
import os import os
import typing
from config import DEBUG
from flask import Flask from flask import Flask
from flask import request from flask import request
def run_server(handlers: typing.Dict): def run_server(handlers:dict):
app = Flask("Battlesnake") app = Flask("Battlesnake")
@app.get("/") @app.get("/")
@@ -39,8 +37,9 @@ def run_server(handlers: typing.Dict):
host = "0.0.0.0" host = "0.0.0.0"
port = int(os.environ.get("PORT", "8000")) port = int(os.environ.get("PORT", "8000"))
debug = bool(os.environ.get("debug", False))
logging.getLogger("werkzeug").setLevel(logging.ERROR) logging.getLogger("werkzeug").setLevel(logging.ERROR)
print(f"\nRunning Battlesnake at http://{host}:{port}") print(f"\nRunning Battlesnake at http://{host}:{port}")
app.run(host=host, port=port, debug=DEBUG) app.run(host=host, port=port, debug=debug)