Implemented ability to read combatant definitions from files

This commit is contained in:
Anna Rose 2012-05-04 11:27:04 -04:00
parent 0fea33760b
commit d62748fa88
2 changed files with 112 additions and 21 deletions

View File

@ -52,33 +52,37 @@ def main():
sys.exit(1) sys.exit(1)
else: else:
for f in settings.files:
for g in battle.combatgroups_from_file(f):
btl.add_group(g)
# hard-coding test cases for now. # hard-coding test cases for now.
# fixme: Eventually, use a state-saving text file that's easy to edit, or at least copy... # fixme: Eventually, use a state-saving text file that's easy to edit, or at least copy...
adele = Combatant("Adele", hp=26, pc=True, surges=8, sw=1) # adele = Combatant("Adele", hp=26, pc=True, surges=8, sw=1)
adele_dict = {adele.index: adele} # adele_dict = {adele.index: adele}
aristaire = Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1) # aristaire = Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1)
aristaire_dict = {aristaire.index: aristaire} # aristaire_dict = {aristaire.index: aristaire}
foobolds = {} # foobolds = {}
for i in range(5): # for i in range(5):
c = Combatant("Foobold", hp=50) # c = Combatant("Foobold", hp=50)
foobolds[c.index] = c # foobolds[c.index] = c
barglins = {} # barglins = {}
for i in range(2): # for i in range(2):
c = Combatant("Barglin", hp=50) # c = Combatant("Barglin", hp=50)
barglins[c.index] = c # barglins[c.index] = c
orcs = {} # orcs = {}
for i in range(2): # for i in range(2):
c = Combatant("Orc", hp=50) # c = Combatant("Orc", hp=50)
orcs[c.index] = c # orcs[c.index] = c
btl.add_group(CombatGroup("Adele", adele_dict, 2)) # btl.add_group(CombatGroup("Adele", adele_dict, 2))
btl.add_group(CombatGroup("Aristaire", aristaire_dict, 0)) # btl.add_group(CombatGroup("Aristaire", aristaire_dict, 0))
btl.add_group(CombatGroup("Foobolds", foobolds, 20)) # btl.add_group(CombatGroup("Foobolds", foobolds, 20))
btl.add_group(CombatGroup("Barglins", barglins, 3)) # btl.add_group(CombatGroup("Barglins", barglins, 3))
btl.add_group(CombatGroup("Orcs of Baz", orcs, 1)) # btl.add_group(CombatGroup("Orcs of Baz", orcs, 1))
print btl print btl
@ -433,6 +437,8 @@ def do_stub():
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description='Command-line interface to manage battle data for D&D 4e', formatter_class=argparse.RawTextHelpFormatter) parser = argparse.ArgumentParser(description='Command-line interface to manage battle data for D&D 4e', formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--resume', '-r', action='store_true', help='Resume the battle from the last run of the program') parser.add_argument('--resume', '-r', action='store_true', help='Resume the battle from the last run of the program')
parser.add_argument('files', nargs=argparse.REMAINDER, help="A list of files containing combat groups to add to the initial battle. Ignored if --resume is specified.")
return parser.parse_args() return parser.parse_args()

View File

@ -662,3 +662,88 @@ def do_combatant_select(battle, data):
print 'Error: Battle not started and no combatant specified.' print 'Error: Battle not started and no combatant specified.'
return c return c
# Read combatant/groups in from a file, return a simple list of CombatGroups
def combatgroups_from_file(filename):
ret = []
with open(filename, 'r') as f:
group_data = {}
for line in f.read().split('\n'):
line = line.strip()
if line == '':
if group_data == {}:
continue
g = _build_group_from_file_data(group_data)
if g is not None:
ret.append(g)
else:
print 'Error: Failed to read a group definition from file: {}'.format(filename)
group_data = {}
else:
try:
var, value = line.split('=')
except ValueError:
print 'Error: Bad value in data file. No data from file parsed.'
return []
group_data[var] = value
return ret
def _build_group_from_file_data(data):
if not _validate_group_data(data):
return None
if 'count' in data:
if 'groupname' in data:
gname = data['groupname']
else:
gname = data['name'] + 's'
count = int(data['count'])
else:
count = 1
gname = data['name']
members = {}
for i in range(count):
c = Combatant(data['name'], int(data['hp']), data['pc'],
int(data['init']), int(data['surges']),
int(data['ap']), data['sw'], data['recharges'])
members[c.index] = c
return CombatGroup(gname, members, data['init'])
def _validate_group_data(data):
if not 'pc' in data:
data['pc'] = False
if not 'ap' in data:
data['ap'] = 0
if not 'init' in data:
data['init'] = 0
if not 'surges' in data:
data['surges'] = 0
if not 'count' in data:
data['count'] = 1
if not 'recharges' in data:
data['recharges'] = []
if data['pc']:
data['sw'] = 1
else:
data['sw'] = 0
return 'name' in data and 'hp' in data