Mercurial > sypikslang
annotate gamelib/gamestate.py @ 33:12c33aac7684
Gate certain missions by reputation.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 06 May 2012 21:27:33 +0200 |
parents | 4d9610c59efa |
children | e285b1e31a08 |
rev | line source |
---|---|
16 | 1 # -*- coding: utf-8 -*- |
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 | |
3 | |
4 """The actual game state object""" | |
5 | |
6 from gamelib import missions, lab, products | |
7 | |
8 | |
9 class Game(object): | |
10 | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
11 def __init__(self, init_data=None): |
16 | 12 self.money = 1000 |
13 # Will be updated on the next turn | |
14 self.points = 0 | |
15 self.reputation = 0 | |
16 # Missions being attempted | |
17 self.cur_missions = [] | |
18 # Science allocation for the current turn | |
19 self.cur_allocation = [] | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
20 self.lab = None |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
21 self.missions = [] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
22 if init_data: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
23 self._load_data(init_data) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
24 else: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
25 self.lab = lab.Lab() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
26 # instantiate all the available missions |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
27 self.missions = [cls() for cls in |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
28 missions.Mission.__subclasses__()] |
16 | 29 |
30 def start_turn(self): | |
31 # Make more flexible? | |
32 self.points += 3 | |
33 self.cur_missions = [] | |
34 self.cur_allocation = [] | |
35 | |
36 def get_available_equipment(self): | |
37 """Return a list of equipment we can produce and afford""" | |
25 | 38 available = [x for x in self.lab.science |
16 | 39 if isinstance(x, products.Product) and x.COST <= self.money] |
40 return available | |
41 | |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
42 def get_available_missions(self): |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
43 """Return a list of missions we can feasibly attempt""" |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
44 available = [x for x in self.missions |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
45 if (x.MINIMUM_REPUTATION is None |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
46 or x.MINIMUM_REPUTATION <= self.reputation)] |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
47 return available |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
48 |
16 | 49 def end_turn(self): |
50 # Attempt the missions | |
51 mission_results = [] | |
52 for mission, equipment in self.cur_missions: | |
25 | 53 mission_results.append(mission.attempt(equipment, self)) |
54 if not mission.available: | |
55 # Mission no longer available, so we clean up | |
56 self.missions.remove(mission) | |
16 | 57 # Do the science |
58 self.points -= len(self.cur_allocation) | |
59 if self.points < 0: | |
60 raise RuntimeError('Spent too many points') | |
61 new_stuff = self.lab.spend_points(self.cur_allocation, self.points) | |
27
4d9610c59efa
Reset points after spending them.
Jeremy Thurgood <firxen@gmail.com>
parents:
25
diff
changeset
|
62 self.points = 0 |
16 | 63 # Process mission results |
64 for result in mission_results: | |
65 result.apply(self) | |
66 for science in new_stuff: | |
23
f6a3b213857b
Fix up some game state logic, add very basic REPL game interface.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
67 # FIXME: Update UI better. |
f6a3b213857b
Fix up some game state logic, add very basic REPL game interface.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
68 print "You learned new stuff:", science.NAME |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
69 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
70 def save_data(self): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
71 """Serialize the game state into a dict""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
72 data = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
73 data['money'] = self.money |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
74 data['reputation'] = self.reputation |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
75 data['points'] = self.points |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
76 data['lab'] = self.lab.serialize() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
77 # Save mission state |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
78 data['missions'] = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
79 for mission in self.missions: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
80 miss_name = type(mission).__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
81 data['missions'][miss_name] = mission.save_data() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
82 return data |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
83 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
84 def _load_data(self, data): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
85 """Restore the game state""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
86 self.money = data['money'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
87 self.reputation = data['reputation'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
88 self.points = data['points'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
89 self.lab = lab.Lab(data['lab']) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
90 for mis_class in missions.Mission.__subclasses__(): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
91 miss_name = mis_class.__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
92 if miss_name in data['missions']: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
93 self.missions.append(mis_class(data['missions'][miss_name])) |