From 035d8cba3750e51128d1d5bed858c20a79f68c93 Mon Sep 17 00:00:00 2001
From: Anna Wiggins <annabunches@gmail.com>
Date: Thu, 5 Apr 2012 17:06:17 -0400
Subject: [PATCH] battleman: Implemented wait/unwait properly, fixed a couple
 of simple bugs

---
 battleman.py  | 18 +++++++++---------
 lib/battle.py | 23 +++++++++++++++++------
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/battleman.py b/battleman.py
index ae86be9..c055749 100755
--- a/battleman.py
+++ b/battleman.py
@@ -14,7 +14,7 @@ sys.path.append('lib/')
 
 import shelve
 import argparse
-import cmd
+from cmd import Cmd
 import os.path
 import battle
 from battle import CombatGroup
@@ -89,11 +89,11 @@ def main():
 
 
 
-class CommandParser(cmd.Cmd):
+class CommandParser(Cmd):
     """Parse the commands from the command-line."""
 
     def __init__(self, btl, session):
-        cmd.Cmd.__init__(self)
+        Cmd.__init__(self)
 
         self.btl = btl
         self.session = session
@@ -116,25 +116,25 @@ class CommandParser(cmd.Cmd):
     # This allows us to do partial command completion without <tab>,
     # as long as 
     def default(self, line):
-        cmd, data, line = self.parseline(line)
-        cmds = self.completenames(cmd)
+        comm, data, line = self.parseline(line)
+        cmds = self.completenames(comm)
         num_cmds = len(cmds)
         if num_cmds == 1:
             getattr(self, 'do_'+cmds[0])(data)
         elif num_cmds > 1:
-            sys.stdout.write('Error: Ambiguous command: {}'.format(cmd))
+            sys.stdout.write('Error: Ambiguous command: {}'.format(comm))
         else:
-            print 'Error: Unrecognized command {}'.format(cmd)
+            print 'Error: Unrecognized command {}'.format(comm)
 
 
     # We are overriding do_help to avoid printing info about
     # undocumented commands
     def do_help(self, arg):
         if arg:
-            Cmd.cmd.do_help(arg)
+            Cmd.do_help(self, arg)
         else:
             # Everything from here to the end is lifted straight
-            # out of Cmd.cmd.do_help()
+            # out of cmd.Cmd.do_help()
             names = self.get_names()
             cmds_doc = []
             cmds_undoc = []
diff --git a/lib/battle.py b/lib/battle.py
index 3397eff..9473dd0 100644
--- a/lib/battle.py
+++ b/lib/battle.py
@@ -26,7 +26,6 @@ class Battle():
         else:
             ret = 'Battle not yet started\n\n'
 
-        ret = ret + 'Combatants\n==========\n'
         ret = ret + self.format_combatants()
 
         return ret
@@ -117,7 +116,7 @@ class Battle():
 
     # Returns a formatted string with all of the combatants
     def format_combatants(self):
-        ret = ''
+        ret = 'Combatants\n==========\n'
 
         for g in self.groups:
             if g.is_solo_group():
@@ -127,10 +126,16 @@ class Battle():
                 for c in g.members.values():
                     ret = ret + '  {}\n'.format(c)
         
+        if self.wait_list:
+            ret = ret + '\nWait List\n=========\n'
+            for c in self.wait_list.values():
+                ret = ret + '{}\n'.format(c)
+
         return ret.rstrip()
 
 
     # Returns a formatted string with just the current group
+    # fixme: non-solo groups only print indexes...
     def format_current_group(self):
         if self.validate_started():
             return self.validate_started()
@@ -145,7 +150,9 @@ class Battle():
             return ret.rstrip()
 
 
-    def next_combatant(self):
+    # Fixme - if someone is waited/unwaited, both them and the person
+    # after them may have begin_turn() and/or end_turn() called more than once
+    def next_combatant(self, same_index = False):
         if self.validate_started():
             print self.validate_started()
             return
@@ -153,7 +160,8 @@ class Battle():
         g = self.get_current_group()
         g.end_turn()
         
-        self.current += 1
+        if not same_index:
+            self.current += 1
 
         if self.current >= len(self.groups):
             self.current = 0
@@ -182,7 +190,6 @@ class Battle():
         return None
 
 
-    # fixme: if removing makes a group empty, nom the group
     def wait(self, index):
         v = self.validate_started()
         if v:
@@ -204,6 +211,7 @@ class Battle():
 
             if c:
                 self.wait_list[index] = c
+                self.next_combatant(same_index = True)
             else:
                 print 'Error: Failed to find combatant {}'.format(index)
 
@@ -215,7 +223,10 @@ class Battle():
             return
 
         if index in self.wait_list:
-            pass # fixme - fix unwaiting
+            c = self.wait_list[index]
+            del self.wait_list[index]
+            self.groups.insert(self.current, CombatGroup(c.name, {c.index: c}, 0))
+            self.next_combatant(same_index = True)
         else:
             if index in self.combatant_hash:
                 print '{} is not waiting'.format(self.combatant_hash[index])