made decoding better

This commit is contained in:
Bill Zorn 2015-07-23 01:04:40 -07:00
parent c5231ac947
commit 08dc3944f8
3 changed files with 79 additions and 45 deletions

View file

@ -366,50 +366,72 @@ class Card:
# all be -1 if the card was parsed from (unordered) json. # all be -1 if the card was parsed from (unordered) json.
def set_field_default(self, field, values): def set_field_default(self, field, values):
first = True
for idx, value in values: for idx, value in values:
self.__dict__[field] = value if first:
break # only use the first one... first = False
self.__dict__[field] = value
else:
# stick it in other so we'll be know about it when we format the card
self.valid = False
self.__dict__[field_other] += [(idx, '<' + field + '> ' + str(value))]
def _set_loyalty(self, values): def _set_loyalty(self, values):
first = True
for idx, value in values: for idx, value in values:
self.__dict__[field_loyalty] = value if first:
try: first = False
self.__dict__[field_loyalty + '_value'] = int(value) self.__dict__[field_loyalty] = value
except ValueError:
self.__dict__[field_loyalty + '_value'] = None
# Technically '*' could still be valid, but it's unlikely...
break # only use the first one...
def _set_pt(self, values):
for idx, value in values:
self.__dict__[field_pt] = value
p_t = value.split('/') # hardcoded
if len(p_t) == 2:
self.__dict__[field_pt + '_p'] = p_t[0]
try: try:
self.__dict__[field_pt + '_p_value'] = int(p_t[0]) self.__dict__[field_loyalty + '_value'] = int(value)
except ValueError: except ValueError:
self.__dict__[field_pt + '_p_value'] = None self.__dict__[field_loyalty + '_value'] = None
self.__dict__[field_pt + '_t'] = p_t[1] # Technically '*' could still be valid, but it's unlikely...
try:
self.__dict__[field_pt + '_t_value'] = int(p_t[1])
except ValueError:
self.__dict__[field_pt + '_t_value'] = None
else: else:
self.valid = False self.valid = False
break # only use the first one... self.__dict__[field_other] += [(idx, '<loyalty> ' + str(value))]
def _set_pt(self, values):
first = True
for idx, value in values:
if first:
first = False
self.__dict__[field_pt] = value
p_t = value.split('/') # hardcoded
if len(p_t) == 2:
self.__dict__[field_pt + '_p'] = p_t[0]
try:
self.__dict__[field_pt + '_p_value'] = int(p_t[0])
except ValueError:
self.__dict__[field_pt + '_p_value'] = None
self.__dict__[field_pt + '_t'] = p_t[1]
try:
self.__dict__[field_pt + '_t_value'] = int(p_t[1])
except ValueError:
self.__dict__[field_pt + '_t_value'] = None
else:
self.valid = False
else:
self.valid = False
self.__dict__[field_other] += [(idx, '<pt> ' + str(value))]
def _set_text(self, values): def _set_text(self, values):
first = True
for idx, value in values: for idx, value in values:
mtext = value if first:
self.__dict__[field_text] = mtext first = False
fulltext = mtext.encode() mtext = value
if fulltext: self.__dict__[field_text] = mtext
self.__dict__[field_text + '_lines'] = map(Manatext, fulltext.split(utils.newline)) fulltext = mtext.encode()
self.__dict__[field_text + '_words'] = re.sub(utils.unletters_regex, if fulltext:
' ', self.__dict__[field_text + '_lines'] = map(Manatext,
fulltext).split() fulltext.split(utils.newline))
break # only use the first one... self.__dict__[field_text + '_words'] = re.sub(utils.unletters_regex,
' ',
fulltext).split()
else:
self.valid = False
self.__dict__[field_other] += [(idx, '<text> ' + str(value))]
def _set_other(self, values): def _set_other(self, values):
# just record these, we could do somthing unset valid if we really wanted # just record these, we could do somthing unset valid if we really wanted
@ -474,6 +496,8 @@ class Card:
outstr = '' outstr = ''
if gatherer: if gatherer:
cardname = self.__dict__[field_name].title() cardname = self.__dict__[field_name].title()
if not cardname:
cardname = '_NONAME_'
if for_forum: if for_forum:
outstr += '[b]' outstr += '[b]'
outstr += cardname outstr += cardname
@ -492,7 +516,12 @@ class Card:
outstr += '\n' outstr += '\n'
outstr += ' '.join(self.__dict__[field_supertypes] + self.__dict__[field_types]).title() basetypes = self.__dict__[field_types]
if len(basetypes) < 1:
basetypes = ['_NOTYPE_']
outstr += ' '.join(self.__dict__[field_supertypes] + basetypes)
if self.__dict__[field_subtypes]: if self.__dict__[field_subtypes]:
outstr += (' ' + utils.dash_marker + ' ' + outstr += (' ' + utils.dash_marker + ' ' +
' '.join(self.__dict__[field_subtypes]).title()) ' '.join(self.__dict__[field_subtypes]).title())
@ -510,8 +539,8 @@ class Card:
mtext = transforms.text_unpass_1_choice(mtext, delimit = False) mtext = transforms.text_unpass_1_choice(mtext, delimit = False)
mtext = transforms.text_unpass_2_counters(mtext) mtext = transforms.text_unpass_2_counters(mtext)
mtext = transforms.text_unpass_3_unary(mtext) mtext = transforms.text_unpass_3_unary(mtext)
mtext = transforms.text_unpass_4_cardname(mtext, cardname) mtext = transforms.text_unpass_4_symbols(mtext, for_forum)
mtext = transforms.text_unpass_5_symbols(mtext, for_forum) mtext = transforms.text_unpass_5_cardname(mtext, cardname)
mtext = transforms.text_unpass_6_newlines(mtext) mtext = transforms.text_unpass_6_newlines(mtext)
newtext = Manatext('') newtext = Manatext('')
newtext.text = mtext newtext.text = mtext
@ -558,8 +587,8 @@ class Card:
mtext = transforms.text_unpass_1_choice(mtext, delimit = True) mtext = transforms.text_unpass_1_choice(mtext, delimit = True)
#mtext = transforms.text_unpass_2_counters(mtext) #mtext = transforms.text_unpass_2_counters(mtext)
mtext = transforms.text_unpass_3_unary(mtext) mtext = transforms.text_unpass_3_unary(mtext)
#mtext = transforms.text_unpass_4_cardname(mtext, cardname) mtext = transforms.text_unpass_4_symbols(mtext, for_forum)
mtext = transforms.text_unpass_5_symbols(mtext, for_forum) #mtext = transforms.text_unpass_5_cardname(mtext, cardname)
mtext = transforms.text_unpass_6_newlines(mtext) mtext = transforms.text_unpass_6_newlines(mtext)
newtext = Manatext('') newtext = Manatext('')
newtext.text = mtext newtext.text = mtext

