Browse Source

Startegická: Počítání bodů

master
Jiří Kalvoda 2 years ago
parent
commit
518879428e
  1. 6
      server/hra/db.py
  2. 15
      server/hra/lib.py
  3. 7
      server/hra/web/pages.py

6
server/hra/db.py

@ -187,6 +187,12 @@ class Move(Base):
team_id = Column(Integer, primary_key=True) team_id = Column(Integer, primary_key=True)
move = Column(Integer, ForeignKey('bigdata.id'), nullable=True) move = Column(Integer, ForeignKey('bigdata.id'), nullable=True)
points = Column(Integer, nullable=False, default=0)
reads_count = Column(Integer, nullable=False, default=0)
ok_pushs_count = Column(Integer, nullable=False, default=0)
warnings_pushs_count = Column(Integer, nullable=False, default=0)
err_pushs_count = Column(Integer, nullable=False, default=0)
def get_move(self): def get_move(self):
if self.move is None: if self.move is None:
return None return None

15
server/hra/lib.py

@ -26,16 +26,24 @@ def game_step(game_id: int):
old_state = ses.query(db.State).filter_by(game_id=game.game_id, round=old_round_id).one_or_none().get_state() old_state = ses.query(db.State).filter_by(game_id=game.game_id, round=old_round_id).one_or_none().get_state()
moves = [None for _ in range(game.teams_count)] moves = [None for _ in range(game.teams_count)]
points = [0 for _ in range(game.teams_count)]
for i in ses.query(db.Move).filter_by(game_id=game.game_id, round=old_round_id).all(): for i in ses.query(db.Move).filter_by(game_id=game.game_id, round=old_round_id).all():
moves[i.team_id] = i.get_move() moves[i.team_id] = i.get_move()
points[i.team_id] = i.points
ses.commit() ses.commit()
x, points = game.get_logic().step(old_state, moves, old_round_id) x, add_points = game.get_logic().step(old_state, moves, old_round_id)
points = [a+b for a,b in zip(points, add_points)]
new_state = db.State(game_id=game.game_id, round=new_round_id, state=db.new_big_data(x), create_time=time) new_state = db.State(game_id=game.game_id, round=new_round_id, state=db.new_big_data(x), create_time=time)
ses.add(new_state) ses.add(new_state)
for i in range(game.teams_count):
db_move = db.Move(team_id=i, game_id=game_id, round=new_round_id, points=points[i])
ses.add(db_move)
ses.expire_all() ses.expire_all()
game = ses.query(db.Game).filter_by(game_id=game_id).with_for_update().one_or_none() game = ses.query(db.Game).filter_by(game_id=game_id).with_for_update().one_or_none()
assert game is not None assert game is not None
@ -65,7 +73,6 @@ def create_game(mode, teams_count, configuration={}, test_for=None, name=None, s
s = db.State(game_id=g.game_id, round=0, state=db.new_big_data(g.get_logic().zero_state()), create_time=datetime.now()) s = db.State(game_id=g.game_id, round=0, state=db.new_big_data(g.get_logic().zero_state()), create_time=datetime.now())
ses.add(s) ses.add(s)
if test_for is not None: if test_for is not None:
for i in range(teams_count): for i in range(teams_count):
t = db.Team(team_id=i, game_id=g.game_id, name=f"test_{i}", user_id=test_for.id) t = db.Team(team_id=i, game_id=g.game_id, name=f"test_{i}", user_id=test_for.id)
@ -75,6 +82,10 @@ def create_game(mode, teams_count, configuration={}, test_for=None, name=None, s
t = db.Team(team_id=i, game_id=g.game_id, name="") t = db.Team(team_id=i, game_id=g.game_id, name="")
ses.add(t) ses.add(t)
for i in range(teams_count):
db_move = db.Move(team_id=i, game_id=g.game_id, round=0)
ses.add(db_move)
g.current_round = 0 g.current_round = 0
g.working_on_next_state = False g.working_on_next_state = False

7
server/hra/web/pages.py

@ -230,6 +230,9 @@ def web_game(game_id):
ses = db.get_session() ses = db.get_session()
game = ses.query(db.Game).filter_by(game_id=game_id).one_or_none() game = ses.query(db.Game).filter_by(game_id=game_id).one_or_none()
teams = ses.query(db.Team).filter_by(game_id=game_id).order_by(db.Team.team_id).all() teams = ses.query(db.Team).filter_by(game_id=game_id).order_by(db.Team.team_id).all()
moves = ses.query(db.Move).filter_by(game_id=game_id, round=game.current_round).order_by(db.Move.team_id).all()
assert len(teams) == len(moves)
if game is None: if game is None:
raise werkzeug.exceptions.NotFound() raise werkzeug.exceptions.NotFound()
if not right_for_game(game): if not right_for_game(game):
@ -246,11 +249,13 @@ def web_game(game_id):
with b.thead(): with b.thead():
b.line().th()("Id") b.line().th()("Id")
b.line().th()("User") b.line().th()("User")
b.line().th()("Bodů")
b.line().th()("Akce") b.line().th()("Akce")
for team in teams: for team, move in zip(teams, moves):
with b.tr(): with b.tr():
b.line().td()(team.team_id) b.line().td()(team.team_id)
b.line().td()(user_link(team.user),": ", team.name) b.line().td()(user_link(team.user),": ", team.name)
b.line().td()(move.points)
with b.td(): with b.td():
with b.div(_class="btn-group", role="group"): with b.div(_class="btn-group", role="group"):
if right_for_team(team): if right_for_team(team):

Loading…
Cancel
Save