-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·65 lines (45 loc) · 1.69 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
from flask import Flask, request, jsonify
import os
import strategies
import data
import logs
import logging
HEAD_TYPE = os.getenv("HEAD_TYPE", "safe")
TAIL_TYPE = os.getenv("TAIL_TYPE", "hook")
COLOR = os.getenv("COLOR", "#A0522D")
app = Flask(__name__)
app.logger.handlers[0].setFormatter(logs.LogFormatter())
app.logger.setLevel(os.getenv("LOG_LEVEL", logging.DEBUG))
@app.after_request
def debug_response(response):
if response.content_type == "application/json":
app.logger.debug("RESPONSE: %s", response.get_json())
return response
@app.route("/")
def base():
return "nothing to see here, move along"
@app.route("/start", methods=["POST"])
def start():
request_data = request.get_json(force=True)
app.logger.debug("START: %s", request_data)
return jsonify({"color": COLOR, "headType": HEAD_TYPE, "tailType": TAIL_TYPE,})
@app.route("/move", methods=["POST"])
def move():
request_data = request.get_json(force=True)
app.logger.debug("MOVE: %s", request_data)
turn_context = data.build_turn_context(request_data)
app.logger.debug(f"turn = {turn_context.turn}")
app.logger.debug(f"width = {turn_context.width}")
app.logger.debug(f"height = {turn_context.height}")
app.logger.debug(f"snakes = {turn_context.snakes}")
app.logger.debug(f"foods = {turn_context.foods}")
app.logger.debug(f"position = {turn_context.position}")
strategy = strategies.choose_strategy(turn_context)
direction = strategy.get_action()
return jsonify({"move": direction,})
@app.route("/end", methods=["POST"])
def end():
return jsonify({})
if __name__ == "__main__":
app.run(debug=True, port=int(os.getenv("PORT", 8000)))