Mercurial > sypikslang
changeset 41:e285b1e31a08
Add can_attempt method for future flexibility
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 13:59:50 +0200 |
parents | 0e178b20e3ff |
children | 47c7e96cf9c8 |
files | gamelib/gamestate.py gamelib/missions.py |
diffstat | 2 files changed, 18 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/gamestate.py Mon May 07 00:27:59 2012 +0200 +++ b/gamelib/gamestate.py Mon May 07 13:59:50 2012 +0200 @@ -41,9 +41,7 @@ def get_available_missions(self): """Return a list of missions we can feasibly attempt""" - available = [x for x in self.missions - if (x.MINIMUM_REPUTATION is None - or x.MINIMUM_REPUTATION <= self.reputation)] + available = [x for x in self.missions if x.can_attempt(self)] return available def end_turn(self): @@ -51,9 +49,6 @@ mission_results = [] for mission, equipment in self.cur_missions: mission_results.append(mission.attempt(equipment, self)) - if not mission.available: - # Mission no longer available, so we clean up - self.missions.remove(mission) # Do the science self.points -= len(self.cur_allocation) if self.points < 0:
--- a/gamelib/missions.py Mon May 07 00:27:59 2012 +0200 +++ b/gamelib/missions.py Mon May 07 13:59:50 2012 +0200 @@ -38,7 +38,8 @@ SHORT_DESCRIPTION = None LONG_DESCRIPTION = None MINIMUM_REPUTATION = None - available = True + # Flag for missions that can only be attempted once + completed = False def __init__(self, init_data=None): pass @@ -47,10 +48,24 @@ """Serialize the mission state for saving, etc.""" return [] + def can_attempt(self, state): + """Can we currently attempt the mission""" + if self.completed: + return False + if (self.MINIMUM_REPUTATION is not None and + self.MINIMUM_REPUTATION > state.reputation): + # Don't have the reputation completed + return False + return True + def attempt(self, equipment, state): """Attempt the mission with the given equipment list. Returns a result object with the results of the mission.""" + # Handle error case + if self.completed: + raise RuntimeError('Cannot attempt a completed mission') + # No equipment is usually a special case. if not equipment: return self.attempt_no_equipment(state) @@ -236,8 +251,6 @@ MINIMUM_REPUTATION = 20 def attempt(self, equipment, state): - if not self.available: - raise RuntimeError('Cannot attempt an unavailable mission') - self.available = False + self.completed = True return Result(SUCCESS, 0, 50, ( "Mount Rushmore is remarkably easy to destroy."))