|
@ -1,6 +1,7 @@ |
|
|
from flask import Flask, redirect, flash, render_template, session, g, request, get_flashed_messages |
|
|
from flask import Flask, redirect, flash, render_template, session, g, request, get_flashed_messages |
|
|
import werkzeug.exceptions |
|
|
import werkzeug.exceptions |
|
|
import json |
|
|
import json |
|
|
|
|
|
import traceback |
|
|
from datetime import datetime |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
import hra.config as config |
|
|
import hra.config as config |
|
@ -9,6 +10,23 @@ from hra.web import app, NeedLoginError |
|
|
import hra.web.jinja_mac as jinja_mac |
|
|
import hra.web.jinja_mac as jinja_mac |
|
|
import hra.lib as lib |
|
|
import hra.lib as lib |
|
|
|
|
|
|
|
|
|
|
|
def log(team, endpoint, status, **kvarg): |
|
|
|
|
|
x = db.Log( |
|
|
|
|
|
source_ip=request.remote_addr, |
|
|
|
|
|
user_id=g.user.id, |
|
|
|
|
|
team_id=team.team_id, |
|
|
|
|
|
game_id=team.game_id, |
|
|
|
|
|
endpoint=endpoint, |
|
|
|
|
|
status=status, |
|
|
|
|
|
url=request.path, |
|
|
|
|
|
get=request.args, |
|
|
|
|
|
time=datetime.now(), |
|
|
|
|
|
**kvarg |
|
|
|
|
|
) |
|
|
|
|
|
db.get_session().add(x) |
|
|
|
|
|
return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def json_endpoint(f): |
|
|
def json_endpoint(f): |
|
|
def l(*arg, **kvarg): |
|
|
def l(*arg, **kvarg): |
|
|
x = f(*arg, **kvarg) |
|
|
x = f(*arg, **kvarg) |
|
@ -57,6 +75,7 @@ def api_state(): |
|
|
t = datetime.now() |
|
|
t = datetime.now() |
|
|
state = game.current_state() |
|
|
state = game.current_state() |
|
|
if state is None: |
|
|
if state is None: |
|
|
|
|
|
log(team, db.Endpoint.state, "working", text="wait 1") |
|
|
ses.commit() |
|
|
ses.commit() |
|
|
return { |
|
|
return { |
|
|
"status": "working", |
|
|
"status": "working", |
|
@ -67,13 +86,16 @@ def api_state(): |
|
|
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: |
|
|
|
|
|
wait = time_to_end + 1.0 if time_to_end is not None else 3.0 |
|
|
|
|
|
log(team, db.Endpoint.state, "too_early", text=f"wait {wait}; min_round {min_round}", round=game.current_round) |
|
|
ses.commit() |
|
|
ses.commit() |
|
|
return { |
|
|
return { |
|
|
"status": "too_early", |
|
|
"status": "too_early", |
|
|
"wait": time_to_end + 1.0 if time_to_end is not None else 3.0, |
|
|
"wait": wait, |
|
|
} |
|
|
} |
|
|
move = team.get_move(state.round) |
|
|
move = team.get_move(state.round) |
|
|
move.reads_count += 1 |
|
|
move.reads_count += 1 |
|
|
|
|
|
log(team, db.Endpoint.state, "ok", round=game.current_round) |
|
|
ses.commit() |
|
|
ses.commit() |
|
|
return { |
|
|
return { |
|
|
"status": "ok", |
|
|
"status": "ok", |
|
@ -88,6 +110,8 @@ def api_state(): |
|
|
def api_action(): |
|
|
def api_action(): |
|
|
ses = db.get_session() |
|
|
ses = db.get_session() |
|
|
def to_late(): |
|
|
def to_late(): |
|
|
|
|
|
log(team, db.Endpoint.action, "too_late", round=round_id) |
|
|
|
|
|
ses.commit() |
|
|
return { |
|
|
return { |
|
|
"status": "too_late", |
|
|
"status": "too_late", |
|
|
} |
|
|
} |
|
@ -106,19 +130,21 @@ def api_action(): |
|
|
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() |
|
|
game = game.lock() |
|
|
move = team.get_move(state.round_id) |
|
|
move = team.get_move(state.round) |
|
|
move.err_pushs_count += 1 |
|
|
move.err_pushs_count += 1 |
|
|
|
|
|
description =f"{type(e).__name__}: {e}" |
|
|
|
|
|
log(team, db.Endpoint.action, "error", round=round_id, text=description, data=db.new_big_data(traceback.format_exc())) |
|
|
ses.commit() |
|
|
ses.commit() |
|
|
return { |
|
|
return { |
|
|
"status": "error", |
|
|
"status": "error", |
|
|
"description": f"{type(e).__name__}: {e}" |
|
|
"description": description |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
db.get_session().expire_all() |
|
|
ses.expire_all() |
|
|
game = game.lock() |
|
|
game = game.lock() |
|
|
|
|
|
|
|
|
if game.working_on_next_state or round_id < game.current_round: |
|
|
if game.working_on_next_state or round_id < game.current_round: |
|
|
db.get_session().commit() |
|
|
ses.commit() |
|
|
return to_late() |
|
|
return to_late() |
|
|
|
|
|
|
|
|
move = team.get_move(state.round) |
|
|
move = team.get_move(state.round) |
|
@ -130,6 +156,7 @@ def api_action(): |
|
|
else: |
|
|
else: |
|
|
move.warnings_pushs_count += 1 |
|
|
move.warnings_pushs_count += 1 |
|
|
|
|
|
|
|
|
|
|
|
log(team, db.Endpoint.action, "warning" if warnings is not None else "ok", round=round_id, data=move.move) |
|
|
ses.commit() |
|
|
ses.commit() |
|
|
|
|
|
|
|
|
if warnings is not None: |
|
|
if warnings is not None: |
|
@ -148,6 +175,8 @@ def api_step(): |
|
|
raise werkzeug.exceptions.Forbidden("Je zakázáno krokovat tuto hru") |
|
|
raise werkzeug.exceptions.Forbidden("Je zakázáno krokovat tuto hru") |
|
|
|
|
|
|
|
|
lib.game_step(game.game_id) |
|
|
lib.game_step(game.game_id) |
|
|
|
|
|
log(team, db.Endpoint.step, "ok") |
|
|
|
|
|
db.get_session.commit() |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
"status": "ok", |
|
|
"status": "ok", |
|
|