Mercurial > sypikslang
changeset 91:c57b5b46d3e0
Better mission data management.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Wed, 09 May 2012 21:08:29 +0200 |
parents | 0823e2529c23 |
children | 56b0a1c4de35 |
files | gamelib/missions.py gamelib/schematics.py |
diffstat | 2 files changed, 30 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/missions.py Wed May 09 20:30:25 2012 +0200 +++ b/gamelib/missions.py Wed May 09 21:08:29 2012 +0200 @@ -35,23 +35,38 @@ SHORT_DESCRIPTION = None LONG_DESCRIPTION = None MINIMUM_REPUTATION = None - # Flag for missions that can only be attempted once - completed = False def __init__(self, init_data=None): + self.data = {} + + if init_data is not None: + # Load stored state. + self._load_data(init_data) + else: + # New instance. + self._new_data() + + def _new_data(self): pass + def _load_data(self, init_data): + # Note: this does not deep-copy. + self.data = init_data.copy() + def save_data(self): - """Serialize the mission state for saving, etc.""" - return [] + # Note: this does not deep-copy. + return self.data.copy() + + def get_data(self, key): + return self.data.get(key, None) def can_attempt(self, state): """Can we currently attempt the mission""" - if self.completed: + if self.get_data('completed'): return False if (self.MINIMUM_REPUTATION is not None and - self.MINIMUM_REPUTATION > state.reputation): - # Don't have the reputation completed + self.MINIMUM_REPUTATION > state.reputation): + # Don't have the reputation required return False return True @@ -60,7 +75,7 @@ Returns a result object with the results of the mission.""" # Handle error case - if self.completed: + if self.get_data('completed'): raise RuntimeError('Cannot attempt a completed mission') # No equipment is usually a special case. @@ -119,14 +134,8 @@ " are small.") MINIMUM_REPUTATION = 100 - def __init__(self, init_data=None): - # Track prior approaches to this mission - self._prior_attempts = set() - if init_data: - self._prior_attempts = set(init_data) - - def save_data(self): - return list(self._prior_attempts) + def _new_data(self): + self.data['prior_attempts'] = [] def attempt_no_equipment(self, state): return Result(FAILURE, 0, -10, ( @@ -153,12 +162,12 @@ " to one weapon of mass destruction at a time.")) [doom] = dooms - if doom.NAME in self._prior_attempts: + if doom.NAME in self.data['prior_attempts']: return Result(FAILURE, 0, 0, ( "'We have devised countermeasures since last time, doctor." " You cannot threaten us with that again.'")) - self._prior_attempts.add(doom.NAME) + self.data['prior_attempts'].add(doom.NAME) return Result(SUCCESS, 1000000, 10, ( "Trembling at you threat of certain doom, the Chinese" " government pays the ransom.")) @@ -248,7 +257,7 @@ MINIMUM_REPUTATION = 20 def attempt(self, equipment, state): - self.completed = True + self.data['completed'] = True return Result(SUCCESS, 0, 50, ( "Mount Rushmore is remarkably easy to destroy.")) @@ -282,6 +291,7 @@ def attempt_with(self, categorised, state): rep = randint(5, 10) + print categorised if cat.MIND_CONTROL in categorised: result = ( "Your creative use of science has paid off nicely.")
--- a/gamelib/schematics.py Wed May 09 20:30:25 2012 +0200 +++ b/gamelib/schematics.py Wed May 09 21:08:29 2012 +0200 @@ -103,7 +103,7 @@ class PropagandaMachine(Schematic): NAME = "propaganda machine" COST = 1000 - CATEGORIES = (cat.MIND_CONTROL) + CATEGORIES = (cat.MIND_CONTROL,) PREREQUISITES = ( (research.Psychology, 2), )