From 129e676335da1ff27d9cdec4f465d0401469e794 Mon Sep 17 00:00:00 2001
From: Stefan Rupp
Date: Sun, 5 Apr 2020 23:27:33 +0200
Subject: [PATCH] added control view
---
routes.py | 2 ++
server.py | 4 ++--
templates/home/control.html | 44 +++++++++++++++++++++++++++++++++++++
templates/home/player.html | 11 ++++++----
views/control.py | 17 ++++++++++++++
views/player.py | 6 ++---
wiz_game.py | 34 +++++++++++++++-------------
7 files changed, 94 insertions(+), 24 deletions(-)
create mode 100644 templates/home/control.html
create mode 100644 views/control.py
diff --git a/routes.py b/routes.py
index 66432f9..2b4a766 100644
--- a/routes.py
+++ b/routes.py
@@ -8,5 +8,7 @@ from views.home import *
from views.temp_css import *
# noinspection PyUnresolvedReferences
from views.player import *
+# noinspection PyUnresolvedReferences
+from views.control import *
api.add_route("/static", static=True)
diff --git a/server.py b/server.py
index a715376..1549830 100755
--- a/server.py
+++ b/server.py
@@ -16,8 +16,8 @@ def main():
wiz_game.the_game.add_player("p1", 1)
wiz_game.the_game.add_player("p2", 2)
wiz_game.the_game.add_player("p3", 3)
- wiz_game.the_game.add_player("p4", 4)
- wiz_game.the_game.deal_cards(4)
+ #wiz_game.the_game.add_player("p4", 4)
+ wiz_game.the_game.deal_cards(12)
api.run(port=19203, address="127.0.0.1")
exit(0)
diff --git a/templates/home/control.html b/templates/home/control.html
new file mode 100644
index 0000000..116ddb2
--- /dev/null
+++ b/templates/home/control.html
@@ -0,0 +1,44 @@
+{% extends "/shared/_layout.html" %}
+
+{% block content %}
+
+
+
Gambling Foo A RESTful gaming service
+
+ Play a nice game of cards?
+
+ Control view
+
+ |
+ {% for card in deck %}
+ >{{loop.index}}: {{card}} |
+ {% endfor %}
+
+
+ Trump Card: {{trump_card}}
+ Trump Color: {{trump_color}}
+
+
+
+
+
+ Spam Bacon Sausage + Spam
+ Spam Spam Spam Spam Spam
+ Spam Baked Beans Spam
+ Spam Spam + Spam
+
+
+
+
+
+{% endblock %}
\ No newline at end of file
diff --git a/templates/home/player.html b/templates/home/player.html
index 116824f..4a69fe6 100644
--- a/templates/home/player.html
+++ b/templates/home/player.html
@@ -8,14 +8,17 @@
Play a nice game of cards?
Player {{player}} view
-
-
+
{% for card in cards %}
-
>card {{loop.index}} is: {{card}}
{% endfor %}
-
+
+ -
+ Trump Card: {{trump_card}}
+ Trump Color: {{trump_color}}
+
-
+
diff --git a/views/control.py b/views/control.py
new file mode 100644
index 0000000..b719007
--- /dev/null
+++ b/views/control.py
@@ -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())
diff --git a/views/player.py b/views/player.py
index 6e2da52..d86a0c7 100644
--- a/views/player.py
+++ b/views/player.py
@@ -6,6 +6,6 @@ from wiz_game import the_game
@api.route("/player/{player}")
def index(req, resp, player: str):
p = int(player)
- print(p)
- print(the_game.players)
- resp.content = api.template('home/player.html', player=player, cards=the_game.players[p].cards)
+ tcard = the_game.trump_card
+ tcolor = the_game.get_trump_color()
+ resp.content = api.template('home/player.html', player=player, cards=the_game.players[p].cards, trump_card=tcard, trump_color=tcolor)
diff --git a/wiz_game.py b/wiz_game.py
index 30c1e1f..4fb59ec 100644
--- a/wiz_game.py
+++ b/wiz_game.py
@@ -38,41 +38,45 @@ class WizGame:
def __init__(self):
self.players = []
self.card_deck = []
- self.trump_color = None
+ self.trump_card = None
def create_deck(self):
for color in ["b", "r", "g", "y"]:
- for val in range(1,14):
+ for val in range(1, 14):
self.card_deck.append(Card(color, val))
- for _ in range(1,5):
+ for _ in range(1, 5):
self.card_deck.append(Card(None, 'Z'))
- for _ in range(1,5):
+ for _ in range(1, 5):
self.card_deck.append(Card(None, 'N'))
print("carddeck:")
print(self.card_deck)
- def set_trump_color(self, card: Card):
- if card.color:
- self.trump_color = card.color
- elif card.value == 'Z':
- self.trump_color = 'choose'
+ def get_trump_color(self):
+ card = self.trump_card
+ if card:
+ if card.color:
+ return card.color
+ elif card.value == 'Z':
+ return 'choose'
+ else:
+ return None
else:
- self.trump_color = None
+ return None
def deal_cards(self, cards_per_player):
random.shuffle(self.card_deck)
cs = list(chunks(self.card_deck, cards_per_player))
- for idx,p in enumerate(self.players):
+ for idx, p in enumerate(self.players):
p.set_cards(cs[idx])
p.show_cards()
if len(cs) > len(self.players):
cc = cs[len(self.players)]
c = cc[0]
- print("trump card: {}".format(c))
- self.set_trump_color(c)
+ #self.set_trump_color(c)
+ self.trump_card = c
else:
- self.trump_color = None
- print("trump color: {}".format(self.trump_color))
+ #self.trump_color = None
+ self.trump_card = None
def add_player(self, name: str, player_id):
if len(self.players) < 6: