Mercurial > sypikslang
annotate gamelib/gamestate.py @ 87:d93e1ea2bd0d
Fix method name assumption bug
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 09 May 2012 20:06:44 +0200 |
parents | b0d97d51df51 |
children | 50f8476aa929 |
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 | |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
6 from gamelib import missions, lab |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
7 from gamelib.game_base import get_subclasses |
80
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
8 from gamelib.constants import NEW_SCIENCE, NEW_SCHEMATIC |
16 | 9 |
10 | |
11 class Game(object): | |
12 | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
13 def __init__(self, init_data=None): |
16 | 14 self.money = 1000 |
15 # Will be updated on the next turn | |
16 self.points = 0 | |
17 self.reputation = 0 | |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
18 self.turn = 0 |
16 | 19 # Missions being attempted |
20 self.cur_missions = [] | |
21 # Science allocation for the current turn | |
22 self.cur_allocation = [] | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
23 self.lab = None |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
24 self.missions = [] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
25 if init_data: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
26 self._load_data(init_data) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
27 else: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
28 self.lab = lab.Lab() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
29 # instantiate all the available missions |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
30 for cls in get_subclasses(missions.Mission): |
42
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
31 self.missions.append(cls()) |
16 | 32 |
33 def start_turn(self): | |
34 # Make more flexible? | |
35 self.points += 3 | |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
36 self.turn += 1 |
16 | 37 self.cur_missions = [] |
38 self.cur_allocation = [] | |
39 | |
40 def get_available_equipment(self): | |
41 """Return a list of equipment we can produce and afford""" | |
25 | 42 available = [x for x in self.lab.science |
49 | 43 if x.SCIENCE_TYPE == 'schematic' and x.COST <= self.money] |
16 | 44 return available |
45 | |
82
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
46 def get_all_equipment(self): |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
47 """Return a list of equipment we could produce, regardless of cost""" |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
48 equipment = [x for x in self.lab.science |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
49 if x.SCIENCE_TYPE == 'schematic'] |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
50 return equipment |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
51 |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
52 def get_available_missions(self): |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
53 """Return a list of missions we can feasibly attempt""" |
41
e285b1e31a08
Add can_attempt method for future flexibility
Neil Muller <drnlmuller@gmail.com>
parents:
33
diff
changeset
|
54 available = [x for x in self.missions if x.can_attempt(self)] |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
55 return available |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
56 |
63
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
57 def get_available_points(self): |
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
58 return self.points - len(self.cur_allocation) |
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
59 |
16 | 60 def end_turn(self): |
61 # Attempt the missions | |
62 mission_results = [] | |
63 for mission, equipment in self.cur_missions: | |
25 | 64 mission_results.append(mission.attempt(equipment, self)) |
16 | 65 # Do the science |
66 self.points -= len(self.cur_allocation) | |
67 if self.points < 0: | |
68 raise RuntimeError('Spent too many points') | |
69 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
|
70 self.points = 0 |
16 | 71 # Process mission results |
80
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
72 messages = [] |
16 | 73 for result in mission_results: |
74 result.apply(self) | |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
75 messages.append((result.outcome, result.message)) |
16 | 76 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
|
77 # FIXME: Update UI better. |
80
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
78 if science.SCIENCE_TYPE == 'research': |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
79 messages.append((NEW_SCIENCE, |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
80 "You've started a new line of research in %s" |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
81 % science.NAME)) |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
82 else: |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
83 messages.append((NEW_SCHEMATIC, |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
84 "You've developed blueprints for a %s" % science.NAME)) |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
85 return messages |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
86 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
87 def save_data(self): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
88 """Serialize the game state into a dict""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
89 data = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
90 data['money'] = self.money |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
91 data['reputation'] = self.reputation |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
92 data['points'] = self.points |
87
d93e1ea2bd0d
Fix method name assumption bug
Neil Muller <drnlmuller@gmail.com>
parents:
82
diff
changeset
|
93 data['lab'] = self.lab.save_data() |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
94 # Save mission state |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
95 data['missions'] = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
96 for mission in self.missions: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
97 miss_name = type(mission).__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
98 data['missions'][miss_name] = mission.save_data() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
99 return data |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
100 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
101 def _load_data(self, data): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
102 """Restore the game state""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
103 self.money = data['money'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
104 self.reputation = data['reputation'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
105 self.points = data['points'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
106 self.lab = lab.Lab(data['lab']) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
107 for mis_class in missions.Mission.__subclasses__(): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
108 miss_name = mis_class.__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
109 if miss_name in data['missions']: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
110 self.missions.append(mis_class(data['missions'][miss_name])) |