# HG changeset patch # User Neil Muller # Date 1336322334 -7200 # Node ID 296ce36fa7d90f108e5e8b7d6380ae764a4fbdbc # Parent bdc6bfc34ef29524b89be71f9c07d74e750c8155 Serialize and unserialize game state and missions diff -r bdc6bfc34ef2 -r 296ce36fa7d9 gamelib/gamestate.py --- a/gamelib/gamestate.py Sun May 06 18:34:28 2012 +0200 +++ b/gamelib/gamestate.py Sun May 06 18:38:54 2012 +0200 @@ -8,19 +8,24 @@ class Game(object): - def __init__(self): - self.lab = lab.Lab() - # FIXME: Generate the initial tech set + def __init__(self, init_data=None): self.money = 1000 # Will be updated on the next turn self.points = 0 self.reputation = 0 - # instantiate the available missions - self.missions = [cls() for cls in missions.Mission.__subclasses__()] # Missions being attempted self.cur_missions = [] # Science allocation for the current turn self.cur_allocation = [] + self.lab = None + self.missions = [] + if init_data: + self._load_data(init_data) + else: + self.lab = lab.Lab() + # instantiate all the available missions + self.missions = [cls() for cls in + missions.Mission.__subclasses__()] def start_turn(self): # Make more flexible? @@ -51,3 +56,28 @@ for science in new_stuff: self.lab.science.append(science) # FIXME: Update UI + + def save_data(self): + """Serialize the game state into a dict""" + data = {} + data['money'] = self.money + data['reputation'] = self.reputation + data['points'] = self.points + data['lab'] = self.lab.serialize() + # Save mission state + data['missions'] = {} + for mission in self.missions: + miss_name = type(mission).__name__ + data['missions'][miss_name] = mission.save_data() + return data + + def _load_data(self, data): + """Restore the game state""" + self.money = data['money'] + self.reputation = data['reputation'] + self.points = data['points'] + self.lab = lab.Lab(data['lab']) + for mis_class in missions.Mission.__subclasses__(): + miss_name = mis_class.__name__ + if miss_name in data['missions']: + self.missions.append(mis_class(data['missions'][miss_name])) diff -r bdc6bfc34ef2 -r 296ce36fa7d9 gamelib/mission.py --- a/gamelib/mission.py Sun May 06 18:34:28 2012 +0200 +++ b/gamelib/mission.py Sun May 06 18:38:54 2012 +0200 @@ -35,6 +35,13 @@ LONG_DESCRIPTION = None available = True + def __init__(self, init_data=None): + pass + + def save_data(self): + """Serialize the mission state for saving, etc.""" + return [] + def attempt(self, equipment, state): """Attempt the mission with the given equipment list. @@ -65,17 +72,22 @@ " are great, but the risks are significant. Without " \ "some serious firepower, the chances of success are small." - def __init__(self): + def __init__(self, init_data=None): # Track prior approaches to this mission self._prior_attempts = [] + if init_data: + self._prior_attempts = init_data + + def save_data(self): + return self._prior_attempts 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) + item.NAME not in self._prior_attempts: + self._prior_attempts.add(item.NAME) return Result(SUCCESS, 1000000, 1, "Trembling at the threat of" " your doomsday virus, the chinese government pays the" " ransom")