|
|
@ -9,6 +9,17 @@ from hra.web import app, NeedLoginError |
|
|
|
import hra.web.jinja_mac as jinja_mac |
|
|
|
import hra.lib as lib |
|
|
|
|
|
|
|
def json_endpoint(f): |
|
|
|
def l(*arg, **kvarg): |
|
|
|
x = f(*arg, **kvarg) |
|
|
|
response = e.get_response() |
|
|
|
response.content_type = "application/json" |
|
|
|
response.data = json.dumps(x) |
|
|
|
return response |
|
|
|
l.__name__ = f.__name__ |
|
|
|
return l |
|
|
|
|
|
|
|
|
|
|
|
def args_get(name, type, optional=False, default=None): |
|
|
|
v = request.args.get(name) |
|
|
|
if v is None: |
|
|
@ -33,6 +44,7 @@ def get_context(): |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/state", methods=['GET']) |
|
|
|
@json_endpoint |
|
|
|
def api_state(): |
|
|
|
team = get_context() |
|
|
|
game = team.game |
|
|
@ -54,15 +66,16 @@ def api_state(): |
|
|
|
"status": "too_early", |
|
|
|
"wait": time_to_end + 1.0 if time_to_end is not None else 3.0, |
|
|
|
}) |
|
|
|
return json.dumps({ |
|
|
|
return { |
|
|
|
"status": "ok", |
|
|
|
"round": state.round, |
|
|
|
"team_id": team_id, |
|
|
|
"time_to_response": time_to_end, |
|
|
|
"state": game.get_logic().personalize_state(state.get_state(), team_id, state.round), |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
@app.route("/api/action", methods=['POST']) |
|
|
|
@json_endpoint |
|
|
|
def api_action(): |
|
|
|
def to_late(): |
|
|
|
return json.dumps({ |
|
|
@ -82,10 +95,10 @@ def api_action(): |
|
|
|
try: |
|
|
|
warnings = game.get_logic().validate_move(state.get_state(), team_id, j, round_id) |
|
|
|
except Exception as e: |
|
|
|
return json.dumps({ |
|
|
|
return { |
|
|
|
"status": "error", |
|
|
|
"description": f"{type(e).__name__}: {e}" |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
db.get_session().expire_all() |
|
|
|
game = game.lock() |
|
|
@ -104,13 +117,14 @@ def api_action(): |
|
|
|
db.get_session().commit() |
|
|
|
|
|
|
|
if warnings is not None: |
|
|
|
return json.dumps(warnings) |
|
|
|
return json.dumps({ |
|
|
|
return warnings |
|
|
|
return { |
|
|
|
"status": "ok", |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/step", methods=['POST']) |
|
|
|
@json_endpoint |
|
|
|
def api_step(): |
|
|
|
team = get_context() |
|
|
|
game = team.game |
|
|
@ -119,6 +133,6 @@ def api_step(): |
|
|
|
|
|
|
|
lib.game_step(game.game_id) |
|
|
|
|
|
|
|
return json.dumps({ |
|
|
|
return { |
|
|
|
"status": "ok", |
|
|
|
}) |
|
|
|
} |
|
|
|