battleman.py: Added sensible no-arguments behavior to the commands that needed them

This commit is contained in:
Anna Rose 2012-03-26 17:38:08 -04:00
parent 331da9c0b7
commit b8d87161f8

View File

@ -427,12 +427,21 @@ class Battle():
def get_current_group(self):
if self.current != -1:
if self.current != None:
return self.groups[self.current]
else:
return None
# If you're in a multi-member group, the first
# member of the group gets returned.
def get_current_combatant(self):
if self.current != None:
return self.groups[self.current].members[0]
else:
return None
def get_combatant(self, index):
if index in self.combatant_hash:
return self.combatant_hash[index]
@ -558,17 +567,16 @@ def main():
do_prompt()
# fixme - change input behavior. If an action has a sensible default, do that when no args are passed - require args to change default behavior
def do_prompt():
print ''
(comm, rdata) = input_str('', default='?', show_default=False, prompt_str='>').partition(' ')[::2]
(comm, rdata) = input_str('', default='n', show_default=False, prompt_str='>').partition(' ')[::2]
data = rdata.split(' ')
if data == ['']:
data = []
if comm == '?':
do_help()
do_help() # fixme - add ability to get command-specific help
elif comm == 'a':
do_add_combatants(data)
elif comm == 'p':
@ -631,8 +639,14 @@ x - force save the progress to the current battle cache (for use with --resume
q - quit""")
# Core data parsing functions
def do_add_combatants(data):
if len(data) >= 1:
ngroups = int(data[0])
else:
ngroups = input_int('number of groups')
for i in range(1, ngroups+1):
print "Adding group {}".format(i)
battle.add_group(CombatGroup.from_input())
@ -650,13 +664,9 @@ def do_print_combatant_info(data):
def do_damage(data):
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
c = do_combatant_select(data)
if not c:
print ('Error: Invalid combatant index.')
return
else:
c = battle.choose_combatant()
if len(data) >= 2:
amount = int(data[1])
@ -667,13 +677,9 @@ def do_damage(data):
def do_heal(data):
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
c = do_combatant_select(data)
if not c:
print ('Error: Invalid combatant index.')
return
else:
c = battle.choose_combatant()
if len(data) >= 2:
amount = int(data[1])
@ -684,13 +690,9 @@ def do_heal(data):
def do_add_temp_hp(data):
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
c = do_combatant_select(data)
if not c:
print ('Error: Invalid combatant index.')
return
else:
c = battle.choose_combatant()
if len(data) >= 2:
amount = int(data[1])
@ -705,26 +707,16 @@ def do_remove_temp_hp(data):
def do_surge(data, heal=True):
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
c = do_combatant_select(data)
if not c:
print ('Error: Invalid combatant index.')
return
else:
c = battle.choose_combatant()
c.use_surge(heal)
def do_second_wind(data):
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
c = do_combatant_select(data)
if not c:
print ('Error: Invalid combatant index.')
return
else:
c = battle.choose_combatant()
c.use_second_wind()
@ -732,43 +724,24 @@ def do_add_condition(data):
duration = None
end_type = 'e'
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
else:
c = battle.choose_combatant()
c = do_combatant_select(data)
if not c:
return
if len(data) >= 2:
name = data[1]
else:
name = input_str('condition name')
if len(data) >= 3:
ctype = data[2]
else:
ctype = input_str('condition type', default='s', show_default=True)
name = do_data_input_str(data, 1, 'condition name')
ctype = do_data_input_str(data, 2, 'condition type', default='s', show_default=True)
if ctype == 't':
if len(data) >= 4:
duration = int(data[3])
else:
duration = input_int('duration')
if len(data) >= 5:
end_type = data[4]
else:
end_type = input_str('(s)tart|(e)nd', default='e', show_default=True)
duration = do_data_input_int(data, 3, 'duration')
end_type = do_data_input_str(data, 4, '(s)tart|(e)nd', default='e', show_default=True)
c.add_condition(name, ctype, duration, end_type)
def do_remove_condition(data):
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
c = do_combatant_select(data)
if not c:
print ('Error: Invalid combatant index.')
return
else:
c = battle.choose_combatant()
if len(data) >= 2:
index = int(data[1])
@ -783,13 +756,9 @@ def do_remove_condition(data):
def do_use_recharge_power(data):
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
c = do_combatant_select(data)
if not c:
print ('Error: Invalid combatant index.')
return
else:
c = battle.choose_combatant()
if len(data) >= 2:
index = int(data[1])
@ -815,6 +784,37 @@ def do_stub():
print "Sorry, this is a stub function"
# Data parsing helper functions
def do_combatant_select(data):
c = None
if len(data) >= 1:
c = battle.get_combatant(int(data[0]))
if not c:
print 'Error: Invalid combatant index.'
else:
c = battle.get_current_combatant()
if not c:
print 'Error: Battle not started and no combatant specified.'
return c
def do_data_input_str(data, index, prompt, default=None, show_default=False, prompt_str=':'):
if len(data) >= index + 1:
return data[index]
else:
return input_str(prompt, default, show_default, prompt_str)
def do_data_input_int(data, index, prompt, default=None, show_default=True, prompt_str=':'):
return int(do_data_select_str(data, index, prompt, default, show_default, prompt_str))
# Input primitives
def input_str(prompt, default=None, show_default=False, prompt_str=':'):
full_prompt = prompt
if default != None and show_default: