You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.0 KiB

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
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]):
s = state.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}")
with b.table(_class="game_table"):
for i, row in enumerate(s["map"]):
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["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["map"]):
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.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: {occupied_by_team}")
if home_for_team is not None:
b.p(_class=f"game_team_{home_for_team}").b(f"Domov týmu: {home_for_team}")
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 {m['team']}")
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()