9 changed files with 392 additions and 41 deletions
@ -1,7 +1,16 @@ |
|||||
import bcrypt |
import bcrypt |
||||
|
from time import time |
||||
|
|
||||
def hash_passwd(a): |
def hash_passwd(a): |
||||
salt = b'$2b$12$V2aIKSJC/uozaodwYnQX3e' |
salt = b'$2b$12$V2aIKSJC/uozaodwYnQX3e' |
||||
hashed = bcrypt.hashpw(a.encode('utf-8'), salt) |
hashed = bcrypt.hashpw(a.encode('utf-8'), salt) |
||||
return hashed.decode('us-ascii') |
return hashed.decode('us-ascii') |
||||
|
|
||||
|
def timer_func(func): |
||||
|
def wrap_func(*args, **kwargs): |
||||
|
t1 = time() |
||||
|
result = func(*args, **kwargs) |
||||
|
t2 = time() |
||||
|
print(f'Function {func.__name__!r} executed in {(t2-t1):.4f}s') |
||||
|
return result |
||||
|
return wrap_func |
||||
|
@ -0,0 +1,101 @@ |
|||||
|
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() |
@ -0,0 +1,135 @@ |
|||||
|
|
||||
|
|
||||
|
table.game_table, table.game_table tr, table.game_table tr td { |
||||
|
border: thin solid black; |
||||
|
border-collapse: collapse; |
||||
|
table-layout: fixed; |
||||
|
} |
||||
|
|
||||
|
table.game_table tr td { |
||||
|
width: 10pt; |
||||
|
height: 10px; |
||||
|
font-size: 8px; |
||||
|
text-align: end; |
||||
|
} |
||||
|
|
||||
|
td.game_home { |
||||
|
border: 4pt solid black; |
||||
|
} |
||||
|
td.game_hill { |
||||
|
background-color: black; |
||||
|
} |
||||
|
td.game_occupied { |
||||
|
color: white; |
||||
|
} |
||||
|
td.game_occupied_by_0 { |
||||
|
background-color: #A50000; |
||||
|
} |
||||
|
td.game_occupied_by_1 { |
||||
|
background-color: #00A6A6; |
||||
|
} |
||||
|
td.game_occupied_by_2 { |
||||
|
background-color: #53A600; |
||||
|
} |
||||
|
td.game_occupied_by_3 { |
||||
|
background-color: #5300A6; |
||||
|
} |
||||
|
td.game_occupied_by_4 { |
||||
|
background-color: #A67D00; |
||||
|
} |
||||
|
td.game_occupied_by_5 { |
||||
|
background-color: #002AA6; |
||||
|
} |
||||
|
td.game_occupied_by_6 { |
||||
|
background-color: #00A62A; |
||||
|
} |
||||
|
td.game_occupied_by_7 { |
||||
|
background-color: #A6007D; |
||||
|
} |
||||
|
td.game_occupied_by_8 { |
||||
|
background-color: #A63D00; |
||||
|
} |
||||
|
td.game_occupied_by_9 { |
||||
|
background-color: #0069A6; |
||||
|
} |
||||
|
td.game_occupied_by_10 { |
||||
|
background-color: #16A600; |
||||
|
} |
||||
|
td.game_occupied_by_11 { |
||||
|
background-color: #9000A6; |
||||
|
} |
||||
|
td.game_occupied_by_12 { |
||||
|
background-color: #93A600; |
||||
|
} |
||||
|
td.game_occupied_by_13 { |
||||
|
background-color: #1300A6; |
||||
|
} |
||||
|
td.game_occupied_by_14 { |
||||
|
background-color: #00A667; |
||||
|
} |
||||
|
td.game_occupied_by_15 { |
||||
|
background-color: #A60040; |
||||
|
} |
||||
|
|
||||
|
.game_team_0 { |
||||
|
color: #A50000; |
||||
|
} |
||||
|
.game_team_1 { |
||||
|
color: #00A6A6; |
||||
|
} |
||||
|
.game_team_2 { |
||||
|
color: #53A600; |
||||
|
} |
||||
|
.game_team_3 { |
||||
|
color: #5300A6; |
||||
|
} |
||||
|
.game_team_4 { |
||||
|
color: #A67D00; |
||||
|
} |
||||
|
.game_team_5 { |
||||
|
color: #002AA6; |
||||
|
} |
||||
|
.game_team_6 { |
||||
|
color: #00A62A; |
||||
|
} |
||||
|
.game_team_7 { |
||||
|
color: #A6007D; |
||||
|
} |
||||
|
.game_team_8 { |
||||
|
color: #A63D00; |
||||
|
} |
||||
|
.game_team_9 { |
||||
|
color: #0069A6; |
||||
|
} |
||||
|
.game_team_10 { |
||||
|
color: #16A600; |
||||
|
} |
||||
|
.game_team_11 { |
||||
|
color: #9000A6; |
||||
|
} |
||||
|
.game_team_12 { |
||||
|
color: #93A600; |
||||
|
} |
||||
|
.game_team_13 { |
||||
|
color: #1300A6; |
||||
|
} |
||||
|
.game_team_14 { |
||||
|
color: #00A667; |
||||
|
} |
||||
|
.game_team_15 { |
||||
|
color: #A60040; |
||||
|
} |
||||
|
|
||||
|
.game_tab { |
||||
|
display: none; |
||||
|
} |
||||
|
.game_tab:target { |
||||
|
display: block; |
||||
|
} |
||||
|
|
||||
|
table.game_table tr td a, table.game_table tr td a:hover, table.game_table tr td a:visited, table.game_table tr td a:active { |
||||
|
display:block; |
||||
|
text-decoration:none; |
||||
|
color: inherit; |
||||
|
text-decoration: none; |
||||
|
} |
Loading…
Reference in new issue