changeset 13:c0966997e0c5

Use a results object instead of tuple (will hopefully make changign stuff easier later)
author Neil Muller <drnlmuller@gmail.com>
date Sun, 06 May 2012 15:56:22 +0200
parents f9756477cbce
children 9d61abb3cfaf
files gamelib/mission.py
diffstat 1 files changed, 35 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/mission.py	Sun May 06 14:58:51 2012 +0200
+++ b/gamelib/mission.py	Sun May 06 15:56:22 2012 +0200
@@ -4,6 +4,25 @@
 from product import DoomsdayVirus, MachineGun, TeslaGun
 
 
+class Result(object):
+    """Results of a mission"""
+
+    def __init__(self, outcome, money, reputation, msg):
+        self.outcome = outcome
+        self.money = money
+        self.reputation = reputation
+        self.message = msg
+        self.applied = False
+
+    def apply(self, state):
+        if not self.applied:
+            state.money += self.money
+            state.reputation += self.reputation
+            self.applied = True
+        else:
+            raise RuntimeError('attempted to apply result twice')
+
+
 class Mission(object):
     """Base class for the mission objects.
        Missions have a name, short description (for list displays) and
@@ -17,9 +36,8 @@
     def attempt(self, equipment, state):
         """Attempt the mission with the given equipment list.
 
-          Returns a tuple (success, message). Updates the game
-          state with the results of the mission."""
-        return False, "You can't succceed at this mission"
+          Returns a result object with the results of the mission."""
+        return Result(False, 0, 0, "You can't succceed at this mission")
 
 
 class RansomChina(Mission):
@@ -37,23 +55,23 @@
 
     def attempt(self, equipment, state):
         failures = []
+        reputation = 0
         for item in equipment:
             if isinstance(item, DoomsdayVirus) and \
                     item not in self._prior_attempts:
                 self._prior_attempts.add(item)
-                state.money += 1000000
-                state.reputation += 1
-                return True, "Trembling at the threat of your " \
-                   " doomsday virus, the chinese government pays the ransom"
+                return Result(True, 1000000, 1, "Trembling at the threat of"
+                    " your doomsday virus, the chinese government pays the"
+                    " ransom")
             elif isinstance(item, DoomsdayVirus):
-                state.reputation -= 1
+                reputation = -1
                 failures.append("'Hah, we've developed an antidote to your"
                         " virus, doctor'. You cannot threaten us with that"
                         " again'")
             else:
                 failures.append("You fail to inspire fear with your %s"
                         % item.name)
-        return False, "\n".join(failures)
+        return Result(False, 0, reputation, "\n".join(failures))
 
 
 class RobBank(Mission):
@@ -75,17 +93,15 @@
                             " it's not worth wasting this on something so"
                             " trivial")
                 else:
-                    state.money + 1000
-                    return True, "Holding up a bank with only a small vial" \
-                            " of clear liquid. Now that is power."
+                    return Result(True, 1000, 0, "Holding up a bank with only"
+                        " a small vial of clear liquid. Now that is power.")
             elif isinstance(item, MachineGun) or isinstance(item, TeslaGun):
-                state.money += 1000
-                return True, "The threat of your weapons is enough to" \
-                        " inspire an impressive level of cooperation"
+                return Result(True, 1000, 0, "The threat of your weapons is"
+                    " enough to inspire an impressive level of cooperation")
             else:
                 failures.append("You fail to inspire fear with your %s"
                         % item.name)
-        return False, 0, "\n".join(failures)
+        return Result(False, 0, 0, "\n".join(failures))
 
 
 class DestroyMountRushmore(Mission):
@@ -97,7 +113,7 @@
 
     def attempt(self, equipment, state):
         if not self.available:
-            return True, "You send a team to kick over the rubble."
+            raise RuntimeError('Cannot attempt an unavailable mission')
         self.available = False
-        state.reputation += 100
-        return True, "Mount Rushmore is remarkably easy to destroy."
+        return Result(True, 0, 100,
+            "Mount Rushmore is remarkably easy to destroy.")