Mercurial > sypikslang
annotate gamelib/game_base.py @ 135:8d1cf98e39b5
Reputation decays over time
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 10 May 2012 17:07:06 +0200 |
parents | 245ef50de84d |
children | 53277724645b |
rev | line source |
---|---|
88 | 1 import sys |
2 import os | |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
3 |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
4 |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
5 def get_subclasses(base_class, leaf_only=True): |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
6 subclasses = [] |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
7 for cls in base_class.__subclasses__(): |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
8 if leaf_only and cls.__subclasses__(): |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
9 # Not a leaf class, and only want leaves |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
10 continue |
94
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
11 if hasattr(cls, 'sanity_check'): |
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
12 cls.sanity_check() |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
13 subclasses.append(cls) |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
14 return subclasses |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
15 |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
16 |
88 | 17 def get_save_filename(): |
18 """Determine the base filename for auto saves""" | |
19 app = "sypikslang" | |
20 if sys.platform.startswith('win'): | |
21 if "APPDATA" in os.environ: | |
22 base = os.path.join(os.environ["APPDATA"], app) | |
23 else: | |
24 base = os.path.join(os.path.expanduser("~"), "." + app) | |
25 elif 'XDG_DATA_HOME' in os.environ: | |
26 base = os.path.join(os.environ["XDG_DATA_HOME"], app) | |
27 else: | |
28 base = os.path.join(os.path.expanduser("~"), ".local", "share", app) | |
29 if not os.path.exists(base): | |
30 os.makedirs(base, mode=0770) | |
31 if os.path.isdir(base): | |
32 return os.path.join(base, 'gamestate.json') | |
33 print 'save game directory is not a directory %s' % base | |
34 return None | |
35 | |
36 | |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
37 class Science(object): |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
38 NAME = None |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
39 PREREQUISITES = () |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
40 ACQUISITION_CHANCE = 1.0 |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
41 SCIENCE_TYPE = None |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
42 |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
43 def __init__(self, points=0): |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
44 self.points = points |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
45 |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
46 def spend_point(self): |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
47 self.points += 1 |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
48 |
58
52913ba12988
Check for multiple points in can_spend
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
49 def can_spend(self, lab, spend): |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
50 return True |
46
d3d277a42ac6
Less getattr/isinstance silliness.
Jeremy Thurgood <firxen@gmail.com>
parents:
45
diff
changeset
|
51 |
d3d277a42ac6
Less getattr/isinstance silliness.
Jeremy Thurgood <firxen@gmail.com>
parents:
45
diff
changeset
|
52 @classmethod |
73
b503ccb0a86e
Only one schematic per turn, please.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
53 def depends_on(self, sciences): |
b503ccb0a86e
Only one schematic per turn, please.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
54 for science_class, _ in self.PREREQUISITES: |
b503ccb0a86e
Only one schematic per turn, please.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
55 if any(isinstance(science, science_class) for science in sciences): |
b503ccb0a86e
Only one schematic per turn, please.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
56 return True |
b503ccb0a86e
Only one schematic per turn, please.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
57 return False |
b503ccb0a86e
Only one schematic per turn, please.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
58 |
b503ccb0a86e
Only one schematic per turn, please.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
59 @classmethod |
46
d3d277a42ac6
Less getattr/isinstance silliness.
Jeremy Thurgood <firxen@gmail.com>
parents:
45
diff
changeset
|
60 def save_name(cls): |
d3d277a42ac6
Less getattr/isinstance silliness.
Jeremy Thurgood <firxen@gmail.com>
parents:
45
diff
changeset
|
61 return "%s.%s" % (cls.SCIENCE_TYPE, cls.__name__) |
47 | 62 |
63 def save_data(self): | |
64 return (self.save_name(), self.points) | |
94
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
65 |
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
66 @classmethod |
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
67 def sanity_check(cls): |
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
68 for science, points in cls.PREREQUISITES: |
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
69 assert issubclass(science, Science) |
245ef50de84d
Sanity-check research, schematic and mission classes. (Ironic, no?)
Jeremy Thurgood <firxen@gmail.com>
parents:
88
diff
changeset
|
70 assert isinstance(points, int) |