From d657dedf5f2d088192bb922555c9cc68585c2eb8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Sender=C3=A1k?=
Date: Tue, 21 Apr 2020 13:36:09 +0200
Subject: [PATCH] first working prototype
---
api/forms.py | 15 ++++
api/routes.py | 50 +++++++++++
api/templates/_guess_form.html | 11 +++
api/templates/status.html | 150 +++++++++++++++++++++++++++++++++
4 files changed, 226 insertions(+)
create mode 100644 api/forms.py
create mode 100644 api/templates/_guess_form.html
create mode 100644 api/templates/status.html
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 @@
+
\ 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 %}
+
+
+ Na ťahu je: {{ status['active_player'] }}
+ |
+
+
+ Tipy:
+
+ | 0 |
+ 1 |
+ 2 |
+ 3 |
+
+ {% for player in status['active_round_guesses'] %}
+ | {{ status['active_round_guesses'][player] }} |
+ {% endfor %}
+
+
+ |
+
+ {% if status['active_round_stashes'] %}
+
+ Kôpky v tomto kole:
+
+ | 0 |
+ 1 |
+ 2 |
+ 3 |
+
+ {% for player in status['active_round_stashes'] %}
+ | {{ player }} |
+ {% endfor %}
+
+
+ |
+ {% endif %}
+
+
+ {% 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) %}
+ | {{ 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 %}
+
+
+ {% endif %}
+
+
+ {% if action != 'play' %}
+ Moje karty:
+
+ {% for card in status['player_cards'] %}
+
+
+ |
+ {% endfor %}
+
+
+ {% else %}
+ {{ form.card.label }}:
+
+
+ {% endif %}
+ {% endif %}
+
+
+ Výsledky
+
+ {% for series in status['standings'] %}
+
+ | 0 |
+ 1 |
+ 2 |
+ 3 |
+
+ {% if series %}
+ {% for round in series %}
+ {% if round %}
+
+ {% for player in round %}
+ | {{ player }} |
+ {% endfor %}
+
+ {% endif %}
+ {% endfor %}
+ {% endif %}
+ {% endfor %}
+
+
+
\ No newline at end of file