2010-07-03 05:30:42 +00:00
|
|
|
import os
|
2010-06-30 06:22:34 +00:00
|
|
|
import re
|
|
|
|
from mtg import *
|
|
|
|
|
|
|
|
class Database:
|
2010-08-26 03:27:22 +00:00
|
|
|
def getCard(self, name):
|
|
|
|
pass
|
|
|
|
def findCard(self, name):
|
2010-06-30 06:22:34 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
""" Text file database class
|
|
|
|
|
|
|
|
Loads the card database files found at http://www.yawgatog.com/resources/oracle/
|
|
|
|
"""
|
|
|
|
class TextDB(Database):
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = filename
|
|
|
|
def getCard(self, name):
|
2010-10-04 14:29:20 +00:00
|
|
|
try:
|
|
|
|
return self.findCard(name).next()
|
|
|
|
except:
|
|
|
|
raise Exception('Invalid card: {0}'.format(name))
|
2010-08-26 04:30:26 +00:00
|
|
|
def findCard(self, name=None):
|
2010-06-30 06:22:34 +00:00
|
|
|
card = None
|
|
|
|
with open(self.filename, 'r') as f:
|
|
|
|
inRecord = False
|
|
|
|
recordName = False
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
if not inRecord:
|
|
|
|
recordName = line
|
|
|
|
inRecord = True
|
2010-08-26 03:27:22 +00:00
|
|
|
if not name or recordName.lower() == name.lower():
|
2010-06-30 06:22:34 +00:00
|
|
|
card = {
|
|
|
|
'name': recordName,
|
|
|
|
'type': '',
|
|
|
|
'attributes': [],
|
|
|
|
'cost': '',
|
|
|
|
'power': 0,
|
2010-06-30 11:52:01 +00:00
|
|
|
'toughness': 0,
|
|
|
|
'rarity': None,
|
2010-07-01 21:34:11 +00:00
|
|
|
'text': [],
|
2010-06-30 06:22:34 +00:00
|
|
|
}
|
|
|
|
elif card and line:
|
|
|
|
# Load the data into the card
|
2010-09-02 15:57:00 +00:00
|
|
|
if not card['cost'] and re.match('^X?\d*(\((\d+|[{0}])/(\d+|[{0}])\))*[{0}]*$'.format(''.join(Mana.types.keys())), line.upper()):
|
2010-06-30 06:22:34 +00:00
|
|
|
card['cost'] = line
|
|
|
|
continue
|
2010-07-01 21:34:11 +00:00
|
|
|
elif not card['type']:
|
2010-06-30 06:22:34 +00:00
|
|
|
attributes = line.split(' -- ')
|
|
|
|
type = attributes.pop(0)
|
|
|
|
card['type'] = type
|
2010-08-26 19:18:46 +00:00
|
|
|
card['attributes'] = [a for a in ''.join(attributes).split(' ') if a]
|
2010-07-01 21:34:11 +00:00
|
|
|
elif not card['power'] and not card['toughness'] and re.match('^\d+([+-]?\*)?\/\d+([+-]?\*)?$', line):
|
2010-06-30 06:22:34 +00:00
|
|
|
(card['power'], card['toughness']) = line.split('/')
|
2010-07-02 04:28:12 +00:00
|
|
|
elif re.match('^([A-Z0-9]+-[{0}](, )?)+$'.format(''.join(Card.rarities.keys())), line):
|
|
|
|
info = line.split(', ')
|
|
|
|
card['sets'] = [i.split('-').pop(0) for i in info]
|
|
|
|
currentSet = info.pop()
|
2010-06-30 11:52:01 +00:00
|
|
|
card['rarity'] = currentSet.split('-').pop()
|
2010-07-01 21:34:11 +00:00
|
|
|
else:
|
|
|
|
# Ability text
|
|
|
|
card['text'].append(line)
|
2010-06-30 06:22:34 +00:00
|
|
|
elif not line:
|
|
|
|
# Finished with current record
|
|
|
|
if card:
|
|
|
|
# We're done here
|
2010-08-26 03:27:22 +00:00
|
|
|
yield Card(card['name'], card['type'], card['attributes'], card['cost'], card['power'], card['toughness'], card['sets'], card['rarity'], card['text'])
|
2010-08-26 04:30:26 +00:00
|
|
|
# Prepare to read in the next record
|
|
|
|
inRecord = False
|
2010-06-30 06:22:34 +00:00
|
|
|
|
2010-07-03 05:30:42 +00:00
|
|
|
class WagicDB(Database):
|
|
|
|
def __init__(self, wagicPath):
|
|
|
|
self.path = wagicPath
|
|
|
|
self.db = {}
|
|
|
|
self.loadDb()
|
|
|
|
def loadDb(self):
|
|
|
|
for root, dirs, files in os.walk(self.path + '/Res/sets'):
|
|
|
|
if '_cards.dat' not in files: continue
|
|
|
|
set = root.split('/')[-1]
|
|
|
|
with open(root + '/_cards.dat', 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
if not line.startswith('primitive='): continue
|
|
|
|
primitive = line.split('=')[1].strip()
|
|
|
|
if not primitive in self.db.keys():
|
|
|
|
self.db[primitive] = []
|
|
|
|
self.db[primitive].append(set)
|
|
|
|
def getCard(self, name, baseCard):
|
|
|
|
card = baseCard
|
|
|
|
unsupported = [set for set in card.sets if set not in self.db[card.name]]
|
|
|
|
#if unsupported:
|
|
|
|
# print '{0}: Unsupported sets: {1}'.format(card.name, unsupported)
|
|
|
|
supported = [set for set in card.sets if set in self.db[card.name]]
|
|
|
|
card.sets = supported
|
|
|
|
return card
|
|
|
|
|
2010-06-30 06:22:34 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
db = TextDB('db.txt')
|
2010-09-02 15:57:00 +00:00
|
|
|
c = db.getCard('Balefire Liege')
|
|
|
|
print c
|