diff --git a/api/forms.py b/api/forms.py index b6289d5..cd46f15 100644 --- a/api/forms.py +++ b/api/forms.py @@ -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() diff --git a/api/routes.py b/api/routes.py index 9fa7a2b..2cbf8ac 100644 --- a/api/routes.py +++ b/api/routes.py @@ -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/') 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//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) diff --git a/api/static/style.css b/api/static/style.css new file mode 100644 index 0000000..a4393bc --- /dev/null +++ b/api/static/style.css @@ -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; +} diff --git a/api/templates/admin.html b/api/templates/admin.html new file mode 100644 index 0000000..121965d --- /dev/null +++ b/api/templates/admin.html @@ -0,0 +1,39 @@ + + + Bridžik admin + + +
+ {{ form.hidden_tag() }} +

+ {{ form.player0.label }}: + {{ form.player0(size=15) }} + {% for error in form.player0.errors %} + [{{ error }}] + {% endfor %} +

+

+ {{ form.player1.label }}: + {{ form.player1(size=15) }} + {% for error in form.player1.errors %} + [{{ error }}] + {% endfor %} +

+

+ {{ form.player2.label }}: + {{ form.player2(size=15) }} + {% for error in form.player2.errors %} + [{{ error }}] + {% endfor %} +

+

+ {{ form.player3.label }}: + {{ form.player3(size=15) }} + {% for error in form.player3.errors %} + [{{ error }}] + {% endfor %} +

+

{{ form.submit() }}

+
+ + \ No newline at end of file diff --git a/api/templates/status.html b/api/templates/status.html index 4b1fb7c..d89983a 100644 --- a/api/templates/status.html +++ b/api/templates/status.html @@ -1,8 +1,9 @@ Bridžik + {% if not action%} - + {% endif %} @@ -19,132 +20,133 @@ {% if 'active_player' not in status %}

Hra sa skončila.

{% else %} - - - - - - {% if status['active_round_stashes'] %} - - {% endif %} -
-

Na ťahu je: {{ status['active_player'] }}

-
-

Tipy:

+
-

Kôpky v tomto kole:

- - - - - + + + + + + + + - {% for player in status['active_round_stashes'] %} - + + {% for player in status['active_round_guesses'] %} + {% endfor %} -
0123
{{ players[0] }}{{ players[1] }}{{ players[2] }}{{ players[3] }}
{{ player }}Tipy v tomto kole:{{ status['active_round_guesses'][player] }}
-
- - {% if action == 'guess' %} -
-

Zadaj tip:

- {% include "_guess_form.html" %} - {% endif %} - - {% if status['active_stash'] %} -
-

Aktuálna kôpka:

- - {% for player in range(status['active_stash']['first_player'], status['active_stash']['first_player'] + 4) %} - - {% endfor %} - - {% for player in range(status['active_stash']['first_player'], status['active_stash']['first_player'] + 4) %} - {% if status['active_stash']['cards'][player % 4] %} - - {% else %} - - {% endif %} - {% endfor %} - -
{{ player % 4 }}
- -
- {% endif %} + {% if status['active_round_stashes'] %} + + Kôpky v tomto kole: + {% for player in status['active_round_stashes'] %} + {{ player }} + {% endfor %} + + {% endif %} + + + + + +
- {% if action != 'play' %} -

Moje karty:

- - {% for card in status['player_cards'] %} - - {% endfor %} -
- -
- - {% else %} -

{{ form.card.label }}:

-
- {{ form.hidden_tag() }} -

- +
+
+ {% if action == 'guess' %} +

Zadaj tip:

+ {% include "_guess_form.html" %} + {% endif %} + + {% if status['active_stash'] %} +

Aktuálna kôpka:

+
+ {% for player in range(status['active_stash']['first_player'], status['active_stash']['first_player'] + 4) %} + + {% endfor %} + + {% for player in range(status['active_stash']['first_player'], status['active_stash']['first_player'] + 4) %} + + {% endfor %} + +
{{ players[player % 4] }}
+ {% if status['active_stash']['cards'][player % 4] %} + + {% endif %} +
+ {% endif %} + +


+ {% if action != 'play' %} +

Moje karty:

+ {% for card in status['player_cards'] %} {% endfor %} - - {% for card in status['player_cards'] %} - - {% endfor %} -
- -
- {% for error in form.card.errors %} - [{{ error }}] - {% endfor %} - {{ form.submit() }} -

-
- {% endif %} - {% endif %} - -
-

Výsledky

- - {% for series in status['standings'] %} - - - - - - - {% if series %} - {% for round in series %} - {% if round %} - - {% for player in round %} - + + {% else %} +

{{ form.card.label }}:

+ + {{ form.hidden_tag() }} +

+

0123
{{ player }}
+ {% for card in status['player_cards'] %} + + {% endfor %} + + {% for card in status['player_cards'] %} + + {% endfor %} + +
+ +
+ +
+ {% for error in form.card.errors %} + [{{ error }}] {% endfor %} - + {{ form.submit() }} +

+ + {% endif %} + {% endif %} + + +
+

Výsledky

+ + {% for series in status['standings'] %} + + + + + + + {% if series %} + {% for round in series %} + {% if round %} + + {% for player in round %} + + {% endfor %} + + {% endif %} + {% endfor %} {% endif %} {% endfor %} - {% endif %} - {% endfor %} -
{{ players[0] }}{{ players[1] }}{{ players[2] }}{{ players[3] }}
{{ player }}
+ +
+ \ No newline at end of file diff --git a/bridzik.py b/bridzik.py index 381483b..d6ba88d 100644 --- a/bridzik.py +++ b/bridzik.py @@ -69,6 +69,18 @@ class Card(): def __repr__(self): return ''.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):