Implement a card generator for searching a database / iterating through all available cards

This commit is contained in:
Correl Roush 2010-08-25 23:27:22 -04:00
parent 4b60a0e712
commit cf2ab5f2bd

View file

@ -3,7 +3,9 @@ import re
from mtg import *
class Database:
def getCard(name):
def getCard(self, name):
pass
def findCard(self, name):
pass
""" Text file database class
@ -14,6 +16,8 @@ class TextDB(Database):
def __init__(self, filename):
self.filename = filename
def getCard(self, name):
return self.findCard(name).next()
def findCard(self, name):
card = None
with open(self.filename, 'r') as f:
inRecord = False
@ -23,7 +27,7 @@ class TextDB(Database):
if not inRecord:
recordName = line
inRecord = True
if recordName.lower() == name.lower():
if not name or recordName.lower() == name.lower():
card = {
'name': recordName,
'type': '',
@ -58,13 +62,10 @@ class TextDB(Database):
# Finished with current record
if card:
# We're done here
break
yield Card(card['name'], card['type'], card['attributes'], card['cost'], card['power'], card['toughness'], card['sets'], card['rarity'], card['text'])
else:
# Prepare to read in the next record
inRecord = False
if not card:
return None
return Card(card['name'], card['type'], card['attributes'], card['cost'], card['power'], card['toughness'], card['sets'], card['rarity'], card['text'])
class WagicDB(Database):
def __init__(self, wagicPath):