commit 17447d0bd0d3c2fbc8d7fc38b40631d2d86ed850 Author: Robbe Van Herck Date: Fri Mar 20 13:41:26 2020 +0100 Working prototype diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27d3239 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +temp/ +.vscode/ +__pycache__/ +venv/ \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..c1da977 --- /dev/null +++ b/game.py @@ -0,0 +1,62 @@ +from faker import Faker +from flask import Flask, render_template, redirect, url_for +app = Flask(__name__) + +from models import Game + +FAKER = Faker() + +CURRENT_GAMES = {} + +@app.route('/') +def root(): + return "I am root" + +@app.route("/new_game") +def new_game_invalid(): + return "Try /new_game/(number of players)" + +@app.route("/new_game/") +def new_game(num_players): + game_id = FAKER.bs().replace(" ", "-").lower() + players = [FAKER.color_name().lower() for _ in range(int(num_players))] + CURRENT_GAMES[game_id] = Game(game_id, players) + return game_id + +@app.route("/games/") +def game_detail(gameid): + return render_template("game.html", game=CURRENT_GAMES[gameid]) + +@app.route("/games//") +def playerview(gameid, playerid): + return render_template("player.html", game=CURRENT_GAMES[gameid], playerid=playerid) + +@app.route("/games///pickup") +def pickup(gameid, playerid): + g = CURRENT_GAMES[gameid] + g.pickup(playerid) + return redirect(url_for('playerview', gameid=gameid, playerid=playerid)) + +@app.route("/games///undo") +def undo(gameid, playerid): + g = CURRENT_GAMES[gameid] + g.undo(playerid) + return redirect(url_for('playerview', gameid=gameid, playerid=playerid)) + +@app.route("/games///play/") +def play(gameid, playerid, card): + g = CURRENT_GAMES[gameid] + g.play(playerid, card) + return redirect(url_for('playerview', gameid=gameid, playerid=playerid)) + +@app.route("/games///give/") +def give(gameid, playerid, victim): + g = CURRENT_GAMES[gameid] + g.pickup(victim) + return redirect(url_for('playerview', gameid=gameid, playerid=playerid)) + +@app.route("/games///shuffle_deck") +def shuffle_deck(gameid, playerid): + g = CURRENT_GAMES[gameid] + g.shuffle_deck() + return redirect(url_for('playerview', gameid=gameid, playerid=playerid)) diff --git a/models.py b/models.py new file mode 100644 index 0000000..bcf5cfc --- /dev/null +++ b/models.py @@ -0,0 +1,48 @@ +import random + +ALL_CARDS = [ number + suit for number in list("A23456789JQK") + ["10"] for suit in "SCHD"] + +class Game: + def __init__(self, game_id, players): + self.game_id = game_id + self.players = players + self.cards = { + "pickup_pile": ALL_CARDS.copy(), + "played_cards": [] + } + random.shuffle(self.cards["pickup_pile"]) + for player in players: + self.cards[player] = [] + + def play(self, player, card): + self.cards[player].remove(card) + self.cards["played_cards"].append(card) + + def undo(self, player): + card = self.cards["played_cards"].pop(-1) + self.cards[player].append(card) + + def pickup(self, player): + card = self.cards["pickup_pile"].pop(-1) + self.cards[player].append(card) + + def shuffle_deck(self): + cards = self.cards["played_cards"].copy() + self.cards["played_cards"] = [] + random.shuffle(cards) + self.cards["pickup_pile"] = cards + + @property + def last_card(self): + if self.cards["played_cards"] == []: + return "XX" + return self.cards["played_cards"][-1] + + def cards_for(self, player): + return self.cards[player] + + def __str__(self): + return f"Game {self.game_id} " + str(self.cards) + + def __repr__(self): + return f"Game {self.game_id} " + str(self.cards) \ No newline at end of file diff --git a/static/images/cards/10C.png b/static/images/cards/10C.png new file mode 100644 index 0000000..f9ade6d Binary files /dev/null and b/static/images/cards/10C.png differ diff --git a/static/images/cards/10D.png b/static/images/cards/10D.png new file mode 100644 index 0000000..a9de67e Binary files /dev/null and b/static/images/cards/10D.png differ diff --git a/static/images/cards/10H.png b/static/images/cards/10H.png new file mode 100644 index 0000000..df551b0 Binary files /dev/null and b/static/images/cards/10H.png differ diff --git a/static/images/cards/10S.png b/static/images/cards/10S.png new file mode 100644 index 0000000..22141df Binary files /dev/null and b/static/images/cards/10S.png differ diff --git a/static/images/cards/2C.png b/static/images/cards/2C.png new file mode 100644 index 0000000..413b0da Binary files /dev/null and b/static/images/cards/2C.png differ diff --git a/static/images/cards/2D.png b/static/images/cards/2D.png new file mode 100644 index 0000000..f966e48 Binary files /dev/null and b/static/images/cards/2D.png differ diff --git a/static/images/cards/2H.png b/static/images/cards/2H.png new file mode 100644 index 0000000..345744e Binary files /dev/null and b/static/images/cards/2H.png differ diff --git a/static/images/cards/2S.png b/static/images/cards/2S.png new file mode 100644 index 0000000..a0de145 Binary files /dev/null and b/static/images/cards/2S.png differ diff --git a/static/images/cards/3C.png b/static/images/cards/3C.png new file mode 100644 index 0000000..f8b8183 Binary files /dev/null and b/static/images/cards/3C.png differ diff --git a/static/images/cards/3D.png b/static/images/cards/3D.png new file mode 100644 index 0000000..cee21f3 Binary files /dev/null and b/static/images/cards/3D.png differ diff --git a/static/images/cards/3H.png b/static/images/cards/3H.png new file mode 100644 index 0000000..8494e1c Binary files /dev/null and b/static/images/cards/3H.png differ diff --git a/static/images/cards/3S.png b/static/images/cards/3S.png new file mode 100644 index 0000000..78e3b20 Binary files /dev/null and b/static/images/cards/3S.png differ diff --git a/static/images/cards/4C.png b/static/images/cards/4C.png new file mode 100644 index 0000000..8938459 Binary files /dev/null and b/static/images/cards/4C.png differ diff --git a/static/images/cards/4D.png b/static/images/cards/4D.png new file mode 100644 index 0000000..d9ae5be Binary files /dev/null and b/static/images/cards/4D.png differ diff --git a/static/images/cards/4H.png b/static/images/cards/4H.png new file mode 100644 index 0000000..41a4488 Binary files /dev/null and b/static/images/cards/4H.png differ diff --git a/static/images/cards/4S.png b/static/images/cards/4S.png new file mode 100644 index 0000000..70bc9cc Binary files /dev/null and b/static/images/cards/4S.png differ diff --git a/static/images/cards/5C.png b/static/images/cards/5C.png new file mode 100644 index 0000000..a7f49be Binary files /dev/null and b/static/images/cards/5C.png differ diff --git a/static/images/cards/5D.png b/static/images/cards/5D.png new file mode 100644 index 0000000..cff0617 Binary files /dev/null and b/static/images/cards/5D.png differ diff --git a/static/images/cards/5H.png b/static/images/cards/5H.png new file mode 100644 index 0000000..1efcbf3 Binary files /dev/null and b/static/images/cards/5H.png differ diff --git a/static/images/cards/5S.png b/static/images/cards/5S.png new file mode 100644 index 0000000..e708806 Binary files /dev/null and b/static/images/cards/5S.png differ diff --git a/static/images/cards/6C.png b/static/images/cards/6C.png new file mode 100644 index 0000000..d8382d3 Binary files /dev/null and b/static/images/cards/6C.png differ diff --git a/static/images/cards/6D.png b/static/images/cards/6D.png new file mode 100644 index 0000000..7ba40e9 Binary files /dev/null and b/static/images/cards/6D.png differ diff --git a/static/images/cards/6H.png b/static/images/cards/6H.png new file mode 100644 index 0000000..93a84f8 Binary files /dev/null and b/static/images/cards/6H.png differ diff --git a/static/images/cards/6S.png b/static/images/cards/6S.png new file mode 100644 index 0000000..874162b Binary files /dev/null and b/static/images/cards/6S.png differ diff --git a/static/images/cards/7C.png b/static/images/cards/7C.png new file mode 100644 index 0000000..f6a9653 Binary files /dev/null and b/static/images/cards/7C.png differ diff --git a/static/images/cards/7D.png b/static/images/cards/7D.png new file mode 100644 index 0000000..6c4393d Binary files /dev/null and b/static/images/cards/7D.png differ diff --git a/static/images/cards/7H.png b/static/images/cards/7H.png new file mode 100644 index 0000000..d4a59ff Binary files /dev/null and b/static/images/cards/7H.png differ diff --git a/static/images/cards/7S.png b/static/images/cards/7S.png new file mode 100644 index 0000000..895f85c Binary files /dev/null and b/static/images/cards/7S.png differ diff --git a/static/images/cards/8C.png b/static/images/cards/8C.png new file mode 100644 index 0000000..bc87bf0 Binary files /dev/null and b/static/images/cards/8C.png differ diff --git a/static/images/cards/8D.png b/static/images/cards/8D.png new file mode 100644 index 0000000..38c5695 Binary files /dev/null and b/static/images/cards/8D.png differ diff --git a/static/images/cards/8H.png b/static/images/cards/8H.png new file mode 100644 index 0000000..c2812aa Binary files /dev/null and b/static/images/cards/8H.png differ diff --git a/static/images/cards/8S.png b/static/images/cards/8S.png new file mode 100644 index 0000000..0277cc6 Binary files /dev/null and b/static/images/cards/8S.png differ diff --git a/static/images/cards/9C.png b/static/images/cards/9C.png new file mode 100644 index 0000000..d81b2f2 Binary files /dev/null and b/static/images/cards/9C.png differ diff --git a/static/images/cards/9D.png b/static/images/cards/9D.png new file mode 100644 index 0000000..e0d3d05 Binary files /dev/null and b/static/images/cards/9D.png differ diff --git a/static/images/cards/9H.png b/static/images/cards/9H.png new file mode 100644 index 0000000..7b679e1 Binary files /dev/null and b/static/images/cards/9H.png differ diff --git a/static/images/cards/9S.png b/static/images/cards/9S.png new file mode 100644 index 0000000..f58493e Binary files /dev/null and b/static/images/cards/9S.png differ diff --git a/static/images/cards/AC.png b/static/images/cards/AC.png new file mode 100644 index 0000000..396c158 Binary files /dev/null and b/static/images/cards/AC.png differ diff --git a/static/images/cards/AD.png b/static/images/cards/AD.png new file mode 100644 index 0000000..d702a39 Binary files /dev/null and b/static/images/cards/AD.png differ diff --git a/static/images/cards/AH.png b/static/images/cards/AH.png new file mode 100644 index 0000000..435fc14 Binary files /dev/null and b/static/images/cards/AH.png differ diff --git a/static/images/cards/AS.png b/static/images/cards/AS.png new file mode 100644 index 0000000..f07f67d Binary files /dev/null and b/static/images/cards/AS.png differ diff --git a/static/images/cards/JC.png b/static/images/cards/JC.png new file mode 100644 index 0000000..16418e4 Binary files /dev/null and b/static/images/cards/JC.png differ diff --git a/static/images/cards/JD.png b/static/images/cards/JD.png new file mode 100644 index 0000000..05b0389 Binary files /dev/null and b/static/images/cards/JD.png differ diff --git a/static/images/cards/JH.png b/static/images/cards/JH.png new file mode 100644 index 0000000..259421c Binary files /dev/null and b/static/images/cards/JH.png differ diff --git a/static/images/cards/JS.png b/static/images/cards/JS.png new file mode 100644 index 0000000..6e0554d Binary files /dev/null and b/static/images/cards/JS.png differ diff --git a/static/images/cards/KC.png b/static/images/cards/KC.png new file mode 100644 index 0000000..70c4ee5 Binary files /dev/null and b/static/images/cards/KC.png differ diff --git a/static/images/cards/KD.png b/static/images/cards/KD.png new file mode 100644 index 0000000..95a3b8f Binary files /dev/null and b/static/images/cards/KD.png differ diff --git a/static/images/cards/KH.png b/static/images/cards/KH.png new file mode 100644 index 0000000..0defcbb Binary files /dev/null and b/static/images/cards/KH.png differ diff --git a/static/images/cards/KS.png b/static/images/cards/KS.png new file mode 100644 index 0000000..43b8e19 Binary files /dev/null and b/static/images/cards/KS.png differ diff --git a/static/images/cards/QC.png b/static/images/cards/QC.png new file mode 100644 index 0000000..6f74177 Binary files /dev/null and b/static/images/cards/QC.png differ diff --git a/static/images/cards/QD.png b/static/images/cards/QD.png new file mode 100644 index 0000000..85c443f Binary files /dev/null and b/static/images/cards/QD.png differ diff --git a/static/images/cards/QH.png b/static/images/cards/QH.png new file mode 100644 index 0000000..ba8e1e4 Binary files /dev/null and b/static/images/cards/QH.png differ diff --git a/static/images/cards/QS.png b/static/images/cards/QS.png new file mode 100644 index 0000000..a6ef69f Binary files /dev/null and b/static/images/cards/QS.png differ diff --git a/static/images/cards/XX.png b/static/images/cards/XX.png new file mode 100644 index 0000000..fd94dad Binary files /dev/null and b/static/images/cards/XX.png differ diff --git a/static/images/cards/back.png b/static/images/cards/back.png new file mode 100644 index 0000000..93429db Binary files /dev/null and b/static/images/cards/back.png differ diff --git a/templates/game.html b/templates/game.html new file mode 100644 index 0000000..39d58fb --- /dev/null +++ b/templates/game.html @@ -0,0 +1,14 @@ +{% from "partials.html" import render_card, deck %} + + +