View file

@ -103,12 +103,17 @@ class Manacost:
self.colors = self.get_colors() self.colors = self.get_colors()
def __str__(self): def __str__(self):
if self.none:
return '_NOCOST_'
return utils.mana_untranslate(utils.mana_open_delimiter + ''.join(self.sequence) return utils.mana_untranslate(utils.mana_open_delimiter + ''.join(self.sequence)
+ utils.mana_close_delimiter) + utils.mana_close_delimiter)
def format(self, for_forum = False): def format(self, for_forum = False):
return utils.mana_untranslate(utils.mana_open_delimiter + ''.join(self.sequence) if self.none:
+ utils.mana_close_delimiter, for_forum) return '_NOCOST_'
else:
return utils.mana_untranslate(utils.mana_open_delimiter + ''.join(self.sequence)
+ utils.mana_close_delimiter, for_forum)
def encode(self, randomize = False): def encode(self, randomize = False):
if self.none: if self.none:

View file

@ -459,13 +459,13 @@ def text_unpass_3_unary(s):
return utils.from_unary(s) return utils.from_unary(s)
def text_unpass_4_cardname(s, name): def text_unpass_4_symbols(s, for_forum):
return s.replace(this_marker, name)
def text_unpass_5_symbols(s, for_forum):
return utils.from_symbols(s, for_forum = for_forum) return utils.from_symbols(s, for_forum = for_forum)
def text_unpass_5_cardname(s, name):
return s.replace(this_marker, name)
def text_unpass_6_newlines(s): def text_unpass_6_newlines(s):
return s.replace(newline, '\n') return s.replace(newline, '\n')