from flask import Flask, redirect, flash, session, g, request, get_flashed_messages, Markup from wtforms import Form, BooleanField, StringField, PasswordField, validators, SubmitField, IntegerField, DateTimeField from wtforms.validators import ValidationError from flask_wtf import FlaskForm from flask_bootstrap import Bootstrap import time from datetime import datetime from sqlalchemy import exc, update import werkzeug.exceptions import wtforms from wtforms.fields import EmailField from wtforms.widgets import NumberInput from typing import Optional, Any, List import hra.config as config import hra.web.html as html import hra.db as db from hra.web import app import hra.web.jinja_mac as jinja_mac from hra.util import hash_passwd from hra.web.pages import BasePage wlogic_by_mode = {} def add_wlogic(cls): wlogic_by_mode[cls.__name__.lower()] = cls def get_wlogic(game): if game.game_mode in wlogic_by_mode: cls = wlogic_by_mode[game.game_mode] else: cls = WLogic return cls(game) class WLogic: def __init__(self, game): self.game = game self.logic = game.get_logic() @add_wlogic class Occupy(WLogic): def view(self, state: db.State, team: Optional[db.Team], teams: List[db.Team]): s = state.get_state() if team is not None: s = self.logic.personalize_state(s, team.team_id, state.round) b = BasePage() b.h2(f"Hra {self.game.print()} kolo {state.round}") b.p().b(_class=f"game_team_{team.team_id}")(f"Pohled týmu {team.print()}") with b.table(_class="game_table"): for i, row in enumerate(s["world"]): with b.tr(): for j, x in enumerate(row): occupied_by_team = x["occupied_by_team"] home_for_team = x["home_for_team"] members = x["members"] with b.td(): classes = [] with b.a(href=f"#cell_{i}_{j}"): if x["protected_for_team"] is not None: classes.append("game_protected") if x["hill"]: classes.append("game_hill") b(Markup(" ")) else: if home_for_team is not None: classes.append(f'game_home') if occupied_by_team is not None: classes.append(f'game_occupied') classes.append(f'game_occupied_by_{occupied_by_team}') if len(members): b(len(members)) else: b(Markup(" ")) b(_class=" ".join(classes)) for i, row in enumerate(s["world"]): for j, x in enumerate(row): occupied_by_team = x["occupied_by_team"] protected_for_team = x["protected_for_team"] home_for_team = x["home_for_team"] members = x["members"] with b.div(id=f"cell_{i}_{j}", _class="game_tab"): b.h4(f"Políčko {i} {j}") if x["hill"]: b.p().b("Pohoří") else: if occupied_by_team is not None: b.p(_class=f"game_team_{occupied_by_team}").b(f"Obsazeno týmem: {teams[occupied_by_team].print()}") if protected_for_team is not None: b.p(_class=f"game_team_{protected_for_team}").b(f"Ochranné území týmu: {teams[protected_for_team].print()}") if home_for_team is not None: b.p(_class=f"game_team_{home_for_team}").b(f"Domov týmu: {teams[home_for_team].print()}") b.p().b(f"Počet osob: {len(members)}") with b.ul(): for m in members: b.li(_class=f"game_team_{home_for_team}")(f"Voják {m['id']} týmu {teams[m['team']].print()}") b.wrap( limited_size=False, sticky_head=False, head=lambda x:x.link(rel="stylesheet", href=app.url_for('static', filename='occupy.css'), type='text/css', media="all") ) return b.print_file()