Implemented ability to read combatant definitions from files

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

View File

@ -662,3 +662,88 @@ def do_combatant_select(battle, data):
print 'Error: Battle not started and no combatant specified.'
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