{{game.game_id | e}}

+

Players:

+
    + {% for playerid in game.players %} +
  • {{ playerid }}: {{ game.cards[playerid] | length}} cards
  • + {% endfor %} +
+

Deck:

+ {{ deck(game, "") }} + + \ No newline at end of file diff --git a/templates/partials.html b/templates/partials.html new file mode 100644 index 0000000..642882f --- /dev/null +++ b/templates/partials.html @@ -0,0 +1,26 @@ +{% macro render_card(card, game, playerid, play=False, undo=False, pickup=False, shuffle_deck=False) -%} + {% if play %} + {{_render_card(card)}} + {% elif undo %} + {{_render_card(card)}} + {% elif pickup %} + {{_render_card(card)}} + {% elif shuffle_deck %} + {{_render_card(card)}} + {% else %} + {{_render_card(card)}} + {% endif %} +{%- endmacro %} + +{% macro _render_card(card) -%} + {{ +{%- endmacro %} + +{% macro deck(game, playerid="") -%} + {% if game.cards["pickup_pile"] == [] %} + {{ render_card("XX", game, playerid, shuffle_deck=(playerid != "")) }} + {% else %} + {{ render_card("back", game, playerid, pickup=(playerid != "")) }} + {% endif %} + {{ render_card(game.last_card, game, playerid, undo=(playerid != "" and game.cards["played_cards"] != [])) }} +{%- endmacro %} \ No newline at end of file diff --git a/templates/player.html b/templates/player.html new file mode 100644 index 0000000..3b317fd --- /dev/null +++ b/templates/player.html @@ -0,0 +1,18 @@ +{% from "partials.html" import render_card, deck %} + + +

{{playerid | e}}

+

Cards:

+ {% for card in game.cards_for(playerid) %} + {{ render_card(card, game, playerid, play=True) }} + {% endfor %} +

Deck:

+ {{ deck(game, playerid) }} +

Other players:

+ {% for other_playerid in game.players %} + {% if other_playerid != playerid %} +
  • {{ other_playerid }}: {{ game.cards[other_playerid] | length}} cards Give card
  • + {% endif %} + {% endfor %} + + \ No newline at end of file