mau_mau_bot/card.py

96 lines
2.2 KiB
Python
Raw Normal View History

2016-04-19 06:41:23 +08:00
from telegram.emoji import Emoji
2016-02-29 06:57:24 +08:00
# Colors
RED = 'r'
BLUE = 'b'
GREEN = 'g'
YELLOW = 'y'
COLORS = (RED, BLUE, GREEN, YELLOW)
2016-04-19 06:41:23 +08:00
COLOR_ICONS = {
RED: Emoji.HEAVY_BLACK_HEART,
BLUE: Emoji.BLUE_HEART,
GREEN: Emoji.GREEN_HEART,
YELLOW: Emoji.YELLOW_HEART,
}
2016-02-29 06:57:24 +08:00
# Values
ZERO = '0'
ONE = '1'
TWO = '2'
THREE = '3'
FOUR = '4'
FIVE = '5'
SIX = '6'
SEVEN = '7'
EIGHT = '8'
NINE = '9'
DRAW_TWO = 'draw'
REVERSE = 'reverse'
SKIP = 'skip'
VALUES = (ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, DRAW_TWO,
REVERSE, SKIP)
# Special cards
CHOOSE = 'colorchooser'
DRAW_FOUR = 'draw_four'
SPECIALS = (CHOOSE, DRAW_FOUR)
IMAGE_PATTERN = 'https://raw.githubusercontent.com/jh0ker/mau_mau_bot/' \
'master/images/jpg/%s.jpg'
THUMB_PATTERN = 'https://raw.githubusercontent.com/jh0ker/mau_mau_bot/' \
'master/images/thumb/%s.jpg'
class Card(object):
2016-03-08 09:50:24 +08:00
"""
This class represents a card.
"""
2016-02-29 06:57:24 +08:00
def __init__(self, color, value, special=None):
self.color = color
self.value = value
self.special = special
def __str__(self):
if self.special:
return self.special
else:
return '%s_%s' % (self.color, self.value)
def __repr__(self):
2016-04-19 06:41:23 +08:00
if self.special:
return '%s%s' % (Emoji.BROKEN_HEART,
' '.join([s.capitalize()
for s in self.special.split('_')]))
else:
return '%s%s' % (COLOR_ICONS[self.color], self.value.capitalize())
2016-02-29 06:57:24 +08:00
2016-02-29 08:53:59 +08:00
def __eq__(self, other):
2016-03-08 09:50:24 +08:00
""" Needed for sorting the cards """
2016-02-29 08:53:59 +08:00
return str(self) == str(other)
2016-03-01 08:25:26 +08:00
def __lt__(self, other):
2016-03-08 09:50:24 +08:00
""" Needed for sorting the cards """
2016-03-01 08:25:26 +08:00
return str(self) < str(other)
2016-02-29 06:57:24 +08:00
def get_image_link(self):
2016-03-08 09:50:24 +08:00
""" Returns a link to the image of this card """
2016-02-29 06:57:24 +08:00
return IMAGE_PATTERN % str(self)
def get_thumb_link(self):
2016-03-08 09:50:24 +08:00
""" Returns a link to the thumbnail-image of this card """
2016-02-29 06:57:24 +08:00
return THUMB_PATTERN % str(self)
def from_str(string):
2016-03-08 09:50:24 +08:00
""" Decode a Card object from a string """
2016-02-29 19:16:12 +08:00
if string not in SPECIALS:
2016-02-29 06:57:24 +08:00
color, value = string.split('_')
return Card(color, value)
else:
2016-02-29 19:16:12 +08:00
return Card(None, None, string)