|
@ -12,9 +12,11 @@ import hra.lib as lib |
|
|
def json_endpoint(f): |
|
|
def json_endpoint(f): |
|
|
def l(*arg, **kvarg): |
|
|
def l(*arg, **kvarg): |
|
|
x = f(*arg, **kvarg) |
|
|
x = f(*arg, **kvarg) |
|
|
response = e.get_response() |
|
|
response = app.response_class( |
|
|
response.content_type = "application/json" |
|
|
response=json.dumps(x), |
|
|
response.data = json.dumps(x) |
|
|
status=200, |
|
|
|
|
|
mimetype='application/json' |
|
|
|
|
|
) |
|
|
return response |
|
|
return response |
|
|
l.__name__ = f.__name__ |
|
|
l.__name__ = f.__name__ |
|
|
return l |
|
|
return l |
|
@ -46,26 +48,33 @@ def get_context(): |
|
|
@app.route("/api/state", methods=['GET']) |
|
|
@app.route("/api/state", methods=['GET']) |
|
|
@json_endpoint |
|
|
@json_endpoint |
|
|
def api_state(): |
|
|
def api_state(): |
|
|
|
|
|
ses = db.get_session() |
|
|
team = get_context() |
|
|
team = get_context() |
|
|
game = team.game |
|
|
game = team.game |
|
|
|
|
|
game = game.lock() |
|
|
team_id = team.team_id |
|
|
team_id = team.team_id |
|
|
min_round = args_get("min_round", int, True) |
|
|
min_round = args_get("min_round", int, True) |
|
|
t = datetime.now() |
|
|
t = datetime.now() |
|
|
state = game.current_state() |
|
|
state = game.current_state() |
|
|
if state is None: |
|
|
if state is None: |
|
|
return json.dumps({ |
|
|
ses.commit() |
|
|
|
|
|
return { |
|
|
"status": "working", |
|
|
"status": "working", |
|
|
"wait": 1.0, |
|
|
"wait": 1.0, |
|
|
}) |
|
|
} |
|
|
if game.step_mode == db.StepMode.automatic: |
|
|
if game.step_mode == db.StepMode.automatic: |
|
|
time_to_end = max(1, game.step_every_s - (t - state.create_time).total_seconds()) |
|
|
time_to_end = max(1, game.step_every_s - (t - state.create_time).total_seconds()) |
|
|
else: |
|
|
else: |
|
|
time_to_end = None |
|
|
time_to_end = None |
|
|
if min_round is not None and min_round > game.current_round: |
|
|
if min_round is not None and min_round > game.current_round: |
|
|
return json.dumps({ |
|
|
ses.commit() |
|
|
|
|
|
return { |
|
|
"status": "too_early", |
|
|
"status": "too_early", |
|
|
"wait": time_to_end + 1.0 if time_to_end is not None else 3.0, |
|
|
"wait": time_to_end + 1.0 if time_to_end is not None else 3.0, |
|
|
}) |
|
|
} |
|
|
|
|
|
move = team.get_move(state.round) |
|
|
|
|
|
move.reads_count += 1 |
|
|
|
|
|
ses.commit() |
|
|
return { |
|
|
return { |
|
|
"status": "ok", |
|
|
"status": "ok", |
|
|
"round": state.round, |
|
|
"round": state.round, |
|
@ -77,10 +86,11 @@ def api_state(): |
|
|
@app.route("/api/action", methods=['POST']) |
|
|
@app.route("/api/action", methods=['POST']) |
|
|
@json_endpoint |
|
|
@json_endpoint |
|
|
def api_action(): |
|
|
def api_action(): |
|
|
|
|
|
ses = db.get_session() |
|
|
def to_late(): |
|
|
def to_late(): |
|
|
return json.dumps({ |
|
|
return { |
|
|
"status": "too_late", |
|
|
"status": "too_late", |
|
|
}) |
|
|
} |
|
|
team = get_context() |
|
|
team = get_context() |
|
|
game = team.game |
|
|
game = team.game |
|
|
team_id = team.team_id |
|
|
team_id = team.team_id |
|
@ -95,6 +105,10 @@ def api_action(): |
|
|
try: |
|
|
try: |
|
|
warnings = game.get_logic().validate_move(state.get_state(), team_id, j, round_id) |
|
|
warnings = game.get_logic().validate_move(state.get_state(), team_id, j, round_id) |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
|
|
|
game = game.lock() |
|
|
|
|
|
move = team.get_move(state.round_id) |
|
|
|
|
|
move.err_pushs_count += 1 |
|
|
|
|
|
ses.commit() |
|
|
return { |
|
|
return { |
|
|
"status": "error", |
|
|
"status": "error", |
|
|
"description": f"{type(e).__name__}: {e}" |
|
|
"description": f"{type(e).__name__}: {e}" |
|
@ -107,14 +121,16 @@ def api_action(): |
|
|
db.get_session().commit() |
|
|
db.get_session().commit() |
|
|
return to_late() |
|
|
return to_late() |
|
|
|
|
|
|
|
|
move = db.get_session().query(db.Move).filter_by(game_id=game.game_id, team_id=team_id, round=round_id).one_or_none() |
|
|
move = team.get_move(state.round) |
|
|
if move is None: |
|
|
|
|
|
move = db.Move(game_id=game.game_id, team_id=team_id, round=round_id) |
|
|
|
|
|
db.get_session().add(move) |
|
|
|
|
|
|
|
|
|
|
|
move.move = db.new_big_data(j) |
|
|
move.move = db.new_big_data(j) |
|
|
|
|
|
|
|
|
db.get_session().commit() |
|
|
if warnings is None: |
|
|
|
|
|
move.ok_pushs_count += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
move.warnings_pushs_count += 1 |
|
|
|
|
|
|
|
|
|
|
|
ses.commit() |
|
|
|
|
|
|
|
|
if warnings is not None: |
|
|
if warnings is not None: |
|
|
return warnings |
|
|
return warnings |
|
|