battleman.py: Implemented adding a combatant to the wait list

This commit is contained in:
Anna Rose 2012-04-02 23:42:31 -04:00
parent e18a5cf840
commit 4239d57d02
2 changed files with 121 additions and 21 deletions

View File

@ -42,6 +42,7 @@ def main():
print "Welcome to 4e Battle Manager.\n" print "Welcome to 4e Battle Manager.\n"
# Resume battle if needed # Resume battle if needed
# fixme: critical: indexes get reused when restoring! Maybe switch to shelve?
if settings.resume: if settings.resume:
try: try:
with open(BP_FILE, 'r') as f: with open(BP_FILE, 'r') as f:
@ -53,13 +54,32 @@ def main():
else: else:
# hard-coding test cases for now. # hard-coding test cases for now.
# 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...
btl.add_group(CombatGroup("Adele", [Combatant("Adele", hp=26, pc=True, surges=8, sw=1)], 2)) adele = Combatant("Adele", hp=26, pc=True, surges=8, sw=1)
btl.add_group(CombatGroup("Aristaire", [Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1)], 0)) 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)) foobolds = {}
btl.add_group(CombatGroup("Barglins", [Combatant("Barglin", hp=1), Combatant("Barglin", hp=1)], 3)) for i in range(5):
btl.add_group(CombatGroup("Orcs of Baz", [Combatant("Orc", hp=32), Combatant("Orc", hp=32)], 1)) 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 print btl
@ -352,16 +372,28 @@ class CommandParser(cmd.Cmd):
def do_wait(self, line): def do_wait(self, line):
"""wait """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): def do_unwait(self, line):
"""unwait """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): def do_next(self, line):

View File

@ -14,6 +14,7 @@ class Battle():
def __init__(self): def __init__(self):
self.combatant_hash = {} self.combatant_hash = {}
self.groups = [] self.groups = []
self.wait_list = {}
self.current = None self.current = None
self.round = None self.round = None
@ -56,8 +57,8 @@ class Battle():
else: else:
self.groups.append(group) self.groups.append(group)
for c in group.members: for i, c in group.members.items():
self.combatant_hash[c.index] = c self.combatant_hash[i] = c
def get_current_group(self): def get_current_group(self):
@ -71,7 +72,7 @@ class Battle():
# member of the group gets returned. # member of the group gets returned.
def get_current_combatant(self): def get_current_combatant(self):
if self.current != None: if self.current != None:
return self.groups[self.current].members[0] return self.groups[self.current].get_member_by_pos(0)
else: else:
return None return None
@ -97,7 +98,7 @@ class Battle():
print "Error: battle is already running" print "Error: battle is already running"
for g in self.groups: 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))) g.set_init(easyinput.input_int('Initiative for {}'.format(g.name)))
else: else:
g.roll_init() g.roll_init()
@ -120,10 +121,10 @@ class Battle():
for g in self.groups: for g in self.groups:
if g.is_solo_group(): if g.is_solo_group():
ret = ret + '{}\n'.format(g.members[0]) ret = ret + '{}\n'.format(g.get_member_by_pos(0))
else: else:
ret = ret + '{}:\n'.format(g.name) ret = ret + '{}:\n'.format(g.name)
for c in g.members: for c in g.members.values():
ret = ret + ' {}\n'.format(c) ret = ret + ' {}\n'.format(c)
return ret.rstrip() return ret.rstrip()
@ -181,6 +182,48 @@ class Battle():
return None 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(): class CombatGroup():
# What we're mostly getting here is a definition of the *members* # What we're mostly getting here is a definition of the *members*
# of the group... then we build them all and stick them in # of the group... then we build them all and stick them in
@ -209,9 +252,10 @@ class CombatGroup():
count = easyinput.input_int('count', 1) count = easyinput.input_int('count', 1)
# Now make the combatants... # Now make the combatants...
members = [] members = {}
for i in range(count): 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: if count > 1:
name = name + 's' name = name + 's'
@ -236,7 +280,31 @@ class CombatGroup():
def add_member(self, c): 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): def is_solo_group(self):
@ -246,12 +314,12 @@ class CombatGroup():
def begin_turn(self): def begin_turn(self):
print '{} {} initiative.'.format(self.name, ['has', 'have'][len(self.members) != 1]) 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() c.begin_turn()
def end_turn(self): def end_turn(self):
for c in self.members: for c in self.members.values():
c.end_turn() c.end_turn()