diff --git a/api/forms.py b/api/forms.py new file mode 100644 index 0000000..b6289d5 --- /dev/null +++ b/api/forms.py @@ -0,0 +1,15 @@ +from flask_wtf import FlaskForm +from wtforms import SubmitField, IntegerField, RadioField +from wtforms.validators import DataRequired, NumberRange + +class GuessForm(FlaskForm): + guess = IntegerField('Tip', validators=[DataRequired(message='Zadaj tip'), NumberRange(min=0, max=8)]) + submit = SubmitField('Zadaj tip') + + def __init__(self, max_guess: int = 8, *args, **kwargs): + super(GuessForm, self).__init__(*args, **kwargs) + self.max_guess = max_guess + +class PlayForm(FlaskForm): + card = RadioField('Vyber kartu', validators=[DataRequired(message='Musíš vybrať kartu')]) + submit = SubmitField('Zahraj') diff --git a/api/routes.py b/api/routes.py index af8fbf6..9fa7a2b 100644 --- a/api/routes.py +++ b/api/routes.py @@ -1,9 +1,59 @@ 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 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]) @app.route('/bridzik_api/get_status/') def get_status(id: int): return json.dumps(b.get_status(int(id)), cls=Card.JSONEncoder) + +@app.route('/bridzik//status') +def status(player): + player = int(player) + game_status = b.get_status(player) + if b.is_completed() or b.series[-1].get_last_round().get_active_player() != player: + return render_template('status.html', status=game_status) + 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) + 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) + +@app.route('/bridzik//guess', methods=['POST']) +def guess(player): + player = int(player) + form = GuessForm() + try: + b.add_player_guess(player, int(form.guess.data)) + except BridzikException: + flash('Nie je možné zadať tip.') + return redirect(url_for('status', player=player)) + +@app.route('/bridzik//play_card', methods=['POST']) +def play_card(player): + player = int(player) + 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] + color, value = form.card.data.split('_') + try: + card = Card(Card_colors[color], Card_values[value]) + except KeyError: + flash('Chyba. Opakuj pokus znovu.') + + try: + b.play_card(player, card) + except BridzikException: + flash('Nie je možné zahrať kartu.') + return redirect(url_for('status', player=player)) diff --git a/api/templates/_guess_form.html b/api/templates/_guess_form.html new file mode 100644 index 0000000..314ae0d --- /dev/null +++ b/api/templates/_guess_form.html @@ -0,0 +1,11 @@ +
+ {{ form.hidden_tag() }} +

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

+
\ No newline at end of file diff --git a/api/templates/status.html b/api/templates/status.html new file mode 100644 index 0000000..4b1fb7c --- /dev/null +++ b/api/templates/status.html @@ -0,0 +1,150 @@ + + + Bridžik + {% if not action%} + + {% endif %} + + + {% with messages = get_flashed_messages() %} + {% if messages %} +
    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor %} +
+
+ {% endif %} + {% endwith %} + {% if 'active_player' not in status %} +

Hra sa skončila.

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

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

+
+

Tipy:

+ + + + + + + {% for player in status['active_round_guesses'] %} + + {% endfor %} + +
0123
{{ status['active_round_guesses'][player] }}
+
+

Kôpky v tomto kole:

+ + + + + + + {% for player in status['active_round_stashes'] %} + + {% endfor %} + +
0123
{{ 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 action != 'play' %} +

Moje karty:

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

{{ form.card.label }}:

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

+ + {% 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 %} +
0123
{{ player }}
+ + \ No newline at end of file