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 * from mtg import *
class Database: class Database:
def getCard(name): def getCard(self, name):
pass
def findCard(self, name):
pass pass
""" Text file database class """ Text file database class
@ -14,6 +16,8 @@ class TextDB(Database):
def __init__(self, filename): def __init__(self, filename):
self.filename = filename self.filename = filename
def getCard(self, name): def getCard(self, name):
return self.findCard(name).next()
def findCard(self, name):
card = None card = None
with open(self.filename, 'r') as f: with open(self.filename, 'r') as f:
inRecord = False inRecord = False
@ -23,7 +27,7 @@ class TextDB(Database):
if not inRecord: if not inRecord:
recordName = line recordName = line
inRecord = True inRecord = True
if recordName.lower() == name.lower(): if not name or recordName.lower() == name.lower():
card = { card = {
'name': recordName, 'name': recordName,
'type': '', 'type': '',
@ -58,13 +62,10 @@ class TextDB(Database):
# Finished with current record # Finished with current record
if card: if card:
# We're done here # 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: else:
# Prepare to read in the next record # Prepare to read in the next record
inRecord = False 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): class WagicDB(Database):
def __init__(self, wagicPath): def __init__(self, wagicPath):