2010-06-30 18:42:18 +00:00
|
|
|
import re
|
2010-06-16 14:16:49 +00:00
|
|
|
import copy
|
|
|
|
import random
|
2010-06-17 19:52:21 +00:00
|
|
|
import logging
|
2010-06-16 14:16:49 +00:00
|
|
|
from observable import Observable
|
|
|
|
|
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
|
|
|
self.players = []
|
|
|
|
|
2010-06-16 17:46:44 +00:00
|
|
|
class Mana:
|
|
|
|
types = {
|
|
|
|
'W': 'White',
|
|
|
|
'U': 'Blue',
|
|
|
|
'B': 'Black',
|
|
|
|
'R': 'Red',
|
|
|
|
'G': 'Green',
|
|
|
|
}
|
|
|
|
def __init__(self, mana=None):
|
|
|
|
self.mana = {}
|
|
|
|
for type in Mana.types.keys():
|
|
|
|
self.mana[type] = 0
|
|
|
|
if mana:
|
|
|
|
# Can't just set self to result... ?
|
|
|
|
result = self + mana
|
|
|
|
self.mana = result.mana
|
|
|
|
def __add__(self, other):
|
|
|
|
result = Mana()
|
|
|
|
if isinstance(other, str):
|
|
|
|
for m in [c for c in other.upper() if c in Mana.types.keys()]:
|
|
|
|
result.mana[m] = result.mana[m] + 1
|
|
|
|
elif isinstance(other,Mana):
|
|
|
|
for (key, value) in other.mana.iteritems():
|
|
|
|
result.mana[key] = self.mana[key] + value
|
|
|
|
else:
|
|
|
|
# TODO: raise exception
|
|
|
|
raise Exception()
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
def __sub__(self, other):
|
|
|
|
result = Mana()
|
|
|
|
if isinstance(other, Mana):
|
|
|
|
for (key, value) in other.mana.iteritems():
|
|
|
|
result.mana[key] = self.mana[key] - value
|
|
|
|
if result.mana[key] < 0:
|
|
|
|
raise Exception('Insufficient {0} Mana'.format(Mana.types[key]))
|
|
|
|
elif isinstance(other,ManaCost):
|
|
|
|
for (key, value) in other.mana.mana.iteritems():
|
|
|
|
result.mana[key] = self.mana[key] - value
|
|
|
|
if result.mana[key] < 0:
|
|
|
|
raise Exception('Insufficient {0} Mana'.format(Mana.types[key]))
|
|
|
|
remaining = other.any
|
|
|
|
for (key, value) in result.mana.iteritems():
|
|
|
|
while result.mana[key] > 0 and remaining > 0:
|
|
|
|
result.mana[key] = result.mana[key] - 1
|
|
|
|
remaining = remaining - 1
|
|
|
|
if remaining > 0:
|
|
|
|
raise Exception('Insufficient Mana')
|
|
|
|
return result
|
|
|
|
def __repr__(self):
|
|
|
|
return ''.join([type * count for (type, count) in self.mana.iteritems()]) if self.converted() > 0 else '0'
|
|
|
|
def converted(self):
|
|
|
|
return sum(self.mana.values())
|
|
|
|
|
|
|
|
class ManaCost:
|
2010-09-02 15:01:43 +00:00
|
|
|
hybridPattern = '[({](.*?)\/(.*?)[)}]'
|
2010-06-16 17:46:44 +00:00
|
|
|
def __init__(self, cost=None):
|
|
|
|
self.any = 0
|
|
|
|
self.mana = Mana()
|
2010-09-02 15:01:43 +00:00
|
|
|
self.snow = 0
|
|
|
|
self.hybrid = []
|
2010-06-16 17:46:44 +00:00
|
|
|
if cost:
|
|
|
|
result = self + cost
|
|
|
|
self.any = result.any
|
|
|
|
self.mana = result.mana
|
2010-09-02 15:01:43 +00:00
|
|
|
self.hybrid = result.hybrid
|
|
|
|
self.snow = result.snow
|
2010-06-16 17:46:44 +00:00
|
|
|
def __add__(self, other):
|
|
|
|
result = ManaCost()
|
|
|
|
if isinstance(other, str):
|
2010-09-02 15:01:43 +00:00
|
|
|
symbols = []
|
|
|
|
hybrid = re.findall(ManaCost.hybridPattern, other)
|
|
|
|
# Remove the hybrid costs from the mana cost string before continuing
|
|
|
|
other = re.sub(ManaCost.hybridPattern, '', other)
|
|
|
|
# Clear any other unecessary tokens
|
|
|
|
other = re.sub('[({})]', '', other)
|
|
|
|
result.hybrid = self.hybrid + [(ManaCost(a), ManaCost(b)) for (a,b) in hybrid]
|
|
|
|
result.mana = self.mana + Mana(other)
|
2010-06-16 17:46:44 +00:00
|
|
|
value = ''
|
|
|
|
for c in other:
|
|
|
|
if c not in '0123456789': break
|
|
|
|
value = value + c
|
2010-09-02 15:01:43 +00:00
|
|
|
result.snow = self.snow + len([c for c in other.lower() if c == 's'])
|
2010-06-16 17:46:44 +00:00
|
|
|
value = int(value) if len(value) > 0 else 0
|
2010-09-02 15:01:43 +00:00
|
|
|
result.any = self.any + value
|
2010-06-16 17:46:44 +00:00
|
|
|
elif isinstance(other, Mana):
|
2010-09-02 15:01:43 +00:00
|
|
|
result.mana = self.mana + other
|
2010-06-16 17:46:44 +00:00
|
|
|
elif isinstance(other, ManaCost):
|
2010-06-16 18:42:22 +00:00
|
|
|
result.any = self.any + other.any
|
|
|
|
result.mana = self.mana + other.mana
|
2010-09-02 15:01:43 +00:00
|
|
|
result.hybrid = self.hybrid + other.hybrid
|
|
|
|
result.snow = self.snow + other.snow
|
2010-06-16 17:46:44 +00:00
|
|
|
return result
|
|
|
|
def __sub__(self, other):
|
2010-06-16 18:42:22 +00:00
|
|
|
result = ManaCost()
|
2010-06-16 17:46:44 +00:00
|
|
|
if isinstance(other, ManaCost):
|
2010-06-16 18:42:22 +00:00
|
|
|
result.any = self.any - other.any
|
|
|
|
result.mana = self.mana - other.mana
|
2010-09-02 15:01:43 +00:00
|
|
|
result.hybrid = self.hybrid - other.hybrid
|
|
|
|
result.snow = self.snow - other.snow
|
2010-06-16 18:42:22 +00:00
|
|
|
return result
|
2010-06-16 17:46:44 +00:00
|
|
|
def __repr__(self):
|
2010-09-02 15:01:43 +00:00
|
|
|
return '{0}{1}{2}{3}'.format(
|
|
|
|
self.any if self.any > 0 or (self.mana.converted() == 0 and not self.hybrid and not self.snow) else '',
|
|
|
|
'S' * self.snow,
|
|
|
|
''.join(['({0}/{1})'.format(a, b) for a,b in self.hybrid]),
|
2010-06-16 17:46:44 +00:00
|
|
|
self.mana if self.mana.converted() > 0 else ''
|
|
|
|
)
|
|
|
|
def converted(self):
|
2010-09-02 15:01:43 +00:00
|
|
|
hybrid = sum([min(a, b).converted() for a, b in self.hybrid])
|
|
|
|
return self.mana.converted() + self.any + hybrid + self.snow
|
2010-06-16 17:46:44 +00:00
|
|
|
|
2010-06-16 14:16:49 +00:00
|
|
|
class Player:
|
2010-06-17 19:52:21 +00:00
|
|
|
def __init__(self, name, game, deck=None):
|
2010-06-16 14:16:49 +00:00
|
|
|
self.name = name
|
2010-06-17 19:52:21 +00:00
|
|
|
self.game = game
|
|
|
|
self.life = 20
|
|
|
|
self.mana = Mana()
|
2010-06-16 14:16:49 +00:00
|
|
|
self.deck = None
|
2010-06-17 19:52:21 +00:00
|
|
|
self.library = CardList(self, 'library')
|
|
|
|
self.hand = CardList(self, 'hand')
|
|
|
|
self.graveyard = CardList(self, 'graveyard')
|
|
|
|
self.battlefield = CardList(self, 'battlefield')
|
|
|
|
|
2010-06-16 14:16:49 +00:00
|
|
|
self.setDeck(deck)
|
2010-06-17 19:52:21 +00:00
|
|
|
|
|
|
|
self.lifeChanged = Observable()
|
|
|
|
self.defeated = Observable()
|
|
|
|
self.casts = Observable()
|
|
|
|
|
|
|
|
logging.debug('Initialized %s', self)
|
2010-06-16 14:16:49 +00:00
|
|
|
def __repr__(self):
|
2010-06-17 19:52:21 +00:00
|
|
|
return 'Player: {0} [Life:{1},Mana:{2},Hand:{3},Library:{4}]'.format(self.name, self.life, self.mana, len(self.hand), len(self.library) if self.library else self.library)
|
2010-06-16 14:16:49 +00:00
|
|
|
def setDeck(self, deck):
|
|
|
|
if not deck:
|
|
|
|
return
|
2010-06-17 19:52:21 +00:00
|
|
|
self.deck = copy.deepcopy(deck)
|
|
|
|
self.deck.owner = self
|
|
|
|
self.library = copy.copy(self.deck)
|
|
|
|
self.library.zone = 'library'
|
|
|
|
for card in self.deck:
|
|
|
|
# Re-initialize so the references are correct
|
|
|
|
card.__init__()
|
2010-06-16 14:16:49 +00:00
|
|
|
card.owner = self
|
2010-06-17 19:52:21 +00:00
|
|
|
def setLife(self, life):
|
|
|
|
self.life = life
|
|
|
|
self.lifeChanged.emit(self.life)
|
|
|
|
def affectLife(self, amount):
|
|
|
|
self.life = self.life + amount
|
|
|
|
if amount:
|
|
|
|
self.lifeChanged.emit(self.life)
|
|
|
|
def payMana(self, cost):
|
|
|
|
try:
|
|
|
|
self.mana -= cost
|
|
|
|
except:
|
|
|
|
logging.debug('%s could not pay mana cost %s', self, cost)
|
|
|
|
return False
|
|
|
|
logging.debug('%s paid mana cost %s', self, cost)
|
|
|
|
return True
|
|
|
|
def draw(self, count=1):
|
|
|
|
for i in xrange(count):
|
|
|
|
card = self.library[0]
|
|
|
|
card.move(self.library, self.hand)
|
|
|
|
logging.debug('%s drew %s', self, card)
|
2010-06-16 17:46:29 +00:00
|
|
|
def cast(self, card):
|
2010-06-17 19:52:21 +00:00
|
|
|
logging.debug('%s attempts to cast %s', self, card)
|
|
|
|
|
|
|
|
if not self.payMana(card.cost):
|
|
|
|
logging.debug('%s failed to cast %s: Not enough mana', self, card)
|
|
|
|
return False
|
|
|
|
card.move(self.hand, self.battlefield)
|
|
|
|
logging.debug('%s successfully casts %s', self, card)
|
|
|
|
self.casts.emit(card)
|
|
|
|
return True
|
2010-06-16 14:16:49 +00:00
|
|
|
|
|
|
|
class Card:
|
2010-06-30 11:52:01 +00:00
|
|
|
rarities = {
|
2010-07-01 22:11:39 +00:00
|
|
|
'L': 'Land',
|
2010-06-30 11:52:01 +00:00
|
|
|
'C': 'Common',
|
|
|
|
'U': 'Uncommon',
|
|
|
|
'R': 'Rare',
|
|
|
|
'M': 'Mythic Rare',
|
2010-08-26 04:45:31 +00:00
|
|
|
'S': 'Special',
|
2010-06-30 11:52:01 +00:00
|
|
|
}
|
|
|
|
|
2010-07-02 04:28:12 +00:00
|
|
|
def __init__(self, name, type, attributes, cost=0, power=0, toughness=0, sets=None, rarity=None, text=[], owner=None):
|
2010-06-17 19:52:21 +00:00
|
|
|
self.name = name
|
|
|
|
self.type = type.lower()
|
2010-06-16 14:16:49 +00:00
|
|
|
self.attributes = [a.lower() for a in attributes]
|
2010-06-17 19:52:21 +00:00
|
|
|
self.cost = ManaCost(cost)
|
2010-06-16 14:16:49 +00:00
|
|
|
self.power = power
|
|
|
|
self.toughness = toughness
|
2010-07-02 04:28:12 +00:00
|
|
|
self.sets = sets
|
2010-06-30 11:52:01 +00:00
|
|
|
self.rarity = rarity
|
2010-07-01 21:34:11 +00:00
|
|
|
self.text = text
|
2010-07-01 21:54:31 +00:00
|
|
|
self.colors = [color for color, cost in self.cost.mana.mana.iteritems() if cost > 0]
|
2010-06-17 19:52:21 +00:00
|
|
|
self.is_tapped = False
|
|
|
|
self.abilities = []
|
2010-06-16 14:16:49 +00:00
|
|
|
|
2010-07-01 22:10:16 +00:00
|
|
|
# Get color from land type
|
|
|
|
if 'land' in self.type.split(' '):
|
|
|
|
if 'mountain' in self.attributes:
|
|
|
|
self.colors.append('R')
|
|
|
|
if 'plains' in self.attributes:
|
|
|
|
self.colors.append('W')
|
|
|
|
if 'swamp' in self.attributes:
|
|
|
|
self.colors.append('B')
|
|
|
|
if 'island' in self.attributes:
|
|
|
|
self.colors.append('U')
|
|
|
|
if 'forest' in self.attributes:
|
|
|
|
self.colors.append('G')
|
|
|
|
|
2010-07-01 22:05:01 +00:00
|
|
|
if not self.colors:
|
|
|
|
# Try to determine the card's color based on its abilities. Not ideal, but maybe it'll help for now
|
|
|
|
for line in self.text:
|
|
|
|
for m in [ManaCost(c) for c in re.findall('{(\d*[WURGB]*)}',line)]:
|
|
|
|
self.colors.extend([color for color, c in m.mana.mana.iteritems() if c > 0 and color not in self.colors])
|
|
|
|
|
2010-06-16 14:16:49 +00:00
|
|
|
self.owner = owner
|
2010-06-17 19:52:21 +00:00
|
|
|
self.zone = None
|
2010-06-16 14:16:49 +00:00
|
|
|
|
|
|
|
# Events
|
|
|
|
self.moved = Observable()
|
|
|
|
self.tapped = Observable()
|
|
|
|
self.attacked = Observable()
|
|
|
|
|
|
|
|
self.store()
|
|
|
|
def __repr__(self):
|
2010-06-17 19:52:21 +00:00
|
|
|
return 'Card: [{3}] {0} -- {1}: {2} [{4}/{5}]{6}'.format(self.name, self.type.title(), ' '.join([a.capitalize() for a in self.attributes]), self.cost, self.power, self.toughness, ' [T]' if self.is_tapped else '')
|
2010-06-17 15:02:05 +00:00
|
|
|
def __mul__(self, other):
|
|
|
|
result = []
|
|
|
|
for i in xrange(other):
|
|
|
|
result.append(copy.copy(self))
|
|
|
|
return result
|
2010-06-16 14:16:49 +00:00
|
|
|
def store(self):
|
|
|
|
self.__stored = copy.copy(self)
|
|
|
|
def restore(self):
|
|
|
|
self = copy.copy(self.__stored)
|
2010-06-17 19:52:21 +00:00
|
|
|
def move(self, origin, destination):
|
|
|
|
origin.remove(self)
|
|
|
|
destination.append(self)
|
|
|
|
logging.debug('%s moved from %s to %s', self, origin.zone, destination.zone)
|
|
|
|
self.moved.emit(origin.zone, destination.zone)
|
2010-06-16 14:16:49 +00:00
|
|
|
def tap(self):
|
2010-06-17 19:52:21 +00:00
|
|
|
if self.is_tapped:
|
|
|
|
return False
|
|
|
|
self.is_tapped = True
|
|
|
|
logging.debug('%s is tapped', self)
|
2010-06-16 14:16:49 +00:00
|
|
|
self.tapped.emit()
|
2010-06-17 19:52:21 +00:00
|
|
|
return True
|
2010-06-16 14:16:49 +00:00
|
|
|
|
|
|
|
class Ability:
|
2010-06-17 19:52:21 +00:00
|
|
|
def __init__(self, card, name='Unknown', cost=0, tap=False):
|
|
|
|
self.card = card
|
|
|
|
self.name = name
|
|
|
|
self.cost = ManaCost(cost)
|
|
|
|
self.tap = tap
|
2010-06-16 14:16:49 +00:00
|
|
|
self.init()
|
2010-06-17 19:52:21 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return 'Ability: {0} [{1}{2}]'.format(self.name, self.cost, ', T' if self.tap else '')
|
2010-06-16 14:16:49 +00:00
|
|
|
def init(self):
|
|
|
|
pass
|
2010-06-17 19:52:21 +00:00
|
|
|
def run(self):
|
|
|
|
pass
|
|
|
|
def activate(self):
|
|
|
|
logging.debug('%s attempting to activate %s on %s', self.card.owner, self, self.card)
|
|
|
|
if self.tap and self.card.is_tapped:
|
|
|
|
logging.debug('%s failed to activate %s on %s', self.card.owner, self, self.card)
|
|
|
|
return False
|
|
|
|
if self.card.owner.payMana(self.cost):
|
|
|
|
if self.tap:
|
|
|
|
self.card.tap()
|
|
|
|
logging.debug('%s succeeded activating %s on %s', self.card.owner, self, self.card)
|
|
|
|
self.run()
|
|
|
|
return True
|
|
|
|
logging.debug('%s failed to activate %s on %s', self.card.owner, self, self.card)
|
|
|
|
return False
|
2010-06-16 14:16:49 +00:00
|
|
|
|
|
|
|
class CardList(list):
|
2010-06-17 19:52:21 +00:00
|
|
|
def __init__(self, owner, zone):
|
2010-06-16 14:16:49 +00:00
|
|
|
list.__init__(self)
|
2010-06-17 19:52:21 +00:00
|
|
|
self.owner = owner
|
|
|
|
self.zone = zone
|
2010-06-16 14:16:49 +00:00
|
|
|
def append(self, item):
|
|
|
|
item.list = self
|
|
|
|
list.append(self, item)
|
|
|
|
|
|
|
|
class Deck(CardList):
|
|
|
|
def __init__(self):
|
2010-06-17 19:52:21 +00:00
|
|
|
CardList.__init__(self, None, 'deck')
|
2010-06-16 14:16:49 +00:00
|
|
|
def shuffle(self):
|
|
|
|
random.shuffle(self)
|
|
|
|
def cards(self):
|
|
|
|
return self.__cards
|
2010-06-30 18:42:18 +00:00
|
|
|
def load(self, filename, db):
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
if not re.match('^\d+ .+$', line):
|
|
|
|
continue
|
|
|
|
line = line.split(' ')
|
|
|
|
count = int(line.pop(0))
|
|
|
|
name = ' '.join(line)
|
|
|
|
card = db.getCard(name)
|
|
|
|
if not card:
|
|
|
|
# TODO: The database should log an error
|
|
|
|
continue
|
|
|
|
self.extend(card * count)
|
2010-07-02 04:28:33 +00:00
|
|
|
def exportWagic(self, filename, name=None):
|
|
|
|
cards = {}
|
|
|
|
for card in self:
|
|
|
|
cards[card] = cards[card] + 1 if card in cards.keys() else 1
|
|
|
|
lines = [
|
|
|
|
'#NAME:{0}\n'.format(name if name else filename),
|
|
|
|
'#DESC: Exported deck\n\n',
|
|
|
|
]
|
|
|
|
for card, count in cards.iteritems():
|
|
|
|
sets = card.sets
|
2010-07-09 02:47:56 +00:00
|
|
|
if not sets:
|
|
|
|
print 'Card not supported:', card
|
|
|
|
continue
|
2010-07-02 04:28:33 +00:00
|
|
|
line = '{0} ({1}) *{2}\n'.format(card.name, card.sets[-1], count)
|
|
|
|
lines.append(line)
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.writelines(lines)
|
|
|
|
f.close()
|