Coverage for Deck.py: 81%
57 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-28 16:41 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-28 16:41 +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=u'simple', card_back=u'back04', width=30, height=42):
25 self.__width = width
26 self.__height = height
27 self.__cardspath = os.path.join(config.graphics_path, u"cards", deck_type).replace('\\', '/')
28 self.__backfile = os.path.join(
29 config.graphics_path, u"cards", u"backs", f"{card_back}.svg"
30 ).replace('\\', '/')
31 self.__cards = dict({'s': None, 'h': None, 'd': None, 'c': None})
32 self.__card_back = None
33 self.__rank_vals = {}
35 for sk in self.__cards:
36 self.__load_suit(sk)
38 self.__card_back = self.__load_svg(self.__backfile)
40 self.__create_rank_lookups()
42 def __create_rank_lookups(self):
43 self.__rank_vals = {
44 '2': 2, '3': 3, '4': 4, '5': 5,
45 '6': 6, '7': 7, '8': 8, '9': 9,
46 'T': 10, 'J': 11, 'Q': 12,
47 'K': 13, 'A': 14}
49 def __load_svg(self, path):
50 renderer = QSvgRenderer(path)
51 pm = QPixmap(self.__width, self.__height)
52 painter = QPainter(pm)
53 renderer.render(painter, QRectF(pm.rect()))
54 return pm
56 def __load_suit(self, suit_key):
57 sd = {}
58 _p = self.__cardspath
59 sd[2] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '2' + '.svg')).replace('\\', '/'))
60 sd[3] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '3' + '.svg')).replace('\\', '/'))
61 sd[4] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '4' + '.svg')).replace('\\', '/'))
62 sd[5] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '5' + '.svg')).replace('\\', '/'))
63 sd[6] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '6' + '.svg')).replace('\\', '/'))
64 sd[7] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '7' + '.svg')).replace('\\', '/'))
65 sd[8] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '8' + '.svg')).replace('\\', '/'))
66 sd[9] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '9' + '.svg')).replace('\\', '/'))
67 sd[10] = self.__load_svg(os.path.join(_p, (suit_key + '_' + '10' + '.svg')).replace('\\', '/'))
68 sd[11] = self.__load_svg(os.path.join(_p, (suit_key + '_' + 'j' + '.svg')).replace('\\', '/'))
69 sd[12] = self.__load_svg(os.path.join(_p, (suit_key + '_' + 'q' + '.svg')).replace('\\', '/'))
70 sd[13] = self.__load_svg(os.path.join(_p, (suit_key + '_' + 'k' + '.svg')).replace('\\', '/'))
71 sd[14] = self.__load_svg(os.path.join(_p, (suit_key + '_' + 'a' + '.svg')).replace('\\', '/'))
72 self.__cards[suit_key] = sd
74 def card(self, suit=None, rank=0):
75 return self.__cards[suit][rank]
77 def back(self):
78 return self.__card_back
80 def rank(self, token=None):
81 key = token.upper()
82 return self.__rank_vals[key]
84 def get_all_card_images(self):
85 # returns a 4x13-element dictionary of every card image +
86 # index-0 = card back each element is a QPixmap
87 card_images = {}
89 for suit in ('s', 'h', 'd', 'c'):
90 card_images[suit] = {}
91 for rank in (14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2):
92 card_images[suit][rank] = self.card(suit, rank)
94 # This is a nice trick. We put the card back image behind key 0,
95 # which allows the old code to work. A dict[0] looks like first
96 # index of an array.
97 card_images[0] = self.back()
98 return card_images