added control view

This commit is contained in:
Stefan Rupp 2020-04-05 23:27:33 +02:00
parent 914e9a1f3d
commit 129e676335
7 changed files with 94 additions and 24 deletions

View File

@ -8,5 +8,7 @@ from views.home import *
from views.temp_css import * from views.temp_css import *
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
from views.player import * from views.player import *
# noinspection PyUnresolvedReferences
from views.control import *
api.add_route("/static", static=True) api.add_route("/static", static=True)

View File

@ -16,8 +16,8 @@ def main():
wiz_game.the_game.add_player("p1", 1) wiz_game.the_game.add_player("p1", 1)
wiz_game.the_game.add_player("p2", 2) wiz_game.the_game.add_player("p2", 2)
wiz_game.the_game.add_player("p3", 3) wiz_game.the_game.add_player("p3", 3)
wiz_game.the_game.add_player("p4", 4) #wiz_game.the_game.add_player("p4", 4)
wiz_game.the_game.deal_cards(4) wiz_game.the_game.deal_cards(12)
api.run(port=19203, address="127.0.0.1") api.run(port=19203, address="127.0.0.1")
exit(0) exit(0)

View File

@ -0,0 +1,44 @@
{% extends "/shared/_layout.html" %}
{% block content %}
<div class="content">
<h1><span class="font-semi-bold">Gambling Foo</span> <span class="smaller">A RESTful gaming service</span></h1>
<p class="lead">
Play a nice game of cards?<br>
<br>
<strong>Control view</strong>
<br>
|
{% for card in deck %}
<strong>>{{loop.index}}: {{card}} </strong> |
{% endfor %}
<li>
<strong>Trump Card: {{trump_card}}</strong><br>
<strong>Trump Color: {{trump_color}}</strong><br>
</li>
<ul>
<li>
<strong>Deal cards</strong><br>
<a href="/control/deal/1">deal 1</a>
<a href="/control/deal/2">deal 2</a>
<a href="/control/deal/5">deal 5</a>
<a href="/control/deal/10">deal 10</a>
<a href="/control/deal/12">deal 12</a>
<a href="/control/deal/20">deal 20</a>
</li>
</ul>
</p>
<p class="disclaimer">
Spam Bacon Sausage + Spam
Spam Spam Spam Spam Spam
Spam Baked Beans Spam
Spam Spam + Spam
</p>
</div>
{% endblock %}

View File

@ -8,14 +8,17 @@
Play a nice game of cards?<br> Play a nice game of cards?<br>
<br> <br>
<strong>Player {{player}} view</strong> <strong>Player {{player}} view</strong>
<table style="width:100%"> <ul>
<tr>
{% for card in cards %} {% for card in cards %}
<li> <li>
<strong>>card {{loop.index}} is: {{card}} </strong><br> <strong>>card {{loop.index}} is: {{card}} </strong><br>
</li> </li>
{% endfor %} {% endfor %}
<li>
<strong>Trump Card: {{trump_card}}</strong><br>
<strong>Trump Color: {{trump_color}}</strong><br>
</li>
<!-- <!--
<li> <li>
<strong>Movie by IMDB code</strong><br> <strong>Movie by IMDB code</strong><br>
@ -34,7 +37,7 @@
<a href="/api/movie/genre/sci-fi">GET /api/movie/genre/{genre}</a> <a href="/api/movie/genre/sci-fi">GET /api/movie/genre/{genre}</a>
</li> </li>
--> -->
</table> </ul>
</p> </p>
<p class="disclaimer"> <p class="disclaimer">

17
views/control.py Normal file
View File

@ -0,0 +1,17 @@
from app_instance import api
from wiz_game import the_game
@api.route("/control/")
def index(req, resp):
print(req.params)
resp.content = api.template('home/control.html', deck=the_game.card_deck,
trump_card=the_game.trump_card, trump_color=the_game.get_trump_color())
@api.route("/control/deal/{cards}")
def deal(req, resp, cards):
the_game.deal_cards(int(cards))
resp.content = api.template('home/control.html', deck=the_game.card_deck,
trump_card=the_game.trump_card, trump_color=the_game.get_trump_color())

View File

@ -6,6 +6,6 @@ from wiz_game import the_game
@api.route("/player/{player}") @api.route("/player/{player}")
def index(req, resp, player: str): def index(req, resp, player: str):
p = int(player) p = int(player)
print(p) tcard = the_game.trump_card
print(the_game.players) tcolor = the_game.get_trump_color()
resp.content = api.template('home/player.html', player=player, cards=the_game.players[p].cards) resp.content = api.template('home/player.html', player=player, cards=the_game.players[p].cards, trump_card=tcard, trump_color=tcolor)

View File

@ -38,7 +38,7 @@ class WizGame:
def __init__(self): def __init__(self):
self.players = [] self.players = []
self.card_deck = [] self.card_deck = []
self.trump_color = None self.trump_card = None
def create_deck(self): def create_deck(self):
for color in ["b", "r", "g", "y"]: for color in ["b", "r", "g", "y"]:
@ -51,13 +51,17 @@ class WizGame:
print("carddeck:") print("carddeck:")
print(self.card_deck) print(self.card_deck)
def set_trump_color(self, card: Card): def get_trump_color(self):
card = self.trump_card
if card:
if card.color: if card.color:
self.trump_color = card.color return card.color
elif card.value == 'Z': elif card.value == 'Z':
self.trump_color = 'choose' return 'choose'
else: else:
self.trump_color = None return None
else:
return None
def deal_cards(self, cards_per_player): def deal_cards(self, cards_per_player):
random.shuffle(self.card_deck) random.shuffle(self.card_deck)
@ -68,11 +72,11 @@ class WizGame:
if len(cs) > len(self.players): if len(cs) > len(self.players):
cc = cs[len(self.players)] cc = cs[len(self.players)]
c = cc[0] c = cc[0]
print("trump card: {}".format(c)) #self.set_trump_color(c)
self.set_trump_color(c) self.trump_card = c
else: else:
self.trump_color = None #self.trump_color = None
print("trump color: {}".format(self.trump_color)) self.trump_card = None
def add_player(self, name: str, player_id): def add_player(self, name: str, player_id):
if len(self.players) < 6: if len(self.players) < 6: