Coverage for Deck.py: 81%
57 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-07 02:19 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-07 02:19 +0000
1# Deck.py
2# -*- coding: utf-8
3# PokerStats, an online poker statistics tracking software for Linux
4# Copyright (C) 2007-2011 Mika Boström <bostik@iki.fi>
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, version 3 of the License.
9#
10"""Deck.py
12Helper class for mucked card display. Loads specified deck from SVG
13images and returns it as a dict of pixbufs.
14"""
16import os
18from PyQt5.QtCore import QRectF
19from PyQt5.QtGui import QPixmap, QPainter
20from PyQt5.QtSvg import QSvgRenderer
23class Deck(object):
24 def __init__(self, config, deck_type="simple", card_back="back04", width=30, height=42):
25 self.__width = width
26 self.__height = height
27 self.__cardspath = os.path.join(config.graphics_path, "cards", deck_type).replace("\\", "/")
28 self.__backfile = os.path.join(config.graphics_path, "cards", "backs", f"{card_back}.svg").replace("\\", "/")
29 self.__cards = dict({"s": None, "h": None, "d": None, "c": None})
30 self.__card_back = None
31 self.__rank_vals = {}
33 for sk in self.__cards:
34 self.__load_suit(sk)
36 self.__card_back = self.__load_svg(self.__backfile)
38 self.__create_rank_lookups()
40 def __create_rank_lookups(self):
41 self.__rank_vals = {
42 "2": 2,
43 "3": 3,
44 "4": 4,
45 "5": 5,
46 "6": 6,
47 "7": 7,
48 "8": 8,
49 "9": 9,
50 "T": 10,
51 "J": 11,
52 "Q": 12,
53 "K": 13,
54 "A": 14,
55 }
57 def __load_svg(self, path):
58 renderer = QSvgRenderer(path)
59 pm = QPixmap(self.__width, self.__height)
60 painter = QPainter(pm)
61 renderer.render(painter, QRectF(pm.rect()))
62 return pm
64 def __load_suit(self, suit_key):
65 sd = {}
66 _p = self.__cardspath
67 sd[2] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "2" + ".svg")).replace("\\", "/"))
68 sd[3] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "3" + ".svg")).replace("\\", "/"))
69 sd[4] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "4" + ".svg")).replace("\\", "/"))
70 sd[5] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "5" + ".svg")).replace("\\", "/"))
71 sd[6] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "6" + ".svg")).replace("\\", "/"))
72 sd[7] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "7" + ".svg")).replace("\\", "/"))
73 sd[8] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "8" + ".svg")).replace("\\", "/"))
74 sd[9] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "9" + ".svg")).replace("\\", "/"))
75 sd[10] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "10" + ".svg")).replace("\\", "/"))
76 sd[11] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "j" + ".svg")).replace("\\", "/"))
77 sd[12] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "q" + ".svg")).replace("\\", "/"))
78 sd[13] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "k" + ".svg")).replace("\\", "/"))
79 sd[14] = self.__load_svg(os.path.join(_p, (suit_key + "_" + "a" + ".svg")).replace("\\", "/"))
80 self.__cards[suit_key] = sd
82 def card(self, suit=None, rank=0):
83 return self.__cards[suit][rank]
85 def back(self):
86 return self.__card_back
88 def rank(self, token=None):
89 key = token.upper()
90 return self.__rank_vals[key]
92 def get_all_card_images(self):
93 # returns a 4x13-element dictionary of every card image +
94 # index-0 = card back each element is a QPixmap
95 card_images = {}
97 for suit in ("s", "h", "d", "c"):
98 card_images[suit] = {}
99 for rank in (14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2):
100 card_images[suit][rank] = self.card(suit, rank)
102 # This is a nice trick. We put the card back image behind key 0,
103 # which allows the old code to work. A dict[0] looks like first
104 # index of an array.
105 card_images[0] = self.back()
106 return card_images