Mercurial > sypikslang
comparison gamelib/gamestate.py @ 22:296ce36fa7d9
Serialize and unserialize game state and missions
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 06 May 2012 18:38:54 +0200 |
parents | af1bfeb648cb |
children | f6a3b213857b |
comparison
equal
deleted
inserted
replaced
21:bdc6bfc34ef2 | 22:296ce36fa7d9 |
---|---|
6 from gamelib import missions, lab, products | 6 from gamelib import missions, lab, products |
7 | 7 |
8 | 8 |
9 class Game(object): | 9 class Game(object): |
10 | 10 |
11 def __init__(self): | 11 def __init__(self, init_data=None): |
12 self.lab = lab.Lab() | |
13 # FIXME: Generate the initial tech set | |
14 self.money = 1000 | 12 self.money = 1000 |
15 # Will be updated on the next turn | 13 # Will be updated on the next turn |
16 self.points = 0 | 14 self.points = 0 |
17 self.reputation = 0 | 15 self.reputation = 0 |
18 # instantiate the available missions | |
19 self.missions = [cls() for cls in missions.Mission.__subclasses__()] | |
20 # Missions being attempted | 16 # Missions being attempted |
21 self.cur_missions = [] | 17 self.cur_missions = [] |
22 # Science allocation for the current turn | 18 # Science allocation for the current turn |
23 self.cur_allocation = [] | 19 self.cur_allocation = [] |
20 self.lab = None | |
21 self.missions = [] | |
22 if init_data: | |
23 self._load_data(init_data) | |
24 else: | |
25 self.lab = lab.Lab() | |
26 # instantiate all the available missions | |
27 self.missions = [cls() for cls in | |
28 missions.Mission.__subclasses__()] | |
24 | 29 |
25 def start_turn(self): | 30 def start_turn(self): |
26 # Make more flexible? | 31 # Make more flexible? |
27 self.points += 3 | 32 self.points += 3 |
28 self.cur_missions = [] | 33 self.cur_missions = [] |
49 result.apply(self) | 54 result.apply(self) |
50 # Update the science state with result of spend_points | 55 # Update the science state with result of spend_points |
51 for science in new_stuff: | 56 for science in new_stuff: |
52 self.lab.science.append(science) | 57 self.lab.science.append(science) |
53 # FIXME: Update UI | 58 # FIXME: Update UI |
59 | |
60 def save_data(self): | |
61 """Serialize the game state into a dict""" | |
62 data = {} | |
63 data['money'] = self.money | |
64 data['reputation'] = self.reputation | |
65 data['points'] = self.points | |
66 data['lab'] = self.lab.serialize() | |
67 # Save mission state | |
68 data['missions'] = {} | |
69 for mission in self.missions: | |
70 miss_name = type(mission).__name__ | |
71 data['missions'][miss_name] = mission.save_data() | |
72 return data | |
73 | |
74 def _load_data(self, data): | |
75 """Restore the game state""" | |
76 self.money = data['money'] | |
77 self.reputation = data['reputation'] | |
78 self.points = data['points'] | |
79 self.lab = lab.Lab(data['lab']) | |
80 for mis_class in missions.Mission.__subclasses__(): | |
81 miss_name = mis_class.__name__ | |
82 if miss_name in data['missions']: | |
83 self.missions.append(mis_class(data['missions'][miss_name])) |