From 4239d57d0290595aeb281ee41590001ddcfcc844 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Mon, 2 Apr 2012 23:42:31 -0400 Subject: [PATCH] battleman.py: Implemented adding a combatant to the wait list --- battleman.py | 52 +++++++++++++++++++++++------ lib/battle.py | 90 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 121 insertions(+), 21 deletions(-) diff --git a/battleman.py b/battleman.py index d909571..4811e3e 100755 --- a/battleman.py +++ b/battleman.py @@ -42,6 +42,7 @@ def main(): print "Welcome to 4e Battle Manager.\n" # Resume battle if needed + # fixme: critical: indexes get reused when restoring! Maybe switch to shelve? if settings.resume: try: with open(BP_FILE, 'r') as f: @@ -53,13 +54,32 @@ def main(): else: # hard-coding test cases for now. - # Eventually, use a state-saving text file that's easy to edit, or at least copy... - btl.add_group(CombatGroup("Adele", [Combatant("Adele", hp=26, pc=True, surges=8, sw=1)], 2)) - btl.add_group(CombatGroup("Aristaire", [Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1)], 0)) + # 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_dict = {adele.index: adele} + aristaire = Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1) + aristaire_dict = {aristaire.index: aristaire} - btl.add_group(CombatGroup("Foobolds", [Combatant("Foobold", hp=50), Combatant("Foobold", hp=50), Combatant("Foobold", hp=50), Combatant("Foobold", hp=50), Combatant("Foobold", hp=50)], 20)) - btl.add_group(CombatGroup("Barglins", [Combatant("Barglin", hp=1), Combatant("Barglin", hp=1)], 3)) - btl.add_group(CombatGroup("Orcs of Baz", [Combatant("Orc", hp=32), Combatant("Orc", hp=32)], 1)) + foobolds = {} + for i in range(5): + c = Combatant("Foobold", hp=50) + foobolds[c.index] = c + + barglins = {} + for i in range(2): + c = Combatant("Barglin", hp=50) + barglins[c.index] = c + + orcs = {} + for i in range(2): + c = Combatant("Orc", hp=50) + orcs[c.index] = c + + btl.add_group(CombatGroup("Adele", adele_dict, 2)) + btl.add_group(CombatGroup("Aristaire", aristaire_dict, 0)) + btl.add_group(CombatGroup("Foobolds", foobolds, 20)) + btl.add_group(CombatGroup("Barglins", barglins, 3)) + btl.add_group(CombatGroup("Orcs of Baz", orcs, 1)) print btl @@ -352,16 +372,28 @@ class CommandParser(cmd.Cmd): def do_wait(self, line): """wait - This function is still a stub""" + Removes the specified combatant from the initiative roster and add them to the wait list.""" - do_stub() + data = parse_data(line) + + c = battle.do_combatant_select(self.btl, data) + if not c: + return + + self.btl.wait(c.index) def do_unwait(self, line): """unwait - This function is still a stub""" + Removes the specified combatant from the wait list and adds them back into the initiative roster.""" - do_stub() + data = parse_data(line) + + c = battle.do_combatant_select(self.btl, data) + if not c: + return + + self.btl.unwait(c.index) def do_next(self, line): diff --git a/lib/battle.py b/lib/battle.py index a45bffc..3397eff 100644 --- a/lib/battle.py +++ b/lib/battle.py @@ -14,6 +14,7 @@ class Battle(): def __init__(self): self.combatant_hash = {} self.groups = [] + self.wait_list = {} self.current = None self.round = None @@ -56,8 +57,8 @@ class Battle(): else: self.groups.append(group) - for c in group.members: - self.combatant_hash[c.index] = c + for i, c in group.members.items(): + self.combatant_hash[i] = c def get_current_group(self): @@ -71,7 +72,7 @@ class Battle(): # member of the group gets returned. def get_current_combatant(self): if self.current != None: - return self.groups[self.current].members[0] + return self.groups[self.current].get_member_by_pos(0) else: return None @@ -97,7 +98,7 @@ class Battle(): print "Error: battle is already running" for g in self.groups: - if g.is_solo_group() and g.members[0].pc: + if g.is_solo_group() and g.get_member_by_pos(0).pc: g.set_init(easyinput.input_int('Initiative for {}'.format(g.name))) else: g.roll_init() @@ -120,10 +121,10 @@ class Battle(): for g in self.groups: if g.is_solo_group(): - ret = ret + '{}\n'.format(g.members[0]) + ret = ret + '{}\n'.format(g.get_member_by_pos(0)) else: ret = ret + '{}:\n'.format(g.name) - for c in g.members: + for c in g.members.values(): ret = ret + ' {}\n'.format(c) return ret.rstrip() @@ -181,6 +182,48 @@ class Battle(): return None + # fixme: if removing makes a group empty, nom the group + def wait(self, index): + v = self.validate_started() + if v: + print v + return + + if index in self.wait_list: + print '{} is already waiting.'.format(self.wait_list[index]) + else: + c = None + i = 0 + for group in self.groups: + c = group.remove_member(index) + if c: + if len(group.members) == 0: + del self.groups[i] + break + i += 1 + + if c: + self.wait_list[index] = c + else: + print 'Error: Failed to find combatant {}'.format(index) + + + def unwait(self, index): + v = self.validate_started() + if v: + print v + return + + if index in self.wait_list: + pass # fixme - fix unwaiting + else: + if index in self.combatant_hash: + print '{} is not waiting'.format(self.combatant_hash[index]) + else: + print "Error: Failed to find combatant {}".format(index) + + + class CombatGroup(): # What we're mostly getting here is a definition of the *members* # of the group... then we build them all and stick them in @@ -209,9 +252,10 @@ class CombatGroup(): count = easyinput.input_int('count', 1) # Now make the combatants... - members = [] + members = {} for i in range(count): - members.append(Combatant(name, hp, pc=False, surges=surges, ap=ap, sw=0, recharges=recharges)) + c = Combatant(name, hp, pc=False, surges=surges, ap=ap, sw=0, recharges=recharges) + members[c.index] = c if count > 1: name = name + 's' @@ -236,7 +280,31 @@ class CombatGroup(): def add_member(self, c): - self.members.append(c) + self.members[c.index] = c + + + # Removes the member from the group and returns her + def remove_member(self, index): + if index not in self.members: + return None + + c = self.members[index] + del self.members[index] + return c + + + def get_member(self, index): + if index in self.members: + return self.members[index] + else: + return None + + + def get_member_by_pos(self, pos): + if pos >= len(self.members): + return None + else: + return self.members.values()[pos] def is_solo_group(self): @@ -246,12 +314,12 @@ class CombatGroup(): def begin_turn(self): print '{} {} initiative.'.format(self.name, ['has', 'have'][len(self.members) != 1]) - for c in self.members: + for c in self.members.values(): c.begin_turn() def end_turn(self): - for c in self.members: + for c in self.members.values(): c.end_turn()