first working prototype

This commit is contained in:
Jakub Senderák
2020-04-21 13:36:09 +02:00
parent a43a7c4881
commit d657dedf5f
4 changed files with 226 additions and 0 deletions
+15
View File
@@ -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')
+50
View File
@@ -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/<id>')
def get_status(id: int):
return json.dumps(b.get_status(int(id)), cls=Card.JSONEncoder)
@app.route('/bridzik/<player>/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/<player>/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/<player>/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))
+11
View File
@@ -0,0 +1,11 @@
<form action="{{ url_for('guess', player=player) }}" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.guess.label }}:
{{ form.guess(size=15) }}
{% for error in form.guess.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
{{ form.submit() }}
</p>
</form>
+150
View File
@@ -0,0 +1,150 @@
<html>
<head>
<title>Bridžik</title>
{% if not action%}
<meta http-equiv="refresh" content="1">
{% endif %}
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
<hr>
{% endif %}
{% endwith %}
{% 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>
<td>
<h1>Tipy:</h1>
<table style="width: auto;">
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<tr>
{% for player in status['active_round_guesses'] %}
<td>{{ 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>
{% for player in status['active_round_stashes'] %}
<td>{{ player }}</td>
{% endfor %}
</tr>
</table>
</td>
{% endif %}
</table>
{% if action == 'guess' %}
<hr>
<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>
{% endfor %}
<tr>
{% for player in range(status['active_stash']['first_player'], status['active_stash']['first_player'] + 4) %}
{% 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 %}
{% endfor %}
</tr>
</table>
{% endif %}
<hr>
{% if action != 'play' %}
<h1>Moje karty:</h1>
<table id="player_hand">
{% for card in status['player_cards'] %}
<td>
<img src="/static/cards/{{ card }}.png" width="80" height="auto">
</td>
{% endfor %}
</table>
{% else %}
<h1>{{ form.card.label }}:</h1>
<form action="{{ url_for('play_card', player=player) }}" method="post">
{{ form.hidden_tag() }}
<p>
<table style="width: auto">
{% for card in status['player_cards'] %}
<td>
<img src="/static/cards/{{ card }}.png" width="80" height="auto">
</td>
{% endfor %}
<tr>
{% for card in status['player_cards'] %}
<td>
<input id="card-{{ loop.index }}" name="card" type="radio" value="{{ card }}">
</td>
{% endfor %}
</tr>
</table>
{% for error in form.card.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
{{ form.submit() }}
</p>
</form>
{% endif %}
{% endif %}
<hr>
<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>
</tr>
{% if series %}
{% for round in series %}
{% if round %}
<tr>
{% for player in round %}
<td>{{ player }}</td>
{% endfor %}
</tr>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</table>
</body>
</html>