ui tweaks, admin form

This commit is contained in:
Jakub Senderák
2020-04-22 14:16:49 +02:00
parent d657dedf5f
commit d12f5c093c
6 changed files with 238 additions and 124 deletions
+8 -1
View File
@@ -1,5 +1,5 @@
from flask_wtf import FlaskForm
from wtforms import SubmitField, IntegerField, RadioField
from wtforms import SubmitField, IntegerField, RadioField, StringField
from wtforms.validators import DataRequired, NumberRange
class GuessForm(FlaskForm):
@@ -13,3 +13,10 @@ class GuessForm(FlaskForm):
class PlayForm(FlaskForm):
card = RadioField('Vyber kartu', validators=[DataRequired(message='Musíš vybrať kartu')])
submit = SubmitField('Zahraj')
class AdminForm(FlaskForm):
player0 = StringField('0', validators=[DataRequired()])
player1 = StringField('1', validators=[DataRequired()])
player2 = StringField('2', validators=[DataRequired()])
player3 = StringField('3', validators=[DataRequired()])
submit = SubmitField()
+33 -10
View File
@@ -2,14 +2,16 @@ from api import app
from bridzik import Bridzik, Card, Card_colors, Card_values, BridzikException
import json
from flask import render_template, url_for, flash, redirect
from api.forms import GuessForm, PlayForm
from api.forms import GuessForm, PlayForm, AdminForm
b = Bridzik()
b.add_player_guess(0, 1)
b.add_player_guess(1, 1)
b.add_player_guess(2, 1)
b.add_player_guess(3, 2)
# b.play_card(3, b.get_status(3)['player_cards'][0])
players = [
'Jakub',
'Timo',
'Katka',
'Ondrej'
]
@app.route('/bridzik_api/get_status/<id>')
def get_status(id: int):
@@ -19,16 +21,20 @@ def get_status(id: int):
def status(player):
player = int(player)
game_status = b.get_status(player)
action = None
form = None
player_cards = Card.sort_card_list(b.series[-1].get_last_round().player_cards[player])
game_status['player_cards'] = [str(c) for c in player_cards]
if b.is_completed() or b.series[-1].get_last_round().get_active_player() != player:
return render_template('status.html', status=game_status)
pass
elif not b.series[-1].get_last_round().is_guessing_completed():
form = GuessForm(max_guess= 8 - b.series[-1].get_last_round().round_number)
return render_template('status.html', status=game_status, player=player, action='guess', form=form)
action = 'guess'
else:
player_cards = b.series[-1].get_last_round().player_cards[player]
form = PlayForm()
form.card.choices = [(str(c), str(c)) for c in player_cards]
return render_template('status.html', status=game_status, player=player, action='play', form=form)
action = 'play'
return render_template('status.html', status=game_status, player=player, action=action, form=form, players=players)
@app.route('/bridzik/<player>/guess', methods=['POST'])
def guess(player):
@@ -57,3 +63,20 @@ def play_card(player):
except BridzikException:
flash('Nie je možné zahrať kartu.')
return redirect(url_for('status', player=player))
@app.route('/bridzik/admin', methods=['GET', 'POST'])
def admin():
form = AdminForm()
if form.validate_on_submit():
players[0] = form.player0.data
players[1] = form.player1.data
players[2] = form.player2.data
players[3] = form.player3.data
return redirect(url_for('admin'))
else:
form.player0.data = players[0]
form.player1.data = players[1]
form.player2.data = players[2]
form.player3.data = players[3]
return render_template('admin.html', form=form)
+31
View File
@@ -0,0 +1,31 @@
#header {
display: flex;
max-width: 600px;
}
#header>div {
flex: 1;
padding: 10px;
border: 2px solid grey;
}
.state_row_header {
white-space: nowrap;
text-align: right;
}
.points {
text-align: center;
}
#wrapper {
display: grid;
grid-template-columns: 1fr 250px;
grid-template-rows: 1fr;
grid-gap: 10px;
}
.card_display {
width: 82px;
height: 150px;
}
+39
View File
@@ -0,0 +1,39 @@
<html>
<head>
<title>Bridžik admin</title>
</head>
<body>
<form action="" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.player0.label }}:
{{ form.player0(size=15) }}
{% for error in form.player0.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.player1.label }}:
{{ form.player1(size=15) }}
{% for error in form.player1.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.player2.label }}:
{{ form.player2(size=15) }}
{% for error in form.player2.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.player3.label }}:
{{ form.player3(size=15) }}
{% for error in form.player3.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.submit() }}</p>
</form>
</body>
</html>
+39 -37
View File
@@ -1,8 +1,9 @@
<html>
<head>
<title>Bridžik</title>
<link href="/static/style.css" rel="stylesheet">
{% if not action%}
<meta http-equiv="refresh" content="1">
<meta http-equiv="refresh" content="2">
{% endif %}
</head>
<body>
@@ -19,66 +20,64 @@
{% if 'active_player' not in status %}
<h1>Hra sa skončila.</h1>
{% else %}
<table style="width: auto;">
<td>
<h1>Na ťahu je: {{ status['active_player'] }}</h1>
</td>
<div id="header">
<div>
<p id="active_player">Na ťahu je: {{ players[status['active_player']] }}</p>
</div>
<td>
<h1>Tipy:</h1>
<div id="active_round_guesses">
<p></p>
<table style="width: auto;">
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<thead>
<th></th>
<th class="state_column_header">{{ players[0] }}</th>
<th class="state_column_header">{{ players[1] }}</th>
<th class="state_column_header">{{ players[2] }}</th>
<th class="state_column_header">{{ players[3] }}</th>
</thead>
<tbody>
<tr>
<th class="state_row_header">Tipy v tomto kole:</th>
{% for player in status['active_round_guesses'] %}
<td>{{ status['active_round_guesses'][player] }}</td>
<td class="points">{{ status['active_round_guesses'][player] }}</td>
{% endfor %}
</tr>
</table>
</td>
{% if status['active_round_stashes'] %}
<td>
<h1>Kôpky v tomto kole:</h1>
<table style="width: auto;">
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<tr>
<th class="state_row_header">Kôpky v tomto kole:</th>
{% for player in status['active_round_stashes'] %}
<td>{{ player }}</td>
<td class="points">{{ player }}</td>
{% endfor %}
</tr>
</table>
</td>
{% endif %}
</tbody>
</table>
</div>
</div>
{% if action == 'guess' %}
<hr>
<div id="wrapper">
<div id="table">
{% if action == 'guess' %}
<h1>Zadaj tip:</h1>
{% include "_guess_form.html" %}
{% endif %}
{% if status['active_stash'] %}
<hr>
<h1>Aktuálna kôpka:</h1>
<table style="width: auto;">
{% for player in range(status['active_stash']['first_player'], status['active_stash']['first_player'] + 4) %}
<th>{{ player % 4 }}</th>
<th>{{ players[player % 4] }}</th>
{% endfor %}
<tr>
{% for player in range(status['active_stash']['first_player'], status['active_stash']['first_player'] + 4) %}
<td class="card_display">
{% if status['active_stash']['cards'][player % 4] %}
<td>
<img src="/static/cards/{{ status['active_stash']['cards'][player % 4] }}.png" width="80" height="auto">
</td>
{% else %}
<td></td>
{% endif %}
</td>
{% endfor %}
</tr>
</table>
@@ -122,23 +121,24 @@
</form>
{% endif %}
{% endif %}
</div>
<hr>
<div id="standings">
<h1>Výsledky</h1>
<table style="width: auto;">
{% for series in status['standings'] %}
<tr>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>{{ players[0] }}</th>
<th>{{ players[1] }}</th>
<th>{{ players[2] }}</th>
<th>{{ players[3] }}</th>
</tr>
{% if series %}
{% for round in series %}
{% if round %}
<tr>
{% for player in round %}
<td>{{ player }}</td>
<td class="points">{{ player }}</td>
{% endfor %}
</tr>
{% endif %}
@@ -146,5 +146,7 @@
{% endif %}
{% endfor %}
</table>
</div>
</div>
</body>
</html>
+12
View File
@@ -69,6 +69,18 @@ class Card():
def __repr__(self):
return '<Card {}_{}>'.format(self.color.name, self.value.name)
@staticmethod
def sort_card_list(input_card_set: []) -> []:
hearts = [c for c in input_card_set if c.color == Card_colors['HEARTS']]
leaves = [c for c in input_card_set if c.color == Card_colors['LEAVES']]
acorns = [c for c in input_card_set if c.color == Card_colors['ACORNS']]
bells = [c for c in input_card_set if c.color == Card_colors['BELLS']]
hearts.sort(key=lambda a : a.value)
leaves.sort(key=lambda a : a.value)
acorns.sort(key=lambda a : a.value)
bells.sort(key=lambda a : a.value)
return hearts + leaves + acorns + bells
class JSONEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, Card):